#!/usr/bin/perl
#======================================================================
# S P L I T P D 0
# doc: Sat Aug 21 22:20:27 2010
# dlm: Sat Jul 30 18:50:43 2016
# (c) 2010 A.M. Thurnherr
# uE-Info: 69 60 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
# NOTES:
# - it is assumed that the input file begins with ensemble #1
# - turning-point ensembles are written to preceding 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 <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
readHeader($ARGV[0],\%hdr); shift; # 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("$WBRcfn: $!");
$last_ens++ unless defined($ARGV[0]);
sysread(WBRF,$ids,2) || die("$WBRcfn: file truncated"); # read 1st ensemble & ensure DSID is 0x7f
$ids = pack('H4','7f7f'); # reset DSID
sysread(WBRF,$febuf,$ens_len-4) == $ens_len-4 ||
die("$WBRcfn: file truncated");
sysread(WBRF,$csum,2) || die("$WBRcfn: 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("$WBRcfn: file truncated");
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);