V1.6
authorA.M. Thurnherr <athurnherr@yahoo.com>
Wed, 25 May 2016 12:23:02 -0400
changeset 34 3b4bcd55e1ea
parent 33 307630665c6c
child 35 7c394a2d1fc9
V1.6
ADCP_tools_lib.pl
HISTORY
INDEX
RDI2grd
RDI_Coords.pl
RDI_Utils.pl
beamStats
editPD0
listEns
listVels
--- a/ADCP_tools_lib.pl
+++ b/ADCP_tools_lib.pl
@@ -1,15 +1,15 @@
 #======================================================================
 #                    A D C P _ T O O L S _ L I B . P L 
 #                    doc: Tue Jan  5 10:45:47 2016
-#                    dlm: Thu Mar 17 07:46:47 2016
+#                    dlm: Tue Apr 12 21:37:23 2016
 #                    (c) 2016 A.M. Thurnherr
-#                    uE-Info: 12 25 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 12 26 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 # HISTORY:
 #	Jan  5, 2015: - created
 
-$ADCP_tools_version = 1.5;		# Jan  5, 2016
+$ADCP_tools_version = 1.6;
 
 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: Tue Mar 29 15:01:05 2016
+                    dlm: Wed May 25 12:22:14 2016
                     (c) 2012 A.M. Thurnherr
-                    uE-Info: 62 35 NIL 0 0 72 3 2 8 NIL ofnI
+                    uE-Info: 72 20 NIL 0 0 72 3 2 8 NIL ofnI
 ======================================================================
 
 May 15, 2012:
@@ -61,3 +61,18 @@
 Mar 29, 2016:
   - published for LADCP_w V1.2beta6
 
+Apr 12, 2016: V1.6
+  - updated version in [ADCP_tools_lib.pl]
+  - [editPD0]: added instrument2beam()
+
+Apr 19, 2016:
+  - added time/date to -E output [listEns]
+
+Apr 25, 2016:
+  - added [listVels]  
+
+May 19, 2016:
+  - began implemeting bin-interpolation in [RDI_Coords.pl], which requires
+    changes to velBeamToInstrument() arguments
+  - adapted several routines to velBeamToEarth()
+
--- a/INDEX
+++ b/INDEX
@@ -1,9 +1,9 @@
 #======================================================================
 #                    I N D E X 
 #                    doc: Thu Feb  7 14:21:21 2008
-#                    dlm: Sun Sep 27 12:18:28 2015
+#                    dlm: Wed May 25 12:17:43 2016
 #                    (c) 2008 A.M. Thurnherr
-#                    uE-Info: 31 0 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 15 33 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 =General Utilities=
@@ -12,6 +12,7 @@
 [listHdr]	list header info
 [listEns]	list ensembles
 [listBT]	list bottom-track data
+[listVels]	list water-track data
 [listW]		list vertical velocities; BROKEN
 [scanBins]	list per-bin stats; BROKEN
 [splitPD0]	split PD0 file using list of ensemble numbers
--- a/RDI2grd
+++ b/RDI2grd
@@ -2,9 +2,9 @@
 #======================================================================
 #                    R D I 2 G R D 
 #                    doc: Wed Aug 30 11:51:22 2006
-#                    dlm: Thu Mar 17 07:39:24 2016
+#                    dlm: Thu May 19 10:21:25 2016
 #                    (c) 2006 A.M. Thurnherr
-#                    uE-Info: 20 0 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 186 33 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 # make GMT grd files from RDI file
@@ -17,6 +17,7 @@
 #	Sep 19, 2007: - adapted to new [RDI_BB_Read.pl]
 #	Jun 18, 2009: - BUG: xysize had been called xyside
 #   Mar 17, 2016: - adapted to new Getopt library
+#	May 19, 2016: - adapted to velBeamToEarth()
 
 # NOTES:
 #	- regular grids only => no dimensional time axis for data collected
@@ -181,11 +182,9 @@
 					 defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1]) &&
 					 defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2]) &&
 					 defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3]));
-		@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} = $beamCoords
-			? velInstrumentToEarth(\%dta,$e,
-				  velBeamToInstrument(\%dta,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]})
-			  )
-			: velApplyHdgBias(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]});
+		@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} =
+			$beamCoords ? velBeamToEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]})
+					    : velApplyHdgBias(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]});
 		$dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b] = 1;
 		$good_vels[$b]++;
 		$lastGoodBin = $b if ($b > $lastGoodBin);
