splitRDI
author A.M. Thurnherr <ant@ldeo.columbia.edu>
Mon, 27 Sep 2010 20:25:11 -0400
changeset 1 a3b6a908dec5
child 7 e06925788055
permissions -rwxr-xr-x
after P302 cruise

#!/usr/bin/perl
#======================================================================
#                    S P L I T R D I 
#                    doc: Sat Aug 21 22:20:27 2010
#                    dlm: Sat Aug 21 23:18:41 2010
#                    (c) 2010 A.M. Thurnherr
#                    uE-Info: 51 44 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

# 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]

$0 =~ m{(.*/)[^/]+};
require "$1RDI_BB_Read.pl";
use Getopt::Std;

die("Usage: $0 " .
	"[out-file -b)asename <bn>] " .
	"[out-file -n) <digits>] " .
	"<RDI file> <ens> <ens[...]>\n")
		unless (&getopts('b:n:') && @ARGV>=3);

chomp($opt_b = `basename $ARGV[0] .000`)		# basename
	unless defined($opt_b);
$opt_n = 2										# number of digits in profile #
	unless defined($opt_n);
		
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 = $opt_b . sprintf("_%0${opt_n}d.000",$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);