LADCP2CTDmatch
author A.M. Thurnherr <athurnherr@yahoo.com>
Wed, 16 May 2012 18:56:20 +0000
changeset 12 65582c172355
parent 0 de00d0f32431
permissions -rwxr-xr-x
V1.0beta3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     1
#!/usr/bin/perl
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     2
#======================================================================
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     3
#                    L A D C P 2 C T D M A T C H 
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     4
#                    doc: Thu Sep 23 22:01:19 2010
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     5
#                    dlm: Tue Sep 28 21:43:29 2010
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     6
#                    (c) 2010 A.M. Thurnherr
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     7
#                    uE-Info: 312 33 NIL 0 0 70 10 2 4 NIL ofnI
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     8
#======================================================================
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     9
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    10
$antsSummary = 'match LADCP to CTD time series';
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    11
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    12
# HISTORY:
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    13
#	Sep 23, 2010: - incepted
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    14
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    15
($ANTS) = ($0 =~ m{^(.*)/[^/]*$});
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    16
$PERL_TOOLS = '/Data/LADCP/Software/Thurnherr/perl-tools';
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    17
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    18
require "$ANTS/ants.pl";
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    19
require "$PERL_TOOLS/RDI_BB_Read.pl";
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    20
require "$PERL_TOOLS/RDI_Coords.pl";
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    21
require "$PERL_TOOLS/RDI_Utils.pl";
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    22
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    23
$antsParseHeader = 0;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    24
&antsUsage('dw:n:',2,
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    25
	'[-d)ebug] ',
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    26
	'[-w)indow <size[30s]>] [-n) <windows[20]]',
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    27
	'<RDI file> <SeaBird file>');
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    28
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    29
&antsCardOpt(\$opt_w,30);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    30
&antsCardOpt(\$opt_n,20);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    31
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    32
$LADCP_file = &antsFileArg();
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    33
$CTD_file 	= &antsFileArg();
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    34
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    35
&antsAddParams('LADCP_file',$LADCP_file,'CTD_file',$CTD_file);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    36
@antsNewLayout = ('elapsed','CTD_w','LADCP_w');
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    37
&antsActivateOut();
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    38
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    39
#----------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    40
# Step 1: Read Data
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    41
#----------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    42
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    43
print(STDERR "Reading LADCP data ($LADCP_file)...");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    44
readData($LADCP_file,\%LADCP);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    45
printf(STDERR "\n\t%d ensembles\n",scalar(@{$LADCP{ENSEMBLE}}));
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    46
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    47
print(STDERR "Reading CTD data ($CTD_file)...");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    48
open(F,$CTD_file) || croak("$CTD_file: $!\n");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    49
while (1) {											# parse header
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    50
	chomp($hdr = <F>);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    51
	$hdr =~ s/\r*$//;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    52
	croak("$0: unexpected EOF (format error)\n") unless defined($hdr);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    53
	last if ($hdr eq '*END*');
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    54
	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    55
	$CTD_nfields = $',next if ($hdr =~ /nquan = /);		# Layout
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    56
	$CTD_nrecs = $',next if ($hdr =~ /nvalues = /);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    57
	$pressF = $1,next if ($hdr =~ /name (\d+) = prDM:/);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    58
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    59
	&antsAddParams('start_time',$1),next			# selected metadata
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    60
		if ($hdr =~ /start_time = (.*)/);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    61
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    62
	&antsAddParams('station',$1),next
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    63
		if ($hdr =~ /Station\s*:\s*(.*)/);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    64
	&antsAddParams('ship',$1),next
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    65
		if ($hdr =~ /Ship\s*:\s*(.*)\s*$/);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    66
	&antsAddParams('cruise',$1),next
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    67
		if ($hdr =~ /Cruise\s*:\s*(.*)\s*$/);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    68
	&antsAddParams('time',$1),next
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    69
		if ($hdr =~ /Time\s*:\s*(.*)/);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    70
	&antsAddParams('date',$1),next
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    71
		if ($hdr =~ /Date\s*:\s*(.*)/);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    72
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    73
	if ($hdr =~ /Latitude\s*[=:]\s*/) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    74
		($deg,$min,$NS) = split(/ /,$');
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    75
		$lat = $deg + $min/60;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    76
		$lat *= -1 if ($NS eq 'S');
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    77
		&antsAddParams('lat',$lat);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    78
		next;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    79
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    80
	if ($hdr =~ /Longitude\s*[=:]\s*/) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    81
		($deg,$min,$EW) = split(/ /,$');
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    82
		$lon = $deg + $min/60;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    83
		$lon *= -1 if ($EW eq 'W');
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    84
		&antsAddParams('lon',$lon);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    85
		next;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    86
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    87
	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    88
	if ($hdr =~ /interval = seconds: /) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    89
		$CTD_sampint = 1*$';
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    90
		&antsAddParams('CTD_interval',1/$CTD_sampint);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    91
		next;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    92
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    93
	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    94
	$CTD_badval = $',next
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    95
		if ($hdr =~ /bad_flag = /);	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    96
	$CTD_file_type = $',next
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    97
		if ($hdr =~ /file_type = /);	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    98
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    99
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   100
croak("$CTD_file: cannot determine CTD file layout\n")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   101
	unless ($CTD_nfields && $CTD_nrecs);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   102
croak("$CTD_file: cannot determine missing value\n")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   103
	unless defined($CTD_badval);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   104
croak("$CTD_file: not a CTD time series file\n")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   105
	unless ($CTD_sampint);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   106
croak("$CTD_file: no pressure field\n")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   107
	unless defined($pressF);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   108
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   109
croak("$0: unknown latitude\n") unless defined($lat);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   110
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   111
if ($CTD_file_type eq 'ascii') {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   112
	while (1) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   113
		last unless (@rec = &antsFileIn(F));
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   114
		push(@CTD_press,($rec[$pressF] == $CTD_badval) ? nan : $rec[$pressF]);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   115
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   116
} elsif ($CTD_file_type eq 'binary') {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   117
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   118
	my($fbits) = 8 * length(pack('f',0));
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   119
	croak(sprintf("$0: incompatible native CPU float representation (%d instead of 32bits)\n",fbits))
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   120
		unless ($fbits == 32);	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   121
		
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   122
	croak("$CTD_file: can't read binary data\n")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   123
		unless (read(F,$dta,4*$CTD_nfields*$CTD_nrecs) == 4*$CTD_nfields*$CTD_nrecs);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   124
	print(STDERR "$CTD_file: WARNING: extraneous data at EOF\n") unless eof(F);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   125
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   126
	$dta = pack('V*',unpack('N*',$dta))				# big-endian CPU
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   127
		if (unpack('h*', pack('s', 1)) =~ /01/);	# c.f. perlport(1)
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   128
	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   129
	@dta = unpack("f*",$dta);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   130
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   131
	for ($r=0; $r<$CTD_nrecs; $r++) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   132
		push(@CTD_press,($dta[$r*$CTD_nfields+$pressF] == $CTD_badval) ? nan : $dta[$r*$CTD_nfields+$pressF]);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   133
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   134
} else {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   135
	croak("$CTD_file: unknown CTD file type $CTD_file_type\n");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   136
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   137
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   138
printf(STDERR "\n\t%d scans\n",scalar(@CTD_press));
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   139
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   140
#----------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   141
# Step 2a: Pre-Process CTD Data
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   142
#----------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   143
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   144
printf(STDERR "Pre-processing data...");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   145
printf(STDERR "\n\tCTD...");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   146
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   147
#------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   148
# calculate w and find deepest record
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   149
#------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   150
$CTD_maxpress = -9e99;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   151
for (my($r)=1; $r<$CTD_nrecs-1; $r++) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   152
	print(STDERR '.') if ($r%500==0);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   153
	$CTD_w[$r] = 0.99*($CTD_press[$r+1] - $CTD_press[$r-1]) / (2*$CTD_sampint);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   154
	if ($CTD_press[$r] > $CTD_maxpress) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   155
		$CTD_maxpress = $CTD_press[$r];
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   156
		$CTD_bottom = $r;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   157
    }										
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   158
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   159
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   160
#----------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   161
# Step 2b: Pre-Process LADCP Data
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   162
#----------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   163
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   164
print(STDERR "\n\tLADCP...");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   165
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   166
#------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   167
# construct a depth-vs-time "profile" from the raw data
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   168
#------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   169
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   170
my($LADCP_start,$LADCP_end,$LADCP_bottom,$w_gap_time,$zErr,$maxz) =
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   171
    mk_prof(\%LADCP,0,undef,1,6,70,0.1,120);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   172
croak("\n$LADCP_file: no good ensembles found\n")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   173
    unless defined($LADCP_start);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   174
    
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   175
if ($opt_d) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   176
	printf(STDERR "\n\t\tStart of cast    : %s (#%5d) at %6.1fm\n",
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   177
	                    $LADCP{ENSEMBLE}[$LADCP_start]->{TIME},
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   178
	                    $LADCP{ENSEMBLE}[$LADCP_start]->{NUMBER},
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   179
	                    $LADCP{ENSEMBLE}[$LADCP_start]->{DEPTH});
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   180
	printf(STDERR "\t\tBottom of cast   : %s (#%5d) at %6.1fm\n",
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   181
	                    $LADCP{ENSEMBLE}[$LADCP_bottom]->{TIME},
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   182
	                    $LADCP{ENSEMBLE}[$LADCP_bottom]->{NUMBER},
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   183
	                    $LADCP{ENSEMBLE}[$LADCP_bottom]->{DEPTH});
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   184
	if (defined($water_depth)) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   185
	    printf(STDERR "\t\tSeabed           :                      at %6.1fm (+-%dm)\n",$water_depth,$sig_wd);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   186
	} else {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   187
	    print(STDERR "\t\tSeabed           : not found\n");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   188
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   189
	printf(STDERR "\t\tEnd of cast      : %s (#%5d) at %6.1fm\n",
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   190
	                    $LADCP{ENSEMBLE}[$LADCP_end]->{TIME},
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   191
	                    $LADCP{ENSEMBLE}[$LADCP_end]->{NUMBER},
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   192
	                    $LADCP{ENSEMBLE}[$LADCP_end]->{DEPTH});
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   193
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   194
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   195
#------------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   196
# find 1st rec & ensemble >=80% down to max depth & make 1st guess at lag
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   197
#------------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   198
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   199
my($CTD_80pct_down) = 0;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   200
$CTD_80pct_down++
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   201
	while ($CTD_press[$CTD_80pct_down] < 0.8*$CTD_maxpress);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   202
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   203
my($LADCP_80pct_down) = 0;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   204
$LADCP_80pct_down++
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   205
	while ($LADCP{ENSEMBLE}[$LADCP_80pct_down]->{DEPTH} < 0.8*$LADCP{ENSEMBLE}[$LADCP_bottom]->{DEPTH});
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   206
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   207
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   208
my($first_guess_lag) = $LADCP{ENSEMBLE}[$LADCP_80pct_down]->{ELAPSED_TIME} -
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   209
					   $CTD_80pct_down*$CTD_sampint;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   210
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   211
printf(STDERR "\t\t1st guess offset = %ds\n",$first_guess_lag*$CTD_sampint)
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   212
	if ($opt_d);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   213
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   214
#------------------------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   215
# Linearly interpolate LADCP time series onto a new grid with $CTD_sampint resolution
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   216
#	ALSO: apply first_guess_lag to make lags small, which keeps the bestlag data
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   217
#		  chunks large
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   218
#------------------------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   219
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   220
my($nGaps) = 0;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   221
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   222
sub interp_LADCP_w($$)
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   223
{
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   224
	my($elapsed,$ens) = @_;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   225
	my($sc) = ($elapsed - $LADCP{ENSEMBLE}[$ens-1]->{ELAPSED_TIME}) /
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   226
			  ($LADCP{ENSEMBLE}[$ens]->{ELAPSED_TIME} -
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   227
					$LADCP{ENSEMBLE}[$ens-1]->{ELAPSED_TIME});
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   228
	unless (numberp($LADCP{ENSEMBLE}[$ens-1]->{W})) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   229
		$nGaps++;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   230
		croak("$0: cannot (yet) handle double gaps --- request feature upgrade\n")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   231
			unless numberp($LADCP{ENSEMBLE}[$ens]->{W});
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   232
		return $LADCP{ENSEMBLE}[$ens]->{W};
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   233
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   234
	return $LADCP{ENSEMBLE}[$ens-1]->{W} +
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   235
				$sc * ($LADCP{ENSEMBLE}[$ens]->{W} - $LADCP{ENSEMBLE}[$ens-1]->{W});
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   236
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   237
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   238
for (my($ens)=$LADCP_start,my($r)=0; $ens<=$LADCP_end; $ens++) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   239
	while ($r*$CTD_sampint < $LADCP{ENSEMBLE}[$ens]->{ELAPSED_TIME}) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   240
		$LADCP_w[$r-$first_guess_lag] = interp_LADCP_w($r*$CTD_sampint,$ens)
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   241
			unless ($first_guess_lag > $r);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   242
		$r++;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   243
    }
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   244
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   245
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   246
print(STDERR "\t\t$nGaps gaps in w timeseries")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   247
	if ($opt_d);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   248
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   249
print(STDERR "\n");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   250
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   251
#----------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   252
# Step 3: Calculate Lags
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   253
#----------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   254
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   255
printf(STDERR "Calculating $opt_n lags from %ds-long windows [s]: ",$opt_w);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   256
$opt_w = int($opt_w / $CTD_sampint);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   257
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   258
sub bestLag($)
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   259
{
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   260
	my($ws) = @_;														# window start index
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   261
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   262
	my($best);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   263
	my($bestmad) = 9e99;												# mean absolute deviation
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   264
	for (my($Llag)=-int($opt_w/2); $Llag<int($opt_w/2); $Llag++) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   265
		my($sad) = my($nad) = 0;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   266
		for (my($Ci)=0; $Ci<$opt_w; $Ci++) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   267
			my($Li) = $Ci + $Llag;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   268
			next if ($Li<0 || $Li>=$opt_w);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   269
			next unless numberp($CTD_w[$ws+$Ci]) && numberp($LADCP_w[$ws+$Li]);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   270
			$sad += abs($CTD_w[$ws+$Ci] - $LADCP_w[$ws+$Li]);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   271
			$nad++;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   272
		}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   273
		next unless ($nad > 0);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   274
		if ($sad/$nad < $bestmad) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   275
			$best = $Llag;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   276
			$bestmad = $sad/$nad;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   277
		}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   278
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   279
	return $best;	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   280
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   281
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   282
#---------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   283
# carry out opt_n lag correlations and keep tally of the results
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   284
#---------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   285
my(@nBest);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   286
for (my($window)=0; $window<$opt_n; $window++) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   287
	my($ws) = $window * int(@LADCP_w/$opt_n);			# window start
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   288
	$ws = @LADCP_w-$opt_w if ($ws+$opt_w >= @LADCP_w);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   289
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   290
	$bestLag = bestLag($ws);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   291
	if (defined($bestLag)) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   292
		printf(STDERR "%d ",$bestLag*$CTD_sampint);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   293
		$nBest{$bestLag}++;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   294
	} else {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   295
		printf(STDERR "nan ");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   296
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   297
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   298
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   299
#----------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   300
# find most popular lag
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   301
#----------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   302
my($best_lag);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   303
foreach my $i (keys(%nBest)) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   304
	$best_lag = $i if ($nBest{$i} > $nBest{$best_lag});
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   305
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   306
croak("\n$0: cannot determine a valid lag\n")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   307
	unless ($nBest{$best_lag} > 1);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   308
print(STDERR "\n\n\tWARNING: only $nBest{$best_lag} of the lag estimates agree!\n")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   309
	if ($nBest{$best_lag} < $opt_n/2);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   310
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   311
if ($nBest{$best_lag} == $opt_n) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   312
	printf(STDERR "\n\tunanimous lag = %ds\n",$best_lag*$CTD_sampint);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   313
} else {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   314
	printf(STDERR "\n\tmost popular lag = %ds\n",$best_lag*$CTD_sampint);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   315
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   316
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   317
#----------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   318
# Step 4: associate CTD data with each LADCP ensemble & write time series
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   319
#----------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   320
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   321
print(STDERR "Associating CTD data with LADCP ensembles...\n");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   322
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   323
my($overall_dt) = $CTD_sampint * ($best_lag + $first_guess_lag);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   324
for (my($ens)=$LADCP_start; $ens<=$LADCP_end; $ens++) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   325
	my($r) = int(($LADCP{ENSEMBLE}[$ens]->{ELAPSED_TIME} - $overall_dt) / $CTD_sampint);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   326
	$LADCP{ENSEMBLE}[$ens]->{CTD_W} = $CTD_w[$r];
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   327
	&antsOut($LADCP{ENSEMBLE}[$ens]->{ELAPSED_TIME},
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   328
			 $LADCP{ENSEMBLE}[$ens]->{CTD_W},
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   329
			 $LADCP{ENSEMBLE}[$ens]->{W});
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   330
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   331
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   332
&antsExit();
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   333