LADCPproc.bestLag
author A.M. Thurnherr <athurnherr@yahoo.com>
Fri, 06 Mar 2015 15:47:19 +0000
changeset 29 f72cd642972c
parent 25 91bd907db97f
child 33 dd5b67a41791
permissions -rw-r--r--
whoosher
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
#======================================================================
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     2
#                    L A D C P P R O C . B E S T L A G 
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     3
#                    doc: Tue Sep 28 21:58:48 2010
29
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
     4
#                    dlm: Sun May 25 21:16:27 2014
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     5
#                    (c) 2010 A.M. Thurnherr
29
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
     6
#                    uE-Info: 31 72 NIL 0 0 72 2 2 4 NIL ofnI
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     7
#======================================================================
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
# TODO:
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    10
#	- first lag is always(?) nan unless CTD is turned on with LADCP in water
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    11
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    12
# HISTORY:
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    13
#	Sep 28, 2010: - created
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    14
#	Dec  9, 2010: - adapted to %CTD
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    15
#	Dec 10, 2010: - hardened bestlag failure test to require 1/3 agreeing lags
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    16
#	Jan  5, 2011: - changed first guess from 80% down to 10% down
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    17
#				  - added LADCP time lag to %PARAMs
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    18
#				  - added support of -i
3
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    19
#	Jul  7, 2011: - added code to remove window-mean of w before lagging to
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    20
#				    make it work in regions of crazy ocean w (IWISE 16007)
6
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    21
#	Jul 15, 2011: - changed screen-output of lag to take first guess lag into
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    22
#					account
9
48b2d27aaebf after fixing bad magdec bug
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
    23
#	Apr 11, 2011: - removed 1st guess lag consistency check based on large
48b2d27aaebf after fixing bad magdec bug
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
    24
#					elapsed offsets
14
e22f4b440407 V1.0beta5
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 9
diff changeset
    25
#	May 18, 2012: - BUG: window start index was not always calculated correctly
17
343ba18738cf V1.0beta8
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    26
#	Oct 19, 2012: - BUG: opt_i had wrong sign!
20
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 17
diff changeset
    27
#	Jun 25, 2013: - adapted to :: %PARAM convention
25
91bd907db97f before debugging weird nsamp problem
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 20
diff changeset
    28
#	Mar 19, 2014: - moved %PARAM to LADCPproc
29
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
    29
#	May 25, 2015: - added assertion to require numeric interpolated LADCP_w
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
    30
#				  - BUG: interp_LADCP_w left gaps
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
    31
#				  - added debug code to output bestLag input time series
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    32
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    33
sub interp_LADCP_w($$)
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
	my($elapsed,$ens) = @_;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    36
	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
    37
			  ($LADCP{ENSEMBLE}[$ens]->{ELAPSED_TIME} -
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    38
					$LADCP{ENSEMBLE}[$ens-1]->{ELAPSED_TIME});
