0
|
1 |
#!/usr/bin/perl
|
|
2 |
#======================================================================
|
|
3 |
# L A D C P _ W
|
|
4 |
# doc: Fri Dec 17 18:11:13 2010
|
|
5 |
# dlm: Sun Dec 26 20:14:32 2010
|
|
6 |
# (c) 2010 A.M. Thurnherr
|
|
7 |
# uE-Info: 34 86 NIL 0 0 72 2 2 4 NIL ofnI
|
|
8 |
#======================================================================
|
|
9 |
|
|
10 |
# TODO:
|
|
11 |
# make timelagging work for short casts (make sure 10% is not more than half window size)
|
|
12 |
# own seabed detection (P403)
|
|
13 |
# instrument tilt in sidelobe editing?
|
|
14 |
# read ADCP soundspeed from data file
|
|
15 |
|
|
16 |
$antsSummary = 'calculate vertical velocities from LADCP & CTD time series';
|
|
17 |
|
|
18 |
# HISTORY:
|
|
19 |
# Dec 17, 2010: - created from [mergeCTD+LADCP]
|
|
20 |
# Dec 18, 2010: - made to work
|
|
21 |
# Dec 19, 2010: - improved considerably
|
|
22 |
# Dec 20, 2010: - onward
|
|
23 |
# - BUG: depth-binning was off by 1 bin?!
|
|
24 |
# - added binning correction for instrument tilt
|
|
25 |
# Dec 21, 2010: - added -h (seafloor depth)
|
|
26 |
# Dec 22, 2010: - BUG: had not applied soundspeed-correction to w
|
|
27 |
# - debugged opt_d
|
|
28 |
# Dec 23, 2010: - continued implementation of soundspeed corrections
|
|
29 |
# Dec 24, 2010: - added winch_w, wave_w
|
|
30 |
# - removed beampair velocities from code
|
|
31 |
# Dec 25, 2010: - adapted for surface-wave correction in terms of acceleration (CTD_w_t)
|
|
32 |
# - removed elapsed_mismatch
|
|
33 |
# - removed winch_w, wave_w
|
|
34 |
# Dec 26, 2010: - made -p output layout independent of -x to avoid Makefile problems
|
|
35 |
|
|
36 |
# CTD REQUIREMENTS
|
|
37 |
# - elapsed elapsed seconds; see note below
|
|
38 |
# - depth
|
|
39 |
# - ss sound speed
|
|
40 |
# - w ddepth/dt
|
|
41 |
# - w_t dw/dt
|
|
42 |
|
|
43 |
# NUMERICAL OPTIONS
|
|
44 |
# - the first option in the list cannot be numerical!
|
|
45 |
# - if need be, use -v 1 as a dummy option
|
|
46 |
|
|
47 |
# ELAPSED TIMES
|
|
48 |
# - there are 2 different elapsed times used in this program:
|
|
49 |
# 1) elapsed based on firstgoodens in the LADCP time series
|
|
50 |
# 2) CTD elapsed time
|
|
51 |
# - CTD elapsed time does not have to start with zero!
|
|
52 |
# - do not use the Seabird elapsed field, which is only reported to
|
|
53 |
# 3 significant digits, causing significant jitter in dt; however,
|
|
54 |
# at least up to 2010 Seabird simply calculates elapsed time by
|
|
55 |
# assuming a 24Hz sampling rate and no record drop; therefore,
|
|
56 |
# it is best to calculate elapsed time as %RECNO/24
|
|
57 |
# - the elapsed field of the output is the elapsed time from the CTD
|
|
58 |
# file; this is required in order to be able to compare the times
|
|
59 |
# from the uplooker and downlooker-derived vertical velocity
|
|
60 |
# profiles
|
|
61 |
# - as a result, a profile only starts with elapsed==0 if the CTD
|
|
62 |
# is turned on when the LADCP is already in the water
|
|
63 |
|
|
64 |
# TIME LAGGING
|
|
65 |
# - occasionally, the time lagging algorithm fails, in particular if the CTD is turned on
|
|
66 |
# some time after the package enters the water
|
|
67 |
# - in this case, an initial guess can be provided with -i
|
|
68 |
# - e.g. plot 'CTD/24Hz/054.1Hz elapsed w','LADCP/raw/054UL.prof =$elapsed-850 w' => -i -850
|
|
69 |
|
|
70 |
# VELOCITY AMBIGUITY ERRORS
|
|
71 |
# - quite extensive tests with DIMES US2 station 146, which has a lot of
|
|
72 |
# ambiguity velocity errors, reveal that -m 1 catches those errors
|
|
73 |
# quite nicely
|
|
74 |
# - even when the errors are not filtered with -m 1, they do not
|
|
75 |
# affect the w profiles, as long as the median bin values are used
|
|
76 |
|
|
77 |
# SCREEN LOGGING
|
|
78 |
# - there are 4 verbosity levels, selected by -v
|
|
79 |
# 0: only print errors
|
|
80 |
# 1: default, UNIX-like (warnings and info messages that are not produced for every cast)
|
|
81 |
# 2: progress messages and useful information
|
|
82 |
# >2: debug messges
|
|
83 |
# - the most useful ones of these are 1 & 2
|
|
84 |
|
|
85 |
($WCALC) = ($0 =~ m{^(.*)/[^/]*$});
|
|
86 |
($ANTS) = (`which list` =~ m{^(.*)/[^/]*$});
|
|
87 |
($PERL_TOOLS) = (`which mkProfile` =~ m{^(.*)/[^/]*$});
|
|
88 |
|
|
89 |
require "$ANTS/ants.pl";
|
|
90 |
require "$ANTS/libstats.pl";
|
|
91 |
require "$WCALC/edit_LADCP_data.pl";
|
|
92 |
require "$WCALC/calc_LADCP_tis.pl";
|
|
93 |
require "$WCALC/calc_timelags.pl";
|
|
94 |
require "$WCALC/find_seabed.pl";
|
|
95 |
require "$PERL_TOOLS/RDI_BB_Read.pl";
|
|
96 |
require "$PERL_TOOLS/RDI_Coords.pl";
|
|
97 |
|
|
98 |
$antsParseHeader = 0;
|
|
99 |
&antsUsage('3:4a:b:c:d:e:f:g:h:i:m:n:o:p:r:s:t:v:w:x:',1,
|
|
100 |
'[-v)erbosity <level[1]>]',
|
|
101 |
'[require -4)-beam solutions]',
|
|
102 |
'[-r)ef-layer <bin[2],bin[6]>]',
|
|
103 |
'[-c)orrelation <min[70]>] [-t)ilt <max[10deg]> [-e)rr-vel <max[0.1m/s]>]',
|
|
104 |
'[-h water <depth>]',
|
|
105 |
'[max LADCP time-series -g)ap <length[60s]>]',
|
|
106 |
'[-m)ax vertical <velocity[1m/s]>',
|
|
107 |
'[-a)djust CTD depth <by[0m]>]',
|
|
108 |
'[-i)nitial CTD time offset <guestimate>]',
|
|
109 |
'[calculate -n) <lags,lags[10,100]>] [lag -w)indow <sz,sz[240s,20s]>]',
|
|
110 |
'[require top-3) lags to account for <frac[0.6]> of all]',
|
|
111 |
'[-x <dc_correction_params/uc_correction_param>]',
|
|
112 |
'[use LADCP -b)ins for w profiles <bin[2],bin[*]>',
|
|
113 |
'[-o)utput bin <resolution[10m]>] [require -s)amples <min[20]>]',
|
|
114 |
'[-f) write time-series <file>] [-d)ump depth-bins to <basename>] [-p)ackage effect <file>]',
|
|
115 |
'<LADCP-file> [CTD-file]');
|
|
116 |
|
|
117 |
&antsCardOpt(\$opt_v,1); # suppress regular info
|
|
118 |
|
|
119 |
$RDI_Coords::minValidVels = 4 if ($opt_4); # suppress 3-beam solutions
|
|
120 |
|
|
121 |
&antsFloatOpt(\$opt_c,70); # min correlation
|
|
122 |
&antsFloatOpt(\$opt_t,5); # max tilt (pitch/roll)
|
|
123 |
&antsFloatOpt(\$opt_e,0.1); # max err vel
|
|
124 |
&antsFloatOpt(\$opt_g,60); # max LADCP gap length
|
|
125 |
&antsFloatOpt(\$opt_m,1); # max allowed vertical velocity
|
|
126 |
|
|
127 |
&antsFloatOpt(\$opt_a,0); # CTD depth adjustment
|
|
128 |
|
|
129 |
$opt_n = '10,100' unless defined($opt_n); # number of time-lags to carry out
|
|
130 |
$opt_w = '240,20' unless defined($opt_w); # time-lag search window (full width)
|
|
131 |
&antsFloatOpt(\$opt_3,0.6);
|
|
132 |
|
|
133 |
&antsFloatOpt(\$opt_o,50); # output bin size
|
|
134 |
&antsCardOpt(\$opt_s,20); # min samples required
|
|
135 |
|
|
136 |
$opt_r = '2,6' unless defined($opt_r); # reference layer bins for w for time matching
|
|
137 |
$opt_b = '2,*' unless defined($opt_b); # bins to use in w calculations
|
|
138 |
|
|
139 |
@n_lags = split(',',$opt_n); # decode -n
|
|
140 |
croak("$0: cannot decode -n $opt_n\n")
|
|
141 |
unless numberp($n_lags[0]) && numberp($n_lags[1]);
|
|
142 |
|
|
143 |
@w_size = split(',',$opt_w); # decode -w
|
|
144 |
croak("$0: cannot decode -w $opt_w\n")
|
|
145 |
unless numberp($w_size[0]) && numberp($w_size[1]);
|
|
146 |
|
|
147 |
($refLr_firstBin,$refLr_lastBin) = split(',',$opt_r); # decode -r
|
|
148 |
croak("$0: cannot decode -r $opt_r\n")
|
|
149 |
unless numberp($refLr_firstBin) && numberp($refLr_lastBin);
|
|
150 |
|
|
151 |
($LADCP_firstBin,$LADCP_lastBin) = split(',',$opt_b); # decode -b
|
|
152 |
croak("$0: cannot decode -b $opt_b\n")
|
|
153 |
unless (numberp($LADCP_firstBin) &&
|
|
154 |
($LADCP_lastBin eq '*' || numberp($LADCP_lastBin)));
|
|
155 |
|
|
156 |
if (defined($opt_x)) { # decode corrections
|
|
157 |
my($dccps,$uccps) = split('/',$opt_x);
|
|
158 |
(@dc_corr_poly) = split(',',$dccps);
|
|
159 |
(@uc_corr_poly) = split(',',$uccps);
|
|
160 |
croak("$0: cannot decode -x $opt_x\n")
|
|
161 |
unless @dc_corr_poly>0 && @uc_corr_poly>0;
|
|
162 |
&antsAddParams('dc_corr_intercept',$dc_corr_poly[0],'dc_corr_slope',$dc_corr_poly[1]);
|
|
163 |
&antsAddParams('uc_corr_intercept',$uc_corr_poly[0],'uc_corr_slope',$uc_corr_poly[1]);
|
|
164 |
}
|
|
165 |
|
|
166 |
if (defined($opt_d)) { # make sure output directory is clean
|
|
167 |
croak("$0: old depth-bin files <${opt_d}[0-9][0-9][0-9].dncast> found --- remove before creating new ones!\n")
|
|
168 |
if (glob("${opt_d}[0-9][0-9][0-9].dncast"));
|
|
169 |
croak("$0: old depth-bin files <${opt_d}[0-9][0-9][0-9].upcast> found --- remove before creating new ones!\n")
|
|
170 |
if (glob("${opt_d}[0-9][0-9][0-9].upcast"));
|
|
171 |
}
|
|
172 |
|
|
173 |
$LADCP_file = &antsFileArg();
|
|
174 |
|
|
175 |
#----------------------------------------------------------------------
|
|
176 |
# Screen Logging
|
|
177 |
# - warning levels:
|
|
178 |
# 0 probably unimportant, e.g. nonsensical parameters that probably won't affect solution
|
|
179 |
# 1 may be somewhat important
|
|
180 |
# 2 important
|
|
181 |
#----------------------------------------------------------------------
|
|
182 |
|
|
183 |
sub progress(@)
|
|
184 |
{ printf(STDERR @_) if ($opt_v > 1); }
|
|
185 |
|
|
186 |
sub info(@)
|
|
187 |
{
|
|
188 |
print(STDERR ($opt_v > 1) ? "\t" : "$LADCP_file: ");
|
|
189 |
printf(STDERR @_) if ($opt_v > 0);
|
|
190 |
}
|
|
191 |
|
|
192 |
sub warning(@)
|
|
193 |
{
|
|
194 |
my($lvl,@msg) = @_;
|
|
195 |
return if ($opt_v == 0);
|
|
196 |
print(STDERR "\n") if ($opt_v > 1);
|
|
197 |
print(STDERR "WARNING (level $lvl): ");
|
|
198 |
printf(STDERR @msg);
|
|
199 |
print(STDERR "\n") if ($opt_v > 1);
|
|
200 |
}
|
|
201 |
|
|
202 |
sub debugmsg(@)
|
|
203 |
{ printf(STDERR @_) if ($opt_v > 2); }
|
|
204 |
|
|
205 |
#----------------
|
|
206 |
# Read LADCP data
|
|
207 |
#----------------
|
|
208 |
|
|
209 |
progress("Reading LADCP data ($LADCP_file)...\n");
|
|
210 |
readData($LADCP_file,\%LADCP);
|
|
211 |
progress("\t%d ensembles\n",scalar(@{$LADCP{ENSEMBLE}}));
|
|
212 |
|
|
213 |
croak("$LADCP_file: not enough LADCP bins ($LADCP{N_BINS}) for choice of -r\n")
|
|
214 |
unless ($LADCP{N_BINS} >= $refLr_lastBin);
|
|
215 |
|
|
216 |
croak("$0: first reference-layer bin outside valid range\n")
|
|
217 |
unless ($refLr_firstBin>=1 && $refLr_firstBin<=$LADCP{N_BINS});
|
|
218 |
croak("$0: last reference-layer bin outside valid range\n")
|
|
219 |
unless ($refLr_lastBin>=1 && $refLr_lastBin<=$LADCP{N_BINS});
|
|
220 |
croak("$0: first reference-layer bin > last reference-layer bin\n")
|
|
221 |
unless ($refLr_firstBin <= $refLr_lastBin);
|
|
222 |
|
|
223 |
$LADCP_lastBin = $LADCP{N_BINS}-1
|
|
224 |
if ($LADCP_lastBin eq '*');
|
|
225 |
croak("$0: first valid LADCP bin outside valid range\n")
|
|
226 |
unless ($LADCP_firstBin>=1 && $LADCP_firstBin<=$LADCP{N_BINS});
|
|
227 |
croak("$0: last valid LADCP bin outside valid range\n")
|
|
228 |
unless ($LADCP_lastBin>=1 && $LADCP_lastBin<=$LADCP{N_BINS});
|
|
229 |
croak("$0: first valid LADCP bin > last valid LADCP bin\n")
|
|
230 |
unless ($LADCP_firstBin <= $LADCP_lastBin);
|
|
231 |
|
|
232 |
warning(0,"first reference-layer bin < first valid LADCP bin\n")
|
|
233 |
unless ($refLr_firstBin >= $LADCP_firstBin);
|
|
234 |
warning(0,"last reference-layer bin > last valid LADCP bin\n")
|
|
235 |
unless ($refLr_lastBin <= $LADCP_lastBin);
|
|
236 |
|
|
237 |
warning(1,"if at all, bin 1 should not be used for short blank-after-transmit values\n")
|
|
238 |
if ($LADCP{BLANKING_DISTANCE}<$LADCP{BIN_LENGTH} && $refLr_firstBin==1);
|
|
239 |
|
|
240 |
#------------------------------------------------------------
|
|
241 |
# Edit beam-velocity data
|
|
242 |
# 1) correlation threshold
|
|
243 |
# 2) tilt threshold (also sets TILT field in all ensembles)
|
|
244 |
#------------------------------------------------------------
|
|
245 |
|
|
246 |
if ($LADCP{BEAM_COORDINATES}) {
|
|
247 |
progress("Editing beam-velocity data...\n");
|
|
248 |
$nvv = $cte = 0;
|
|
249 |
for ($ens=0; $ens<=$#{$LADCP{ENSEMBLE}}; $ens++) {
|
|
250 |
$nvv += countValidBeamVels($ens);
|
|
251 |
$cte += editCorr($ens,$opt_c);
|
|
252 |
$pte += editTilt($ens,$opt_t);
|
|
253 |
}
|
|
254 |
progress("\tcorrelation threshold (-c %d): %d velocites removed (%d%% of total)\n",$opt_c,$cte,round(100*$cte/$nvv));
|
|
255 |
progress("\tattitude threshold (-p %d): %d velocites removed (%d%% of total)\n",$opt_t,$pte,round(100*$pte/$nvv));
|
|
256 |
} else {
|
|
257 |
progress("Editing velocity data...\n");
|
|
258 |
$nvv = $cte = 0;
|
|
259 |
for ($ens=0; $ens<=$#{$LADCP{ENSEMBLE}}; $ens++) {
|
|
260 |
$nvv += countValidBeamVels($ens);
|
|
261 |
$cte += editCorr_Earthcoords($ens,$opt_c);
|
|
262 |
$pte += editTilt($ens,$opt_t);
|
|
263 |
}
|
|
264 |
progress("\tcorrelation threshold (-c %d): %d velocites removed (%d%% of total)\n",$opt_c,$cte,round(100*$cte/$nvv));
|
|
265 |
progress("\tattitude threshold (-p %d): %d velocites removed (%d%% of total)\n",$opt_t,$pte,round(100*$pte/$nvv));
|
|
266 |
}
|
|
267 |
|
|
268 |
#-------------------------------------------------------------------
|
|
269 |
# Calculate earth velocities
|
|
270 |
# - this is done for all bins (not just valid ones), to allow
|
|
271 |
# useless possibility that invalid bins are used for reflr calcs
|
|
272 |
# - also calculate separate beam-pair velocities
|
|
273 |
#-------------------------------------------------------------------
|
|
274 |
|
|
275 |
if ($LADCP{BEAM_COORDINATES}) {
|
|
276 |
progress("Calculating earth-coordinate velocities...\n");
|
|
277 |
$nvw = 0;
|
|
278 |
for ($ens=0; $ens<=$#{$LADCP{ENSEMBLE}}; $ens++) {
|
|
279 |
for (my($bin)=0; $bin<$LADCP{N_BINS}; $bin++) {
|
|
280 |
($LADCP{ENSEMBLE}[$ens]->{U}[$bin],
|
|
281 |
$LADCP{ENSEMBLE}[$ens]->{V}[$bin],
|
|
282 |
$LADCP{ENSEMBLE}[$ens]->{W}[$bin],
|
|
283 |
$LADCP{ENSEMBLE}[$ens]->{ERRVEL}[$bin]) =
|
|
284 |
velInstrumentToEarth(\%LADCP,$ens,
|
|
285 |
velBeamToInstrument(\%LADCP,
|
|
286 |
@{$LADCP{ENSEMBLE}[$ens]->{VELOCITY}[$bin]}));
|
|
287 |
$nvw += defined($LADCP{ENSEMBLE}[$ens]->{W}[$bin]);
|
|
288 |
$LADCP{ENSEMBLE}[$ens]->{GIMBAL_PITCH} =
|
|
289 |
gimbal_pitch($LADCP{ENSEMBLE}[$ens]->{PITCH},$LADCP{ENSEMBLE}[$ens]->{ROLL});
|
|
290 |
}
|
|
291 |
}
|
|
292 |
progress("\t$nvw valid velocities\n");
|
|
293 |
progress("\t3-beam solutions : $RDI_Coords::threeBeam_1 " .
|
|
294 |
"$RDI_Coords::threeBeam_2 " .
|
|
295 |
"$RDI_Coords::threeBeam_3 " .
|
|
296 |
"$RDI_Coords::threeBeam_4\n")
|
|
297 |
unless ($opt_4);
|
|
298 |
} else {
|
|
299 |
progress("Counting valid vertical velocities...\n");
|
|
300 |
$nvw = 0;
|
|
301 |
for ($ens=0; $ens<=$#{$LADCP{ENSEMBLE}}; $ens++) {
|
|
302 |
for (my($bin)=0; $bin<$LADCP{N_BINS}; $bin++) {
|
|
303 |
($LADCP{ENSEMBLE}[$ens]->{U}[$bin],
|
|
304 |
$LADCP{ENSEMBLE}[$ens]->{V}[$bin],
|
|
305 |
$LADCP{ENSEMBLE}[$ens]->{W}[$bin],
|
|
306 |
$LADCP{ENSEMBLE}[$ens]->{ERRVEL}[$bin]) = @{$LADCP{ENSEMBLE}[$ens]->{VELOCITY}[$bin]};
|
|
307 |
$nvw += defined($LADCP{ENSEMBLE}[$ens]->{W}[$bin]);
|
|
308 |
}
|
|
309 |
}
|
|
310 |
progress("\t$nvw valid velocities\n");
|
|
311 |
}
|
|
312 |
|
|
313 |
#----------------------------------------------
|
|
314 |
# S1 STEP: Edit earth-coordinate -velocity data
|
|
315 |
# 1) error-velocity threshold
|
|
316 |
#----------------------------------------------
|
|
317 |
|
|
318 |
progress("Editing earth-coordinate velocity data...\n");
|
|
319 |
|
|
320 |
$ete = 0;
|
|
321 |
for ($ens=0; $ens<=$#{$LADCP{ENSEMBLE}}; $ens++) {
|
|
322 |
$ete += editErrVel($ens,$opt_e);
|
|
323 |
}
|
|
324 |
progress("\t error-velocity threshold (-e %.1f): %d velocites removed (%d%% of total)\n",
|
|
325 |
$opt_e,$ete,round(100*$ete/$nvw));
|
|
326 |
|
|
327 |
#----------------------------
|
|
328 |
# Calculate LADCP time series
|
|
329 |
#----------------------------
|
|
330 |
|
|
331 |
progress("Calculating LADCP time-series...\n");
|
|
332 |
|
|
333 |
($firstGoodEns,$lastGoodEns,$LADCP_atbottom,$LADCP_w_gap_time) =
|
|
334 |
calcLADCPts(\%LADCP,$refLr_firstBin,$refLr_lastBin,$opt_g);
|
|
335 |
croak("$LADCP_file: no good ensembles\n")
|
|
336 |
unless defined($firstGoodEns) && ($lastGoodEns-$firstGoodEns > 0);
|
|
337 |
|
|
338 |
my($cast_duration) = $LADCP{ENSEMBLE}[$lastGoodEns]->{ELAPSED} -
|
|
339 |
$LADCP{ENSEMBLE}[$firstGoodEns]->{ELAPSED};
|
|
340 |
croak("$0: implausibly short cast ($cast_duration seconds)\n")
|
|
341 |
unless ($cast_duration > 600);
|
|
342 |
|
|
343 |
$LADCP{MEAN_DT} = $cast_duration / ($lastGoodEns-$firstGoodEns-1);
|
|
344 |
|
|
345 |
progress("\tStart of cast : %s (#%5d)\n",
|
|
346 |
$LADCP{ENSEMBLE}[$firstGoodEns]->{TIME},
|
|
347 |
$LADCP{ENSEMBLE}[$firstGoodEns]->{NUMBER});
|
|
348 |
progress("\tBottom of cast : %s (#%5d) @ dz~%.1fm\n",
|
|
349 |
$LADCP{ENSEMBLE}[$LADCP_atbottom]->{TIME},
|
|
350 |
$LADCP{ENSEMBLE}[$LADCP_atbottom]->{NUMBER},
|
|
351 |
$LADCP{ENSEMBLE}[$LADCP_atbottom]->{DEPTH});
|
|
352 |
progress("\tEnd of cast : %s (#%5d)\n",
|
|
353 |
$LADCP{ENSEMBLE}[$lastGoodEns]->{TIME},
|
|
354 |
$LADCP{ENSEMBLE}[$lastGoodEns]->{NUMBER});
|
|
355 |
progress("\tCast duration : %.1f hours (pinging for %.1f hours)\n",
|
|
356 |
$cast_duration / 3600,
|
|
357 |
($LADCP{ENSEMBLE}[$#{$LADCP{ENSEMBLE}}]->{UNIX_TIME} -
|
|
358 |
$LADCP{ENSEMBLE}[0]->{UNIX_TIME}) / 3600);
|
|
359 |
progress("\tMean ping interval: %.1f seconds\n",$LADCP{MEAN_DT});
|
|
360 |
|
|
361 |
#--------------
|
|
362 |
# Read CTD data
|
|
363 |
#--------------
|
|
364 |
|
|
365 |
progress("Reading CTD data...\n");
|
|
366 |
croak("$0: no CTD data\n") unless (&antsIn());
|
|
367 |
($CTD_elapsed,$CTD_depth,$CTD_svel,$CTD_w,$CTD_w_t) =
|
|
368 |
&fnr('elapsed','depth','ss','w','w_t');
|
|
369 |
|
|
370 |
$CTD_maxdepth = -1;
|
|
371 |
|
|
372 |
do {
|
|
373 |
croak("$0: cannot deal with non-numeric CTD fields\n")
|
|
374 |
unless &antsNumbers($CTD_elapsed,$CTD_depth,$CTD_w);
|
|
375 |
push(@{$CTD{ELAPSED}},$ants_[0][$CTD_elapsed]);
|
|
376 |
push(@{$CTD{DEPTH}}, $ants_[0][$CTD_depth]+$opt_a);
|
|
377 |
push(@{$CTD{SVEL}}, $ants_[0][$CTD_svel]);
|
|
378 |
push(@{$CTD{W}}, $ants_[0][$CTD_w]);
|
|
379 |
push(@{$CTD{W_T}}, $ants_[0][$CTD_w_t]);
|
|
380 |
if ($ants_[0][$CTD_depth]+$opt_a > $CTD_maxdepth) {
|
|
381 |
$CTD_maxdepth = $ants_[0][$CTD_depth]+$opt_a;
|
|
382 |
$CTD_atbottom = $#{$CTD{DEPTH}};
|
|
383 |
}
|
|
384 |
} while (&antsIn());
|
|
385 |
|
|
386 |
$CTD{DT} = ($CTD{ELAPSED}[$#{$CTD{ELAPSED}}] - $CTD{ELAPSED}[0]) / $#{$CTD{ELAPSED}};
|
|
387 |
|
|
388 |
progress("\t%d scans at %.1fHz\n",scalar(@{$CTD{DEPTH}}),1/$CTD{DT});
|
|
389 |
progress("\tstart depth = %.1fm\n",$CTD{DEPTH}[0]);
|
|
390 |
progress("\tmax depth = %.1fm (# $CTD_atbottom)\n",$CTD_maxdepth);
|
|
391 |
|
|
392 |
#--------------------------------------------------------------------
|
|
393 |
# Construct sound-speed correction profile from CTD 1Hz downcast data
|
|
394 |
# very simple algorithm that stores the last value found
|
|
395 |
# in each 1m bin
|
|
396 |
#--------------------------------------------------------------------
|
|
397 |
|
|
398 |
progress("Constructing sound-speed correction profile\n");
|
|
399 |
|
|
400 |
my($scans_per_sec) = int(1/$CTD{DT}+0.5);
|
|
401 |
for (my($s)=0; $s<=$CTD_atbottom; $s+=$scans_per_sec) {
|
|
402 |
next unless ($CTD{DEPTH}[$s] >= 0);
|
|
403 |
$sVelProf[int($CTD{DEPTH}[$s])] = $CTD{SVEL}[$s];
|
|
404 |
}
|
|
405 |
|
|
406 |
#-------------------
|
|
407 |
# Determine time lag
|
|
408 |
#-------------------
|
|
409 |
|
|
410 |
if (defined($opt_i)) {
|
|
411 |
progress("Setting initial time lag...\n");
|
|
412 |
$CTD{TIME_LAG} = $opt_i;
|
|
413 |
progress("\t-i => elapsed(CTD) ~ elapsed(LADCP) + %.1fs\n",$CTD{TIME_LAG});
|
|
414 |
} else {
|
|
415 |
progress("Guestimating time lag...\n");
|
|
416 |
|
|
417 |
my($CTD_10pct_down) = my($LADCP_10pct_down) = 0;
|
|
418 |
$CTD_10pct_down++
|
|
419 |
until ($CTD{DEPTH}[$CTD_10pct_down]-$CTD{DEPTH}[0] >= 0.1*($CTD_maxdepth-$CTD{DEPTH}[0]));
|
|
420 |
$LADCP_10pct_down++
|
|
421 |
until ($LADCP{ENSEMBLE}[$LADCP_10pct_down]->{DEPTH} >= 0.1*$LADCP{ENSEMBLE}[$LADCP_atbottom]->{DEPTH});
|
|
422 |
|
|
423 |
$CTD{TIME_LAG} = $CTD{ELAPSED}[$CTD_10pct_down] - $LADCP{ENSEMBLE}[$LADCP_10pct_down]->{ELAPSED};
|
|
424 |
|
|
425 |
progress("\telapsed(dz(CTD)=%.1fm) ~ elapsed(dz(LADCP)=%.1fm) + %.1fs\n",
|
|
426 |
$CTD{DEPTH}[$CTD_10pct_down]-$CTD{DEPTH}[0],$LADCP{ENSEMBLE}[$LADCP_10pct_down]->{DEPTH},$CTD{TIME_LAG});
|
|
427 |
}
|
|
428 |
|
|
429 |
$CTD{TIME_LAG} = calc_lag($n_lags[0],$w_size[0],int(1/$CTD{DT}+0.5));
|
|
430 |
progress("\telapsed(CTD) ~ elapsed(LADCP) + %.2fs\n",$CTD{TIME_LAG});
|
|
431 |
|
|
432 |
$CTD{TIME_LAG} = calc_lag($n_lags[1],$w_size[1],1);
|
|
433 |
progress("\telapsed(CTD) = elapsed(LADCP) + %.2fs\n",$CTD{TIME_LAG});
|
|
434 |
&antsAddParams('CTD_time_lag',$CTD{TIME_LAG});
|
|
435 |
|
|
436 |
#------------------------------------------------
|
|
437 |
# Merge CTD with LADCP data
|
|
438 |
# - after this step, reflr w is sound-speed corrected!!!
|
|
439 |
#------------------------------------------------
|
|
440 |
|
|
441 |
progress("Merging CTD with LADCP data...\n");
|
|
442 |
|
|
443 |
for (my($warned),my($ens)=$firstGoodEns; $ens<=$lastGoodEns; $ens++) {
|
|
444 |
my($scan) = int(($LADCP{ENSEMBLE}[$ens]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[0]) / $CTD{DT} + 0.5);
|
|
445 |
if ($scan <= 0) { # NB: must be <=, rather than <, or assertion below sometimes fails
|
|
446 |
info("CTD data begin with LADCP in water\n") unless ($warned++);
|
|
447 |
$firstGoodEns = $ens+1;
|
|
448 |
next;
|
|
449 |
}
|
|
450 |
unless ($scan <= $#{$CTD{ELAPSED}}) {
|
|
451 |
info("CTD data end with LADCP in water\n");
|
|
452 |
$lastGoodEns = $ens-1;
|
|
453 |
last;
|
|
454 |
}
|
|
455 |
|
|
456 |
die("assertion failed!\n" .
|
|
457 |
"\ttest: abs($LADCP{ENSEMBLE}[$ens]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[$scan]) <= $CTD{DT}/2\n" .
|
|
458 |
"\tens = $ens, scan = $scan\n" .
|
|
459 |
sprintf("\tadjusted LADCP time = %f\n",$LADCP{ENSEMBLE}[$ens]->{ELAPSED} + $CTD{TIME_LAG}) .
|
|
460 |
sprintf("\tCTD($scan) time = %f\n",$CTD{ELAPSED}[$scan]) .
|
|
461 |
"=> Did you use SeaBird elapsed time? Don't!"
|
|
462 |
) unless (abs($LADCP{ENSEMBLE}[$ens]->{ELAPSED} + $CTD{TIME_LAG} - $CTD{ELAPSED}[$scan]) <= $CTD{DT}/2);
|
|
463 |
|
|
464 |
$LADCP{ENSEMBLE}[$ens]->{CTD_ELAPSED} = $CTD{ELAPSED}[$scan]; # elapsed field for output
|
|
465 |
|
|
466 |
if (defined($LADCP{ENSEMBLE}[$ens]->{REFLR_W})) { # not a gap
|
|
467 |
$LADCP{ENSEMBLE}[$ens]->{REFLR_W} *= $CTD{SVEL}[$scan]/1500; # correct (inadequately) for sound-speed variations
|
|
468 |
croak(sprintf("\n$0: negative depth (%.1fm) in CTD file at elapsed(CTD) = %.1fs (use -a?)\n",
|
|
469 |
$CTD{DEPTH}[$scan],$CTD{ELAPSED}[$scan]))
|
|
470 |
unless ($CTD{DEPTH}[$scan] >= 0);
|
|
471 |
$LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH} = $CTD{DEPTH}[$scan];
|
|
472 |
$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN} = $scan;
|
|
473 |
my($reflr_ocean_w) = $LADCP{ENSEMBLE}[$ens]->{REFLR_W} - $CTD{W}[$scan];
|
|
474 |
if (abs($reflr_ocean_w) <= $opt_m) {
|
|
475 |
$sumWsq += &SQR($reflr_ocean_w);
|
|
476 |
$nWsq++;
|
|
477 |
if ($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH} > 100 &&
|
|
478 |
$LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH} < $LADCP{ENSEMBLE}[$LADCP_atbottom]->{CTD_DEPTH}-100) {
|
|
479 |
$sumWsqI += &SQR($reflr_ocean_w);
|
|
480 |
$nWsqI++;
|
|
481 |
}
|
|
482 |
} else {
|
|
483 |
undef($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}); # DON'T USE THIS ENSEMBLE LATER
|
|
484 |
}
|
|
485 |
} else{
|
|
486 |
undef($LADCP{ENSEMBLE}[$ens]->{REFLR_W}); # don't output in time-series file
|
|
487 |
undef($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}); # old DEPTH from calcLADCPts()
|
|
488 |
}
|
|
489 |
}
|
|
490 |
|
|
491 |
if ($nWsq > 0 && $nWsqI > 0) {
|
|
492 |
&antsAddParams('rms_w_reflr_err',sqrt($sumWsq/$nWsq),'rms_w_reflr_err_interior',sqrt($sumWsqI/$nWsqI));
|
|
493 |
progress("\t%.2f cm/s rms reference-layer w_ocean, %.2f cm/s away from boundaries\n",
|
|
494 |
100*sqrt($sumWsq/$nWsq),100*sqrt($sumWsqI/$nWsqI));
|
|
495 |
warning(0,"%.2f cm/s reference-layer w_ocean away from boundaries\n",100*sqrt($sumWsqI/$nWsqI))
|
|
496 |
if (sqrt($sumWsqI/$nWsqI) > 0.05);
|
|
497 |
croak("$0: rms reference-layer w_ocean is too large\n")
|
|
498 |
unless (sqrt($sumWsqI/$nWsqI) < 0.07);
|
|
499 |
} elsif ($nWsq > 0) {
|
|
500 |
&antsAddParams('rms_w_reflr_err',sqrt($sumWsq/$nWsq),'rms_w_reflr_err_interior',nan);
|
|
501 |
progress("\t%.2f cm/s rms reference-layer w_ocean\n",100*sqrt($sumWsq/$nWsq));
|
|
502 |
} else {
|
|
503 |
croak("$0: no valid vertical velocities\n");
|
|
504 |
}
|
|
505 |
|
|
506 |
#-------------------
|
|
507 |
# Find or set seabed
|
|
508 |
#--------------------
|
|
509 |
|
|
510 |
if (numberp($opt_h)) {
|
|
511 |
progress("Setting water depth (-h)\n");
|
|
512 |
$water_depth = $opt_h;
|
|
513 |
$sig_water_depth = 0;
|
|
514 |
} else {
|
|
515 |
progress("Finding seabed...\n");
|
|
516 |
($water_depth,$sig_water_depth) =
|
|
517 |
find_seabed(\%LADCP,$LADCP_atbottom,$LADCP{BEAM_COORDINATES});
|
|
518 |
}
|
|
519 |
|
|
520 |
&antsAddParams('water_depth',$water_depth,'water_depth.sig',$sig_water_depth);
|
|
521 |
progress("\t%d(%d) m water depth\n",$water_depth,$sig_water_depth)
|
|
522 |
if defined($water_depth);
|
|
523 |
|
|
524 |
#----------------------------------------------------------------------------
|
|
525 |
# Remove data contaminated by sidelobe reflection from seabed and sea surface
|
|
526 |
#----------------------------------------------------------------------------
|
|
527 |
|
|
528 |
if ($LADCP{ENSEMBLE}[$LADCP_atbottom]->{XDUCER_FACING_DOWN}) {
|
|
529 |
&antsAddParams('ADCP_orientation','downlooker');
|
|
530 |
if (defined($water_depth)) {
|
|
531 |
progress("Editing data to remove sidelobe interference from seabed...\n");
|
|
532 |
($nvrm,$nerm) = editSideLobes($firstGoodEns,$lastGoodEns,$water_depth);
|
|
533 |
progress("\t$nvrm velocities from $nerm ensembles removed\n");
|
|
534 |
} else {
|
|
535 |
info("could not find seabed --- no sidelobe editing done\n");
|
|
536 |
}
|
|
537 |
} else {
|
|
538 |
&antsAddParams('ADCP_orientation','uplooker');
|
|
539 |
progress("Editing data to remove sidelobe interference from sea surface...\n");
|
|
540 |
($nvrm,$nerm) = editSideLobes($firstGoodEns,$lastGoodEns,$water_depth);
|
|
541 |
progress("\t$nvrm velocities from $nerm ensembles removed\n");
|
|
542 |
}
|
|
543 |
|
|
544 |
#---------------------------------------------------------------------------
|
|
545 |
# Depth-bin LADCP velocity data
|
|
546 |
#
|
|
547 |
# NOTES:
|
|
548 |
# 1) ensemble and bin numbers are saved for maximum flexibility
|
|
549 |
# 2) only ensemble/bins with valid vertical velocities are saved
|
|
550 |
# 3) applying the full soundspeed correction to w is most likely pointless in
|
|
551 |
# practice, but hey!, CPU cycles are cheap; [in a cast in the Gulf of Mexico
|
|
552 |
# which has fairly pronounce soundspeed gradients, the max value of Kn
|
|
553 |
# is 1.00004160558372, which gives rise to a correction of less than 0.2mm/s
|
|
554 |
# at a winch+wave speed of 3m/s....]
|
|
555 |
# 4) as far as I can tell, the soundspeed correction for bin length also
|
|
556 |
# has only a minute effect
|
|
557 |
#---------------------------------------------------------------------------
|
|
558 |
|
|
559 |
my($tanSqBeamAngle) = tan(rad($LADCP{BEAM_ANGLE}))**2;
|
|
560 |
|
|
561 |
sub sscorr_w($$$$) # sound-speed correction for w
|
|
562 |
{ # see RDI Coord. Trans. manual sec. 4.1, ...
|
|
563 |
my($wObs,$wCTD,$dADCP,$dBin) = @_; # but there is an error: the ^2 applies to the []
|
|
564 |
|
|
565 |
$dADCP = int($dADCP); # @sVelProf is binned to 1m
|
|
566 |
$dBin = int($dBin);
|
|
567 |
|
|
568 |
while (!numberp($sVelProf[$dADCP])) { $dADCP--; } # skip gaps & bottom of profile
|
|
569 |
while (!numberp($sVelProf[$dBin ])) { $dBin--; }
|
|
570 |
|
|
571 |
my($Kn) = sqrt(1 + (1 - $sVelProf[$dBin]/$sVelProf[$dADCP])**2 * $tanSqBeamAngle);
|
|
572 |
return ($wObs*$sVelProf[$dBin]/1500 - $wCTD) / $Kn;
|
|
573 |
}
|
|
574 |
|
|
575 |
sub sscorr_bindz($) # see RDI Coord Trans manual sec. 4.2
|
|
576 |
{
|
|
577 |
my($ens) = @_;
|
|
578 |
my(@bindz);
|
|
579 |
|
|
580 |
my($curdz) = 0; # calc avg sndspeed btw transducer & 1st bin
|
|
581 |
$curdz-- until numberp($sVelProf[int($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}+$curdz)]);
|
|
582 |
my($avgss) = my($ADCPss) = $sVelProf[int($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}+$curdz)];
|
|
583 |
|
|
584 |
my($sumss) = my($nss) = 0;
|
|
585 |
if ($LADCP{ENSEMBLE}[$ens]->{XDUCER_FACING_UP}) {
|
|
586 |
while ($curdz >= -$LADCP{DISTANCE_TO_BIN1_CENTER}*cos(rad($LADCP{ENSEMBLE}[$ens]->{TILT}))) {
|
|
587 |
if (numberp($sVelProf[int($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}+$curdz)])) {
|
|
588 |
$sumss += $sVelProf[int($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}+$curdz)]; $nss++;
|
|
589 |
}
|
|
590 |
$curdz--;
|
|
591 |
}
|
|
592 |
} else {
|
|
593 |
while ($curdz <= $LADCP{DISTANCE_TO_BIN1_CENTER}*cos(rad($LADCP{ENSEMBLE}[$ens]->{TILT}))) {
|
|
594 |
if (numberp($sVelProf[int($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}+$curdz)])) {
|
|
595 |
$sumss += $sVelProf[int($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}+$curdz)]; $nss++;
|
|
596 |
}
|
|
597 |
$curdz++;
|
|
598 |
}
|
|
599 |
}
|
|
600 |
$avgss = $sumss/$nss if ($nss>0);
|
|
601 |
|
|
602 |
my($Kn) = sqrt(1 + (1 - $avgss/$ADCPss)**2 * $tanSqBeamAngle);
|
|
603 |
$bindz[0] = $LADCP{ENSEMBLE}[$ens]->{XDUCER_FACING_UP} ?
|
|
604 |
- $LADCP{DISTANCE_TO_BIN1_CENTER}*$Kn*$avgss/1500*cos(rad($LADCP{ENSEMBLE}[$ens]->{TILT})) :
|
|
605 |
+ $LADCP{DISTANCE_TO_BIN1_CENTER}*$Kn*$avgss/1500*cos(rad($LADCP{ENSEMBLE}[$ens]->{TILT}));
|
|
606 |
|
|
607 |
for (my($bin)=1; $bin<=$LADCP_lastBin-1; $bin++) {
|
|
608 |
$sumss = $nss = 0;
|
|
609 |
if ($LADCP{ENSEMBLE}[$ens]->{XDUCER_FACING_UP}) {
|
|
610 |
while ($curdz >= $bindz[$bin-1]-$LADCP{BIN_LENGTH}*cos(rad($LADCP{ENSEMBLE}[$ens]->{TILT}))) {
|
|
611 |
if (numberp($sVelProf[int($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}+$curdz)])) {
|
|
612 |
$sumss += $sVelProf[int($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}+$curdz)]; $nss++;
|
|
613 |
}
|
|
614 |
$curdz--;
|
|
615 |
}
|
|
616 |
} else {
|
|
617 |
while ($curdz <= $bindz[$bin-1]+$LADCP{BIN_LENGTH}*cos(rad($LADCP{ENSEMBLE}[$ens]->{TILT}))) {
|
|
618 |
if (numberp($sVelProf[int($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}+$curdz)])) {
|
|
619 |
$sumss += $sVelProf[int($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH}+$curdz)]; $nss++;
|
|
620 |
}
|
|
621 |
$curdz++;
|
|
622 |
}
|
|
623 |
}
|
|
624 |
$avgss = $sumss/$nss if ($nss > 0); # otherwise, leave avgss as is
|
|
625 |
|
|
626 |
$Kn = sqrt(1 + (1 - $avgss/$ADCPss)**2 * $tanSqBeamAngle);
|
|
627 |
$bindz[$bin] = $LADCP{ENSEMBLE}[$ens]->{XDUCER_FACING_UP} ?
|
|
628 |
$bindz[$bin-1] - $LADCP{BIN_LENGTH}*$Kn*$avgss/1500*cos(rad($LADCP{ENSEMBLE}[$ens]->{TILT})) :
|
|
629 |
$bindz[$bin-1] + $LADCP{BIN_LENGTH}*$Kn*$avgss/1500*cos(rad($LADCP{ENSEMBLE}[$ens]->{TILT}));
|
|
630 |
}
|
|
631 |
|
|
632 |
return @bindz;
|
|
633 |
}
|
|
634 |
|
|
635 |
progress("Binning velocities...\n");
|
|
636 |
|
|
637 |
progress("\tdowncast...\n");
|
|
638 |
for ($ens=$firstGoodEns; $ens<$LADCP_atbottom; $ens++) { # downcast
|
|
639 |
next unless numberp($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH});
|
|
640 |
my(@dz) = sscorr_bindz($ens);
|
|
641 |
for ($bin=$LADCP_firstBin-1; $bin<=$LADCP_lastBin-1; $bin++) {
|
|
642 |
next unless numberp($LADCP{ENSEMBLE}[$ens]->{W}[$bin]);
|
|
643 |
|
|
644 |
my($depth) = $LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH} + $dz[$bin]; # depth-bin velocities
|
|
645 |
$LADCP{ENSEMBLE}[$ens]->{CORRECTED_OCEAN_W}[$bin] =
|
|
646 |
sscorr_w($LADCP{ENSEMBLE}[$ens]->{W}[$bin],
|
|
647 |
$CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
|
|
648 |
$LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH},
|
|
649 |
$depth);
|
|
650 |
my($bi) = $depth/$opt_o;
|
|
651 |
push(@{$DNCAST{ENSEMBLE}[$bi]},$ens);
|
|
652 |
push(@{$DNCAST{ELAPSED}[$bi]},$CTD{ELAPSED}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]);
|
|
653 |
push(@{$DNCAST{CTD_W}[$bi]},$CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]);
|
|
654 |
push(@{$DNCAST{BIN}[$bi]},$bin);
|
|
655 |
push(@{$DNCAST{DEPTH}[$bi]},$depth);
|
|
656 |
push(@{$DNCAST{W}[$bi]},$LADCP{ENSEMBLE}[$ens]->{CORRECTED_OCEAN_W}[$bin]);
|
|
657 |
|
|
658 |
push(@{$DNCAST{PKGCORR_W}[100*round($CTD{W_T}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],0.01)+500]},
|
|
659 |
$LADCP{ENSEMBLE}[$ens]->{CORRECTED_OCEAN_W}[$bin])
|
|
660 |
if defined($opt_p) && abs($CTD{W_T}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]) < 5;
|
|
661 |
}
|
|
662 |
}
|
|
663 |
|
|
664 |
if (defined($opt_x)) {
|
|
665 |
progress("\t\tapplying surface-wave correction...\n");
|
|
666 |
for (my($bi)=0; $bi<=$#{$DNCAST{ENSEMBLE}}; $bi++) { # first apply polynomial correction
|
|
667 |
for (my($i)=0; $i<@{$DNCAST{W}[$bi]}; $i++) {
|
|
668 |
for (my($e)=0; $e<@dc_corr_poly; $e++) {
|
|
669 |
$DNCAST{W}[$bi][$i] -= $dc_corr_poly[$e] *
|
|
670 |
$CTD{W_T}[$LADCP{ENSEMBLE}[$DNCAST{ENSEMBLE}[$bi][$i]]->{CTD_SCAN}]**$e;
|
|
671 |
}
|
|
672 |
}
|
|
673 |
}
|
|
674 |
}
|
|
675 |
|
|
676 |
for (my($bi)=0; $bi<=$#{$DNCAST{ENSEMBLE}}; $bi++) {
|
|
677 |
$DNCAST{MEAN_DEPTH}[$bi] = avg(@{$DNCAST{DEPTH}[$bi]});
|
|
678 |
$DNCAST{MEAN_ELAPSED}[$bi] = avg(@{$DNCAST{ELAPSED}[$bi]});
|
|
679 |
$DNCAST{MEDIAN_W}[$bi] = median(@{$DNCAST{W}[$bi]});
|
|
680 |
$DNCAST{MAD_W}[$bi] = mad2($DNCAST{MEDIAN_W}[$bi],@{$DNCAST{W}[$bi]});
|
|
681 |
$DNCAST{N_SAMP}[$bi] = @{$DNCAST{W}[$bi]};
|
|
682 |
}
|
|
683 |
|
|
684 |
|
|
685 |
progress("\tupcast...\n"); # upcast
|
|
686 |
|
|
687 |
for ($ens=$LADCP_atbottom; $ens<=$lastGoodEns; $ens++) {
|
|
688 |
next unless numberp($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH});
|
|
689 |
my(@dz) = sscorr_bindz($ens);
|
|
690 |
for ($bin=$LADCP_firstBin-1; $bin<=$LADCP_lastBin-1; $bin++) {
|
|
691 |
next unless numberp($LADCP{ENSEMBLE}[$ens]->{W}[$bin]);
|
|
692 |
my($depth) = $LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH} + $dz[$bin];
|
|
693 |
$LADCP{ENSEMBLE}[$ens]->{CORRECTED_OCEAN_W}[$bin] =
|
|
694 |
sscorr_w($LADCP{ENSEMBLE}[$ens]->{W}[$bin],
|
|
695 |
$CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
|
|
696 |
$LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH},
|
|
697 |
$depth);
|
|
698 |
my($bi) = $depth/$opt_o;
|
|
699 |
push(@{$UPCAST{ENSEMBLE}[$bi]},$ens);
|
|
700 |
push(@{$UPCAST{ELAPSED}[$bi]},$CTD{ELAPSED}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]);
|
|
701 |
push(@{$UPCAST{CTD_W}[$bi]},$CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]);
|
|
702 |
push(@{$UPCAST{BIN}[$bi]},$bin);
|
|
703 |
push(@{$UPCAST{DEPTH}[$bi]},$depth);
|
|
704 |
push(@{$UPCAST{W}[$bi]},$LADCP{ENSEMBLE}[$ens]->{CORRECTED_OCEAN_W}[$bin]);
|
|
705 |
|
|
706 |
push(@{$UPCAST{PKGCORR_W}[100*round($CTD{W_T}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],0.01)+500]},
|
|
707 |
$LADCP{ENSEMBLE}[$ens]->{CORRECTED_OCEAN_W}[$bin])
|
|
708 |
if defined($opt_p) && abs($CTD{W_T}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]) < 5;
|
|
709 |
}
|
|
710 |
}
|
|
711 |
|
|
712 |
if (defined($opt_x)) {
|
|
713 |
progress("\t\tapplying surface-wave correction...\n");
|
|
714 |
for (my($bi)=0; $bi<=$#{$UPCAST{ENSEMBLE}}; $bi++) { # first apply polynomial correction
|
|
715 |
for (my($i)=0; $i<@{$UPCAST{W}[$bi]}; $i++) {
|
|
716 |
for (my($e)=0; $e<@uc_corr_poly; $e++) {
|
|
717 |
$UPCAST{W}[$bi][$i] -= $uc_corr_poly[$e] *
|
|
718 |
$CTD{W_T}[$LADCP{ENSEMBLE}[$UPCAST{ENSEMBLE}[$bi][$i]]->{CTD_SCAN}]**$e;
|
|
719 |
}
|
|
720 |
}
|
|
721 |
}
|
|
722 |
}
|
|
723 |
|
|
724 |
for (my($bi)=0; $bi<=$#{$UPCAST{ENSEMBLE}}; $bi++) {
|
|
725 |
$UPCAST{MEAN_DEPTH}[$bi] = avg(@{$UPCAST{DEPTH}[$bi]});
|
|
726 |
$UPCAST{MEAN_ELAPSED}[$bi] = avg(@{$UPCAST{ELAPSED}[$bi]});
|
|
727 |
$UPCAST{MEDIAN_W}[$bi] = median(@{$UPCAST{W}[$bi]});
|
|
728 |
$UPCAST{MAD_W}[$bi] = mad2($UPCAST{MEDIAN_W}[$bi],@{$UPCAST{W}[$bi]});
|
|
729 |
$UPCAST{N_SAMP}[$bi] = @{$UPCAST{W}[$bi]};
|
|
730 |
}
|
|
731 |
|
|
732 |
#---------------
|
|
733 |
# Output profile
|
|
734 |
#---------------
|
|
735 |
|
|
736 |
progress("Writing vertical-velocity profile...\n");
|
|
737 |
|
|
738 |
@antsNewLayout = ('depth','dc_depth','dc_elapsed','dc_w','dc_w.mad','dc_w.N',
|
|
739 |
'uc_depth','uc_elapsed','uc_w','uc_w.mad','uc_w.N');
|
|
740 |
|
|
741 |
for (my($bi)=0; $bi<=max($#{$DNCAST{ENSEMBLE}},$#{$UPCAST{ENSEMBLE}}); $bi++) {
|
|
742 |
&antsOut(($bi+0.5)*$opt_o, # nominal depth
|
|
743 |
$DNCAST{MEAN_DEPTH}[$bi],$DNCAST{MEAN_ELAPSED}[$bi],
|
|
744 |
$DNCAST{N_SAMP}[$bi]>=$opt_s?$DNCAST{MEDIAN_W}[$bi]:nan,
|
|
745 |
$DNCAST{MAD_W}[$bi],$DNCAST{N_SAMP}[$bi],
|
|
746 |
$UPCAST{MEAN_DEPTH}[$bi],$UPCAST{MEAN_ELAPSED}[$bi],
|
|
747 |
$UPCAST{N_SAMP}[$bi]>=$opt_s?$UPCAST{MEDIAN_W}[$bi]:nan,
|
|
748 |
$UPCAST{MAD_W}[$bi],$UPCAST{N_SAMP}[$bi]);
|
|
749 |
}
|
|
750 |
|
|
751 |
#-------------------------
|
|
752 |
# surface-wave effect file
|
|
753 |
#-------------------------
|
|
754 |
|
|
755 |
if (defined($opt_p)) {
|
|
756 |
progress("Writing surface-wave-correction data to <$opt_p>...\n");
|
|
757 |
|
|
758 |
@antsNewLayout = ('CTD_w_t','dc_w','dc_w.mad','dc_w.N','uc_w','uc_w.mad','uc_w.N','dc_w_corr','uc_w_corr');
|
|
759 |
&antsOut('EOF');
|
|
760 |
|
|
761 |
close(STDOUT);
|
|
762 |
open(STDOUT,">$opt_p") || croak("$opt_p: $!\n");
|
|
763 |
|
|
764 |
for (my($bi)=0; $bi<=max($#{$DNCAST{PKGCORR_W}},$#{$UPCAST{PKGCORR_W}}); $bi++) {
|
|
765 |
my($dc_N) = scalar(@{$DNCAST{PKGCORR_W}[$bi]});
|
|
766 |
my($uc_N) = scalar(@{$UPCAST{PKGCORR_W}[$bi]});
|
|
767 |
next unless ($dc_N>0 || $uc_N>0);
|
|
768 |
my($dc_w) = median(@{$DNCAST{PKGCORR_W}[$bi]});
|
|
769 |
my($uc_w) = median(@{$UPCAST{PKGCORR_W}[$bi]});
|
|
770 |
my($w_t) = ($bi-500) / 100;
|
|
771 |
if (defined($opt_x)) {
|
|
772 |
my($dc_corr) = my($uc_corr) = 0;
|
|
773 |
for (my($e)=0; $e<@dc_corr_poly; $e++) {
|
|
774 |
$dc_corr += $dc_corr_poly[$e]*$w_t**$e;
|
|
775 |
}
|
|
776 |
for (my($e)=0; $e<@uc_corr_poly; $e++) {
|
|
777 |
$uc_corr += $uc_corr_poly[$e]*$w_t**$e;
|
|
778 |
}
|
|
779 |
&antsOut($w_t,$dc_w,mad2($dc_w,@{$DNCAST{PKGCORR_W}[$bi]}),$dc_N,
|
|
780 |
$uc_w,mad2($uc_w,@{$UPCAST{PKGCORR_W}[$bi]}),$uc_N,
|
|
781 |
$dc_corr,$uc_corr);
|
|
782 |
} else {
|
|
783 |
&antsOut($w_t,$dc_w,mad2($dc_w,@{$DNCAST{PKGCORR_W}[$bi]}),$dc_N,
|
|
784 |
$uc_w,mad2($uc_w,@{$UPCAST{PKGCORR_W}[$bi]}),$uc_N);
|
|
785 |
}
|
|
786 |
}
|
|
787 |
}
|
|
788 |
|
|
789 |
#--------------------------------------
|
|
790 |
# write time-series output if requested
|
|
791 |
#--------------------------------------
|
|
792 |
|
|
793 |
if (defined($opt_f)) {
|
|
794 |
progress("Writing time-series data to <$opt_f>...\n");
|
|
795 |
|
|
796 |
@antsNewLayout = ('ens','elapsed',
|
|
797 |
'depth','sound_speed','pitch','gimbal_pitch','roll','tilt','heading',
|
|
798 |
'CTD_w','CTD_w_t','LADCP_reflr_w','LADCP_reflr_w_err',
|
|
799 |
'ocean_reflr_w');
|
|
800 |
&antsOut('EOF');
|
|
801 |
|
|
802 |
close(STDOUT);
|
|
803 |
open(STDOUT,">$opt_f") || croak("$opt_f: $!\n");
|
|
804 |
|
|
805 |
for ($ens=$firstGoodEns; $ens<=$lastGoodEns; $ens++) {
|
|
806 |
my($reflr_w) = defined($LADCP{ENSEMBLE}[$ens]->{REFLR_W})
|
|
807 |
? $LADCP{ENSEMBLE}[$ens]->{REFLR_W} - $CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]
|
|
808 |
: undef;
|
|
809 |
&antsOut($ens,
|
|
810 |
$CTD{ELAPSED}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
|
|
811 |
$LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH},
|
|
812 |
$CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
|
|
813 |
$LADCP{ENSEMBLE}[$ens]->{PITCH},
|
|
814 |
$LADCP{ENSEMBLE}[$ens]->{GIMBAL_PITCH},
|
|
815 |
$LADCP{ENSEMBLE}[$ens]->{ROLL},
|
|
816 |
$LADCP{ENSEMBLE}[$ens]->{TILT},
|
|
817 |
$LADCP{ENSEMBLE}[$ens]->{HEADING},
|
|
818 |
$CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
|
|
819 |
$CTD{W_T}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
|
|
820 |
$LADCP{ENSEMBLE}[$ens]->{REFLR_W},
|
|
821 |
$LADCP{ENSEMBLE}[$ens]->{REFLR_W_ERR},
|
|
822 |
$reflr_w);
|
|
823 |
}
|
|
824 |
|
|
825 |
close(STDOUT);
|
|
826 |
undef($antsHeadersPrinted);
|
|
827 |
}
|
|
828 |
|
|
829 |
#--------------------------------------------------------------------------------------------
|
|
830 |
# Output all bins as separate files if requested
|
|
831 |
# NB: - vertical LADCP velocities are corrected inaccurately for sound-speed variations!!!!
|
|
832 |
# - full correction is used, on the other hand, for ocean velocities (w)
|
|
833 |
#--------------------------------------------------------------------------------------------
|
|
834 |
|
|
835 |
if (defined($opt_d)) {
|
|
836 |
|
|
837 |
sub outProfBinRec($$$)
|
|
838 |
{
|
|
839 |
my($ens,$bin,$depth) = @_;
|
|
840 |
my($sscorr) = $CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]/1500;
|
|
841 |
|
|
842 |
&antsOut($ens,
|
|
843 |
$bin,
|
|
844 |
$CTD{ELAPSED}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
|
|
845 |
$depth,
|
|
846 |
$CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
|
|
847 |
$LADCP{ENSEMBLE}[$ens]->{PITCH},
|
|
848 |
$LADCP{ENSEMBLE}[$ens]->{GIMBAL_PITCH},
|
|
849 |
$LADCP{ENSEMBLE}[$ens]->{ROLL},
|
|
850 |
$LADCP{ENSEMBLE}[$ens]->{TILT},
|
|
851 |
$LADCP{ENSEMBLE}[$ens]->{HEADING},
|
|
852 |
$CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
|
|
853 |
$CTD{W_T}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
|
|
854 |
$LADCP{ENSEMBLE}[$ens]->{W}[$bin]*$sscorr,
|
|
855 |
$LADCP{ENSEMBLE}[$ens]->{ERRVEL}[$bin],
|
|
856 |
$LADCP{ENSEMBLE}[$ens]->{REFLR_W},
|
|
857 |
$LADCP{ENSEMBLE}[$ens]->{REFLR_W_ERR},
|
|
858 |
$LADCP{ENSEMBLE}[$ens]->{CORRECTED_OCEAN_W}[$bin]);
|
|
859 |
}
|
|
860 |
|
|
861 |
progress("Writing profile-bin data of downcast...\n");
|
|
862 |
|
|
863 |
$commonParams = $antsCurParams;
|
|
864 |
@antsNewLayout = ('ens','bin','elapsed','depth','sound_speed','pitch','gimbal_pitch',
|
|
865 |
'roll','tilt','heading','CTD_w','CTD_w_t','LADCP_w','LADCP_errvel',
|
|
866 |
'LADCP_reflr_w','LADCP_reflr_w_err','w');
|
|
867 |
|
|
868 |
for (my($bi)=0; $bi<=$#{$DNCAST{ENSEMBLE}}; $bi++) {
|
|
869 |
my($fn) = sprintf("$opt_d%03d.dncast",$bi);
|
|
870 |
&antsOut('EOF');
|
|
871 |
close(STDOUT);
|
|
872 |
open(STDOUT,">$fn") || croak("$fn: $!\n");
|
|
873 |
$antsCurParams = $commonParams;
|
|
874 |
&antsAddParams('CTD_w',avg(@{$DNCAST{CTD_W}[$bi]}));
|
|
875 |
for (my($eii)=0; $eii<=$#{$DNCAST{ENSEMBLE}[$bi]}; $eii++) {
|
|
876 |
&outProfBinRec($DNCAST{ENSEMBLE}[$bi][$eii],$DNCAST{BIN}[$bi][$eii],$DNCAST{DEPTH}[$bi][$eii]);
|
|
877 |
}
|
|
878 |
}
|
|
879 |
|
|
880 |
progress("Writing profile-bin data of upcast...\n");
|
|
881 |
|
|
882 |
for (my($bi)=0; $bi<=$#{$UPCAST{ENSEMBLE}}; $bi++) {
|
|
883 |
my($fn) = sprintf("$opt_d%03d.upcast",$bi);
|
|
884 |
&antsOut('EOF');
|
|
885 |
close(STDOUT);
|
|
886 |
open(STDOUT,">$fn") || croak("$fn: $!\n");
|
|
887 |
$antsCurParams = $commonParams;
|
|
888 |
&antsAddParams('CTD_w',avg(@{$UPCAST{CTD_W}[$bi]}));
|
|
889 |
for (my($eii)=0; $eii<=$#{$UPCAST{ENSEMBLE}[$bi]}; $eii++) {
|
|
890 |
&outProfBinRec($UPCAST{ENSEMBLE}[$bi][$eii],$UPCAST{BIN}[$bi][$eii],$UPCAST{DEPTH}[$bi][$eii]);
|
|
891 |
}
|
|
892 |
close(STDOUT);
|
|
893 |
}
|
|
894 |
}
|
|
895 |
|
|
896 |
&antsExit();
|