splitPD0
author A.M. Thurnherr <athurnherr@yahoo.com>
Tue, 29 Jun 2021 12:21:32 -0400
changeset 59 4f4530fa35da
parent 43 b63fa355644c
permissions -rwxr-xr-x
V2.4 - New Features: - support for Nortek PD0 files - patchPD0 support for moored ADCP data - bug fixes & minor improvements
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     1
#!/usr/bin/perl
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     2
#======================================================================
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
     3
#                    S P L I T P D 0 
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     4
#                    doc: Sat Aug 21 22:20:27 2010
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
     5
#                    dlm: Mon Apr  2 15:49:09 2018
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     6
#                    (c) 2010 A.M. Thurnherr
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
     7
#                    uE-Info: 28 36 NIL 0 0 72 2 2 4 NIL ofnI
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     8
#======================================================================
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     9
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    10
# split RDI files based on list of ensemble numbers (e.g. from yoyo -t)
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    11
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    12
# HISTORY:
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    13
#	Aug 21, 2010: - created
7
e06925788055 post IWISE_2011
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    14
#	Jun 24, 2011: - replaced -b, -n by -o
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 7
diff changeset
    15
#   Feb 13, 2014: - updated doc
18
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    16
#	Mar 19, 2014: - added -s)kip small files
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    17
#	Sep 14, 2014: - added -f)irst
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    18
#	Jul 26, 2016: - changed file numbering to 1-relative
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    19
#	Jul 30, 2016: - modified -o default
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    20
#				  - added code to set DSID of first ensemble of each
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    21
#					output file to 0x7f7f
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    22
#	Apr  2, 1018: - BUG in error messages
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    23
#				  - added header id check
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    24
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    25
# NOTES:
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 7
diff changeset
    26
#   - it is assumed that the input file begins with ensemble #1
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    27
#   - turning-point ensembles are written to next profile,
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    28
#     for compatibility with [yoyo]?
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    29
7
e06925788055 post IWISE_2011
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    30
# FILE NAME CONVENTION:
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 7
diff changeset
    31
#   - in order to assign individual yoyo casts numerical station numbers,
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    32
#     by default, an underscore and the yoyo cast number is added to the basename
7
e06925788055 post IWISE_2011
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    33
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 7
diff changeset
    34
# EXAMPLES:
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 7
diff changeset
    35
#   splitRDI 017DL000.000 `mkProfile 017DL000.000 | yoyo -QFens -ut`
7
e06925788055 post IWISE_2011
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    36
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    37
$0 =~ m{(.*/)[^/]+};
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    38
require "$1RDI_BB_Read.pl";
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    39
use Getopt::Std;
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    40
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    41
die("Usage: $0 " .
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    42
	"[-o)ut-file <fmt[e.g. 017DL_%02d.000]>] " .
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    43
	"[-f)irst output <cast #>] " .
18
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    44
	"[require -m)in <ens> to produce output] " .
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    45
	"<RDI file> <ens> <ens[...]>\n")
21
0b5bbe60131c version sent to P16N
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 18
diff changeset
    46
		unless (&getopts('f:o:m:') && @ARGV>=3);
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    47
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    48
unless (defined($opt_o)) {
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    49
	my($bn,$extn) = ($ARGV[0] =~ m{([^/]+)\.([^\.]+)$});
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    50
	$opt_o = "${bn}_%02d.$extn";
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    51
}
18
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    52
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    53
$opt_m = 0 unless defined($opt_m);								# default: produce tiny files as well
7
e06925788055 post IWISE_2011
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    54
	
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    55
$fn = $ARGV[0]; shift;
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    56
readHeader($fn,\%hdr); 											# get length of ensembles
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    57
$ens_len = $hdr{ENSEMBLE_BYTES} + 2;
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    58
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    59
$first_ens = $ARGV[0]+1; shift;									# initialize loop
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    60
$last_ens  = $ARGV[0]; shift;
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    61
$cnr = 1;
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    62
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    63
do {															# split data
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    64
	sysseek(WBRF,($first_ens-2)*$ens_len,0) ||					# begin next block of ensembles
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    65
		die("$fn: $!");
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    66
	$last_ens++ unless defined($fn);
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    67
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    68
	sysread(WBRF,$ids,2) || die("$fn: file truncated");			# read 1st ensemble & ensure DSID is 0x7f
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    69
	die("$fn: illegal header id [0x" . unpack('H4',$ids) . "]")	# require 1st byte to be 0x7f
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    70
		unless (substr(unpack('H4',$ids),0,2) eq '7f');
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    71
	$ids = pack('H4','7f7f');									# reset DSID
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    72
	sysread(WBRF,$febuf,$ens_len-4) == $ens_len-4 ||
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    73
		die("$fn: file truncated");
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    74
	sysread(WBRF,$csum,2) || die("$fn: file truncated");
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    75
	$csum = pack('v',unpack('%16C*',$ids.$febuf));				# re-calculate checksum
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    76
	
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    77
	$nBytes = ($last_ens-$first_ens) * $ens_len;				# read remaining ensembles in block
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    78
	sysread(WBRF,$buf,$nBytes) == $nBytes ||
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    79
		die("$fn: file truncated (ends before ens#$last_ens)");
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    80
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    81
	if ($last_ens-$first_ens+1 >= $opt_m) {						# write output only if sufficient # of ensembles
21
0b5bbe60131c version sent to P16N
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 18
diff changeset
    82
		$fn = sprintf($opt_o,$opt_f+$cnr++);
18
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    83
		open(F,">$fn") || die("$fn: $!\n");
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    84
		syswrite(F,$ids.$febuf.$csum.$buf,$nBytes+$ens_len) == $nBytes+$ens_len ||
18
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    85
			die("$fn: $!\n");
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    86
		close(F);
36
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    87
	    printf(STDERR "$fn: %d ensembles (%d bytes)\n",
515b06dae59c version at end of ECOGIG EN586 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    88
						$last_ens-$first_ens+1,$nBytes+$ens_len);
18
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    89
	}
1
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    90
	
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    91
	$first_ens = $last_ens+1;
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    92
	$last_ens  = $ARGV[0]; shift;
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    93
} while defined($last_ens);
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    94
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    95
exit(0);
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    96
a3b6a908dec5 after P302 cruise
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    97