splitPD0
author A.M. Thurnherr <athurnherr@yahoo.com>
Wed, 25 May 2016 12:23:02 -0400
changeset 34 3b4bcd55e1ea
parent 23 fb0c269b1eaa
child 36 515b06dae59c
permissions -rwxr-xr-x
V1.6

#!/usr/bin/perl
#======================================================================
#                    S P L I T R D I 
#                    doc: Sat Aug 21 22:20:27 2010
#                    dlm: Sun Sep 14 17:47:48 2014
#                    (c) 2010 A.M. Thurnherr
#                    uE-Info: 54 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
#	Mar 19, 2014: - added -s)kip small files
#	Sep 14, 2014: - added -f)irst 

# 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]>] " .
	"[-f)irst <cast #>] " .
	"[require -m)in <ens> to produce output] " .
	"<RDI file> <ens> <ens[...]>\n")
		unless (&getopts('f:o:m:') && @ARGV>=3);

$opt_o = substr($ARGV[0],0,3)					# default output filename format
		 . '%02d'
		 . substr($ARGV[0],-9)
			unless defined($opt_o);

$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 = 0;

do {											# split data
	sysseek(WBRF,($first_ens-2)*$ens_len,0) ||	# read next block of data
		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");

	if ($last_ens-$first_ens+1 > $opt_m) {		# produce file only if sufficient # of ensembles
		$fn = sprintf($opt_o,$opt_f+$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);