--- a/RDI_Coords.pl
+++ b/RDI_Coords.pl
@@ -1,9 +1,9 @@
 #======================================================================
 #                    R D I _ C O O R D S . P L 
 #                    doc: Sun Jan 19 17:57:53 2003
-#                    dlm: Mon Feb 29 18:03:31 2016
+#                    dlm: Thu May 19 10:18:44 2016
 #                    (c) 2003 A.M. Thurnherr
-#                    uE-Info: 44 85 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 167 0 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 # RDI Workhorse Coordinate Transformations
@@ -43,6 +43,7 @@
 #	Jan  9, 2016: - added &velEarthToBeam(), &velBeamToEarth()
 #	Feb 29, 2016: - debugged & verified velEarthToInstrument(), velInstrumentToBeam()
 #				  - added velBeamToEarth()
+#	May 19, 2016: - begin implemeting bin interpolation
 
 use strict;
 use POSIX;
@@ -67,7 +68,7 @@
 
 	sub velBeamToInstrument(@)
 	{
-		my($dta,$v1,$v2,$v3,$v4) = @_;
+		my($dta,$ens,$v1,$v2,$v3,$v4) = @_;
 		return undef unless (defined($v1) + defined($v2) +
 					   		 defined($v3) + defined($v4)
 								>= $RDI_Coords::minValidVels);
@@ -163,7 +164,7 @@
 sub velBeamToEarth(@)
 {
 	my($dtaR,$e,@v) = @_;
-	return velInstrumentToEarth($dtaR,$e,velBeamToInstrument($dtaR,@v));
+	return velInstrumentToEarth($dtaR,$e,velBeamToInstrument($dtaR,$e,@v));
 }
 
 #----------------------------------------------------------------------
--- a/RDI_Utils.pl
+++ b/RDI_Utils.pl
@@ -1,9 +1,9 @@
 #======================================================================
 #                    R D I _ U T I L S . P L 
 #                    doc: Wed Feb 12 10:21:32 2003
-#                    dlm: Sat Jan  9 16:36:52 2016
+#                    dlm: Thu May 19 10:23:48 2016
 #                    (c) 2003 A.M. Thurnherr
-#                    uE-Info: 55 41 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 56 62 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 # miscellaneous RDI-specific utilities
@@ -53,6 +53,7 @@
 #	Mar 22, 2015: - BUG: mk_prof could bomb because of division-by-zero in return statement
 #	Jan  9, 2016: - renamed ref_lr_w to mk_prof_ref_lr_w because the old name conflicts
 #				    with a sub in LADCP_w
+#   May 19, 2016: - adapted to new velBeamToInstrument() usage
 
 use strict;
 
@@ -133,11 +134,8 @@
 					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[1]) &&
 					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[2]) &&
 					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[3]));
-		my(@BT) = $beamCoords
-				? velInstrumentToEarth($d,$i,
-					velBeamToInstrument($d,
-						@{$d->{ENSEMBLE}[$i]->{BT_VELOCITY}}))
-				: velApplyHdgBias($d,$i,@{$d->{ENSEMBLE}[$i]->{BT_VELOCITY}});
+		my(@BT) = $beamCoords ? velBeamToEarth($d,$i,@{$d->{ENSEMBLE}[$i]->{BT_VELOCITY}})
+							  : velApplyHdgBias($d,$i,@{$d->{ENSEMBLE}[$i]->{BT_VELOCITY}});
 		next unless (abs($BT[3]) < 0.05);
 		$d->{ENSEMBLE}[$i]->{seabed} =
 			 $d->{ENSEMBLE}[$i]->{BT_RANGE}[0]/4 +
@@ -289,7 +287,7 @@
 				if ($dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][2] < $min_pctg);
 			undef($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][3])
 	            if ($dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][3] < $min_pctg);
-	        @vi = velBeamToInstrument($dta,@{$dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i]});
+	        @vi = velBeamToInstrument($dta,$ens,@{$dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i]});
 			@v = velInstrumentToEarth($dta,$ens,@vi);
 			@vbp = velBeamToBPEarth($dta,$ens,@{$dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i]});
 		} else {
@@ -299,7 +297,6 @@
 					 $dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][3] < $min_pctg);
 			@v = @{$dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i]};
 		}
