splitPD0
changeset 36 515b06dae59c
parent 23 fb0c269b1eaa
child 43 b63fa355644c
equal deleted inserted replaced
35:7c394a2d1fc9 36:515b06dae59c
     1 #!/usr/bin/perl
     1 #!/usr/bin/perl
     2 #======================================================================
     2 #======================================================================
     3 #                    S P L I T R D I 
     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: Sun Sep 14 17:47:48 2014
     5 #                    dlm: Sat Jul 30 18:50:43 2016
     6 #                    (c) 2010 A.M. Thurnherr
     6 #                    (c) 2010 A.M. Thurnherr
     7 #                    uE-Info: 54 0 NIL 0 0 72 2 2 4 NIL ofnI
     7 #                    uE-Info: 69 60 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:
    13 #	Aug 21, 2010: - created
    13 #	Aug 21, 2010: - created
    14 #	Jun 24, 2011: - replaced -b, -n by -o
    14 #	Jun 24, 2011: - replaced -b, -n by -o
    15 #   Feb 13, 2014: - updated doc
    15 #   Feb 13, 2014: - updated doc
    16 #	Mar 19, 2014: - added -s)kip small files
    16 #	Mar 19, 2014: - added -s)kip small files
    17 #	Sep 14, 2014: - added -f)irst 
    17 #	Sep 14, 2014: - added -f)irst
       
    18 #	Jul 26, 2016: - changed file numbering to 1-relative
       
    19 #	Jul 30, 2016: - modified -o default
       
    20 #				  - added code to set DSID of first ensemble of each
       
    21 #					output file to 0x7f7f
    18 
    22 
    19 # NOTES:
    23 # NOTES:
    20 #   - it is assumed that the input file begins with ensemble #1
    24 #   - it is assumed that the input file begins with ensemble #1
    21 #   - turning-point ensembles are written to preceding profile,
    25 #   - turning-point ensembles are written to preceding profile,
    22 #     for compatibility with [yoyo]
    26 #     for compatibility with [yoyo]
    23 
    27 
    24 # FILE NAME CONVENTION:
    28 # FILE NAME CONVENTION:
    25 #   - in order to assign individual yoyo casts numerical station numbers,
    29 #   - in order to assign individual yoyo casts numerical station numbers,
    26 #     by default, the yoyo cast number is inserted after the station number
    30 #     by default, an underscore and the yoyo cast number is added to the basename
    27 
    31 
    28 # EXAMPLES:
    32 # EXAMPLES:
    29 #   splitRDI 017DL000.000 `mkProfile 017DL000.000 | yoyo -QFens -ut`
    33 #   splitRDI 017DL000.000 `mkProfile 017DL000.000 | yoyo -QFens -ut`
    30 
    34 
    31 $0 =~ m{(.*/)[^/]+};
    35 $0 =~ m{(.*/)[^/]+};
    32 require "$1RDI_BB_Read.pl";
    36 require "$1RDI_BB_Read.pl";
    33 use Getopt::Std;
    37 use Getopt::Std;
    34 
    38 
    35 die("Usage: $0 " .
    39 die("Usage: $0 " .
    36 	"[-o)ut-file <fmt[e.g. 017%02dDL000.000]>] " .
    40 	"[-o)ut-file <fmt[e.g. 017DL_%02d.000]>] " .
    37 	"[-f)irst <cast #>] " .
    41 	"[-f)irst <cast #>] " .
    38 	"[require -m)in <ens> to produce output] " .
    42 	"[require -m)in <ens> to produce output] " .
    39 	"<RDI file> <ens> <ens[...]>\n")
    43 	"<RDI file> <ens> <ens[...]>\n")
    40 		unless (&getopts('f:o:m:') && @ARGV>=3);
    44 		unless (&getopts('f:o:m:') && @ARGV>=3);
    41 
    45 
    42 $opt_o = substr($ARGV[0],0,3)					# default output filename format
    46 unless (defined($opt_o)) {
    43 		 . '%02d'
    47 	my($bn,$extn) = ($ARGV[0] =~ m{([^/]+)\.([^\.]+)$});
    44 		 . substr($ARGV[0],-9)
    48 	$opt_o = "${bn}_%02d.$extn";
    45 			unless defined($opt_o);
    49 }
    46 
    50 
    47 $opt_m = 0 unless defined($opt_m);				# default: produce tiny files as well
    51 $opt_m = 0 unless defined($opt_m);								# default: produce tiny files as well
    48 	
    52 	
    49 readHeader($ARGV[0],\%hdr); shift;				# get length of ensembles
    53 readHeader($ARGV[0],\%hdr); shift;								# get length of ensembles
    50 $ens_len = $hdr{ENSEMBLE_BYTES} + 2;
    54 $ens_len = $hdr{ENSEMBLE_BYTES} + 2;
    51 
    55 
    52 $first_ens = $ARGV[0]+1; shift;					# initialize loop
    56 $first_ens = $ARGV[0]+1; shift;									# initialize loop
    53 $last_ens  = $ARGV[0]; shift;
    57 $last_ens  = $ARGV[0]; shift;
    54 $cnr = 0;
    58 $cnr = 1;
    55 
    59 
    56 do {											# split data
    60 do {															# split data
    57 	sysseek(WBRF,($first_ens-2)*$ens_len,0) ||	# read next block of data
    61 	sysseek(WBRF,($first_ens-2)*$ens_len,0) ||					# begin next block of ensembles
    58 		die("$WBRcfn: $!");
    62 		die("$WBRcfn: $!");
    59 	$last_ens++ unless defined($ARGV[0]);
    63 	$last_ens++ unless defined($ARGV[0]);
    60 	$nBytes = ($last_ens-$first_ens+1) * $ens_len;
    64 
       
    65 	sysread(WBRF,$ids,2) || die("$WBRcfn: file truncated");		# read 1st ensemble & ensure DSID is 0x7f
       
    66 	$ids = pack('H4','7f7f');									# reset DSID
       
    67 	sysread(WBRF,$febuf,$ens_len-4) == $ens_len-4 ||
       
    68 		die("$WBRcfn: file truncated");
       
    69 	sysread(WBRF,$csum,2) || die("$WBRcfn: file truncated");
       
    70 	$csum = pack('v',unpack('%16C*',$ids.$febuf));				# re-calculate checksum
       
    71 	
       
    72 	$nBytes = ($last_ens-$first_ens) * $ens_len;				# read remaining ensembles in block
    61 	sysread(WBRF,$buf,$nBytes) == $nBytes ||
    73 	sysread(WBRF,$buf,$nBytes) == $nBytes ||
    62 		die("$WBRcfn: file truncated");
    74 		die("$WBRcfn: file truncated");
    63 
    75 
    64 	if ($last_ens-$first_ens+1 > $opt_m) {		# produce file only if sufficient # of ensembles
    76 	if ($last_ens-$first_ens+1 >= $opt_m) {						# write output only if sufficient # of ensembles
    65 		$fn = sprintf($opt_o,$opt_f+$cnr++);
    77 		$fn = sprintf($opt_o,$opt_f+$cnr++);
    66 		open(F,">$fn") || die("$fn: $!\n");
    78 		open(F,">$fn") || die("$fn: $!\n");
    67 		syswrite(F,$buf,$nBytes) == $nBytes ||
    79 		syswrite(F,$ids.$febuf.$csum.$buf,$nBytes+$ens_len) == $nBytes+$ens_len ||
    68 			die("$fn: $!\n");
    80 			die("$fn: $!\n");
    69 		close(F);
    81 		close(F);
    70 	    printf(STDERR "$fn: %d ensembles ($nBytes bytes)\n",$last_ens-$first_ens+1);
    82 	    printf(STDERR "$fn: %d ensembles (%d bytes)\n",
       
    83 						$last_ens-$first_ens+1,$nBytes+$ens_len);
    71 	}
    84 	}
    72 	
    85 	
    73 	$first_ens = $last_ens+1;
    86 	$first_ens = $last_ens+1;
    74 	$last_ens  = $ARGV[0]; shift;
    87 	$last_ens  = $ARGV[0]; shift;
    75 } while defined($last_ens);
    88 } while defined($last_ens);