time_lag.pl
author A.M. Thurnherr <athurnherr@yahoo.com>
Wed, 28 Aug 2019 17:51:34 -0400
changeset 52 4ccccbf69dfd
parent 49 5006e9158207
child 56 8f120b9f795a
permissions -rw-r--r--
working?
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     1
#======================================================================
2
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
     2
#                    T I M E _ L A G . P L 
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     3
#                    doc: Fri Dec 17 21:59:07 2010
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
     4
#                    dlm: Tue Oct 16 16:26:06 2018
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     5
#                    (c) 2010 A.M. Thurnherr
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
     6
#                    uE-Info: 80 38 NIL 0 0 72 2 2 4 NIL ofnI
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     7
#======================================================================
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     8
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     9
# HISTORY:
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    10
#	Dec 17, 2010: - created
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    11
#	Dec 18, 2010: - adapted for multi-pass lagging
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    12
#	Dec 20: 2010: - added code to adjust start and end of profile ens
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    13
#				    based on extent of CTD profile and guestimated time
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    14
#				    ofset
2
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    15
#	Jun 26, 2010: - added heuristic to chose between weighted-mean and
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    16
#					unambiguously best offsets
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    17
#				  - turned -3 criterion into warning when 3 lags are consecutive
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    18
#	Jul  4, 2011: - increased MAX_ALLOWED_THREE_LAG_SPREAD from 2 to 3
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    19
#	Jul  7, 2011: - removed window-mean w before time lagging to allow lagging
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    20
#				    of casts with large w
3
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 2
diff changeset
    21
#	Aug  4, 2011: - made code use weighted average unless best lag accounts for
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 2
diff changeset
    22
#				    more than 2/3 of lags (instead of 50%)
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 2
diff changeset
    23
#	Sep 23, 2011: - added mad info to best lag guesses
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 2
diff changeset
    24
#				  - removed window-doubling heuristics
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    25
#	Oct 12, 2011: - moved defaults to [defaults.pl]
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    26
#				  - BUG: code did not work correctly when there were less than 3
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    27
#						 valid offsets
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    28
#				  - BUG: code did not work correctly unless all windows
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    29
#						 returned valid offsets
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    30
#	Oct 13, 2011: - added $TL_out and $TL_hist_out
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    31
#				  - restricted 2nd-pass to time lags +-1.5s
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    32
#				  - tightened allowed spread for weighted-average calc
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    33
#				  - BUG: $le in &bestLag() could be > $lastGoodEns
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    34
#				  - disabled weighted-average offset calculation
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    35
#	Oct 14, 2011: - improved handling of %PARAMs
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    36
#				  - BUG: last ens of window estimation was off, probably accounting
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    37
#						 for Oct 13 BUG (fix disabled)
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
    38
#				  - renamed _out to out_
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
    39
#	Oct 17, 2011: - BUG: closed STDOUT caused problems with tee in plotting scripts
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
    40
#	Oct 19, 2011: - BUG: windowing did not work correctly for short casts
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
    41
#				  - BUG: search restarting did not work correctly
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
    42
#				  - modified edge-of-window heuristics
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
    43
#				  - added step to remove all lags with mad > median(mads)
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
    44
#	Oct 20, 2011: - losened too-restrictive last step
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
    45
#	Oct 21, 2011: - BUG: forgot to update $n_valid_windows while removing outlier lags
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
    46
#	Oct 15, 2012: - added $cast_type to &calc_lag()
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
    47
#				  - removed support for TLhist
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
    48
#	Oct 16, 2012: - renamed field elapsed to elapsed.LADCP for clarity
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
    49
#				  - made failure "soft"
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
    50
#	Mar 23, 2012: - adapted to piece-wise time lagging
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
    51
#	Apr 22, 2013: - replaced $max_allowed_w by $opt_m, $TL_required_top_three_fraction by $opt_3
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
    52
#	May 14, 2013: - opt_m => w_max_lim
15
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 13
diff changeset
    53
#	Mar  3, 2014: - BUG: var-name typo
16
29e867b3e070 whoosher version at beginning of FZ1
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 15
diff changeset
    54
