--- a/ADCP_tools_lib.pl
+++ b/ADCP_tools_lib.pl
@@ -1,9 +1,9 @@
#======================================================================
# A D C P _ T O O L S _ L I B . P L
# doc: Tue Jan 5 10:45:47 2016
-# dlm: Sun Mar 12 12:09:33 2017
+# dlm: Mon Nov 20 10:21:30 2017
# (c) 2016 A.M. Thurnherr
-# uE-Info: 13 51 NIL 0 0 72 0 2 4 NIL ofnI
+# uE-Info: 14 57 NIL 0 0 72 0 2 4 NIL ofnI
#======================================================================
# HISTORY:
@@ -11,8 +11,9 @@
# ...
# Aug 23, 2016: - updated to V1.8
# Mar 12, 2017: - updated to V1.9 for LADCP_w 1.3
+# Nov 20, 2017: - updated to V2.0 to include [patchPD0]
-$ADCP_tools_version = 1.9;
+$ADCP_tools_version = 2.0;
die(sprintf("$0: obsolete ADCP_tools V%.1f; V%.1f required\n",
$ADCP_tools_version,$ADCP_tools_minVersion))
--- a/HISTORY
+++ b/HISTORY
@@ -1,9 +1,9 @@
======================================================================
H I S T O R Y
doc: Tue May 15 18:04:39 2012
- dlm: Thu Mar 16 11:51:19 2017
+ dlm: Mon Nov 20 10:21:00 2017
(c) 2012 A.M. Thurnherr
- uE-Info: 174 15 NIL 0 0 72 3 2 4 NIL ofnI
+ uE-Info: 190 21 NIL 0 0 72 3 2 4 NIL ofnI
======================================================================
--------------------------------------
@@ -149,6 +149,10 @@
- minor bug in [RDI_PD0_IO.pl]
- improvements to [splitPD0]
+#--------------------------------
+# V1.8 (improved support for IMP)
+#--------------------------------
+
Aug 23, 2016:
- added &clearEns to [RDI_PD0_IO.pl]
- updated to V1.8
@@ -166,9 +170,21 @@
Mar 7, 2017:
- namespace clash in [RDI_PD0_IO.pl]
+#-------------------------------------
+# V1.9 (published version for LADCP_w)
+#-------------------------------------
+
Mar 12, 2017:
- updated HISTORY
- updated to V1.9 [ADCP_tools_lib.pl]
Mar 15, 2017:
- published
+
+#--------------------------
+# V2.0 (for DT KVH project)
+---------------------------
+
+Nov 20, 2017:
+ - added patchPD0 to these tools
+ - updated to V2.0 [ADCP_tools_lib.pl]
--- a/INDEX
+++ b/INDEX
@@ -1,9 +1,9 @@
#======================================================================
# I N D E X
# doc: Thu Feb 7 14:21:21 2008
-# dlm: Wed May 25 12:17:43 2016
+# dlm: Mon Nov 20 10:15:08 2017
# (c) 2008 A.M. Thurnherr
-# uE-Info: 15 33 NIL 0 0 72 0 2 4 NIL ofnI
+# uE-Info: 17 77 NIL 0 0 72 0 2 4 NIL ofnI
#======================================================================
=General Utilities=
@@ -14,6 +14,7 @@
[listBT] list bottom-track data
[listVels] list water-track data
[listW] list vertical velocities; BROKEN
+[patchPD0] patch PD0 file with external attitude data (pitch, roll, heading)
[scanBins] list per-bin stats; BROKEN
[splitPD0] split PD0 file using list of ensemble numbers
--- a/RDI_PD0_IO.pl
+++ b/RDI_PD0_IO.pl
@@ -1,9 +1,9 @@
#======================================================================
-# R D I _ B B _ R E A D . P L
+# R D I _ P D 0 _ I O . P L
# doc: Sat Jan 18 14:54:43 2003
-# dlm: Tue Aug 8 16:19:45 2017
+# dlm: Wed Nov 22 11:03:59 2017
# (c) 2003 A.M. Thurnherr
-# uE-Info: 562 21 NIL 0 0 72 0 2 4 NIL ofnI
+# uE-Info: 156 40 NIL 0 0 72 0 2 4 NIL ofnI
#======================================================================
# Read RDI PD0 binary data files (*.[0-9][0-9][0-9])
@@ -91,6 +91,7 @@
# - added SPEED_OF_SOUND to header
# Aug 8, 2017: - replaced croak() by die()
# - added actual transducer frequencies
+# Nov 22, 2017: - BUG: dayNo() and monthLength() clashed with [libconv.pl]
# FIRMWARE VERSIONS:
# It appears that different firmware versions generate different file
@@ -150,6 +151,9 @@
# 0x00008180 LDEO downlooker (master) during NBP0402 (Firmware 16.21)
# According to the manual (January 2001 version) this would, for example,
# indicate power failures on both FSU and LDEO slave instruments...
+# - defining the variable "$RDI_PD0_IO::IGNORE_Y2K_CLOCK" before calling &readData()
+# makes the code ignore the Y2K clock and use the old clock instead; this
+# is used for Dan Torres' KVH system
# &readData() returns perl obj (ref to anonymous hash) with the following
# structure:
@@ -293,9 +297,10 @@
#----------------------------------------------------------------------
# Time Conversion Subroutines
+# - prepended with _ to avoid conflicts with [libconv.pl]
#----------------------------------------------------------------------
- sub monthLength($$) # of days in month
+ sub _monthLength($$) # of days in month
{
my($Y,$M) = @_;
@@ -310,7 +315,7 @@
{ my($epoch,$lM,$lD,$lY,$ldn); # static scope
- sub dayNo($$$$$$)
+ sub _dayNo($$$$$$)
{
my($Y,$M,$D,$h,$m,$s) = @_;
my($dn);
@@ -322,12 +327,12 @@
$lY = $Y; $lM = $M; $lD = $D; # store
for ($dn=0,my($cY)=$epoch; $cY<$Y; $cY++) { # multiple years
- $dn += 337 + &monthLength($Y,$M);
+ $dn += 337 + &_monthLength($Y,$M);
}
$dn += $D; # day in month
while (--$M > 0) { # preceding months
- $dn += &monthLength($Y,$M);
+ $dn += &_monthLength($Y,$M);
}
$ldn = $dn; # store
@@ -774,16 +779,16 @@
die(sprintf($FmtErr,$WBRcfn,"Variable Leader",$id,$ensNo + ($lastEns - ($lastEns & 0xFFFF))));
}
- if ($fixed_leader_bytes==42 || $fixed_leader_bytes==58) { # BB150 & Explorer DVL
+# if ($fixed_leader_bytes==42 || $fixed_leader_bytes==58) { # BB150 & Explorer DVL
sysread(WBRF,$buf,7) == 7 || die("$WBRcfn: $!");
(${$E}[$ens]->{YEAR},${$E}[$ens]->{MONTH},
${$E}[$ens]->{DAY},${$E}[$ens]->{HOUR},${$E}[$ens]->{MINUTE},
${$E}[$ens]->{SECONDS},$B4) = unpack('CCCCCCC',$buf);
${$E}[$ens]->{SECONDS} += $B4/100;
${$E}[$ens]->{YEAR} += (${$E}[$ens]->{YEAR} > 80) ? 1900 : 2000;
- } else {
- sysseek(WBRF,7,1) || die("$WBRcfn: $!"); # use Y2K RTC instead
- }
+# } else {
+# sysseek(WBRF,7,1) || die("$WBRcfn: $!"); # use Y2K RTC instead
+# }
sysread(WBRF,$buf,1) == 1 || die("$WBRcfn: $!");
$ensNo += unpack('C',$buf) << 16;
@@ -829,7 +834,8 @@
${$E}[$ens]->{PITCH_STDDEV} /= 10;
${$E}[$ens]->{ROLL_STDDEV} /= 10;
- if ($fixed_leader_bytes==53 || $fixed_leader_bytes==59) { # Workhorse instruments
+ if (($fixed_leader_bytes==53 || $fixed_leader_bytes==59) && # Workhorse instruments
+ !defined($RDI_PD0_IO::IGNORE_Y2K_CLOCK)) {
sysread(WBRF,$buf,23) == 23 || die("$WBRcfn: $!");
(${$E}[$ens]->{ERROR_STATUS_WORD},
$dummy,${$E}[$ens]->{PRESSURE},${$E}[$ens]->{PRESSURE_STDDEV},
@@ -866,8 +872,8 @@
${$E}[$ens]->{MINUTE},
${$E}[$ens]->{SECONDS});
${$E}[$ens]->{DAYNO}
- = &dayNo(${$E}[$ens]->{YEAR},${$E}[$ens]->{MONTH},${$E}[$ens]->{DAY},
- ${$E}[$ens]->{HOUR},${$E}[$ens]->{MINUTE},${$E}[$ens]->{SECONDS});
+ = &_dayNo(${$E}[$ens]->{YEAR},${$E}[$ens]->{MONTH},${$E}[$ens]->{DAY},
+ ${$E}[$ens]->{HOUR},${$E}[$ens]->{MINUTE},${$E}[$ens]->{SECONDS});
# when analyzing an STA file from an OS75 SADCP (Poseidion),
# I noticed that there is no time information. This causes
--- a/editPD0
+++ b/editPD0
@@ -2,9 +2,9 @@
#======================================================================
# E D I T P D 0
# doc: Mon Nov 25 20:24:31 2013
-# dlm: Tue Nov 15 11:05:46 2016
+# dlm: Thu Jul 27 18:30:16 2017
# (c) 2013 A.M. Thurnherr
-# uE-Info: 73 76 NIL 0 0 72 2 2 4 NIL ofnI
+# uE-Info: 122 0 NIL 0 0 72 2 2 4 NIL ofnI
#======================================================================
# edit RDI PD0 file, e.g. to replace pitch/roll/heading with external values
@@ -40,6 +40,9 @@
# ensure_UL() correct data for wrong transducer orientation
# ensure_DL() - sets correct flag & negates roll value
#
+# dealias(<WV lim[m/s]>) correct data for erroneously low WV setting
+# - HEURISTIC, i.e. may not work
+#
# - -x notes:
# - multiple perl expressions can be combined with ,
#
@@ -71,6 +74,7 @@
# - made single-ping ensemble requirement for most routines
# Jul 12, 2016: - updated ensure_{DL,UL} routines
# Nov 15, 2016: - BUG: ensure_{DL,UL} routines did not negate heading data
+# Jul 27, 2017: - began working on dealias()
use Getopt::Std;
@@ -101,14 +105,58 @@
# Data Editing Library
#--------------------------------------------------
-#
+#--------------------------------------------------
# override pitch/roll/heading
-#
+#--------------------------------------------------
+
sub p($) { $dta{ENSEMBLE}[$e]->{PITCH} = $_[0]; return 1; }
sub r($) { $dta{ENSEMBLE}[$e]->{ROLL} = $_[0]; return 1; }
sub h($) { $dta{ENSEMBLE}[$e]->{HEADING} = $_[0]; return 1;}
-#
+#--------------------------------------------------
+# correct data for erroneously low WV limit
+#--------------------------------------------------
+
+{ my(@target); # static scope, undef initially
+
+sub dealias_V0($)
+{
+ my($WV) = @_;
+
+ if (@target) { # dealias everything but first ensemble
+ for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
+ for (my($beam)=0; $beam<4; $beam++) {
+ next unless defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam]);
+ my($dealiased) = 0;
+ $dealiased = ($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] - 2*$WV)
+ if ($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] > 0);
+ $dealiased = ($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] + 2*$WV)
+ if ($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] < 0);
+ if (abs($target[$beam]-$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam]) >
+ abs($target[$beam]-$dealiased)) {
+ $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] = undef; #$dealiased;
+ $dealiased++;
+ }
+ }
+ }
+ } else { # @target is undef
+ $dealiased = 0;
+ }
+
+ @target = (0,0,0,0); # calc ref-lr average target for next ens
+ for (my($bin)=1; $bin<=5; $bin++) { # should work even if N_BINS < 5
+ for (my($beam)=0; $beam<4; $beam++) {
+ $target[$beam] += $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam]/4
+ if defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam]);
+ }
+ }
+
+ return 1;
+}
+
+} # static scope
+
+#--------------------------------------------------
# override transducer orientation
#
# These routines are intended to correct ADCP data for
@@ -125,6 +173,7 @@
# Hdg: Based on the time-series of headings recorded
# during P18 profile 003 the heading needs
# to be negated. Doing so yields a good profile.
+#--------------------------------------------------
sub ensure_DL()
{
@@ -152,9 +201,10 @@
return 1;
}
-#
+#--------------------------------------------------
# swap data from two mis-connected beams
-#
+#--------------------------------------------------
+
sub swap_beams($$)
{
my($b1,$b2) = @_;
@@ -210,9 +260,10 @@
}
-#
+#--------------------------------------------------
# transform earth to beam coordinates
-#
+#--------------------------------------------------
+
{ my($checked);
sub earth2beam()
@@ -242,9 +293,10 @@
}
-#
+#--------------------------------------------------
# transform instrument to beam coordinates
-#
+#--------------------------------------------------
+
{ my($checked);
sub instrument2beam()
@@ -268,9 +320,10 @@
}
-#
+#--------------------------------------------------
# transform instrument to earth coordinates
-#
+#--------------------------------------------------
+
{ my($checked);
sub instrument2earth()
@@ -294,9 +347,10 @@
}
-#
+#--------------------------------------------------
# transform beam to earth coordinates
-#
+#--------------------------------------------------
+
{ my($checked);
sub beam2earth()
@@ -345,7 +399,7 @@
next if m/^\s+$/;
my($ens,$expr) = m/^\s*(\*|\d+)\s+(.*)$/;
- my($id) = ($expr =~ m/^([A-Z]+)\s/); # e.g. PITCH, ROLL, HEADING
+ my($id) = ($expr =~ m/^([A-Z]+)\s/); # e.g. PITCH, ROLL, HEADING
$expr = sprintf('$dta{ENSEMBLE}[$e]->{%s}',$id)
if defined($id);
--- a/listBins
+++ b/listBins
@@ -2,9 +2,9 @@
#======================================================================
# L I S T B I N S
# doc: Fri Aug 25 15:57:05 2006
-# dlm: Fri Jan 27 17:41:10 2017
+# dlm: Mon Jun 19 03:49:12 2017
# (c) 2006 A.M. Thurnherr
-# uE-Info: 68 81 NIL 0 0 72 2 2 4 NIL ofnI
+# uE-Info: 168 1 NIL 0 0 72 0 2 4 NIL ofnI
#======================================================================
# Split data file into per-bin time series.
@@ -164,8 +164,8 @@
print(P "$k\{$P{$k}\} ");
}
my($pct3b) = 100*$three_beam[$b]/(($le-$fe+1) * $dta{PINGS_PER_ENSEMBLE});
- die("assertion failed: gv[$b] = $good_vels[$b]; 3b = $three_beam[$b]; ens=$fe..$le; ")
- if ($pct3b > 100);
+# die("assertion failed ($pct3b > 100): gv[$b] = $good_vels[$b]; 3b = $three_beam[$b]; ens=$fe..$le; ")
+# if ($pct3b > 100);
printf(STDERR "%02d:%.0f%%/%.0f%% ",$b+1,100*$good_vels[$b]/($le-$fe+1),$pct3b);
printf(P "pct_3_beam{%.0f} ",$pct3b);
new file mode 100755
--- /dev/null
+++ b/patchPD0
@@ -0,0 +1,158 @@
+#!/usr/bin/perl
+#======================================================================
+# P A T C H P D 0
+# doc: Tue Aug 23 20:00:15 2016
+# dlm: Mon Nov 20 12:27:15 2017
+# (c) 2010 A.M. Thurnherr
+# uE-Info: 32 16 NIL 0 0 72 0 2 4 NIL ofnI
+#======================================================================
+
+$antsSummary = 'patch TRDI PD0 file with external attitude data';
+
+# History:
+# Aug 23, 2016: - exported from IMP+LADCP
+# Aug 25, 2016: - completed basic structure
+# Nov 20, 2017: - major code cleanup
+# - added -d) to keep original data source id
+
+# PATCH-FILE REQUIREMENTS (ANTS format)
+# - %LADCP_pitch.mu %LADCP_roll.mu mean LADCP pitch and roll
+# - %IMU_hdg_offset heading offset of external IMU
+# - LADCP_ens ADCP ensemble number
+# - pitch, roll external pitch/roll *anomalies*
+# - hdg external heading rotated into ADCP coord system
+
+# PATCHED PD0 FILE:
+#
+# - pitch = RDI_pitch(mean_LADCP_gimbal_pitch + rotated_external_pitch_anomaly)
+# - roll = mean_LADCP_roll + rotated_external_roll_anomaly
+# - hdg = external_hdg - heading_offset
+#
+# - unless -d is used, every patched ensemble has set the DATA_SOURCE_ID as follows;
+# PROCESSING SOFTWARE NEEDS TO BE ABLE TO DEAL WITH DSID values != 0x7F
+# 0xA0 no values patched
+# 0xA1 heading patched
+# 0xA2 roll patched
+# 0xA3 roll & heading patched
+# 0xA4 pitch patched
+# 0xA5 pitch & heading patched
+# 0xA6 pitch & roll patched
+# 0xA7 pitch, roll & heading patched
+#
+# - additionally, all velocities from ensembles with missing pitch/roll/heading
+# values are removed unless -k is set
+
+($ANTS) = (`which ANTSlib` =~ m{^(.*)/[^/]*$});
+($ADCP_TOOLS) = ($0 =~ m{^(.*)/[^/]*$});
+
+$antsMinLibVersion = 6.7;
+$ADCP_tools_minVersion = 1.8;
+
+require "$ANTS/ants.pl";
+require "$ANTS/libvec.pl";
+require "$ANTS/libstats.pl";
+require "$ADCP_TOOLS/ADCP_tools_lib.pl";
+
+$antsParseHeader = 0;
+&antsUsage('dhko:pr',2,
+ '[patch -p)itch] [-r)oll] [-h)eading] (none patches all)',
+ '[-o) <heading-offset>] [-k)eep velocities of unpatched ensembles]',
+ '[keep original -d)ata-source id]',
+ '<original PD0 file> <patched PD0 file> [external attitude file]');
+
+$opt_p = $opt_r = $opt_h = 1
+ unless ($opt_p || $opt_r || $opt_h);
+
+$LADCP_file = &antsFileArg();
+$outPD0 = $ARGV[0]; shift;
+
+#----------------------------------------------------------------------
+# Step 1: Read LADCP Data
+#----------------------------------------------------------------------
+
+readData($LADCP_file,\%LADCP);
+
+#----------------------------------------------------------------------
+# Step 2: Process External Attidue Input to Patch PD0 file
+#----------------------------------------------------------------------
+
+my($pr_missing,$hdg_missing) = (0,0);
+
+&antsIn();
+
+my($ensF) = &fnr('LADCP_ens');
+my($pitchF) = &fnr('pitch');
+my($rollF) = &fnr('roll');
+my($hdgF) = &fnr('hdg');
+my($LADCP_pitch_mean) = &antsRequireParam('LADCP_pitch.mu');
+my($LADCP_roll_mean) = &antsRequireParam('LADCP_roll.mu');
+
+my($rho,$crho,$srho);
+if (defined($opt_o)) {
+ $rho = $opt_o - &antsRequireParam('IMU_hdg_offset');
+ $crho = cos(rad($rho));
+ $srho = sin(rad($rho));
+}
+
+do {
+ my($ens) = $P{RECNO};
+ die("assertion failed [$ants_[0][$ensF] != $LADCP{ENSEMBLE}[$ens]->{NUMBER} --- 1-$LADCP{ENSEMBLE}[0]->{NUMBER} + $P{RECNO} + $d]")
+ unless ($ants_[0][$ensF] == $LADCP{ENSEMBLE}[$ens]->{NUMBER});
+# print(STDERR "assertion succeeded [$ants_[0][$ensF] == $LADCP{ENSEMBLE}[$ens]->{NUMBER} --- 1-$LADCP{ENSEMBLE}[0]->{NUMBER} + $P{RECNO} + $d]\n");
+ $LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} = 0xA0;
+
+ if (numbersp($ants_[0][$pitchF],$ants_[0][$rollF])) {
+ if (defined($opt_o)) {
+ my($rot_p) = ($ants_[$r][$pitchF] * $crho +
+ $ants_[$r][$rollF] * $srho);
+ my($rot_r) = (-$ants_[$r][$pitchF] * $srho +
+ $ants_[$r][$rollF] * $crho);
+ $ants_[$r][$pitchF] = $rot_p;
+ $ants_[$r][$rollF] = $rot_r;
+ }
+ if ($opt_p) {
+ $LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= ($opt_p<<2);
+ $LADCP{ENSEMBLE}[$ens]->{PITCH} = RDI_pitch($LADCP_pitch_mean + $ants_[0][$pitchF],
+ $LADCP_roll_mean + $ants_[0][$rollF]);
+ }
+ if ($opt_r) {
+ $LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= ($opt_r<<1);
+ $LADCP{ENSEMBLE}[$ens]->{ROLL} = $LADCP_roll_mean + $ants_[0][$rollF];
+ }
+ } else {
+ $pr_missing++;
+ unless ($opt_k) {
+ clearEns(\%LADCP,$ens);
+ $LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID}= 0xA0;
+ }
+ }
+
+ if (numberp($ants_[0][$hdgF])) {
+ $LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= $opt_h;
+ if (defined($opt_o)) {
+ $ants_[0][$hdgF] -= $rho;
+ $ants_[0][$hdgF] += 360 if ($ants_[0][$hdgF] < 0);
+ }
+ $LADCP{ENSEMBLE}[$ens]->{HEADING} = $ants_[0][$hdgF]
+ if $opt_h;
+ } else {
+ $hdg_missing++;
+ unless ($opt_k) {
+ clearEns(\%LADCP,$ens);
+ $LADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID}= 0xA0;
+ }
+ }
+} while (&antsIn());
+
+$LADCP{ENSEMBLE}[0]->{DATA_SOURCE_ID} = 0x7F; # ensure correct DSID (1st ens: orig; 2nd ens: this prog)
+$LADCP{ENSEMBLE}[1]->{DATA_SOURCE_ID} = 0xA0
+ unless ($LADCP{ENSEMBLE}[1]->{DATA_SOURCE_ID}&0xF0 == 0xA0);
+
+writeData($outPD0,\%LADCP);
+
+my($verb) = $opt_k ? 'retained' : 'cleared';
+printf(STDERR "$outPD0: %d pitch/roll & %d heading values $verb\n",$pr_missing,$hdg_missing)
+ if ($pr_missing+$hdg_missing);
+
+exit(0);
+