splitRDI
changeset 18 bb7bb9f83db9
parent 14 8c79b38a7086
child 21 0b5bbe60131c
equal deleted inserted replaced
17:591779f6df30 18:bb7bb9f83db9
     1 #!/usr/bin/perl
     1 #!/usr/bin/perl
     2 #======================================================================
     2 #======================================================================
     3 #                    S P L I T R D I 
     3 #                    S P L I T R D I 
     4 #                    doc: Sat Aug 21 22:20:27 2010
     4 #                    doc: Sat Aug 21 22:20:27 2010
     5 #                    dlm: Thu Feb 13 14:42:02 2014
     5 #                    dlm: Wed Mar 19 16:35:45 2014
     6 #                    (c) 2010 A.M. Thurnherr
     6 #                    (c) 2010 A.M. Thurnherr
     7 #                    uE-Info: 19 0 NIL 0 0 72 2 2 4 NIL ofnI
     7 #                    uE-Info: 36 43 NIL 0 0 72 2 2 4 NIL ofnI
     8 #======================================================================
     8 #======================================================================
     9 
     9 
    10 # split RDI files based on list of ensemble numbers (e.g. from yoyo -t)
    10 # split RDI files based on list of ensemble numbers (e.g. from yoyo -t)
    11 
    11 
    12 # HISTORY:
    12 # HISTORY:
    13 #	Aug 21, 2010: - created
    13 #	Aug 21, 2010: - created
    14 #	Jun 24, 2011: - replaced -b, -n by -o
    14 #	Jun 24, 2011: - replaced -b, -n by -o
    15 #   Feb 13, 2014: - updated doc
    15 #   Feb 13, 2014: - updated doc
       
    16 #	Mar 19, 2014: - added -s)kip small files
    16 
    17 
    17 # NOTES:
    18 # NOTES:
    18 #   - it is assumed that the input file begins with ensemble #1
    19 #   - it is assumed that the input file begins with ensemble #1
    19 #   - turning-point ensembles are written to preceding profile,
    20 #   - turning-point ensembles are written to preceding profile,
    20 #     for compatibility with [yoyo]
    21 #     for compatibility with [yoyo]
    30 require "$1RDI_BB_Read.pl";
    31 require "$1RDI_BB_Read.pl";
    31 use Getopt::Std;
    32 use Getopt::Std;
    32 
    33 
    33 die("Usage: $0 " .
    34 die("Usage: $0 " .
    34 	"[-o)ut-file <fmt[e.g. 017%02dDL000.000]>] " .
    35 	"[-o)ut-file <fmt[e.g. 017%02dDL000.000]>] " .
       
    36 	"[require -m)in <ens> to produce output] " .
    35 	"<RDI file> <ens> <ens[...]>\n")
    37 	"<RDI file> <ens> <ens[...]>\n")
    36 		unless (&getopts('o:') && @ARGV>=3);
    38 		unless (&getopts('o:m:') && @ARGV>=3);
    37 
    39 
    38 $opt_o = substr($ARGV[0],0,3)					# default output filename format
    40 $opt_o = substr($ARGV[0],0,3)					# default output filename format
    39 		 . '%02d'
    41 		 . '%02d'
    40 		 . substr($ARGV[0],-9)
    42 		 . substr($ARGV[0],-9)
    41 			unless defined($opt_o);
    43 			unless defined($opt_o);
       
    44 
       
    45 $opt_m = 0 unless defined($opt_m);				# default: produce tiny files as well
    42 	
    46 	
    43 readHeader($ARGV[0],\%hdr); shift;				# get length of ensembles
    47 readHeader($ARGV[0],\%hdr); shift;				# get length of ensembles
    44 $ens_len = $hdr{ENSEMBLE_BYTES} + 2;
    48 $ens_len = $hdr{ENSEMBLE_BYTES} + 2;
    45 
    49 
    46 $first_ens = $ARGV[0]+1; shift;					# initialize loop
    50 $first_ens = $ARGV[0]+1; shift;					# initialize loop
    47 $last_ens  = $ARGV[0]; shift;
    51 $last_ens  = $ARGV[0]; shift;
    48 $cnr = 0;
    52 $cnr = 0;
    49 
    53 
    50 do {											# split data
    54 do {											# split data
    51 	sysseek(WBRF,($first_ens-2)*$ens_len,0) ||
    55 	sysseek(WBRF,($first_ens-2)*$ens_len,0) ||	# read next block of data
    52 		die("$WBRcfn: $!");
    56 		die("$WBRcfn: $!");
    53 	$last_ens++ unless defined($ARGV[0]);
    57 	$last_ens++ unless defined($ARGV[0]);
    54 	$nBytes = ($last_ens-$first_ens+1) * $ens_len;
    58 	$nBytes = ($last_ens-$first_ens+1) * $ens_len;
    55 	sysread(WBRF,$buf,$nBytes) == $nBytes ||
    59 	sysread(WBRF,$buf,$nBytes) == $nBytes ||
    56 		die("$WBRcfn: file truncated");
    60 		die("$WBRcfn: file truncated");
    57 
    61 
    58 	$fn = sprintf($opt_o,$cnr++);
    62 	if ($last_ens-$first_ens+1 > $opt_m) {		# produce file only if sufficient # of ensembles
    59 	open(F,">$fn") || die("$fn: $!\n");
    63 		$fn = sprintf($opt_o,$cnr++);
    60 	syswrite(F,$buf,$nBytes) == $nBytes ||
    64 		open(F,">$fn") || die("$fn: $!\n");
    61 		die("$fn: $!\n");
    65 		syswrite(F,$buf,$nBytes) == $nBytes ||
    62 	close(F);
    66 			die("$fn: $!\n");
    63 	printf(STDERR "$fn: %d ensembles ($nBytes bytes)\n",$last_ens-$first_ens+1);
    67 		close(F);
       
    68 	    printf(STDERR "$fn: %d ensembles ($nBytes bytes)\n",$last_ens-$first_ens+1);
       
    69 	}
    64 	
    70 	
    65 	$first_ens = $last_ens+1;
    71 	$first_ens = $last_ens+1;
    66 	$last_ens  = $ARGV[0]; shift;
    72 	$last_ens  = $ARGV[0]; shift;
    67 } while defined($last_ens);
    73 } while defined($last_ens);
    68 
    74