#	May 23, 2014: - BUG: $s range check required in mad_w()
25
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
    55
#   Apr 16, 2015: - turned output specifies into lists (re-design of
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
    56
#                   plotting sub-system)
26
b89d4b01fcc5 after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
    57
#				  - BUG: executable flag was not set on file output
b89d4b01fcc5 after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
    58
#				  - disabled active output when ANTS are not available
27
2053d8de8d6b updated plots & seabed code; GoM data process correctly
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 26
diff changeset
    59
#				  - croak -> error
29
c1ff35103176 a few days pre WHOI
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 27
diff changeset
    60
#	May 15, 2015: - fiddled with assertions
c1ff35103176 a few days pre WHOI
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 27
diff changeset
    61
#	Jun 19, 2015: - disabled L2 warning on partial-depth time-lagging failures
30
7fb67e771d85 LWplots expunged
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 29
diff changeset
    62
#	Jul 29, 2015: - support for new plotting system
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
    63
#	Jan 22, 2016: - started adding support for timelag-filtering
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
    64
#	Jan 23, 2016: - continued
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
    65
#	Jan 24, 2016: - made it work
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
    66
#				  - BUG: time-lag plot was not produced when final lag piece had problems
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
    67
#	Jan 25, 2016: - search-radius-doubling heuristic had typo
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
    68
#				  - added %PARAMs
35
54b8bb450e5f Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
    69
#	Feb 19, 2016: - added support for -l
54b8bb450e5f Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
    70
#				  - added warning
54b8bb450e5f Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
    71
#	Mar  7, 2016: - BUG: editing did not work correctly in all cases
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
    72
#	Mar  6, 2017: - BUG: assertion in mad_w failed with 2017 P18 DL#206
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
    73
#	Mar  9, 2017: - tightened timelag editing (good_diff: 2->1)
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
    74
#	Mar 22, 2018: - re-wrote heuristics to remove lags with large mads
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
    75
#				  - BUG: bestLag with 1 valid sample returned 0 mad
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
    76
#				  - BUG: timelag editing did not work correctly when there was not a sufficiently long valid lag
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
    77
#	Mar 27, 2018: - BUG: re-written heuristic could fail when there was a valid but unpopular lag with very low mad.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
    78
#						 Solution: remove very unpopular lags first
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
    79
#	Oct  4, 2018: - added timelagging debug code
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
    80
#	Oct 16, 2018: - removed debug code
3
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 2
diff changeset
    81
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 2
diff changeset
    82
# DIFFICULT STATIONS:
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
    83
#	NBP0901#131		this requires the search-radius doubling heuristic
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    84
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    85
# TODO:
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    86
#	- better seabed code (from LADCPproc)
2
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    87
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
    88
my($TINY) = 1e-6;
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
    89
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
    90
