author | A.M. Thurnherr <athurnherr@yahoo.com> |
Thu, 16 Apr 2015 10:31:09 +0000 | |
changeset 26 | b89d4b01fcc5 |
parent 25 | bd38f8bfb8e6 |
child 27 | 2053d8de8d6b |
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 |
26
b89d4b01fcc5
after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
25
diff
changeset
|
4 |
# dlm: Thu Apr 16 10:25:37 2015 |
0 | 5 |
# (c) 2010 A.M. Thurnherr |
26
b89d4b01fcc5
after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
25
diff
changeset
|
6 |
# uE-Info: 63 0 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 |
3
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
59 |
|
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
60 |
# DIFFICULT STATIONS: |
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
61 |
# NBP0901#131 this requires the search-radius doubling heuristic |
0 | 62 |
|
63 |
# TODO: |
|
64 |
# - better seabed code (from LADCPproc) |
|
2 | 65 |
|
13 | 66 |
my($TINY) = 1e-6; |
67 |
||
0 | 68 |
sub mad_w($$$) # mean absolute deviation |
69 |
{ |
|
70 |
my($fe,$le,$so) = @_; # first/last LADCP ens, CTD scan offset |
|
71 |
my($sad) = my($n) = 0; |
|
72 |
||
2 | 73 |
my($LADCP_mean_w,$CTD_mean_w,$nsamp) = (0,0,0); |
74 |
for (my($e)=$fe; $e<=$le; $e++) { # first, calculate mean w in window |
|
0 | 75 |
my($s) = int(($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[0]) / $CTD{DT} + 0.5); |
16
29e867b3e070
whoosher version at beginning of FZ1
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
15
diff
changeset
|
76 |
next unless ($s>=0 && $s<=$#{$CTD{ELAPSED}}); |
0 | 77 |
die("assertion failed\n" . |
78 |
"\ttest: abs($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[$s]) <= $CTD{DT}/2\n" . |
|
79 |
"\te = $e, s = $s, ensemble = $LADCP{ENSEMBLE}[$e]->{NUMBER}" |
|
13 | 80 |
) unless (abs($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[$s]) <= $CTD{DT}/2+$TINY); |
2 | 81 |
next unless numberp($LADCP{ENSEMBLE}[$e]->{REFLR_W}); |
82 |
my($dw) = $LADCP{ENSEMBLE}[$e]->{REFLR_W}-$LADCP_mean_w - ($CTD{W}[$s+$so]-$CTD_mean_w); |
|
13 | 83 |
next unless (abs($dw) <= $w_max_lim); |
0 | 84 |
|
2 | 85 |
$LADCP_mean_w += $LADCP{ENSEMBLE}[$e]->{REFLR_W}; |
86 |
$CTD_mean_w += $CTD{W}[$s+$so]; |
|
87 |
$nsamp++; |
|
88 |
} |
|
89 |
return 9e99 unless ($nsamp); |
|
90 |
$LADCP_mean_w /= $nsamp; |
|
91 |
$CTD_mean_w /= $nsamp; |
|
92 |
||
93 |
for (my($e)=$fe; $e<=$le; $e++) { # now, calculate mad |
|
94 |
my($s) = int(($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[0]) / $CTD{DT} + 0.5); |
|
95 |
my($dw) = $LADCP{ENSEMBLE}[$e]->{REFLR_W}-$LADCP_mean_w - ($CTD{W}[$s+$so]-$CTD_mean_w); |
|
96 |
next unless numberp($LADCP{ENSEMBLE}[$e]->{REFLR_W}); |
|
13 | 97 |
next unless (abs($dw) <= $w_max_lim); |
0 | 98 |
$sad += abs($dw); |
99 |
$n++; |
|
100 |
} |
|
101 |
return ($n>0) ? $sad/$n : 9e99; # n == 0, e.g. in bottom gap |
|
102 |
} |
|
103 |
||
104 |
||
105 |
sub bestLag($$$$) # find best lag in window |
|
106 |
{ |
|
107 |
my($fe,$le,$ww,$soi) = @_; # first/last LADCP ens, window width, scan-offset 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
|
108 |
# $le = $lastGoodEns |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
109 |
# unless ($le <= $lastGoodEns); |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
110 |
die("assertion failed\n\tfe = $fe, le = $le, firstGoodEns = $firstGoodEns, lastGoodEns = $lastGoodEns") |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
111 |
unless ($fe>=$firstGoodEns && $le<=$lastGoodEns); |
0 | 112 |
|
113 |
my($bestso) = 0; # error at first-guess offset |
|
114 |
my($bestmad) = mad_w($fe,$le,0); |
|
115 |
||
116 |
for (my($dso) = 1; $dso <= int($ww/2/$CTD{DT} + 0.5); $dso+=$soi) { |
|
117 |
my($mad) = mad_w($fe,$le,-$dso); |
|
118 |
$bestmad=$mad,$bestso=-$dso if ($mad < $bestmad); |
|
119 |
$mad = mad_w($fe,$le,$dso); |
|
120 |
$bestmad=$mad,$bestso=$dso if ($mad < $bestmad); |
|
121 |
} |
|
122 |
return ($bestso,$bestmad); |
|
123 |
} |
|
124 |
||
125 |
#---------------------------------------------------------------------- |
|
126 |
# carry out lag correlations and keep tally of the results |
|
127 |
#---------------------------------------------------------------------- |
|
128 |
||
11 | 129 |
{ # STATIC SCOPE |
130 |
||
13 | 131 |
my(@elapsed_buf,@so_buf,@mad_buf,@bmo_buf,@te_buf,$elapsed_min_buf); |
11 | 132 |
|
13 | 133 |
sub calc_lag($$$$$) |
0 | 134 |
{ |
13 | 135 |
my($n_windows,$w_size,$scan_increment,$first_ens,$last_ens) = @_; |
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
136 |
my($search_radius) = $scan_increment==1 ? 3 : $w_size; |
0 | 137 |
|
11 | 138 |
my($ctmsg); |
13 | 139 |
if ($first_ens==$firstGoodEns && $last_ens==$lastGoodEns) { $ctmsg = "full-cast"; } |
140 |
else { $ctmsg = "partial-cast"; } |
|
11 | 141 |
|
0 | 142 |
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
|
143 |
my($failed) = undef; |
11 | 144 |
progress("Calculating $n_windows $ctmsg time lags from ${w_size}s-long windows at %dHz resolution...\n", |
0 | 145 |
int(1/$scan_increment/$CTD{DT}+0.5)); |
146 |
||
147 |
my($approx_CTD_profile_start_ens) = |
|
148 |
$firstGoodEns + int(($CTD{ELAPSED}[0] - $CTD{TIME_LAG}) / $LADCP{MEAN_DT}); |
|
149 |
my($approx_CTD_profile_end_ens) = |
|
150 |
$firstGoodEns + int(($CTD{ELAPSED}[$#{$CTD{ELAPSED}}] + $CTD{ELAPSED}[0] - $CTD{TIME_LAG}) / $LADCP{MEAN_DT}); |
|
151 |
||
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
152 |
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
|
153 |
my($approx_joint_profile_end_ens) = min($lastGoodEns,$approx_CTD_profile_end_ens) - 10; |
0 | 154 |
debugmsg("profile start: $firstGoodEns -> $approx_joint_profile_start_ens\n"); |
155 |
debugmsg("profile end : $lastGoodEns -> $approx_joint_profile_end_ens\n"); |
|
156 |
||
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
157 |
my($window_ens) = int($w_size/$LADCP{MEAN_DT}+0.5); |
0 | 158 |
|
4
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
159 |
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
|
160 |
my($n_valid_windows) = 0; |
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
161 |
|
13 | 162 |
$first_ens = $approx_joint_profile_start_ens |
163 |
if ($first_ens < $approx_joint_profile_start_ens); |
|
164 |
my($last_lag_piece) = ($last_ens == $lastGoodEns); # none is following |
|
165 |
$last_ens = $approx_joint_profile_end_ens |
|
166 |
if ($last_ens > $approx_joint_profile_end_ens); |
|
11 | 167 |
|
0 | 168 |
for (my($wi)=0; $wi<$n_windows; $wi++) { |
11 | 169 |
my($fe) = $first_ens + int(($last_ens-$first_ens-$window_ens)*$wi/($n_windows-1)+0.5); |
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
170 |
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
|
171 |
$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
|
172 |
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
|
173 |
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
|
174 |
$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
|
175 |
$n_valid_windows++; |
3
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
176 |
$nBest{$so}++; $madBest{$so} += $mad; |
0 | 177 |
} |
178 |
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
|
179 |
$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
|
180 |
} |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
181 |
|
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
182 |
my($med_mad) = median(values(%madBest)); # remove lags with large mads |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
183 |
my($mad_mad) = mad2($med_mad,values(%madBest)); |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
184 |
foreach my $lag (keys(%nBest)) { |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
185 |
next if ($madBest{$lag} <= $med_mad+$mad_mad); |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
186 |
$n_valid_windows -= $nBest{$lag}; |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
187 |
$nBest{$lag} = 0; |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
188 |
} |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
189 |
|
4
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
190 |
my(@best_lag); |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
191 |
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
|
192 |
$best_lag[0] = $lag if ($nBest{$lag} > $nBest{$best_lag[0]}); |
0 | 193 |
} |
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 |
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
|
195 |
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
|
196 |
$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
|
197 |
} |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
198 |
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
|
199 |
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
|
200 |
$best_lag[2] = $lag if ($nBest{$lag} > $nBest{$best_lag[2]}); |
0 | 201 |
} |
4
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
202 |
if ($nBest{$best_lag[2]}) { |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
203 |
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
|
204 |
$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
|
205 |
$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
|
206 |
$best_lag[2],int(($nBest{$best_lag[2]}/$n_valid_windows)*100+0.5),100*$madBest{$best_lag[2]}); |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
207 |
} elsif ($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
|
208 |
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
|
209 |
$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
|
210 |
$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
|
211 |
} else { |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
212 |
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
|
213 |
$best_lag[0],int(($nBest{$best_lag[0]}/$n_valid_windows)*100+0.5),100*$madBest{$best_lag[0]}); |
0 | 214 |
} |
215 |
||
13 | 216 |
unless ($nBest{$best_lag[0]}+$nBest{$best_lag[1]}+$nBest{$best_lag[2]} >= $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
|
217 |
if (max(@best_lag)-min(@best_lag) > $TL_max_allowed_three_lag_spread) { |
11 | 218 |
warning(2,"$0: cannot determine a valid $ctmsg lag; top 3 tags account for %d%% of total (use -3 to relax criterion)\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
|
219 |
int(100*($nBest{$best_lag[0]}+$nBest{$best_lag[1]}+$nBest{$best_lag[2]})/$n_valid_windows+0.5)); |
11 | 220 |
$failed = 1; |
2 | 221 |
} else { |
222 |
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
|
223 |
int(100*($nBest{$best_lag[0]}+$nBest{$best_lag[1]}+$nBest{$best_lag[2]})/$n_valid_windows+0.5)); |
2 | 224 |
} |
225 |
} |
|
0 | 226 |
|
4
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
227 |
my($bmo) = $best_lag[0]; |
0 | 228 |
|
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
229 |
if ($bmo > 0.9*$search_radius/2/$CTD{DT}) { |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
230 |
if ($search_radius == $w_size) { |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
231 |
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
|
232 |
$CTD{TIME_LAG} += $search_radius/2; |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
233 |
} else { |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
234 |
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
|
235 |
$search_radius *= 2; |
15 | 236 |
$search_radius =- $w_size if ($search_radius > $w_size); |
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
237 |
} |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
238 |
undef(%nBest); undef(%madBest); undef(@best_lag); |
0 | 239 |
goto RETRY; |
240 |
} |
|
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
241 |
if (-$bmo > 0.9*$search_radius/2/$CTD{DT}) { |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
242 |
if ($search_radius == $w_size) { |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
243 |
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
|
244 |
$CTD{TIME_LAG} -= $search_radius/2; |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
245 |
} else { |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
246 |
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
|
247 |
$search_radius *= 2; |
15 | 248 |
$search_radius =- $w_size if ($search_radius > $w_size); |
5
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
249 |
} |
509cc9966b68
======================================================================
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
4
diff
changeset
|
250 |
undef(%nBest); undef(%madBest); undef(@best_lag); |
0 | 251 |
goto RETRY; |
252 |
} |
|
253 |
||
26
b89d4b01fcc5
after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
25
diff
changeset
|
254 |
if (@out_TL && $scan_increment==1) { |
13 | 255 |
push(@elapsed_buf,@elapsed); # buffer elapsed data in static scope |
256 |
push(@so_buf,@so); # scan offset |
|
257 |
push(@mad_buf,@mad); # mean absolute deviation |
|
258 |
push(@bmo_buf,$bmo); # best median offset |
|
259 |
push(@te_buf,$elapsed[$#elapsed]); # to elapsed (from elapsed to elapsed, capisc?) |
|
260 |
$elapsed_min_buf = $elapsed[0] # min of valid elapsed (for plotting) |
|
261 |
unless defined($elapsed_min_buf); |
|
262 |
||
263 |
if ($last_lag_piece) { # output all data |
|
26
b89d4b01fcc5
after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
25
diff
changeset
|
264 |
progress("\tWriting time-lagging time series to "); |
11 | 265 |
my($saveParams) = $antsCurParams; |
266 |
@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
|
267 |
|
13 | 268 |
&antsAddParams('best_scan_offsets',"@bmo_buf"); |
269 |
&antsAddParams('to_elapsed_limits',"@te_buf"); |
|
270 |
&antsAddParams('elapsed.min',$elapsed_min_buf); |
|
271 |
&antsAddParams('elapsed.max',$elapsed_buf[$#elapsed_buf]); |
|
272 |
&antsAddParams('elapsed.bot',$LADCP{ENSEMBLE}[$LADCP_atbottom]->{ELAPSED}); |
|
273 |
||
25
bd38f8bfb8e6
first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
16
diff
changeset
|
274 |
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
|
275 |
progress("<$of> "); |
b89d4b01fcc5
after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
25
diff
changeset
|
276 |
$of = ">$of" unless ($of =~ /^$|^\s*\|/); |
25
bd38f8bfb8e6
first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
16
diff
changeset
|
277 |
open(STDOUT,$of) || croak("$of: $!\n"); |
26
b89d4b01fcc5
after implementing new schemes for plotting and active files
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
25
diff
changeset
|
278 |
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
|
279 |
|
bd38f8bfb8e6
first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
16
diff
changeset
|
280 |
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
|
281 |
&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
|
282 |
($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
|
283 |
} |
bd38f8bfb8e6
first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
16
diff
changeset
|
284 |
|
bd38f8bfb8e6
first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
16
diff
changeset
|
285 |
&antsOut('EOF'); open(STDOUT,">&2"); |
bd38f8bfb8e6
first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
16
diff
changeset
|
286 |
} |
11 | 287 |
$antsCurParams = $saveParams; |
25
bd38f8bfb8e6
first step at re-designing plotting system
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
16
diff
changeset
|
288 |
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
|
289 |
} |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
290 |
} |
e681262d0fd7
yay! first version I am really happy with. With plots 'n all.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
3
diff
changeset
|
291 |
|
11 | 292 |
return defined($failed) ? undef : $CTD{TIME_LAG}+$bmo*$CTD{DT}; |
0 | 293 |
} |
294 |
||
11 | 295 |
} # STATIC SCOPE |
296 |
||
0 | 297 |
|
298 |
1; |