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