author | A.M. Thurnherr <athurnherr@yahoo.com> |
Thu, 29 May 2014 08:41:19 +0000 | |
changeset 18 | bb7bb9f83db9 |
parent 13 | b176da8559b3 |
child 21 | 0b5bbe60131c |
permissions | -rwxr-xr-x |
0 | 1 |
#!/usr/bin/perl |
2 |
#====================================================================== |
|
3 |
# L I S T B I N S |
|
4 |
# doc: Fri Aug 25 15:57:05 2006 |
|
18 | 5 |
# dlm: Tue Apr 22 12:39:09 2014 |
0 | 6 |
# (c) 2006 A.M. Thurnherr |
18 | 7 |
# uE-Info: 50 45 NIL 0 0 72 2 2 4 NIL ofnI |
0 | 8 |
#====================================================================== |
9 |
||
10 |
# Split data file into per-bin time series. |
|
11 |
||
12 |
# HISTORY: |
|
13 |
# Aug 25, 2006: - created from [listEns] |
|
14 |
# Aug 26, 2006: - added -M)agdecl |
|
15 |
# - changed -b to -f |
|
16 |
# Aug 27, 2006: - added %bin |
|
17 |
# Aug 28, 2006: - BUG: options were confused |
|
18 |
# Jan 4, 2007: - improved usage message for -a |
|
19 |
# - added %mag_decl |
|
20 |
# - BUG: roundoff error in %pct_good_vels |
|
21 |
# Sep 19, 2007: - adapted to new [RDI_BB_Read.pl] |
|
22 |
# Feb 7, 2008: - added sound-speed correction |
|
23 |
# - enabled 3-beam solutions |
|
24 |
# Feb 8, 2008: - added -d)iscard <beam> |
|
25 |
# - added -b)eam coordinate output |
|
26 |
# Feb 12, 2008: - modified 3-beam output |
|
27 |
# - added -p)ct_good <min> |
|
28 |
# Feb 13, 2008: - various improvements |
|
29 |
# Feb 19, 2008: - BUG: division by zero |
|
30 |
# BUG: min() did not work with 1st elt undef |
|
31 |
# Feb 21, 2008: - BUG: had forgotten to undo debugging changes |
|
32 |
# - removed missing magdecl warning on -b |
|
33 |
# Feb 22, 2008: - moved ssCorr() to [RDI_Utils.pl] |
|
34 |
# - BUG: %d complete ensembles was written to STDOUT |
|
35 |
# - BUG: %N_ensembles was total number of ensembles, not |
|
36 |
# only the ones used |
|
37 |
# - BUG: 0 errvel was output for 3-beam solutions => |
|
38 |
# wrong time-average statistics |
|
39 |
# May 19, 2009: - added -w to calculate vertical velocities from |
|
40 |
# two beam pairs separately |
|
41 |
# May 21, 2009: - added horizontal beampair velocities on -w |
|
42 |
# - -P)itchRoll <bias/bias> |
|
43 |
# May 22, 2009: - added -B) <bias/bias/bias/bias> |
|
44 |
# May 23, 2009: - adapted to changed beampair-velocity fun name |
|
1 | 45 |
# Aug 22, 2010: - added -R |
12 | 46 |
# Apr 29, 2013: - cosmetics |
47 |
# - added warning on missing -S |
|
13
b176da8559b3
before implementing WBWens (PD0 writing)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
12
diff
changeset
|
48 |
# Aug 7, 2013: - BUG: -w did not respect -d |
18 | 49 |
# Mar 4, 2014: - added support for missing PITCH/ROLL/HEADING |
50 |
# Apr 22, 2014: - BUG: extraneous semicolon |
|
0 | 51 |
|
52 |
# General Notes: |
|
53 |
# - everything (e.g. beams) is numbered from 1 |
|
54 |
# - no support for BT data |
|
55 |
||
56 |
# Soundspeed Correction: |
|
57 |
# - applied as described in the RDI coord-trans manual |
|
58 |
# - sound-speed variation over range is ignored (valid for small gradients) |
|
59 |
# => - same simple correction for all velocity components |
|
60 |
# - simple correction for cell depths |
|
61 |
||
62 |
# Min %-good (min_pcg): |
|
63 |
# - nan for records w/o valid velocities |
|
64 |
# - min(%-good) of the beams used for the velocity solution |
|
65 |
# - min_pcg does not have to decrease monotonically with distance, |
|
66 |
# at least when 3-beam solutions are allowed and when -p is used to |
|
67 |
# edit the data |
|
68 |
# - non-monotonic min_pcg is particularly obvious with the DYNAMUCK BM_ADCP |
|
69 |
# data, where one of the beams performed much worse than the others |
|
70 |
||
71 |
require "getopts.pl"; |
|
72 |
$0 =~ m{(.*/)[^/]+}; |
|
73 |
require "$1RDI_BB_Read.pl"; |
|
74 |
require "$1RDI_Coords.pl"; |
|
75 |
require "$1RDI_Utils.pl"; |
|
76 |
||
1 | 77 |
die("Usage: $0 [-r)ange <first_ens,last_ens>] [-R)enumber ensembles] " . |
0 | 78 |
"[output -f)ile <pat[bin%d.raw]>] " . |
79 |
"[-a)ll ens (not just those with good vels)] " . |
|
80 |
"[-M)agnetic <declination>] " . |
|
81 |
"[-S)oundspeed correction <salin|*,temp|*,depth|*> " . |
|
82 |
"[-P)itch/Roll <bias/bias>] [-B)eamvel <bias/bias/bias/bias>] " . |
|
83 |
"[require -4)-beam solutions] [-d)iscard <beam#>] " . |
|
12 | 84 |
"[-p)ct-good <min>] " . |
0 | 85 |
"[output -b)eam coordinates] [output two separate -w) estimates] " . |
86 |
"<RDI file>\n") |
|
1 | 87 |
unless (&Getopts("4abB:d:f:M:p:r:P:RS:w") && @ARGV == 1); |
0 | 88 |
|
89 |
($P{pitch_bias},$P{roll_bias}) = split('[,/]',$opt_P); |
|
90 |
($P{velbias_b1},$P{velbias_b2},$P{velbias_b3},$P{velbias_b4}) = split('[,/]',$opt_B); |
|
91 |
||
92 |
die("$0: -4 and -d are mutually exclusive\n") |
|
93 |
if ($opt_4 && defined($opt_d)); |
|
94 |
||
95 |
die("$0: -p and -b are mutually exclusive\n") |
|
96 |
if ($opt_b && defined($opt_p)); |
|
97 |
||
98 |
$opt_p = 0 unless defined($opt_p); |
|
99 |
||
100 |
$RDI_Coords::minValidVels = 4 if ($opt_4); # no 3-beam solutions |
|
101 |
||
102 |
print(STDERR "WARNING: magnetic declination not set!\n") |
|
103 |
unless defined($opt_M) || defined($opt_b); |
|
104 |
||
105 |
$opt_f = 'bin%d.raw' unless defined($opt_f); |
|
106 |
$ifn = $ARGV[0]; |
|
107 |
||
108 |
($first_ens,$last_ens) = split(',',$opt_r) |
|
109 |
if defined($opt_r); |
|
110 |
||
12 | 111 |
if (defined($opt_S)) { |
112 |
($SS_salin,$SS_temp,$SS_depth) = split(',',$opt_S); |
|
113 |
$variable_ssCorr = ($SS_salin eq '*' || $SS_temp eq '*' || $SS_depth eq '*'); |
|
114 |
} else { |
|
115 |
print(STDERR "WARNING: no soundspeed correction applied!\n"); |
|
116 |
} |
|
0 | 117 |
|
118 |
#---------------------------------------------------------------------- |
|
119 |
||
120 |
sub min(@) # return minimum |
|
121 |
{ |
|
122 |
my($min) = 99e99; |
|
123 |
for (my($i)=0; $i<=$#_; $i++) { |
|
124 |
$min = $_[$i] if defined($_[$i]) && ($_[$i] < $min); |
|
125 |
} |
|
126 |
return ($min == 99e99) ? nan : $min; |
|
127 |
} |
|
128 |
||
129 |
sub dumpBin($$$) # write time series of single bin |
|
130 |
{ |
|
131 |
my($b,$fe,$le) = @_; |
|
132 |
my($file) = sprintf($opt_f,$b+1); |
|
133 |
||
134 |
open(P,">$file") || die("$file: $!\n"); |
|
135 |
print(P "#ANTS#PARAMS# "); |
|
136 |
foreach my $k (keys(%P)) { |
|
137 |
print(P "$k\{$P{$k}\} "); |
|
138 |
} |
|
139 |
if ($opt_b) { |
|
140 |
printf(STDERR "%.0f%% ",100*$good_vels[$b]/($le-$fe+1)); |
|
141 |
} else { |
|
142 |
my($pct3b) = ($good_vels[$b] > 0) ? 100*$three_beam[$b]/$good_vels[$b] : nan; |
|
143 |
printf(STDERR "%.0f%%/%.0f%% ",100*$good_vels[$b]/($le-$fe+1),$pct3b); |
|
144 |
printf(P "pct_3_beam{%.0f} ",$pct3b); |
|
145 |
} |
|
146 |
printf(P "pct_good_vels{%.0f} ",100*$good_vels[$b]/($le-$fe+1)); |
|
147 |
printf(P "bin{%d}",$b+1); |
|
148 |
printf(P " soundspeed_correction{%s}",defined($opt_S) ? $opt_S : 'NONE!'); |
|
149 |
printf(P " dz{%g}",$dz[$b] * |
|
150 |
(defined($opt_S) ? ssCorr($dta{ENSEMBLE}[$fe],$SS_salin,$SS_temp,$SS_depth) : 1) |
|
151 |
) unless ($variable_ssCorr); |
|
152 |
print( P "\n"); |
|
153 |
||
154 |
print(P "#ANTS#FIELDS# " . |
|
155 |
"{ensemble} {date} {time} {elapsed} " . |
|
156 |
"{heading} {pitch} {roll} " . |
|
157 |
"{sig_heading} {sig_pitch} {sig_roll} " . |
|
158 |
"{xmit_current} {xmit_voltage} " . |
|
159 |
"{temp} " . |
|
160 |
($opt_b ? "{v1} {v2} {v3} {v4} " : "{u} {v} {w} {err_vel} ") . |
|
161 |
($opt_w ? "{v12} {w12} {v34} {w34} " : "" ) . |
|
162 |
"{corr1} {corr2} {corr3} {corr4} " . |
|
163 |
"{amp1} {amp2} {amp3} {amp4} " . |
|
164 |
($opt_b ? "{pcg1} {pcg2} {pcg3} {pcg4}" : "{3_beam} {min_pcg}") |
|
165 |
); |
|
166 |
print(P " {dz}") if ($variable_ssCorr); |
|
167 |
print(P "\n"); |
|
168 |
||
169 |
my($t0) = $dta{ENSEMBLE}[$fe]->{UNIX_TIME}; |
|
170 |
for (my($e)=$fe; $e<=$le; $e++) { |
|
171 |
next unless ($opt_a || $dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b]); |
|
172 |
||
173 |
my($ssCorr) = defined($opt_S) ? ssCorr($dta{ENSEMBLE}[$e],$SS_salin,$SS_temp,$SS_depth) : 1; |
|
174 |
||
175 |
print(P "$dta{ENSEMBLE}[$e]->{NUMBER} "); |
|
176 |
print(P "$dta{ENSEMBLE}[$e]->{DATE} "); |
|
177 |
print(P "$dta{ENSEMBLE}[$e]->{TIME} "); |
|
178 |
printf(P "%d ",$dta{ENSEMBLE}[$e]->{UNIX_TIME}-$t0); |
|
18 | 179 |
print(P defined($dta{ENSEMBLE}[$e]->{HEADING}) ? "$dta{ENSEMBLE}[$e]->{HEADING} " : 'nan '); |
180 |
print(P defined($dta{ENSEMBLE}[$e]->{PITCH}) ? "$dta{ENSEMBLE}[$e]->{PITCH} " : 'nan '); |
|
181 |
print(P defined($dta{ENSEMBLE}[$e]->{ROLL}) ? "$dta{ENSEMBLE}[$e]->{ROLL} " : 'nan '); |
|
0 | 182 |
print(P "$dta{ENSEMBLE}[$e]->{HEADING_STDDEV} "); |
183 |
print(P "$dta{ENSEMBLE}[$e]->{PITCH_STDDEV} "); |
|
184 |
print(P "$dta{ENSEMBLE}[$e]->{ROLL_STDDEV} "); |
|
185 |
print(P "$dta{ENSEMBLE}[$e]->{ADC_XMIT_CURRENT} "); |
|
186 |
print(P "$dta{ENSEMBLE}[$e]->{ADC_XMIT_VOLTAGE} "); |
|
187 |
print(P "$dta{ENSEMBLE}[$e]->{TEMPERATURE} "); |
|
188 |
if ($dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b]) { |
|
189 |
printf(P "%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0] * $ssCorr); |
|
190 |
printf(P "%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1] * $ssCorr); |
|
191 |
printf(P "%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2] * $ssCorr); |
|
192 |
if ($dta{ENSEMBLE}[$e]->{THREE_BEAM}[$b]) { |
|
193 |
print(P "nan "); |
|
194 |
} else { |
|
195 |
printf(P "%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3] * $ssCorr); |
|
196 |
} |
|
197 |
if ($opt_w) { |
|
198 |
printf(P defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][0]) ? "%g " : "nan ", |
|
199 |
$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][0]); |
|
200 |
printf(P defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][1]) ? "%g " : "nan ", |
|
201 |
$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][1]); |
|
202 |
printf(P defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][2]) ? "%g " : "nan ", |
|
203 |
$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][2]); |
|
204 |
printf(P defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][3]) ? "%g " : "nan ", |
|
205 |
$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][3]); |
|
206 |
} |
|
207 |
} else { |
|
208 |
print(P "nan nan nan nan "); |
|
209 |
print(P "nan nan nan nan ") if ($opt_w); |
|
210 |
} |
|
211 |
print(P "@{$dta{ENSEMBLE}[$e]->{CORRELATION}[$b]} "); |
|
212 |
print(P "@{$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$b]} "); |
|
213 |
if ($opt_b) { |
|
214 |
print(P "@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]} "); |
|
215 |
} else { |
|
216 |
printf(P "%d ",$dta{ENSEMBLE}[$e]->{THREE_BEAM}[$b]); |
|
217 |
printf(P "%s ",min(@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]})); |
|
218 |
} |
|
219 |
printf(P "%g ",$dz[$b]*$ssCorr) if ($variable_ssCorr); |
|
220 |
print(P "\n"); |
|
221 |
} |
|
222 |
close(P); |
|
223 |
} |
|
224 |
||
225 |
#---------------------------------------------------------------------- |
|
226 |
# MAIN |
|
227 |
#---------------------------------------------------------------------- |
|
228 |
||
229 |
$P{RDI_file} = $ifn; |
|
230 |
$P{mag_decl} = $opt_M if defined($opt_M); |
|
231 |
||
232 |
readData($ifn,\%dta); |
|
233 |
printf(STDERR "%d complete ensembles...\n",scalar(@{$dta{ENSEMBLE}})); |
|
234 |
$dta{HEADING_BIAS} = -$opt_M; # magnetic declination |
|
235 |
||
236 |
if ($dta{BEAM_COORDINATES}) { # coords |
|
237 |
$beamCoords = 1; |
|
238 |
} else { |
|
239 |
die("$0: -b requires input in beam coordinates\n") |
|
240 |
if ($opt_b); |
|
241 |
die("$ifn: only beam and earth coordinates implemented so far\n") |
|
242 |
if (!$dta{EARTH_COORDINATES}); |
|
243 |
} |
|
244 |
||
245 |
for (my($b)=0; $b<$dta{N_BINS}; $b++) { # calc dz |
|
246 |
$dz[$b] = $dta{DISTANCE_TO_BIN1_CENTER} + $b*$dta{BIN_LENGTH}; |
|
247 |
} |
|
248 |
||
249 |
$lastGoodBin = 0; |
|
1 | 250 |
for ($e=0; $e<=$#{$dta{ENSEMBLE}}; $e++) { # check/transform velocities |
251 |
$dta{ENSEMBLE}[$e]->{NUMBER} = $e+1 if ($opt_R); # renumber ensembles |
|
0 | 252 |
next if (defined($first_ens) && |
253 |
$dta{ENSEMBLE}[$e]->{NUMBER} < $first_ens); |
|
254 |
||
18 | 255 |
$dta{ENSEMBLE}[$e]->{PITCH} -= $P{pitch_bias} |
256 |
if defined($dta{ENSEMBLE}[$e]->{PITCH}); |
|
257 |
$dta{ENSEMBLE}[$e]->{ROLL} -= $P{roll_bias} |
|
258 |
if defined($dta{ENSEMBLE}[$e]->{ROLL}); |
|
0 | 259 |
|
260 |
$P{first_ens} = $dta{ENSEMBLE}[$e]->{NUMBER},$fe = $e |
|
261 |
unless defined($P{first_ens}); |
|
262 |
last if (defined($last_ens) && |
|
263 |
$dta{ENSEMBLE}[$e]->{NUMBER} > $last_ens); |
|
264 |
$P{last_ens} = $dta{ENSEMBLE}[$e]->{NUMBER}; |
|
265 |
$le = $e; |
|
266 |
||
267 |
die("3-beams used in ensemble #$dta{ENSEMBLE}[$e]->{NUMBER}\n") |
|
268 |
if ($dta{ENSEMBLE}[$e]->{N_BEAMS_USED} < 4); |
|
269 |
die("BIT error in ensemble $dta{ENSEMBLE}[$e]->{NUMBER}\n") |
|
270 |
if defined($dta{ENSEMBLE}[$e]->{BUILT_IN_TEST_ERROR}); |
|
271 |
die("Low gain in ensemble #$dta{ENSEMBLE}[$e]->{NUMBER}\n") |
|
272 |
if ($dta{ENSEMBLE}[$e]->{LOW_GAIN}); |
|
273 |
||
274 |
for (my($b)=0; $b<$dta{N_BINS}; $b++) { |
|
275 |
$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0] -= $P{velbias_b1}; |
|
276 |
$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1] -= $P{velbias_b2}; |
|
277 |
$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2] -= $P{velbias_b3}; |
|
278 |
$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3] -= $P{velbias_b4}; |
|
13
b176da8559b3
before implementing WBWens (PD0 writing)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
12
diff
changeset
|
279 |
if (defined($opt_d)) { |
b176da8559b3
before implementing WBWens (PD0 writing)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
12
diff
changeset
|
280 |
undef($dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b][$opt_d-1]); |
b176da8559b3
before implementing WBWens (PD0 writing)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
12
diff
changeset
|
281 |
undef($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][$opt_d-1]); |
b176da8559b3
before implementing WBWens (PD0 writing)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
12
diff
changeset
|
282 |
} |
0 | 283 |
if ($opt_w) { |
284 |
@{$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b]} = |
|
285 |
velBeamToBPEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]}); |
|
286 |
} |
|
287 |
for (my($i)=0; $i<4; $i++) { |
|
288 |
if ($dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b][$i] < $opt_p) { |
|
289 |
undef($dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b][$i]); |
|
290 |
undef($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][$i]); |
|
291 |
} |
|
292 |
} |
|
293 |
@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} = $beamCoords |
|
294 |
? velInstrumentToEarth(\%dta,$e, |
|
295 |
velBeamToInstrument(\%dta,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]}) |
|
296 |
) |
|
297 |
: velApplyHdgBias(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]}) |
|
298 |
unless ($opt_b); |
|
299 |
unless (defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0])) { |
|
300 |
undef(@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]}); |
|
301 |
next; |
|
302 |
} |
|
303 |
$dta{ENSEMBLE}[$e]->{THREE_BEAM}[$b] = $RDI_Coords::threeBeamFlag; |
|
304 |
$three_beam[$b] += $RDI_Coords::threeBeamFlag; |
|
305 |
$dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b] = 1; |
|
306 |
$good_vels[$b]++; |
|
307 |
$lastGoodBin = $b if ($b > $lastGoodBin); |
|
308 |
$firstGoodEns = $e unless defined($firstGoodEns); |
|
309 |
$lastGoodEns = $e; |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
unless (defined($opt_r)) { |
|
314 |
$fe = $firstGoodEns; |
|
315 |
$le = $lastGoodEns; |
|
316 |
} |
|
317 |
||
318 |
$P{N_ensembles} = $le - $fe + 1; |
|
319 |
||
320 |
$firstBin = 0; |
|
321 |
$lastBin = $lastGoodBin; |
|
322 |
||
323 |
print( STDERR "Start : $dta{ENSEMBLE}[$fe]->{DATE} $dta{ENSEMBLE}[$fe]->{TIME}\n"); |
|
324 |
print( STDERR "End : $dta{ENSEMBLE}[$le]->{DATE} $dta{ENSEMBLE}[$le]->{TIME}\n"); |
|
325 |
printf(STDERR "Bins : %d-%d\n",$firstBin+1,$lastBin+1); |
|
326 |
if ($opt_b) { |
|
327 |
print(STDERR "Good : "); |
|
328 |
} else { |
|
329 |
printf(STDERR "3-Beam : %d %d %d %d\n",$RDI_Coords::threeBeam_1, |
|
330 |
$RDI_Coords::threeBeam_2, |
|
331 |
$RDI_Coords::threeBeam_3, |
|
332 |
$RDI_Coords::threeBeam_4); |
|
333 |
||
334 |
print(STDERR "Good/3-beam: "); |
|
335 |
} |
|
336 |
for ($b=$firstBin; $b<=$lastBin; $b++) { # generate output |
|
337 |
dumpBin($b,$fe,$le); |
|
338 |
} |
|
339 |
print(STDERR "\n"); |
|
340 |
||
341 |
exit(0); |