14
|
1 |
#!/usr/bin/perl
|
|
2 |
#======================================================================
|
|
3 |
# E D I T P D 0
|
|
4 |
# doc: Mon Nov 25 20:24:31 2013
|
34
|
5 |
# dlm: Tue Apr 12 21:45:31 2016
|
14
|
6 |
# (c) 2013 A.M. Thurnherr
|
34
|
7 |
# uE-Info: 235 46 NIL 0 0 72 2 2 4 NIL ofnI
|
14
|
8 |
#======================================================================
|
|
9 |
|
|
10 |
# edit RDI PD0 file, e.g. to replace pitch/roll/heading with external values
|
|
11 |
|
31
|
12 |
# NOTES:
|
|
13 |
#
|
|
14 |
# - editing instructions can be provided either in an editing file (primarily
|
|
15 |
# for ensemble-specific editing), or with the -x option on the command line
|
|
16 |
# (only or editing applied to all ensembles)
|
|
17 |
#
|
|
18 |
# - Data-Editing Library:
|
|
19 |
# p(<pitch>) set pitch value (RDI not gimbal pitch) of current ensemble
|
|
20 |
# r(<roll>) set roll alue value of current ensemble
|
|
21 |
# h(<heading>) set heading alue value of current ensemble
|
|
22 |
#
|
|
23 |
# swap_beams(<b1>,<b2>) swap data from beams b1 and b2
|
|
24 |
# input in beam coords required
|
|
25 |
# beam rotation is equivalent to 3 consecutive beam swaps
|
32
|
26 |
# basic BT data are swapped as well (not RL and not SIGNAL_STRENGTH)
|
31
|
27 |
#
|
|
28 |
# earth2beam() transform beam to earth coordinates
|
|
29 |
# does not handle bin-remapping
|
|
30 |
# input in beam coords required
|
|
31 |
#
|
34
|
32 |
# instrument2beam() transform instrument to earth coordinates
|
|
33 |
# does not handle bin-remapping
|
|
34 |
# input in instrument coords required
|
|
35 |
#
|
32
|
36 |
# ensure_UL() overwrite transducer-orientation flag in data file
|
|
37 |
# ensure_DL()
|
|
38 |
#
|
31
|
39 |
# - -x notes:
|
|
40 |
# - multiple perl expressions can be combined with ,
|
|
41 |
#
|
|
42 |
# - Edit File Syntax:
|
|
43 |
# - # comments ignored
|
|
44 |
# - empty lines ignored
|
|
45 |
# - [space] <ensemble-number|*> <space> <perl-expr>
|
|
46 |
# - Examples:
|
|
47 |
# 162 p(3), r(4), h(3.14)
|
|
48 |
|
14
|
49 |
# HISTORY:
|
31
|
50 |
# Nov 25, 2013: - created
|
|
51 |
# Dec 18, 2015: - added switch_beams()
|
|
52 |
# - added -x
|
|
53 |
# Jan 9, 2016: - renamed switch_beams() to swap_beams()
|
|
54 |
# - wrote documentation
|
|
55 |
# - change output data-source ID from 0x7F to 0xE0
|
|
56 |
# - updated getopts to current perl version
|
|
57 |
# - adapted to [ADCP_tools_lib.pl]
|
32
|
58 |
# Feb 15, 2016: - added ensure_UL() ensure_DL()
|
|
59 |
# Feb 23, 2016: - added -k
|
|
60 |
# Feb 26, 2016: - added basic BT data to swap_beams()
|
34
|
61 |
# - added earth2beam()
|
|
62 |
# Apr 12, 2016: - added instrument2beam()
|
14
|
63 |
|
31
|
64 |
use Getopt::Std;
|
|
65 |
|
|
66 |
($ADCP_TOOLS) = ($0 =~ m{(.*/)[^/]+});
|
|
67 |
$ADCP_tools_minVersion = 1.4;
|
|
68 |
require "$ADCP_TOOLS/ADCP_tools_lib.pl";
|
14
|
69 |
|
|
70 |
$USAGE = "$0 @ARGV";
|
|
71 |
die("Usage: $0 " .
|
31
|
72 |
'-e) <edit-file> | -x) <expr> ' .
|
32
|
73 |
'-k)eep original data-source id' .
|
31
|
74 |
"<input file> <output file>\n")
|
32
|
75 |
unless (&getopts('ke:x:') && @ARGV == 2);
|
14
|
76 |
|
28
|
77 |
die("$0: -e <edit-file> or -x <expr> required\n")
|
31
|
78 |
unless (defined($opt_x) || -r $opt_e);
|
14
|
79 |
|
31
|
80 |
print(STDERR "Reading $ARGV[0]..."); # read data
|
14
|
81 |
readData($ARGV[0],\%dta);
|
|
82 |
print(STDERR "done\n");
|
|
83 |
|
|
84 |
#----------------------------------------------------------------------
|
|
85 |
|
31
|
86 |
print(STDERR "Editing Data...");
|
14
|
87 |
|
|
88 |
#--------------------------------------------------
|
31
|
89 |
# Data Editing Library
|
14
|
90 |
#--------------------------------------------------
|
|
91 |
|
32
|
92 |
#
|
|
93 |
# override pitch/roll/heading
|
|
94 |
#
|
31
|
95 |
sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; return 1; }
|
|
96 |
sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; return 1; }
|
|
97 |
sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; return 1;}
|
14
|
98 |
|
32
|
99 |
#
|
|
100 |
# override transducer orientation
|
|
101 |
#
|
|
102 |
sub ensure_DL()
|
|
103 |
{
|
|
104 |
$dta{ENSEMBLE}[$e]->{XDUCER_FACING_DOWN} = 1;
|
|
105 |
$dta{ENSEMBLE}[$e]->{XDUCER_FACING_UP} = undef;
|
|
106 |
return 1;
|
|
107 |
}
|
|
108 |
sub ensure_UL()
|
|
109 |
{
|
|
110 |
$dta{ENSEMBLE}[$e]->{XDUCER_FACING_DOWN} = undef;
|
|
111 |
$dta{ENSEMBLE}[$e]->{XDUCER_FACING_UP} = 1;
|
|
112 |
return 1;
|
|
113 |
}
|
|
114 |
|
|
115 |
#
|
|
116 |
# swap data from two mis-connected beams
|
|
117 |
#
|
31
|
118 |
sub swap_beams($$)
|
28
|
119 |
{
|
|
120 |
my($b1,$b2) = @_;
|
32
|
121 |
my($tmp);
|
28
|
122 |
|
31
|
123 |
# print(STDERR "\n entering swap_beams($b1,$b2) for ens = $e...");
|
|
124 |
|
|
125 |
die("$ARGV[0]: beam-coordinate data required\n")
|
|
126 |
unless ($dta{BEAM_COORDINATES});
|
28
|
127 |
|
32
|
128 |
if ($dta{BT_PRESENT}) {
|
|
129 |
$tmp = $dta{ENSEMBLE}[$e]->{BT_RANGE}[$b1-1];
|
|
130 |
$dta{ENSEMBLE}[$e]->{BT_RANGE}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_RANGE}[$b2-1];
|
|
131 |
$dta{ENSEMBLE}[$e]->{BT_RANGE}[$b2-1] = $tmp;
|
|
132 |
|
|
133 |
$tmp = $dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b1-1];
|
|
134 |
$dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b2-1];
|
|
135 |
$dta{ENSEMBLE}[$e]->{BT_VELOCITY}[$b2-1] = $tmp;
|
|
136 |
|
|
137 |
$tmp = $dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b1-1];
|
|
138 |
$dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b2-1];
|
|
139 |
$dta{ENSEMBLE}[$e]->{BT_CORRELATION}[$b2-1] = $tmp;
|
|
140 |
|
|
141 |
$tmp = $dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b1-1];
|
|
142 |
$dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b2-1];
|
|
143 |
$dta{ENSEMBLE}[$e]->{BT_EVAL_AMPLITUDE}[$b2-1] = $tmp;
|
|
144 |
|
|
145 |
$tmp = $dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b1-1];
|
|
146 |
$dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b1-1] = $dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b2-1];
|
|
147 |
$dta{ENSEMBLE}[$e]->{BT_PERCENT_GOOD}[$b2-1] = $tmp;
|
|
148 |
}
|
|
149 |
|
28
|
150 |
for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
|
32
|
151 |
$tmp = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1];
|
28
|
152 |
$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1];
|
|
153 |
$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1] = $tmp;
|
|
154 |
|
|
155 |
$tmp = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1];
|
|
156 |
$dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b2-1];
|
|
157 |
$dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b2-1] = $tmp;
|
|
158 |
|
|
159 |
$tmp = $dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b1-1];
|
|
160 |
$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b2-1];
|
|
161 |
$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b2-1] = $tmp;
|
|
162 |
|
|
163 |
$tmp = $dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b1-1];
|
|
164 |
$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b2-1];
|
|
165 |
$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b2-1] = $tmp;
|
|
166 |
}
|
31
|
167 |
return 1;
|
|
168 |
}
|
28
|
169 |
|
31
|
170 |
|
32
|
171 |
#
|
|
172 |
# transform earth to beam coordinates
|
|
173 |
#
|
31
|
174 |
{ my($checked);
|
|
175 |
|
|
176 |
sub earth2beam()
|
|
177 |
{
|
|
178 |
unless ($checked) {
|
|
179 |
die("$ARGV[0]: earth-coordinate data required\n")
|
|
180 |
unless ($dta{EARTH_COORDINATES});
|
|
181 |
$dta{BEAM_COORDINATES} = 1; undef($dta{EARTH_COORDINATES});
|
|
182 |
$checked = 1;
|
|
183 |
}
|
|
184 |
|
|
185 |
for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
|
|
186 |
@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
|
|
187 |
velEarthToBeam(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
|
|
188 |
}
|
|
189 |
|
|
190 |
return 1;
|
|
191 |
}
|
|
192 |
|
28
|
193 |
}
|
|
194 |
|
34
|
195 |
#
|
|
196 |
# transform instrument to beam coordinates
|
|
197 |
#
|
|
198 |
{ my($checked);
|
|
199 |
|
|
200 |
sub instrument2beam()
|
|
201 |
{
|
|
202 |
unless ($checked) {
|
|
203 |
die("$ARGV[0]: instrument-coordinate data required\n")
|
|
204 |
unless ($dta{INSTRUMENT_COORDINATES});
|
|
205 |
$dta{BEAM_COORDINATES} = 1; undef($dta{INSTRUMENT_COORDINATES});
|
|
206 |
$checked = 1;
|
|
207 |
}
|
|
208 |
|
|
209 |
for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
|
|
210 |
@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
|
|
211 |
velInstrumentToBeam(\%dta,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
|
|
212 |
}
|
|
213 |
|
|
214 |
return 1;
|
|
215 |
}
|
|
216 |
|
|
217 |
}
|
|
218 |
|
|
219 |
#
|
|
220 |
# transform instrument to earth coordinates
|
|
221 |
#
|
|
222 |
{ my($checked);
|
|
223 |
|
|
224 |
sub instrument2earth()
|
|
225 |
{
|
|
226 |
unless ($checked) {
|
|
227 |
die("$ARGV[0]: instrument-coordinate data required\n")
|
|
228 |
unless ($dta{INSTRUMENT_COORDINATES});
|
|
229 |
$dta{BEAM_COORDINATES} = 1; undef($dta{INSTRUMENT_COORDINATES});
|
|
230 |
$checked = 1;
|
|
231 |
}
|
|
232 |
|
|
233 |
for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
|
|
234 |
@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
|
|
235 |
velInstrumentToEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
|
|
236 |
}
|
|
237 |
|
|
238 |
return 1;
|
|
239 |
}
|
|
240 |
|
|
241 |
}
|
|
242 |
|
14
|
243 |
#--------------------------------------------------
|
|
244 |
# Main Routine
|
|
245 |
#--------------------------------------------------
|
|
246 |
|
31
|
247 |
if (defined($opt_x)) { # edit instructions on the command line
|
28
|
248 |
push(@EE,'*');
|
|
249 |
my($id) = ($opt_x =~ m/^([A-Z]+)\s/); # e.g. PITCH, ROLL, HEADING
|
|
250 |
$opt_x = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
|
14
|
251 |
if defined($id);
|
28
|
252 |
push(@EX,$opt_x);
|
|
253 |
}
|
14
|
254 |
|
31
|
255 |
if (defined($opt_e)) { # edit instructions in edit file
|
28
|
256 |
open(EF,$opt_e) || die("$opt_e: $!\n");
|
|
257 |
while (<EF>) {
|
|
258 |
s/\#.*//;
|
|
259 |
next if m/^\s+$/;
|
|
260 |
my($ens,$expr) = m/^\s*(\*|\d+)\s+(.*)$/;
|
|
261 |
|
|
262 |
my($id) = ($expr =~ m/^([A-Z]+)\s/); # e.g. PITCH, ROLL, HEADING
|
|
263 |
$expr = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
|
|
264 |
if defined($id);
|
|
265 |
|
|
266 |
push(@EE,$ens);
|
|
267 |
push(@EX,$expr);
|
|
268 |
}
|
|
269 |
close(EF);
|
|
270 |
}
|
|
271 |
|
|
272 |
for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) { # local() needed for p(), r(), h()
|
32
|
273 |
$dta{ENSEMBLE}[$e]->{DATA_SOURCE_ID} = 0xE0 # mark all ensembles except first
|
|
274 |
unless ($opt_k || $e==0);
|
28
|
275 |
if ($EE[$eei] eq '*' || $EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) { # match => edit
|
14
|
276 |
eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
|
28
|
277 |
} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) { # next edit later in file => skip
|
14
|
278 |
next;
|
28
|
279 |
} else { # need next edit
|
14
|
280 |
$eei++;
|
|
281 |
last if ($eei >= @EE);
|
|
282 |
redo;
|
|
283 |
}
|
|
284 |
}
|
|
285 |
|
|
286 |
print(STDERR "done\n");
|
|
287 |
|
|
288 |
#----------------------------------------------------------------------
|
|
289 |
|
|
290 |
print(STDERR "Writing $ARGV[1]..."); # write data
|
|
291 |
writeData($ARGV[1],\%dta);
|
|
292 |
print(STDERR "done\n");
|
|
293 |
|
|
294 |
exit(0);
|