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