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