author | A.M. Thurnherr <athurnherr@yahoo.com> |
Tue, 11 Oct 2011 17:05:36 -0400 | |
changeset 3 | 9c021fdea1ff |
parent 2 | a077ea2a9f36 |
child 4 | e681262d0fd7 |
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 |
3
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
4 |
# dlm: Fri Sep 23 21:08:01 2011 |
0 | 5 |
# (c) 2010 A.M. Thurnherr |
3
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
6 |
# uE-Info: 101 42 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 |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
25 |
|
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
26 |
# DIFFICULT STATIONS: |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
27 |
# NBP0901#005 |
0 | 28 |
|
29 |
# TODO: |
|
30 |
# - better seabed code (from LADCPproc) |
|
31 |
# - intermediate-step timelagging guess |
|
32 |
# - flip aliased ensembles |
|
33 |
||
2 | 34 |
my($MAX_ALLOWED_THREE_LAG_SPREAD) = 3; # this was initially set to 2 but found to be |
35 |
# violated quite often during 2011_IWISE. A |
|
36 |
# large spread may indicate dropped CTD scans. |
|
37 |
# The optimum value may be cast-duration dependent. |
|
38 |
||
0 | 39 |
sub mad_w($$$) # mean absolute deviation |
40 |
{ |
|
41 |
my($fe,$le,$so) = @_; # first/last LADCP ens, CTD scan offset |
|
42 |
my($sad) = my($n) = 0; |
|
43 |
||
2 | 44 |
my($LADCP_mean_w,$CTD_mean_w,$nsamp) = (0,0,0); |
45 |
for (my($e)=$fe; $e<=$le; $e++) { # first, calculate mean w in window |
|
0 | 46 |
my($s) = int(($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[0]) / $CTD{DT} + 0.5); |
47 |
die("assertion failed\n" . |
|
48 |
"\ttest: abs($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[$s]) <= $CTD{DT}/2\n" . |
|
49 |
"\te = $e, s = $s, ensemble = $LADCP{ENSEMBLE}[$e]->{NUMBER}" |
|
50 |
) unless (abs($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[$s]) <= $CTD{DT}/2); |
|
2 | 51 |
next unless numberp($LADCP{ENSEMBLE}[$e]->{REFLR_W}); |
52 |
my($dw) = $LADCP{ENSEMBLE}[$e]->{REFLR_W}-$LADCP_mean_w - ($CTD{W}[$s+$so]-$CTD_mean_w); |
|
53 |
next unless (abs($dw) <= $opt_m); |
|
0 | 54 |
|
2 | 55 |
$LADCP_mean_w += $LADCP{ENSEMBLE}[$e]->{REFLR_W}; |
56 |
$CTD_mean_w += $CTD{W}[$s+$so]; |
|
57 |
$nsamp++; |
|
58 |
} |
|
59 |
return 9e99 unless ($nsamp); |
|
60 |
$LADCP_mean_w /= $nsamp; |
|
61 |
$CTD_mean_w /= $nsamp; |
|
62 |
||
63 |
for (my($e)=$fe; $e<=$le; $e++) { # now, calculate mad |
|
64 |
my($s) = int(($LADCP{ENSEMBLE}[$e]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[0]) / $CTD{DT} + 0.5); |
|
65 |
my($dw) = $LADCP{ENSEMBLE}[$e]->{REFLR_W}-$LADCP_mean_w - ($CTD{W}[$s+$so]-$CTD_mean_w); |
|
66 |
next unless numberp($LADCP{ENSEMBLE}[$e]->{REFLR_W}); |
|
0 | 67 |
next unless (abs($dw) <= $opt_m); |
68 |
$sad += abs($dw); |
|
69 |
$n++; |
|
70 |
} |
|
71 |
return ($n>0) ? $sad/$n : 9e99; # n == 0, e.g. in bottom gap |
|
72 |
} |
|
73 |
||
74 |
||
75 |
sub bestLag($$$$) # find best lag in window |
|
76 |
{ |
|
77 |
my($fe,$le,$ww,$soi) = @_; # first/last LADCP ens, window width, scan-offset increment |
|
78 |
||
79 |
my($bestso) = 0; # error at first-guess offset |
|
80 |
my($bestmad) = mad_w($fe,$le,0); |
|
81 |
||
82 |
for (my($dso) = 1; $dso <= int($ww/2/$CTD{DT} + 0.5); $dso+=$soi) { |
|
83 |
my($mad) = mad_w($fe,$le,-$dso); |
|
84 |
$bestmad=$mad,$bestso=-$dso if ($mad < $bestmad); |
|
85 |
$mad = mad_w($fe,$le,$dso); |
|
86 |
$bestmad=$mad,$bestso=$dso if ($mad < $bestmad); |
|
87 |
} |
|
88 |
return ($bestso,$bestmad); |
|
89 |
} |
|
90 |
||
91 |
#---------------------------------------------------------------------- |
|
92 |
# carry out lag correlations and keep tally of the results |
|
93 |
# - fist and last 10% of LADCP profile ignored |
|
94 |
#---------------------------------------------------------------------- |
|
95 |
||
96 |
sub calc_lag($$$) |
|
97 |
{ |
|
98 |
my($n_windows,$w_size,$scan_increment) = @_; |
|
99 |
||
100 |
RETRY: |
|
3
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
101 |
progress("Calculating $n_windows time lags from ${w_size}s-long windows at %dHz resolution...\n", |
0 | 102 |
int(1/$scan_increment/$CTD{DT}+0.5)); |
103 |
||
104 |
my($approx_CTD_profile_start_ens) = |
|
105 |
$firstGoodEns + int(($CTD{ELAPSED}[0] - $CTD{TIME_LAG}) / $LADCP{MEAN_DT}); |
|
106 |
my($approx_CTD_profile_end_ens) = |
|
107 |
$firstGoodEns + int(($CTD{ELAPSED}[$#{$CTD{ELAPSED}}] + $CTD{ELAPSED}[0] - $CTD{TIME_LAG}) / $LADCP{MEAN_DT}); |
|
108 |
||
109 |
my($approx_joint_profile_start_ens) = max($firstGoodEns,$approx_CTD_profile_start_ens); |
|
110 |
my($approx_joint_profile_end_ens) = min($lastGoodEns,$approx_CTD_profile_end_ens); |
|
111 |
debugmsg("profile start: $firstGoodEns -> $approx_joint_profile_start_ens\n"); |
|
112 |
debugmsg("profile end : $lastGoodEns -> $approx_joint_profile_end_ens\n"); |
|
113 |
||
114 |
my($skip_ens) = int(($approx_joint_profile_end_ens - $approx_joint_profile_start_ens) / 10 + 0.5); |
|
115 |
||
3
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
116 |
my(%nBest,%madBest); |
0 | 117 |
for (my($wi)=0; $wi<$n_windows; $wi++) { |
118 |
my($fe) = $approx_joint_profile_start_ens + $skip_ens + $wi*int(($approx_joint_profile_end_ens-$approx_joint_profile_start_ens-2*$skip_ens)/$n_windows+0.5); |
|
119 |
my($so,$mad) = bestLag($fe,$fe+int($w_size/$LADCP{MEAN_DT}+0.5),$w_size,$scan_increment); |
|
120 |
debugmsg("%.1f cm/s mad(w) at %3d scans offset\n",100*$mad,$so); |
|
3
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
121 |
$nBest{$so}++; $madBest{$so} += $mad; |
0 | 122 |
} |
123 |
||
124 |
my(@best_lag); |
|
125 |
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
|
126 |
$madBest{$i} /= $nBest{$i}; |
0 | 127 |
$best_lag[0] = $i if ($nBest{$i} > $nBest{$best_lag[0]}); |
128 |
} |
|
129 |
foreach my $i (keys(%nBest)) { |
|
130 |
next if ($i == $best_lag[0]); |
|
131 |
$best_lag[1] = $i if ($nBest{$i} > $nBest{$best_lag[1]}); |
|
132 |
} |
|
133 |
foreach my $i (keys(%nBest)) { |
|
134 |
next if ($i == $best_lag[0] || $i == $best_lag[1]); |
|
135 |
$best_lag[2] = $i if ($nBest{$i} > $nBest{$best_lag[2]}); |
|
136 |
} |
|
3
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
137 |
progress("\t3 most popular offsets: %d (%d%% %.1fcm/s mad), %d (%d%% %.1fcm/s mad), %d (%d%% %.1fcm/s mad)\n", |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
138 |
$best_lag[0],int(($nBest{$best_lag[0]}/$n_windows)*100+0.5),100*$madBest{$best_lag[0]}, |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
139 |
$best_lag[1],int(($nBest{$best_lag[1]}/$n_windows)*100+0.5),100*$madBest{$best_lag[1]}, |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
140 |
$best_lag[2],int(($nBest{$best_lag[2]}/$n_windows)*100+0.5),100*$madBest{$best_lag[2]}); |
0 | 141 |
|
3
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
142 |
# BETTER HEURISTIC NEEDED! |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
143 |
### if ($nBest{$best_lag[0]}+$nBest{$best_lag[1]}+$nBest{$best_lag[2]} <= 6) { |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
144 |
### warning(0,"cannot determine valid lag => trying again with doubled window size\n"); |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
145 |
### undef(%nBest); undef(%madBest); |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
146 |
### $w_size *= 2; |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
147 |
### goto RETRY; |
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
148 |
### } |
0 | 149 |
|
2 | 150 |
unless ($nBest{$best_lag[0]}+$nBest{$best_lag[1]}+$nBest{$best_lag[2]} >= $opt_3*$n_windows) { |
151 |
if (max(@best_lag)-min(@best_lag) > $MAX_ALLOWED_THREE_LAG_SPREAD) { |
|
152 |
croak(sprintf("$0: cannot determine a valid lag; top 3 tags account for %d%% of total (use -3 to relax criterion)\n", |
|
153 |
int(100*($nBest{$best_lag[0]}+$nBest{$best_lag[1]}+$nBest{$best_lag[2]})/$n_windows+0.5))) |
|
154 |
} else { |
|
155 |
warning(1,"top 3 tags account for only %d%% of total\n", |
|
156 |
int(100*($nBest{$best_lag[0]}+$nBest{$best_lag[1]}+$nBest{$best_lag[2]})/$n_windows+0.5)); |
|
157 |
} |
|
158 |
} |
|
0 | 159 |
|
2 | 160 |
my($bmo); |
3
9c021fdea1ff
Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
2
diff
changeset
|
161 |
if (max(@best_lag)-min(@best_lag) > 5 || $nBest{$best_lag[0]}/$n_windows >= 2/3) { |
2 | 162 |
$bmo = $best_lag[0]; |
163 |
progress("\tunambiguously best offset = %d scans\n",$bmo); |
|
164 |
} else { |
|
165 |
$bmo = ($nBest{$best_lag[0]}*$best_lag[0] + |
|
0 | 166 |
$nBest{$best_lag[1]}*$best_lag[1] + |
167 |
$nBest{$best_lag[2]}*$best_lag[2]) / ($nBest{$best_lag[0]} + |
|
168 |
$nBest{$best_lag[1]} + |
|
169 |
$nBest{$best_lag[2]}); |
|
2 | 170 |
progress("\tweighted-mean offset = %.1f scans\n",$bmo); |
171 |
} |
|
0 | 172 |
|
2 | 173 |
if ($bmo > 0.9*$w_size/2/$CTD{DT}) { |
0 | 174 |
warning(0,"lag too close to the edge of the window --- trying again after adjusting the guestimated offset\n"); |
175 |
$CTD{TIME_LAG} += $w_size/2; |
|
176 |
undef(%nBest); |
|
177 |
goto RETRY; |
|
178 |
} |
|
2 | 179 |
if (-$bmo > 0.9*$w_size/2/$CTD{DT}) { |
0 | 180 |
warning(0,"lag too close to the edge of the window --- trying again after adjusting the guestimated offset\n"); |
181 |
$CTD{TIME_LAG} -= $w_size/2; |
|
182 |
undef(%nBest); |
|
183 |
goto RETRY; |
|
184 |
} |
|
185 |
||
2 | 186 |
return $CTD{TIME_LAG}+$bmo*$CTD{DT}; |
0 | 187 |
} |
188 |
||
189 |
||
190 |
1; |