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