14
|
1 |
#!/usr/bin/perl
|
|
2 |
#======================================================================
|
|
3 |
# E D I T P D 0
|
|
4 |
# doc: Mon Nov 25 20:24:31 2013
|
31
|
5 |
# dlm: Sat Jan 9 20:07:59 2016
|
14
|
6 |
# (c) 2013 A.M. Thurnherr
|
31
|
7 |
# uE-Info: 128 0 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
|
|
26 |
#
|
|
27 |
# earth2beam() transform beam to earth coordinates
|
|
28 |
# does not handle bin-remapping
|
|
29 |
# input in beam coords required
|
|
30 |
#
|
|
31 |
# - -x notes:
|
|
32 |
# - multiple perl expressions can be combined with ,
|
|
33 |
#
|
|
34 |
# - Edit File Syntax:
|
|
35 |
# - # comments ignored
|
|
36 |
# - empty lines ignored
|
|
37 |
# - [space] <ensemble-number|*> <space> <perl-expr>
|
|
38 |
# - Examples:
|
|
39 |
# 162 p(3), r(4), h(3.14)
|
|
40 |
|
14
|
41 |
# HISTORY:
|
31
|
42 |
# Nov 25, 2013: - created
|
|
43 |
# Dec 18, 2015: - added switch_beams()
|
|
44 |
# - added -x
|
|
45 |
# Jan 9, 2016: - renamed switch_beams() to swap_beams()
|
|
46 |
# - wrote documentation
|
|
47 |
# - change output data-source ID from 0x7F to 0xE0
|
|
48 |
# - updated getopts to current perl version
|
|
49 |
# - adapted to [ADCP_tools_lib.pl]
|
14
|
50 |
|
31
|
51 |
use Getopt::Std;
|
|
52 |
|
|
53 |
($ADCP_TOOLS) = ($0 =~ m{(.*/)[^/]+});
|
|
54 |
$ADCP_tools_minVersion = 1.4;
|
|
55 |
require "$ADCP_TOOLS/ADCP_tools_lib.pl";
|
14
|
56 |
|
|
57 |
$USAGE = "$0 @ARGV";
|
|
58 |
die("Usage: $0 " .
|
31
|
59 |
'-e) <edit-file> | -x) <expr> ' .
|
|
60 |
"<input file> <output file>\n")
|
|
61 |
unless (&getopts('e:x:') && @ARGV == 2);
|
14
|
62 |
|
28
|
63 |
die("$0: -e <edit-file> or -x <expr> required\n")
|
31
|
64 |
unless (defined($opt_x) || -r $opt_e);
|
14
|
65 |
|
31
|
66 |
print(STDERR "Reading $ARGV[0]..."); # read data
|
14
|
67 |
readData($ARGV[0],\%dta);
|
|
68 |
print(STDERR "done\n");
|
|
69 |
|
|
70 |
#----------------------------------------------------------------------
|
|
71 |
|
31
|
72 |
print(STDERR "Editing Data...");
|
14
|
73 |
|
|
74 |
#--------------------------------------------------
|
31
|
75 |
# Data Editing Library
|
14
|
76 |
#--------------------------------------------------
|
|
77 |
|
31
|
78 |
sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; return 1; }
|
|
79 |
sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; return 1; }
|
|
80 |
sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; return 1;}
|
14
|
81 |
|
31
|
82 |
sub swap_beams($$)
|
28
|
83 |
{
|
|
84 |
my($b1,$b2) = @_;
|
|
85 |
|
31
|
86 |
# print(STDERR "\n entering swap_beams($b1,$b2) for ens = $e...");
|
|
87 |
|
|
88 |
die("$ARGV[0]: beam-coordinate data required\n")
|
|
89 |
unless ($dta{BEAM_COORDINATES});
|
28
|
90 |
|
|
91 |
for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
|
|
92 |
my($tmp) = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1];
|
|
93 |
$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1];
|
|
94 |
$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$b2-1] = $tmp;
|
|
95 |
|
|
96 |
$tmp = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1];
|
|
97 |
$dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b2-1];
|
|
98 |
$dta{ENSEMBLE}[$e]->{CORRELATION}[$bin][$b2-1] = $tmp;
|
|
99 |
|
|
100 |
$tmp = $dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b1-1];
|
|
101 |
$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b2-1];
|
|
102 |
$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$bin][$b2-1] = $tmp;
|
|
103 |
|
|
104 |
$tmp = $dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b1-1];
|
|
105 |
$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b1-1] = $dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b2-1];
|
|
106 |
$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$bin][$b2-1] = $tmp;
|
|
107 |
}
|
31
|
108 |
return 1;
|
|
109 |
}
|
28
|
110 |
|
31
|
111 |
|
|
112 |
{ my($checked);
|
|
113 |
|
|
114 |
sub earth2beam()
|
|
115 |
{
|
|
116 |
unless ($checked) {
|
|
117 |
die("$ARGV[0]: earth-coordinate data required\n")
|
|
118 |
unless ($dta{EARTH_COORDINATES});
|
|
119 |
$dta{BEAM_COORDINATES} = 1; undef($dta{EARTH_COORDINATES});
|
|
120 |
$checked = 1;
|
|
121 |
}
|
|
122 |
|
|
123 |
for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
|
|
124 |
@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
|
|
125 |
velEarthToBeam(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
|
|
126 |
}
|
|
127 |
|
|
128 |
return 1;
|
|
129 |
}
|
|
130 |
|
28
|
131 |
}
|
|
132 |
|
14
|
133 |
#--------------------------------------------------
|
|
134 |
# Main Routine
|
|
135 |
#--------------------------------------------------
|
|
136 |
|
31
|
137 |
if (defined($opt_x)) { # edit instructions on the command line
|
28
|
138 |
push(@EE,'*');
|
|
139 |
my($id) = ($opt_x =~ m/^([A-Z]+)\s/); # e.g. PITCH, ROLL, HEADING
|
|
140 |
$opt_x = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
|
14
|
141 |
if defined($id);
|
28
|
142 |
push(@EX,$opt_x);
|
|
143 |
}
|
14
|
144 |
|
31
|
145 |
if (defined($opt_e)) { # edit instructions in edit file
|
28
|
146 |
open(EF,$opt_e) || die("$opt_e: $!\n");
|
|
147 |
while (<EF>) {
|
|
148 |
s/\#.*//;
|
|
149 |
next if m/^\s+$/;
|
|
150 |
my($ens,$expr) = m/^\s*(\*|\d+)\s+(.*)$/;
|
|
151 |
|
|
152 |
my($id) = ($expr =~ m/^([A-Z]+)\s/); # e.g. PITCH, ROLL, HEADING
|
|
153 |
$expr = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
|
|
154 |
if defined($id);
|
|
155 |
|
|
156 |
push(@EE,$ens);
|
|
157 |
push(@EX,$expr);
|
|
158 |
}
|
|
159 |
close(EF);
|
|
160 |
}
|
|
161 |
|
|
162 |
for (local($e)=my($eei)=0; $e<@{$dta{ENSEMBLE}}; $e++) { # local() needed for p(), r(), h()
|
31
|
163 |
$dta{ENSEMBLE}[$e]->{DATA_SOURCE_ID} = 0xE0; # mark all ensembles
|
28
|
164 |
if ($EE[$eei] eq '*' || $EE[$eei] == $dta{ENSEMBLE}[$e]->{NUMBER}) { # match => edit
|
14
|
165 |
eval($EX[$eei]) || die("$@ while executing <$EX[$eei]>\n");
|
28
|
166 |
} elsif ($EE[$eei] > $dta{ENSEMBLE}[$e]->{NUMBER}) { # next edit later in file => skip
|
14
|
167 |
next;
|
28
|
168 |
} else { # need next edit
|
14
|
169 |
$eei++;
|
|
170 |
last if ($eei >= @EE);
|
|
171 |
redo;
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
print(STDERR "done\n");
|
|
176 |
|
|
177 |
#----------------------------------------------------------------------
|
|
178 |
|
|
179 |
print(STDERR "Writing $ARGV[1]..."); # write data
|
|
180 |
writeData($ARGV[1],\%dta);
|
|
181 |
print(STDERR "done\n");
|
|
182 |
|
|
183 |
exit(0);
|