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