splitPD0
changeset 43 b63fa355644c
parent 36 515b06dae59c
equal deleted inserted replaced
42:80d039881d2c 43:b63fa355644c
     1 #!/usr/bin/perl
     1 #!/usr/bin/perl
     2 #======================================================================
     2 #======================================================================
     3 #                    S P L I T P D 0 
     3 #                    S P L I T P D 0 
     4 #                    doc: Sat Aug 21 22:20:27 2010
     4 #                    doc: Sat Aug 21 22:20:27 2010
     5 #                    dlm: Sat Jul 30 18:50:43 2016
     5 #                    dlm: Mon Apr  2 15:49:09 2018
     6 #                    (c) 2010 A.M. Thurnherr
     6 #                    (c) 2010 A.M. Thurnherr
     7 #                    uE-Info: 69 60 NIL 0 0 72 2 2 4 NIL ofnI
     7 #                    uE-Info: 28 36 NIL 0 0 72 2 2 4 NIL ofnI
     8 #======================================================================
     8 #======================================================================
     9 
     9 
    10 # split RDI files based on list of ensemble numbers (e.g. from yoyo -t)
    10 # split RDI files based on list of ensemble numbers (e.g. from yoyo -t)
    11 
    11 
    12 # HISTORY:
    12 # HISTORY:
    17 #	Sep 14, 2014: - added -f)irst
    17 #	Sep 14, 2014: - added -f)irst
    18 #	Jul 26, 2016: - changed file numbering to 1-relative
    18 #	Jul 26, 2016: - changed file numbering to 1-relative
    19 #	Jul 30, 2016: - modified -o default
    19 #	Jul 30, 2016: - modified -o default
    20 #				  - added code to set DSID of first ensemble of each
    20 #				  - added code to set DSID of first ensemble of each
    21 #					output file to 0x7f7f
    21 #					output file to 0x7f7f
       
    22 #	Apr  2, 1018: - BUG in error messages
       
    23 #				  - added header id check
    22 
    24 
    23 # NOTES:
    25 # NOTES:
    24 #   - it is assumed that the input file begins with ensemble #1
    26 #   - it is assumed that the input file begins with ensemble #1
    25 #   - turning-point ensembles are written to preceding profile,
    27 #   - turning-point ensembles are written to next profile,
    26 #     for compatibility with [yoyo]
    28 #     for compatibility with [yoyo]?
    27 
    29 
    28 # FILE NAME CONVENTION:
    30 # FILE NAME CONVENTION:
    29 #   - in order to assign individual yoyo casts numerical station numbers,
    31 #   - in order to assign individual yoyo casts numerical station numbers,
    30 #     by default, an underscore and the yoyo cast number is added to the basename
    32 #     by default, an underscore and the yoyo cast number is added to the basename
    31 
    33 
    36 require "$1RDI_BB_Read.pl";
    38 require "$1RDI_BB_Read.pl";
    37 use Getopt::Std;
    39 use Getopt::Std;
    38 
    40 
    39 die("Usage: $0 " .
    41 die("Usage: $0 " .
    40 	"[-o)ut-file <fmt[e.g. 017DL_%02d.000]>] " .
    42 	"[-o)ut-file <fmt[e.g. 017DL_%02d.000]>] " .
    41 	"[-f)irst <cast #>] " .
    43 	"[-f)irst output <cast #>] " .
    42 	"[require -m)in <ens> to produce output] " .
    44 	"[require -m)in <ens> to produce output] " .
    43 	"<RDI file> <ens> <ens[...]>\n")
    45 	"<RDI file> <ens> <ens[...]>\n")
    44 		unless (&getopts('f:o:m:') && @ARGV>=3);
    46 		unless (&getopts('f:o:m:') && @ARGV>=3);
    45 
    47 
    46 unless (defined($opt_o)) {
    48 unless (defined($opt_o)) {
    48 	$opt_o = "${bn}_%02d.$extn";
    50 	$opt_o = "${bn}_%02d.$extn";
    49 }
    51 }
    50 
    52 
    51 $opt_m = 0 unless defined($opt_m);								# default: produce tiny files as well
    53 $opt_m = 0 unless defined($opt_m);								# default: produce tiny files as well
    52 	
    54 	
    53 readHeader($ARGV[0],\%hdr); shift;								# get length of ensembles
    55 $fn = $ARGV[0]; shift;
       
    56 readHeader($fn,\%hdr); 											# get length of ensembles
    54 $ens_len = $hdr{ENSEMBLE_BYTES} + 2;
    57 $ens_len = $hdr{ENSEMBLE_BYTES} + 2;
    55 
    58 
    56 $first_ens = $ARGV[0]+1; shift;									# initialize loop
    59 $first_ens = $ARGV[0]+1; shift;									# initialize loop
    57 $last_ens  = $ARGV[0]; shift;
    60 $last_ens  = $ARGV[0]; shift;
    58 $cnr = 1;
    61 $cnr = 1;
    59 
    62 
    60 do {															# split data
    63 do {															# split data
    61 	sysseek(WBRF,($first_ens-2)*$ens_len,0) ||					# begin next block of ensembles
    64 	sysseek(WBRF,($first_ens-2)*$ens_len,0) ||					# begin next block of ensembles
    62 		die("$WBRcfn: $!");
    65 		die("$fn: $!");
    63 	$last_ens++ unless defined($ARGV[0]);
    66 	$last_ens++ unless defined($fn);
    64 
    67 
    65 	sysread(WBRF,$ids,2) || die("$WBRcfn: file truncated");		# read 1st ensemble & ensure DSID is 0x7f
    68 	sysread(WBRF,$ids,2) || die("$fn: file truncated");			# read 1st ensemble & ensure DSID is 0x7f
       
    69 	die("$fn: illegal header id [0x" . unpack('H4',$ids) . "]")	# require 1st byte to be 0x7f
       
    70 		unless (substr(unpack('H4',$ids),0,2) eq '7f');
    66 	$ids = pack('H4','7f7f');									# reset DSID
    71 	$ids = pack('H4','7f7f');									# reset DSID
    67 	sysread(WBRF,$febuf,$ens_len-4) == $ens_len-4 ||
    72 	sysread(WBRF,$febuf,$ens_len-4) == $ens_len-4 ||
    68 		die("$WBRcfn: file truncated");
    73 		die("$fn: file truncated");
    69 	sysread(WBRF,$csum,2) || die("$WBRcfn: file truncated");
    74 	sysread(WBRF,$csum,2) || die("$fn: file truncated");
    70 	$csum = pack('v',unpack('%16C*',$ids.$febuf));				# re-calculate checksum
    75 	$csum = pack('v',unpack('%16C*',$ids.$febuf));				# re-calculate checksum
    71 	
    76 	
    72 	$nBytes = ($last_ens-$first_ens) * $ens_len;				# read remaining ensembles in block
    77 	$nBytes = ($last_ens-$first_ens) * $ens_len;				# read remaining ensembles in block
    73 	sysread(WBRF,$buf,$nBytes) == $nBytes ||
    78 	sysread(WBRF,$buf,$nBytes) == $nBytes ||
    74 		die("$WBRcfn: file truncated");
    79 		die("$fn: file truncated (ends before ens#$last_ens)");
    75 
    80 
    76 	if ($last_ens-$first_ens+1 >= $opt_m) {						# write output only if sufficient # of ensembles
    81 	if ($last_ens-$first_ens+1 >= $opt_m) {						# write output only if sufficient # of ensembles
    77 		$fn = sprintf($opt_o,$opt_f+$cnr++);
    82 		$fn = sprintf($opt_o,$opt_f+$cnr++);
    78 		open(F,">$fn") || die("$fn: $!\n");
    83 		open(F,">$fn") || die("$fn: $!\n");
    79 		syswrite(F,$ids.$febuf.$csum.$buf,$nBytes+$ens_len) == $nBytes+$ens_len ||
    84 		syswrite(F,$ids.$febuf.$csum.$buf,$nBytes+$ens_len) == $nBytes+$ens_len ||