-###		next if (!defined($v[3]) || abs($v[3]) > $max_e);		# disallow 3-beam solutions
 		next if (defined($v[3]) && abs($v[3]) > $max_e);		# allow 3-beam solutions
 
 		if (defined($v[2])) {									# valid vertical velocity
--- a/beamStats
+++ b/beamStats
@@ -2,9 +2,9 @@
 #======================================================================
 #                    B E A M S T A T S 
 #                    doc: Fri Aug 25 15:57:05 2006
-#                    dlm: Thu Mar 17 07:39:34 2016
+#                    dlm: Thu May 19 10:24:48 2016
 #                    (c) 2006 A.M. Thurnherr
-#                    uE-Info: 35 0 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 254 0 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 # Split data file into per-bin time series.
@@ -32,6 +32,7 @@
 #				  - removed missing magdecl warning on -b
 #	Mar  4, 2014: - added support for missing PITCH/ROLL/HEADING
 #   Mar 17, 2016: - adapted to new Getopt library
+#   May 19, 2016: - adapted to velBeamToEarth()
 
 # General Notes:
 #	- everything (e.g. beams) is numbered from 1
@@ -248,11 +249,9 @@
 				undef($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][$i]);
 			}
         }
-		@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} = $beamCoords
-			? velInstrumentToEarth(\%dta,$e,
-				  velBeamToInstrument(\%dta,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]})
-			  )
-			: velApplyHdgBias(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]})
+		@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} = 
+			$beamCoords ? velBeamToEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]})
+		   			    : velApplyHdgBias(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]})
 				unless ($opt_b);
 		unless (defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0])) {
 			undef(@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]});
--- a/editPD0
+++ b/editPD0
@@ -2,9 +2,9 @@
 #======================================================================
 #                    E D I T P D 0 
 #                    doc: Mon Nov 25 20:24:31 2013
-#                    dlm: Fri Feb 26 17:24:55 2016
+#                    dlm: Tue Apr 12 21:45:31 2016
 #                    (c) 2013 A.M. Thurnherr
-#                    uE-Info: 26 98 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 235 46 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 # edit RDI PD0 file, e.g. to replace pitch/roll/heading with external values
@@ -29,6 +29,10 @@
 #								does not handle bin-remapping
 #								input in beam coords required
 #
+#		instrument2beam()		transform instrument to earth coordinates
+#								does not handle bin-remapping
+#								input in instrument coords required
+#
 #		ensure_UL()				overwrite transducer-orientation flag in data file
 #		ensure_DL()
 #
@@ -54,6 +58,8 @@
 #	Feb 15, 2016: - added ensure_UL() ensure_DL()
 #	Feb 23, 2016: - added -k
 #	Feb 26, 2016: - added basic BT data to swap_beams()
+#				  - added earth2beam()
+#	Apr 12, 2016: - added instrument2beam()
 
 use Getopt::Std;
 
@@ -186,6 +192,54 @@
 
 }
 
+#
+# transform instrument to beam coordinates
+#
+{ my($checked);
+
+	sub instrument2beam()
+	{
+		unless ($checked) {
+			die("$ARGV[0]: instrument-coordinate data required\n")
+				unless ($dta{INSTRUMENT_COORDINATES});
+			$dta{BEAM_COORDINATES} = 1; undef($dta{INSTRUMENT_COORDINATES});
+			$checked = 1;
+		}
+	    
+		for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
+			@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
+				velInstrumentToBeam(\%dta,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
+		}
+	
+		return 1;
+	}
+
+}
+
+#
+# transform instrument to earth coordinates
+#
+{ my($checked);
+
+	sub instrument2earth()
+	{
+		unless ($checked) {
+			die("$ARGV[0]: instrument-coordinate data required\n")
+				unless ($dta{INSTRUMENT_COORDINATES});
+			$dta{BEAM_COORDINATES} = 1; undef($dta{INSTRUMENT_COORDINATES});
+			$checked = 1;
+		}
+	    
+		for (my($bin)=0; $bin<$dta{N_BINS}; $bin++) {
+			@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]} =
+				velInstrumentToEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin]});
+		}
+	
+		return 1;
+	}
+
+}
+
 #--------------------------------------------------
 # Main Routine
 #--------------------------------------------------
--- a/listEns
+++ b/listEns
@@ -2,9 +2,9 @@
 #======================================================================
 #                    L I S T E N S 
 #                    doc: Sat Jan 18 18:41:49 2003
