splitRDI
author A.M. Thurnherr <athurnherr@yahoo.com>
Sat, 22 Feb 2014 09:43:48 -0500
changeset 16 68a9fc5e7d45
parent 14 8c79b38a7086
child 18 bb7bb9f83db9
permissions -rwxr-xr-x
.

#!/usr/bin/perl
#======================================================================
#                    S P L I T R D I 
#                    doc: Sat Aug 21 22:20:27 2010
#                    dlm: Thu Feb 13 14:42:02 2014
#                    (c) 2010 A.M. Thurnherr
#                    uE-Info: 19 0 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

# 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, the yoyo cast number is inserted after the station number

# 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. 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);