.
#!/usr/bin/perl
#======================================================================
# S P L I T R D I
# doc: Sat Aug 21 22:20:27 2010
# dlm: Fri Jun 24 11:30:06 2011
# (c) 2010 A.M. Thurnherr
# uE-Info: 27 68 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
# NOTES:
# - it is assumed that the input file begins with ensemble #1
# - input file extension 000 is assumed
# - 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, the yoyo cast number is inserted after the station number
# EXAMPLE:
# 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. 017%02dDL000.000]>] " .
"<RDI file> <ens> <ens[...]>\n")
unless (&getopts('o:') && @ARGV>=3);
$opt_o = substr($ARGV[0],0,3) # default output filename format
. '%02d'
. substr($ARGV[0],-9)
unless defined($opt_o);
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 = 0;
do { # split data
sysseek(WBRF,($first_ens-2)*$ens_len,0) ||
die("$WBRcfn: $!");
$last_ens++ unless defined($ARGV[0]);
$nBytes = ($last_ens-$first_ens+1) * $ens_len;
sysread(WBRF,$buf,$nBytes) == $nBytes ||
die("$WBRcfn: file truncated");
$fn = sprintf($opt_o,$cnr++);
open(F,">$fn") || die("$fn: $!\n");
syswrite(F,$buf,$nBytes) == $nBytes ||
die("$fn: $!\n");
close(F);
printf(STDERR "$fn: %d ensembles ($nBytes bytes)\n",$last_ens-$first_ens+1);
$first_ens = $last_ens+1;
$last_ens = $ARGV[0]; shift;
} while defined($last_ens);
exit(0);