-#                    dlm: Thu Mar 17 07:39:49 2016
+#                    dlm: Tue Apr 19 21:11:12 2016
 #                    (c) 2003 A.M. Thurnherr
-#                    uE-Info: 49 0 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 181 53 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 # Print useful info from the ensemble list or dump ensembles to
@@ -46,6 +46,7 @@
 #				  - added support for missing PITCH/ROLL/HEADING
 #	Sep  8, 2014: - added -B)T  
 #   Mar 17, 2016: - adapted to new Getopt library
+#	Apr 19, 2016: - added %date, %time to -E output
 
 # Notes:
 #	- -E outputs data in earth coordinates, unless -b is set also
@@ -176,6 +177,8 @@
 			open(P,">$file") || die("$file: $!\n");
 			print(P "#!/usr/bin/perl -S list\n");
 			print(P "#ANTS#PARAMS# " .
+					"date{$dta{ENSEMBLE}[$e]->{DATE}} " .
+					"time{$dta{ENSEMBLE}[$e]->{TIME}} " .
 					"BT_u{$dta{ENSEMBLE}[$e]->{BT_VELOCITY}[0]} " .
 					"BT_v{$dta{ENSEMBLE}[$e]->{BT_VELOCITY}[1]} " .
 					"BT_w{$dta{ENSEMBLE}[$e]->{BT_VELOCITY}[2]} " .
new file mode 100755
--- /dev/null
+++ b/listVels
@@ -0,0 +1,248 @@
+#!/usr/bin/perl
+#======================================================================
+#                    L I S T V E L S 
+#                    doc: Mon Apr 25 21:12:54 2016
+#                    dlm: Mon Apr 25 21:48:18 2016
+#                    (c) 2016 A.M. Thurnherr
+#                    uE-Info: 180 0 NIL 0 0 72 10 2 4 NIL ofnI
+#======================================================================
+
+# list water-track velocity samples as ANTS records (PD02ANTS)
+
+# HISTORY:
+#	Apr 25, 2016: - created from [listBins]
+
+# General Notes:
+#	- everything (e.g. beams) is numbered from 1
+#	- no support for BT data
+
+use Getopt::Std;
+
+$ADCP_tools_minVersion = 1.4;
+($ADCP_TOOLS) = ($0 =~ m{(.*/)[^/]+});
+require "$ADCP_TOOLS/ADCP_tools_lib.pl";
+
+die("Usage: $0 [-r)ange <first_ens,last_ens>] [-b)ins <first_bin,last_bin>] " .
+			  "[-M)agnetic <declination>] " .
+			  "[-S)oundspeed correction <salin|*,temp|*,depth|*> " .
+			  "[-P)itch/Roll <bias/bias>] [-B)eamvel <bias/bias/bias/bias>] " .
+		 	  "[require -4)-beam solutions] [-d)iscard <beam#>] " .
+			  "<RDI file>\n")
+	unless (&getopts("4b:B:d:M:r:P:S:") && @ARGV == 1);
+
+($P{pitch_bias},$P{roll_bias}) = split('[,/]',$opt_P);
+($P{velbias_b1},$P{velbias_b2},$P{velbias_b3},$P{velbias_b4}) = split('[,/]',$opt_B);
+
+die("$0: -4 and -d are mutually exclusive\n")
+	if ($opt_4 && defined($opt_d));
+
+$RDI_Coords::minValidVels = 4 if ($opt_4);			# no 3-beam solutions
+
+print(STDERR "WARNING: magnetic declination not set!\n")
+	unless defined($opt_M);
+
+$ifn = $ARGV[0];
+
+($first_ens,$last_ens) = split(',',$opt_r) if defined($opt_r);
+($first_bin,$last_bin) = split(',',$opt_b) if defined($opt_b);
+
+if (defined($opt_S)) {
+	($SS_salin,$SS_temp,$SS_depth) = split(',',$opt_S);
+	$variable_ssCorr = ($SS_salin eq '*' || $SS_temp eq '*' || $SS_depth eq '*');
+} else {
+	print(STDERR "WARNING: no soundspeed correction applied!\n");
+}
+
+#----------------------------------------------------------------------
+# MAIN
+#----------------------------------------------------------------------
+
+$P{RDI_file} = $ifn;
+$P{mag_decl} = $opt_M if defined($opt_M);
+
+readData($ifn,\%dta);
+printf(STDERR "%d complete ensembles...\n",scalar(@{$dta{ENSEMBLE}}));
+$dta{HEADING_BIAS} = -$opt_M;						# magnetic declination
+
+if ($dta{BEAM_COORDINATES}) {						# coords
+	$beamCoords = 1;
+} else {
+	die("$ifn: only beam and earth coordinates supported\n")
+		if (!$dta{EARTH_COORDINATES});
+	die("$ifn: -d requires beam-coordinate data\n")
+		if defined($opt_d);
+	die("$ifn: -B requires beam-coordinate data\n")
+		if defined($opt_B);
+}
+
+$first_bin = 1 				unless ($first_bin > 0);
+$last_bin  = $dta{N_BINS}+1 unless ($last_bin >= $first_bin && $last_bin <= $dta{N_BINS}+1);
+
+$lastGoodBin = 0;
+for ($e=0; $e<=$#{$dta{ENSEMBLE}}; $e++) {				# check/transform velocities
+	next if (defined($first_ens) &&
+			 $dta{ENSEMBLE}[$e]->{NUMBER} < $first_ens);
+
+	$dta{ENSEMBLE}[$e]->{PITCH} -= $P{pitch_bias}
+		if defined($dta{ENSEMBLE}[$e]->{PITCH});
+	$dta{ENSEMBLE}[$e]->{ROLL}  -= $P{roll_bias}
+		if defined($dta{ENSEMBLE}[$e]->{ROLL});
+
+	$P{first_ens} = $dta{ENSEMBLE}[$e]->{NUMBER},$fe = $e
+		unless defined($P{first_ens});
+	last if (defined($last_ens) &&
+			 $dta{ENSEMBLE}[$e]->{NUMBER} > $last_ens);
+	$P{last_ens} = $dta{ENSEMBLE}[$e]->{NUMBER};
+	$le = $e;
+
+	die("3-beams used in ensemble #$dta{ENSEMBLE}[$e]->{NUMBER}\n")
+		if ($dta{ENSEMBLE}[$e]->{N_BEAMS_USED} < 4);
+	die("BIT error in ensemble $dta{ENSEMBLE}[$e]->{NUMBER}\n")
+		if defined($dta{ENSEMBLE}[$e]->{BUILT_IN_TEST_ERROR});
+	die("Low gain in ensemble #$dta{ENSEMBLE}[$e]->{NUMBER}\n")
+        if ($dta{ENSEMBLE}[$e]->{LOW_GAIN});
+
+	for (my($b)=0; $b<$dta{N_BINS}; $b++) {
+		if ($beamCoords) {
+			$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0] -= $P{velbias_b1}			# beam-velocity biases (-B)
+				if defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0]);
+			$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1] -= $P{velbias_b2}
+				if defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1]);
+			$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2] -= $P{velbias_b3}
+				if defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2]);
+			$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3] -= $P{velbias_b4}
+				if defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3]);
+
+			if (defined($opt_d)) {											# discard data from given beam (-d)
+				undef($dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b][$opt_d-1]);
+				undef($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][$opt_d-1]);
+			}
+
+			@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]} =					# save beam velocities
+				@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]};
+
+			@{$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b]} =				# calculate w12, w34
+				velBeamToBPEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]});
+
+			@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} = 						# calculate earth velocities
+				velBeamToEarth(\%dta,@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]});
+			$dta{ENSEMBLE}[$e]->{THREE_BEAM}[$b] = $RDI_Coords::threeBeamFlag;
+			$three_beam[$b] += $RDI_Coords::threeBeamFlag;
+
+			unless (defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0])) {
+				undef(@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]});			# not sure when this can happen
+				next;
+			}
+		} else { 															# Earth coordinates
+			@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]} =					# calculate beam velocities
+				velEarthToBeam(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]});
+				                                            
+			@{$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b]} =				# calculate w12, w34
+				velBeamToBPEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]});
+
+			@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} = 						# correct for heading bias
+				velApplyHdgBias(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]});
+
+			unless (defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0])) {
+				undef(@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]});			# not sure when/if this can happen
+				next;
+			}
+
+		}
+
+		$dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b] = 1;
+		$good_vels[$b]++; 
+		$lastGoodBin = $b if ($b > $lastGoodBin);
+		$firstGoodEns = $e unless defined($firstGoodEns);
+		$lastGoodEns = $e;
+    }
+}
+
+unless (defined($opt_r)) {
+	$fe = $firstGoodEns;
+	$le = $lastGoodEns;
+}
+
+$P{N_ensembles} = $le - $fe + 1;
+
+$firstBin = 0;
+$lastBin = $lastGoodBin;
+
+print( STDERR "Start      : $dta{ENSEMBLE}[$fe]->{DATE} $dta{ENSEMBLE}[$fe]->{TIME}\n");
+print( STDERR "End        : $dta{ENSEMBLE}[$le]->{DATE} $dta{ENSEMBLE}[$le]->{TIME}\n");
+printf(STDERR "Bins       : %d-%d\n",$firstBin+1,$lastBin+1);
+if ($beamCoords) {
+	printf(STDERR "3-Beam     : %d %d %d %d\n",$RDI_Coords::threeBeam_1,
+											   $RDI_Coords::threeBeam_2,
+											   $RDI_Coords::threeBeam_3,
+											   $RDI_Coords::threeBeam_4);
+	
+}
+
+
+#----------------------------------------------------------------------
+# generate output
+#----------------------------------------------------------------------
+
+print("#ANTS#PARAMS# ");														# %PARAMs
+foreach my $k (keys(%P)) {
+	print(P "$k\{$P{$k}\} ");
+}
+printf(" soundspeed_correction{%s}",defined($opt_S) ? $opt_S : 'NONE!');
+print("\n");
+
+print("#ANTS#FIELDS# " .														# LAYOUT
+		"{ensemble} {heading} {pitch} {roll} {bin} " .
+		"{b1} {b2} {b3} {b4} {u} {v} {w} {err_vel} " .
+		"{v12} {w12} {v34} {w34} " .
+		"{corr1} {corr2} {corr3} {corr4} " .
+		"{amp1} {amp2} {amp3} {amp4} " .
+		"{pcg1} {pcg2} {pcg3} {pcg4}\n"
+);
+
+for (my($e)=$fe; $e<=$le; $e++) {
+	my($ssCorr) = defined($opt_S) ? ssCorr($dta{ENSEMBLE}[$e],$SS_salin,$SS_temp,$SS_depth) : 1;
+	for (my($b)=$first_bin=1; $b<=$last_bin-1; $b++) {
+		print("$dta{ENSEMBLE}[$e]->{NUMBER} ");
+		print(defined($dta{ENSEMBLE}[$e]->{HEADING}) ? "$dta{ENSEMBLE}[$e]->{HEADING} " : 'nan ');
+		print(defined($dta{ENSEMBLE}[$e]->{PITCH}) ? "$dta{ENSEMBLE}[$e]->{PITCH} " : 'nan ');
+		print(defined($dta{ENSEMBLE}[$e]->{ROLL}) ? "$dta{ENSEMBLE}[$e]->{ROLL} " : 'nan ');
+		printf("%d ",$b+1);
+
+		if ($dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b]) {
+			printf("%g %g %g %g ",@{$dta{ENSEMBLE}[$e]->{BEAM_VELOCITY}[$b]});
+			printf("%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0] * $ssCorr);
+			printf("%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1] * $ssCorr);
+			printf("%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2] * $ssCorr);
+			if ($dta{ENSEMBLE}[$e]->{THREE_BEAM}[$b]) {
+				print("nan ");
+			} else {
+				printf("%g ",$dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3] * $ssCorr);
+			}
+			printf(defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][0]) ? "%g " : "nan ",
+						$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][0]);
+			printf(defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][1]) ? "%g " : "nan ",
+						$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][1]);
+			printf(defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][2]) ? "%g " : "nan ",
+						$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][2]);
+			printf(defined($dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][3]) ? "%g " : "nan ",
+						$dta{ENSEMBLE}[$e]->{BEAMPAIR_VELOCITY}[$b][3]);
+		} else {
+			print("nan nan nan nan ");
+			print("nan nan nan nan ");
+			print("nan nan nan nan ");
+	    }
+	    
+		print("@{$dta{ENSEMBLE}[$e]->{CORRELATION}[$b]} ");
+		print("@{$dta{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$b]} ");
+
+		if ($beamCoords) {
+			print("@{$dta{ENSEMBLE}[$e]->{PERCENT_GOOD}[$b]} ");
+		} else {
+			printf("nan nan nan nan ");
+		}
+		print("\n");
+	}
+}
+
+exit(0);