sub mad_w($$$)																# mean absolute deviation
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    91
{
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
    92
	my($fe,$le,$so) = @_;													# first/last LADCP ens, CTD scan offset
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    93
2
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
    94
	my($LADCP_mean_w,$CTD_mean_w,$nsamp) = (0,0,0);
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
    95
	for (my($e)=$fe; $e<=$le; $e++) {										# first, calculate mean w in window
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    96
		my($s) = int(($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[0]) / $CTD{DT} + 0.5);
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
    97
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
    98
#	THE FOLLOWING LINE CAUSES AN ASSERTION FAILURE WITH 2017 P08 DL#206. I AM NOT SURE WHETHER MY
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
    99
#	FIX SOLVES THE UNDERLYING PROBLEM OR ONLY THIS SPECIAL CASE.
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   100
#		next unless ($s>=0 && $s<=$#{$CTD{ELAPSED}});
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   101
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   102
		next unless ($s>0 && $s<=$#{$CTD{ELAPSED}});
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   103
		die("assertion failed\n" .
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   104
			"\ttest: abs($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[$s]) <= $CTD{DT}/2\n" .
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   105
			"\te = $e, s = $s, ensemble = $LADCP{ENSEMBLE}[$e]->{NUMBER}"
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   106
		) unless (abs($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[$s]) <= $CTD{DT}/2+$TINY);
2
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   107
		next unless numberp($LADCP{ENSEMBLE}[$e]->{REFLR_W});
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   108
		$LADCP_mean_w += $LADCP{ENSEMBLE}[$e]->{REFLR_W};
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   109
		$CTD_mean_w   += $CTD{W}[$s+$so];
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   110
		$nsamp++;
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   111
	}
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   112
	return 9e99 unless ($nsamp > 1);
2
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   113
	$LADCP_mean_w /= $nsamp;
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   114
	$CTD_mean_w /= $nsamp;
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   115
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   116
	my($sad) = $nsamp = 0;													# now, calculate mad
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   117
	for (my($e)=$fe; $e<=$le; $e++) {			
2
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   118
		my($s) = int(($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[0]) / $CTD{DT} + 0.5);
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   119
		next unless ($s>=0 && $s<=$#{$CTD{ELAPSED}});
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   120
		next unless numberp($LADCP{ENSEMBLE}[$e]->{REFLR_W});
2
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   121
		my($dw) = $LADCP{ENSEMBLE}[$e]->{REFLR_W}-$LADCP_mean_w - ($CTD{W}[$s+$so]-$CTD_mean_w);
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   122
#		print(STDERR "dw = $dw ($LADCP{ENSEMBLE}[$e]->{REFLR_W}-$LADCP_mean_w - ($CTD{W}[$s+$so]-$CTD_mean_w)\n");
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   123
		next unless (abs($dw) <= $w_max_lim);
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   124
		$sad += abs($dw); $nsamp++;
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   125
	}
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   126
	return $nsamp ? $sad/$nsamp : 9e99;
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   127
}
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   128
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   129
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   130
sub bestLag($$$$)								# find best lag in window
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   131
{
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   132
	my($fe,$le,$ww,$soi) = @_;					# first/last LADCP ens, window width, scan-offset increment
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   133
	my($bestso) = 0;							# error at first-guess offset
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   134
	my($bestmad) = mad_w($fe,$le,0);
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   135
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   136
#	print(STDERR "bestLag($fe,$le,$ww,$soi)\n");
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   137
	for (my($dso) = 1; $dso <= int($ww/2/$CTD{DT} + 0.5); $dso+=$soi) {
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   138
		my($mad) = mad_w($fe,$le,-$dso);
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   139
#		print(STDERR "-$dso $mad\n");
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   140
		$bestmad=$mad,$bestso=-$dso if ($mad < $bestmad);
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   141
		$mad = mad_w($fe,$le,$dso);
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   142
#		print(STDERR " $dso $mad\n");
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   143
		$bestmad=$mad,$bestso=$dso if ($mad < $bestmad);
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   144
	}
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   145
#	print(STDERR "-> $bestso $bestmad\n");
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   146
	return ($bestso,$bestmad);
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   147
}
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   148
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   149
#----------------------------------------------------------------------
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   150
# carry out lag correlations and keep tally of the results
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   151
#----------------------------------------------------------------------
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   152
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   153
{ # STATIC SCOPE
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   154
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   155
local(@elapsed_buf,@so_buf,@mad_buf,@bmo_buf,@fg_buf,$lg_buf,$elapsed_min_buf);		# available to plot routines
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   156
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   157
sub calc_lag($$$$$)
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   158
{
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   159
	my($n_windows,$w_size,$scan_increment,$first_ens,$last_ens) = @_;
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   160
	my($search_radius) = $scan_increment==1 ? 3 : $w_size;
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   161
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   162
	&antsAddParams('TL_max_allowed_three_lag_spread',$TL_max_allowed_three_lag_spread);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   163
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   164
	my($ctmsg);
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   165
	if ($first_ens==$firstGoodEns && $last_ens==$lastGoodEns) 	{ $ctmsg = "full-cast"; }
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   166
	else														{ $ctmsg = "partial-cast"; }
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   167
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   168
	my($last_lag_piece) = ($last_ens == $lastGoodEns);								# none is following
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   169
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   170
RETRY:
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   171
	my($failed) = undef;
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   172
	progress("Calculating $n_windows $ctmsg time lags from ${w_size}s-long windows at %dHz resolution...\n",
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   173
		int(1/$scan_increment/$CTD{DT}+0.5));
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   174
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   175
	my($approx_CTD_profile_start_ens) =
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   176
		$firstGoodEns + int(($CTD{ELAPSED}[0] - $CTD{TIME_LAG}) / $LADCP{MEAN_DT});
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   177
	my($approx_CTD_profile_end_ens) =
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   178
		$firstGoodEns + int(($CTD{ELAPSED}[$#{$CTD{ELAPSED}}] + $CTD{ELAPSED}[0] - $CTD{TIME_LAG}) / $LADCP{MEAN_DT});
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   179
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   180
	my($approx_joint_profile_start_ens) = max($firstGoodEns,$approx_CTD_profile_start_ens) + 10;
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   181
	my($approx_joint_profile_end_ens) 	= min($lastGoodEns,$approx_CTD_profile_end_ens) - 10;
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   182
	debugmsg("profile start: $firstGoodEns -> $approx_joint_profile_start_ens\n");
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   183
	debugmsg("profile end  : $lastGoodEns -> $approx_joint_profile_end_ens\n");
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   184
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   185
	my($window_ens) = int($w_size/$LADCP{MEAN_DT}+0.5);
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   186
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   187
	my(@elapsed,@so,@mad,%nBest,%madBest);
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   188
	my($n_valid_windows) = 0;
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   189
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   190
	$first_ens = $approx_joint_profile_start_ens
29
c1ff35103176 a few days pre WHOI
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 27
diff changeset
   191
		if ($first_ens < $approx_joint_profile_start_ens);
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   192
	$last_ens = $approx_joint_profile_end_ens
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   193
		if ($last_ens > $approx_joint_profile_end_ens);
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   194
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   195
	for (my($wi)=0; $wi<$n_windows; $wi++) {									# use bestLag() in each window
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   196
		my($fe) = $first_ens + int(($last_ens-$first_ens-$window_ens)*$wi/($n_windows-1)+0.5);
29
c1ff35103176 a few days pre WHOI
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 27
diff changeset
   197
		die("assertion failed\n\tfe = $fe, first_ens = $first_ens, last_ens = $last_ens, window_ens = $window_ens, firstGoodEns = $firstGoodEns, lastGoodEns = $lastGoodEns")
c1ff35103176 a few days pre WHOI
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 27
diff changeset
   198
			unless ($fe>=$firstGoodEns && $fe+$window_ens<=$lastGoodEns);
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   199
		my($so,$mad) = bestLag($fe,$fe+$window_ens,$search_radius,$scan_increment);
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   200
		$elapsed[$wi] = $LADCP{ENSEMBLE}[$fe+int($w_size/2/$LADCP{MEAN_DT}+0.5)]->{ELAPSED};
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   201
		die("assertion failed\nfe=$fe, lastGoodEns=$lastGoodEns, w_size=$w_size") unless ($elapsed[$wi]);
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   202
		next unless ($mad < 9e99);
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   203
		$so[$wi] = $so; $mad[$wi] = $mad;
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   204
		$n_valid_windows++;
3
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 2
diff changeset
   205
		$nBest{$so}++; $madBest{$so} += $mad;
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   206
	}
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   207
	my($maxN) = 0;
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   208
	foreach my $i (keys(%nBest)) {
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   209
		$maxN = $nBest{$i} if ($nBest{$i} > $maxN);
3
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 2
diff changeset
   210
		$madBest{$i} /= $nBest{$i};
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   211
	}
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   212
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   213
	foreach my $lag (keys(%nBest)) {										# remove unpopular lags
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   214
		next if ($nBest{$lag} >= $maxN/10);
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   215
		$n_valid_windows -= $nBest{$lag};
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   216
		$nBest{$lag} = 0; $madBest{$lag} = 9e99;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   217
	}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   218
	
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   219
##	my($med_mad) = median(values(%madBest));								# remove lags with large mads
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   220
##	my($mad_mad) = mad2($med_mad,values(%madBest));
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   221
##	foreach my $lag (keys(%nBest)) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   222
##		next if ($madBest{$lag} <= $med_mad+$mad_mad);
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   223
##		$n_valid_windows -= $nBest{$lag};
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   224
##		$nBest{$lag} = 0;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   225
##  }
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   226
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   227
	my($min_mad) = min(values(%madBest));									# remove lags with large mads
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   228
	foreach my $lag (keys(%nBest)) {
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   229
		next if ($madBest{$lag} <= 3*$min_mad);
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   230
		$n_valid_windows -= $nBest{$lag};
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   231
		$nBest{$lag} = 0;
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   232
	}
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   233
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   234
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   235
	my(@best_lag);															# find 3 most popular lags
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   236
	foreach my $lag (keys(%nBest)) {
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   237
		$best_lag[0] = $lag if ($nBest{$lag} > $nBest{$best_lag[0]});
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   238
	}
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   239
	foreach my $lag (keys(%nBest)) {
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   240
		next if ($lag == $best_lag[0]);
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   241
		$best_lag[1] = $lag if ($nBest{$lag} > $nBest{$best_lag[1]});
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   242
	}
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   243
	foreach my $lag (keys(%nBest)) {
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   244
		next if ($lag == $best_lag[0] || $lag == $best_lag[1]);
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   245
		$best_lag[2] = $lag if ($nBest{$lag} > $nBest{$best_lag[2]});
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   246
	}
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   247
	if ($nBest{$best_lag[2]}) {												# there are at least 3 lags
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   248
		progress("\t3 most popular offsets: %d (%d%% %.1fcm/s mad), %d (%d%% %.1fcm/s mad), %d (%d%% %.1fcm/s mad)\n",
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   249
			$best_lag[0],int(($nBest{$best_lag[0]}/$n_valid_windows)*100+0.5),100*$madBest{$best_lag[0]},
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   250
			$best_lag[1],int(($nBest{$best_lag[1]}/$n_valid_windows)*100+0.5),100*$madBest{$best_lag[1]},
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   251
	        $best_lag[2],int(($nBest{$best_lag[2]}/$n_valid_windows)*100+0.5),100*$madBest{$best_lag[2]});
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   252
	} elsif ($nBest{$best_lag[1]}) {										# there are only 2 lags
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   253
		progress("\toffsets: %d (%d%% %.1fcm/s mad), %d (%d%% %.1fcm/s mad)\n",
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   254
			$best_lag[0],int(($nBest{$best_lag[0]}/$n_valid_windows)*100+0.5),100*$madBest{$best_lag[0]},
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   255
			$best_lag[1],int(($nBest{$best_lag[1]}/$n_valid_windows)*100+0.5),100*$madBest{$best_lag[1]});
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   256
	} else {																# there is only 1 lag
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   257
		progress("\toffset: %d (%d%% %.1fcm/s mad)\n",
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   258
			$best_lag[0],int(($nBest{$best_lag[0]}/$n_valid_windows)*100+0.5),100*$madBest{$best_lag[0]});
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   259
	}
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   260
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   261
	unless ($nBest{$best_lag[0]}+$nBest{$best_lag[1]}+$nBest{$best_lag[2]}	# require quorum
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   262
				>= $opt_3*$n_valid_windows) {
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   263
		if (max(@best_lag)-min(@best_lag) > $TL_max_allowed_three_lag_spread) {
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   264
			warning(2,"$0: cannot determine a valid $ctmsg lag; top 3 tags account for %d%% of total (use -3 to relax criterion)\n",
29
c1ff35103176 a few days pre WHOI
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 27
diff changeset
   265
				int(100*($nBest{$best_lag[0]}+$nBest{$best_lag[1]}+$nBest{$best_lag[2]})/$n_valid_windows+0.5))
c1ff35103176 a few days pre WHOI
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 27
diff changeset
   266
					unless ($ctmsg == 'partial-cast');
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   267
			$failed = 1;				
2
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   268
		} else {
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   269
			warning(1,"top 3 tags account for only %d%% of total\n",
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   270
				int(100*($nBest{$best_lag[0]}+$nBest{$best_lag[1]}+$nBest{$best_lag[2]})/$n_valid_windows+0.5));
2
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   271
		}
a077ea2a9f36 post 2011_IWISE
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 0
diff changeset
   272
	}
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   273
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   274
	my($bmo) = $best_lag[0];												# best mean offset
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   275
	if ($bmo > 0.9*$search_radius/2/$CTD{DT}) { 							# cannot be near edge of window
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   276
		if ($search_radius == $w_size) {
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   277
			warning(0,"lag too close to edge of search --- trying again after shifting the initial offset\n");
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   278
			$CTD{TIME_LAG} += $search_radius/2;
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   279
		} else {
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   280
			warning(0,"lag too close to edge of search --- trying again after doubling the search radius\n");
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   281
			$search_radius *= 2;
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   282
			$search_radius = $w_size if ($search_radius > $w_size);
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   283
		}
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   284
		undef(%nBest); undef(%madBest); undef(@best_lag);
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   285
		goto RETRY;
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   286
	}
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   287
	if (-$bmo > 0.9*$search_radius/2/$CTD{DT}) {
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   288
		if ($search_radius == $w_size) {
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   289
			warning(0,"lag too close to edge of search --- trying again after shifting the initial offset\n");
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   290
			$CTD{TIME_LAG} -= $search_radius/2;
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   291
		} else {
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   292
			warning(0,"lag too close to edge of search --- trying again after doubling the search radius\n");
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   293
			$search_radius *= 2;
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   294
			$search_radius = $w_size if ($search_radius > $w_size);
5
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   295
		}
509cc9966b68 ======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 4
diff changeset
   296
		undef(%nBest); undef(%madBest); undef(@best_lag);
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   297
		goto RETRY;
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   298
    }
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   299
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   300
	#----------------------------------------------------
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   301
	# Here, either $failed is set, or we have a valid lag.
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   302
	#----------------------------------------------------
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   303
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   304
#	if ($failed) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   305
#		for (my($wi)=0; $wi<$n_windows; $wi++) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   306
#			print(STDERR "$wi $so[$wi] $mad[$wi]\n");
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   307
#		}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   308
#	}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   309
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   310
	#----------------------------------------------------
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   311
	# Here, either $failed is set, or we have a valid lag.
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   312
	# If we have a valid lag, a continuous range of good 
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   313
	# lags is determined using a finite-state machine.
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   314
	# 	state == 0		no good run found yet
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   315
	# 	state == 1		good run found, $fg is set
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   316
	# A good run is at least $min_runlength long,
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   317
	# and every $scan_runlength-long sequence contains at
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   318
	# least $min_good scan offsets that
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   319
	# agree with the median offset within +/- $good_diff.
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   320
	#----------------------------------------------------
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   321
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   322
	my(@fg,@lg);
48
d9309804b6cf V1.3 release
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 35
diff changeset
   323
	my($min_runlength) = 7; my($scan_runlength) = 7; my($min_good) = 4; my($good_diff) = 1;
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   324
	unless ($failed || $scan_increment>1) {
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   325
		my($state) = 0; 
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   326
		for (my($i)=0; 1; $i++) {
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   327
#			printf(STDERR "$i: state = $state\n");
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   328
			if ($state == 0) {
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   329
				last if ($i >= @elapsed-$scan_runlength);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   330
				my($ngood) = 0;
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   331
				for (my($j)=0; $j<$scan_runlength; $j++) {
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   332
					$ngood += (abs($bmo-$so[$i+$j]) <= $good_diff);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   333
				}
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   334
#				printf(STDERR "$i: ngood = $ngood\n");
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   335
				if ($ngood >= $min_good) {							# we want at least 3 out of 5
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   336
					$state = 1;
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   337
					if ($i == 0) {									# run begins at start
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   338
						push(@fg,0);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   339
					} else {										# run begins at first matching offset
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   340
						my($fg);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   341
						for (my($j)=0; $j<$scan_runlength; $j++) {
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   342
							$fg = $i+$scan_runlength-1-$j
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   343
								if (abs($bmo-$so[$i+$scan_runlength-1-$j]) <= $good_diff);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   344
						}
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   345
						push(@fg,$fg);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   346
					}
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   347
				}
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   348
			} elsif ($state == 1) {									# growing run
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   349
				die("assertion failed (i = $i)")
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   350
					if ($i > @elapsed-$scan_runlength);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   351
				if ($i == @elapsed-$scan_runlength) {				# run extends to end
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   352
					push(@lg,$#elapsed);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   353
					last;
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   354
				}
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   355
				my($ngood) = 0;
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   356
				for (my($j)=0; $j<$scan_runlength; $j++) {
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   357
					$ngood += (abs($bmo-$so[$i+$j]) <= $good_diff);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   358
				}
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   359
#				printf(STDERR "$i: ngood = $ngood\n");
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   360
				if ($ngood < $min_good) {							# run ended
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   361
					my($lg);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   362
					for (my($j)=0; $j<$scan_runlength; $j++) {
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   363
						$lg = $i if (abs($bmo-$so[$i+$j]) <= 1);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   364
					}
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   365
					push(@lg,$lg);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   366
					$state = 0;
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   367
				}
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   368
			} # if state == 1
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   369
		} # for i
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   370
#		printf(STDERR "%d runs found\n",scalar(@lg));
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   371
	} # unless $failed || scan_increment > 1
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   372
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   373
	#--------------------------------------------------
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   374
	# Filter LADCP data for measurements during times
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   375
	# of uncertain time lags
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   376
	#--------------------------------------------------
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   377
35
54b8bb450e5f Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
   378
	if ($scan_increment == 1 && !$opt_l) {
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   379
		progress("\tEditing data with unknown time-lags...\n");
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   380
		my(@elim);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   381
#		print(STDERR "fg = @fg; lg = @lg\n");
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   382
		for (my($i)=0; $i<@fg; $i++) {
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   383
			next if ($lg[$i]-$fg[$i] < $min_runlength);
35
54b8bb450e5f Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
   384
			push(@elim,($fg[$i] == 0) 			 ? $LADCP{ENSEMBLE}[$firstGoodEns]->{ELAPSED} : $elapsed[$fg[$i]],
54b8bb450e5f Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
   385
					   ($lg[$i] == $n_windows-1) ? $LADCP{ENSEMBLE}[$lastGoodEns]->{ELAPSED}  : $elapsed[$lg[$i]]);
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   386
		}
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   387
#		print(STDERR "elim = @elim\n");
49
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 48
diff changeset
   388
		$failed = 1 unless (@elim);
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   389
		$nerm = $failed
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   390
			  ? editBadTimeLagging($first_ens,$last_ens,-1)
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   391
			  : editBadTimeLagging($first_ens,$last_ens,@elim);
35
54b8bb450e5f Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
   392
		my($pct) = round(100*$nerm/($last_ens-$first_ens+1));
54b8bb450e5f Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
   393
	    progress("\t\t$nerm ensembles removed ($pct%% of total), leaving %d run(s)\n",scalar(@elim)/2);
54b8bb450e5f Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
   394
		warning(1,"time-lag editing removed large fraction of samples (%d%% of total)\n",$pct)
54b8bb450e5f Version 1.2 finished. Out for testing to Jay Hooper.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
   395
			if ($pct > 30);
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   396
	}
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   397
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   398
	#------------------------------------------------------
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   399
	# Produce plot on fine-grained time-lagging
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   400
	#	- accumulate data into plot buffer
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   401
	#	- on last lag piece (usually upcast), plot is drawn
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   402
	#------------------------------------------------------
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   403
26
b89d4b01fcc5 after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   404
	if (@out_TL && $scan_increment==1) {
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   405
		push(@elapsed_buf,@elapsed);								# buffer elapsed data in static scope
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   406
		push(@so_buf,@so);											# scan offset
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   407
		push(@mad_buf,@mad);										# mean absolute deviation
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   408
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   409
		for (my($i)=0; $i<@fg; $i++) {
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   410
			next if ($lg[$i]-$fg[$i] < $min_runlength);
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   411
			push(@bmo_buf,$bmo);									# best median offset (copy for each run)
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   412
			push(@fg_buf,$elapsed[$fg[$i]]);						# first good so in lag-piece
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   413
			push(@lg_buf,$elapsed[$lg[$i]]);						# last good so in lag-piece
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   414
		}
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   415
		$elapsed_min_buf = $elapsed[0]								# min of valid elapsed (for plotting)
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   416
			unless defined($elapsed_min_buf);
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   417
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   418
		if ($last_lag_piece) {										
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   419
			progress("\tWriting time-lagging time series to ");		# output all data
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   420
			my($saveParams) = $antsCurParams;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   421
			@antsNewLayout = ('elapsed.LADCP','scan_offset','mad','downcast');
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   422
	
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   423
			&antsAddParams('best_scan_offsets',"@bmo_buf");
34
e550db661c17 pre-Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 30
diff changeset
   424
#			&antsAddParams('to_elapsed_limits',"@te_buf");
13
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   425
			&antsAddParams('elapsed.min',$elapsed_min_buf);
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   426
			&antsAddParams('elapsed.max',$elapsed_buf[$#elapsed_buf]);
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   427
			&antsAddParams('elapsed.bot',$LADCP{ENSEMBLE}[$LADCP_atbottom]->{ELAPSED});
2788bf1bf1de after DIMES US5
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 11
diff changeset
   428
25
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
   429
			foreach my $of (@out_TL) {
26
b89d4b01fcc5 after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   430
				progress("<$of> ");
30
7fb67e771d85 LWplots expunged
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 29
diff changeset
   431
				my($plot,$out) = ($of =~ /^([^\(]+)\(([^\)]+)\)$/); 					# plot_sub(out_file)
7fb67e771d85 LWplots expunged
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 29
diff changeset
   432
				if (defined($out)) {
7fb67e771d85 LWplots expunged
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 29
diff changeset
   433
					require "$WCALC/${plot}.pl";
7fb67e771d85 LWplots expunged
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 29
diff changeset
   434
					&{$plot}($out);
7fb67e771d85 LWplots expunged
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 29
diff changeset
   435
					next;
7fb67e771d85 LWplots expunged
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 29
diff changeset
   436
		        }
26
b89d4b01fcc5 after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   437
				$of = ">$of" unless ($of =~ /^$|^\s*\|/);
27
2053d8de8d6b updated plots & seabed code; GoM data process correctly
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 26
diff changeset
   438
		        open(STDOUT,$of) || error("$of: $!\n");
26
b89d4b01fcc5 after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 25
diff changeset
   439
				undef($antsActiveHeader) unless ($ANTS_TOOLS_AVAILABLE);
25
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
   440
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
   441
				for (my($wi)=0; $wi<@elapsed_buf; $wi++) {
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
   442
					&antsOut($elapsed_buf[$wi],$so_buf[$wi],$mad_buf[$wi],
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
   443
								($elapsed_buf[$wi]<$LADCP{ENSEMBLE}[$LADCP_atbottom]->{ELAPSED}));
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
   444
				}
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
   445
	    
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
   446
	            &antsOut('EOF'); open(STDOUT,">&2");
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
   447
            }
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   448
	        $antsCurParams = $saveParams;
25
bd38f8bfb8e6 first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 16
diff changeset
   449
			progress("\n");
4
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   450
		}
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   451
	}
e681262d0fd7 yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 3
diff changeset
   452
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   453
	return defined($failed) ? undef : $CTD{TIME_LAG}+$bmo*$CTD{DT};
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   454
}
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   455
11
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   456
} # STATIC SCOPE
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   457
0
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   458
3365828b1004 after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   459
1;