splitPD0
author A.M. Thurnherr <athurnherr@yahoo.com>
Tue, 29 Jun 2021 12:21:32 -0400
changeset 59 4f4530fa35da
parent 43 b63fa355644c
permissions -rwxr-xr-x
V2.4 - New Features: - support for Nortek PD0 files - patchPD0 support for moored ADCP data - bug fixes & minor improvements

#!/usr/bin/perl
#======================================================================
#                    S P L I T P D 0 
#                    doc: Sat Aug 21 22:20:27 2010
#                    dlm: Mon Apr  2 15:49:09 2018
#                    (c) 2010 A.M. Thurnherr
#                    uE-Info: 28 36 NIL 0 0 72 2 2 4 NIL ofnI
#======================================================================

# split RDI files based on list of ensemble numbers (e.g. from yoyo -t)

# HISTORY:
#	Aug 21, 2010: - created
#	Jun 24, 2011: - replaced -b, -n by -o
#   Feb 13, 2014: - updated doc
#	Mar 19, 2014: - added -s)kip small files
#	Sep 14, 2014: - added -f)irst
#	Jul 26, 2016: - changed file numbering to 1-relative
#	Jul 30, 2016: - modified -o default
#				  - added code to set DSID of first ensemble of each
#					output file to 0x7f7f
#	Apr  2, 1018: - BUG in error messages
#				  - added header id check

# NOTES:
#   - it is assumed that the input file begins with ensemble #1
#   - turning-point ensembles are written to next profile,
#     for compatibility with [yoyo]?

# FILE NAME CONVENTION:
#   - in order to assign individual yoyo casts numerical station numbers,
#     by default, an underscore and the yoyo cast number is added to the basename

# EXAMPLES:
#   splitRDI 017DL000.000 `mkProfile 017DL000.000 | yoyo -QFens -ut`

$0 =~ m{(.*/)[^/]+};
require "$1RDI_BB_Read.pl";
use Getopt::Std;

die("Usage: $0 " .
	"[-o)ut-file <fmt[e.g. 017DL_%02d.000]>] " .
	"[-f)irst output <cast #>] " .
	"[require -m)in <ens> to produce output] " .
	"<RDI file> <ens> <ens[...]>\n")
		unless (&getopts('f:o:m:') && @ARGV>=3);

unless (defined($opt_o)) {
	my($bn,$extn) = ($ARGV[0] =~ m{([^/]+)\.([^\.]+)$});
	$opt_o = "${bn}_%02d.$extn";
}

$opt_m = 0 unless defined($opt_m);								# default: produce tiny files as well
	
$fn = $ARGV[0]; shift;
readHeader($fn,\%hdr); 											# get length of ensembles
$ens_len = $hdr{ENSEMBLE_BYTES} + 2;

$first_ens = $ARGV[0]+1; shift;									# initialize loop
$last_ens  = $ARGV[0]; shift;
$cnr = 1;

do {															# split data
	sysseek(WBRF,($first_ens-2)*$ens_len,0) ||					# begin next block of ensembles
		die("$fn: $!");
	$last_ens++ unless defined($fn);

	sysread(WBRF,$ids,2) || die("$fn: file truncated");			# read 1st ensemble & ensure DSID is 0x7f
	die("$fn: illegal header id [0x" . unpack('H4',$ids) . "]")	# require 1st byte to be 0x7f
		unless (substr(unpack('H4',$ids),0,2) eq '7f');
	$ids = pack('H4','7f7f');									# reset DSID
	sysread(WBRF,$febuf,$ens_len-4) == $ens_len-4 ||
		die("$fn: file truncated");
	sysread(WBRF,$csum,2) || die("$fn: file truncated");
	$csum = pack('v',unpack('%16C*',$ids.$febuf));				# re-calculate checksum
	
	$nBytes = ($last_ens-$first_ens) * $ens_len;				# read remaining ensembles in block
	sysread(WBRF,$buf,$nBytes) == $nBytes ||
		die("$fn: file truncated (ends before ens#$last_ens)");

	if ($last_ens-$first_ens+1 >= $opt_m) {						# write output only if sufficient # of ensembles
		$fn = sprintf($opt_o,$opt_f+$cnr++);
		open(F,">$fn") || die("$fn: $!\n");
		syswrite(F,$ids.$febuf.$csum.$buf,$nBytes+$ens_len) == $nBytes+$ens_len ||
			die("$fn: $!\n");
		close(F);
	    printf(STDERR "$fn: %d ensembles (%d bytes)\n",
						$last_ens-$first_ens+1,$nBytes+$ens_len);
	}
	
	$first_ens = $last_ens+1;
	$last_ens  = $ARGV[0]; shift;
} while defined($last_ens);

exit(0);