29
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
    39
	unless (numberp($LADCP{ENSEMBLE}[$ens]->{W})) {
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    40
		$nGaps++;
29
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
    41
		$LADCP{ENSEMBLE}[$ens]->{W} = $LADCP{ENSEMBLE}[$ens-1]->{W};
0
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
	return $LADCP{ENSEMBLE}[$ens-1]->{W} +
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    44
				$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
    45
}
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
sub bestLag($)
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    48
{
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    49
	my($ws) = @_;														# window start index
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    50
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    51
	my($best);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    52
	my($bestmad) = 9e99;												# mean absolute deviation
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    53
	for (my($Llag)=-int($opt_w/2); $Llag<int($opt_w/2); $Llag++) {
3
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    54
		my($mCw,$mLw,$nw) = (0,0,0);									# first calc means
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    55
		for (my($Ci)=0; $Ci<$opt_w; $Ci++) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    56
			my($Li) = $Ci + $Llag;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    57
			next if ($Li<0 || $Li>=$opt_w);
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    58
			next unless numberp($CTD{w}[$ws+$Ci]) && numberp($LADCP_w[$ws+$Li]);
3
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    59
			$mCw += $CTD{w}[$ws+$Ci];
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    60
			$mLw += $LADCP_w[$ws+$Li];
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    61
			$nw++;
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    62
		}
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    63
		next unless ($nw > 0);
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    64
		$mCw /= $nw; $mLw /= $nw;
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    65
		
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    66
		my($sad) = my($nad) = 0;										# calc mad with means removed
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    67
		for (my($Ci)=0; $Ci<$opt_w; $Ci++) {
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    68
			my($Li) = $Ci + $Llag;
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    69
			next if ($Li<0 || $Li>=$opt_w);
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    70
			next unless numberp($CTD{w}[$ws+$Ci]) && numberp($LADCP_w[$ws+$Li]);
711dd29cb6dd post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 1
diff changeset
    71
			$sad += abs($CTD{w}[$ws+$Ci]-$mCw - ($LADCP_w[$ws+$Li]-$mLw));
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    72
			$nad++;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    73
		}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    74
		if ($sad/$nad < $bestmad) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    75
			$best = $Llag;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    76
			$bestmad = $sad/$nad;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    77
		}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    78
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    79
	return $best;	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    80
}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    81
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    82
sub lagLADCP2CTD()
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    83
{
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    84
	#------------------------------------------------------------------------
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    85
	# find 1st rec & ensemble >=10% down to max depth & make 1st guess at lag
0
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
	
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    88
	my($first_guess_lag);											# in units of CTD records
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    89
	
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    90
	if (defined($opt_i)) {
17
343ba18738cf V1.0beta8
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    91
		$first_guess_lag = $opt_i / $CTD{sampint};
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    92
	} else {
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    93
		my($CTD_10pct_down) = 0;
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    94
		$CTD_10pct_down++											# "until" formulation allows for missing pressures
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    95
			until ($CTD{press}[$CTD_10pct_down]-$CTD{press}[0] >= 0.1*($CTD{maxpress}-$CTD{press}[0]));
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    96
	    
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    97
		my($LADCP_10pct_down) = 0;
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    98
		$LADCP_10pct_down++
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    99
			while ($LADCP{ENSEMBLE}[$LADCP_10pct_down]->{DEPTH} < 0.1*$LADCP{ENSEMBLE}[$LADCP_bottom]->{DEPTH});
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   100
	    
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   101
		$first_guess_lag = ($LADCP{ENSEMBLE}[$LADCP_10pct_down]->{ELAPSED_TIME} -
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   102
							   $CTD_10pct_down*$CTD{sampint}) / $CTD{sampint};
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   103
	    
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   104
		printf(STDERR "\n\t1st guess offset [CTD pressure, LADCP estimated depth] = %ds [%ddbar, %dm]\n",
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   105
				$first_guess_lag*$CTD{sampint},$CTD{press}[$CTD_10pct_down],$LADCP{ENSEMBLE}[$LADCP_10pct_down]->{DEPTH})
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   106
					if ($opt_d);
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   107
	}
0
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
	#------------------------------------------------------------------------------------
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   110
	# Linearly interpolate LADCP time series onto a new grid with $CTD{sampint} resolution
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   111
	#	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
   112
	#		  chunks large
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   113
	#------------------------------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   114
	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   115
	$nGaps = 0;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   116
	
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   117
	for (my($ens)=$LADCP_start,my($r)=0; $ens<=$LADCP_end; $ens++) {
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   118
		while ($r*$CTD{sampint} < $LADCP{ENSEMBLE}[$ens]->{ELAPSED_TIME}) {
14
e22f4b440407 V1.0beta5
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 9
diff changeset
   119
			unless ($first_guess_lag > $r) {
29
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   120
				my($w) = interp_LADCP_w($r*$CTD{sampint},$ens);
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   121
				next if (!defined($firstValid) && !defined($w));
14
e22f4b440407 V1.0beta5
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 9
diff changeset
   122
				$firstValid = $r-$first_guess_lag unless defined($firstValid);
29
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   123
				die("assertion failed") unless defined($w);
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   124
				$LADCP_w[$r-$first_guess_lag] = $w;
14
e22f4b440407 V1.0beta5
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 9
diff changeset
   125
				$nValid++;
e22f4b440407 V1.0beta5
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 9
diff changeset
   126
			}
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   127
			$r++;
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
	}
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
	print(STDERR "\t$nGaps gaps in w timeseries")
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   132
		if ($opt_d);
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
	print(STDERR "\n");
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   135
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   136
	#----------------------------------------------------------------------
29
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   137
	# Output w Time Series
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   138
	#----------------------------------------------------------------------
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   139
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   140
#	open(F,'>bestLag.out');
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   141
#	print(F "#ANTS#FIELDS# {rec} {LADCP_w} {CTD_w}\n");
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   142
#	for (my($r)=$firstValid; $r<$firstValid+$nValid; $r++) {
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   143
#		print(F "$r $LADCP_w[$r] $CTD{w}[$r]\n");
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   144
#	}
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   145
#	close(F);
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   146
f72cd642972c whoosher
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   147
	#----------------------------------------------------------------------
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   148
	# Calculate lags
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
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   151
	printf(STDERR "\tcalculating $opt_n lags from %ds-long windows [s]:",$opt_w);
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   152
	$opt_w = int($opt_w / $CTD{sampint});
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   153
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   154
	#---------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   155
	# 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
   156
	#---------------------------------------------------------------
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   157
	my(%nBest);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   158
	my($nLags) = 0;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   159
	my($lags) = '';
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   160
	my($lastLag) = 9e99; my($nSame) = 1;
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   161
	for (my($window)=0; $window<$opt_n; $window++) {
14
e22f4b440407 V1.0beta5
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 9
diff changeset
   162
		my($ws) = $firstValid + $window * int($nValid/$opt_n);	# window start
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   163
		$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
   164
		$bestLag = bestLag($ws);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   165
		if (defined($bestLag)) {
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   166
			if (defined($lastLag) && $bestLag == $lastLag) {
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   167
				$nSame++;
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   168
			} else {
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   169
				printf(STDERR "(x%d)",$nSame)
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   170
					if ($nSame > 1);
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   171
				printf(STDERR " %d",$bestLag*$CTD{sampint});
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   172
				$nSame = 1;
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   173
				$lastLag = $bestLag;
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   174
			}
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   175
			$lags .= sprintf(" %s",$bestLag*$CTD{sampint});
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   176
			$nBest{$bestLag}++;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   177
			$nLags++;
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   178
		} else {
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   179
			if (!defined($lastLag)) {
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   180
				$nSame++;
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   181
			} else {
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   182
				printf(STDERR "(x%d)",$nSame)
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   183
					if ($nSame > 1);
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   184
				printf(STDERR " nan");
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   185
				$nSame = 1;
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   186
				$lastLag = $bestLag;
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   187
			}
0
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
	}
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   190
	printf(STDERR "(x%d)",$nSame) if ($nSame > 1);
20
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 17
diff changeset
   191
    &antsAddParams('LADCPproc::time_lags',$lags);
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   192
	
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
	# find most popular lag
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
	my($best_lag);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   197
	foreach my $i (keys(%nBest)) {
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   198
		$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
   199
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   200
	croak("\n$0: cannot determine a valid lag\n")
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   201
		unless ($nBest{$best_lag} > $opt_n/3);
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   202
	print(STDERR "\n\n\t\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
   203
		if ($nBest{$best_lag} < $opt_n/2);
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   204
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   205
	if ($nBest{$best_lag} == $nLags) {
6
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   206
		printf(STDERR "\n\t\tunanimous lag = %ds\n",($first_guess_lag+$best_lag)*$CTD{sampint});
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   207
	} else {
6
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   208
		printf(STDERR "\n\t\tmost popular lag = %ds\n",($first_guess_lag+$best_lag)*$CTD{sampint});
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   209
	}
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   210
1
54222c82435f after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   211
	return ($first_guess_lag + $best_lag) * $CTD{sampint};
0
de00d0f32431 initial version to process P403 yoyo
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
   212
}
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
1;