2022 A16N pre-cruise draft
authorA.M. Thurnherr <athurnherr@yahoo.com>
Fri, 18 Nov 2022 12:53:19 -0500
changeset 61 69192495f0db
parent 60 890e267a2d98
child 62 9e13aca980c7
2022 A16N pre-cruise
RDI_PD0_IO.pl
editPD0
listHdr
mkProfile
patchPD0
--- a/RDI_PD0_IO.pl
+++ b/RDI_PD0_IO.pl
@@ -1,9 +1,9 @@
 #======================================================================
 #                    R D I _ P D 0 _ I O . P L 
 #                    doc: Sat Jan 18 14:54:43 2003
-#                    dlm: Wed Mar 17 22:54:27 2021
+#                    dlm: Fri Nov 19 12:09:58 2021
 #                    (c) 2003 A.M. Thurnherr
-#					 uE-Info: 129 35 NIL 0 0 72 2 2 4 NIL ofnI
+#					 uE-Info: 131 28 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
     
 # Read RDI PD0 binary data files (*.[0-9][0-9][0-9])
@@ -127,6 +127,9 @@
 #	Mar  3, 2021: - adapted to Nortek PD0 files
 #	Mar 13, 2021: - finished adaptation to Nortek files
 #	Mar 17, 2021: - updated HISTORY
+#	Sep  1, 2021: - BUG: unexpected length warning did not have ens number
+#	Nov 18, 2021: - improved how ens patching is dealing with garbage
+#					in files
 # END OF HISTORY
     
 # FIRMWARE VERSIONS:
@@ -773,7 +776,8 @@
 
 		if (defined($ens_length) && ($el != $ens_length)) {
 			$RDI_PD0_IO::File_Dirty = 1;
-			print(STDERR "WARNING (RDI_PD0_IO): ensemble ${$E}[$#{$E}]->{NUMBER} skipped (unexpected length)\n");
+			printf(STDERR "WARNING (RDI_PD0_IO): ensemble %d(?) skipped (unexpected length)\n",
+								${$E}[$ens-1]->{NUMBER}+1);
 			pop(@{$E});
 			$ens--;
 			next;
@@ -1299,7 +1303,8 @@
 
 		#----------------------------------------------------------------------
 		# Variable Leader #0
-		#	- read ensNo for debugging purposes
+		#	- read ensNo to make sure there are no problems
+		#	- in files with garbage, this test fails
 		#----------------------------------------------------------------------
 
 		sysseek(WBPF,$start_ens+$WBPofs[1]+2,0) || die("$WBPcfn: $!");
@@ -1308,8 +1313,11 @@
 		sysseek(WBPF,$start_ens+$WBPofs[1]+11,0) || die("$WBPcfn: $!");			# jump to high byte
 		sysread(WBPF,$buf,1) == 1 || die("$WBPcfn: $!");
 		$ensNo += unpack('C',$buf) << 16;
-		die("ensNo = $ensNo (should be $dta->{ENSEMBLE}[$ens]->{NUMBER})\n")
-			unless ($ensNo == $dta->{ENSEMBLE}[$ens]->{NUMBER});
+		unless ($ensNo == $dta->{ENSEMBLE}[$ens]->{NUMBER}) {					# data file with garbage
+			printf(STDERR "$WBPcfn: WARNING: ensNo = $ensNo (should be $dta->{ENSEMBLE}[$ens]->{NUMBER})\n")
+				if ($#{$dta->{ENSEMBLE}}-$ens > 100);
+			last;
+		}
 
 		#----------------------------------------------------------------------
 		# Variable Leader #1
--- a/editPD0
+++ b/editPD0
@@ -2,9 +2,9 @@
 #======================================================================
 #                    E D I T P D 0 
 #                    doc: Mon Nov 25 20:24:31 2013
-#                    dlm: Wed Mar 14 21:15:51 2018
+#                    dlm: Mon Oct 18 11:46:58 2021
 #                    (c) 2013 A.M. Thurnherr
-#                    uE-Info: 417 0 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 45 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,8 +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
+#		dealias_attempt(<WV lim[m/s]>) 	correct data for erroneously low WV setting
+#									- LIKELY DOES NOT WORK AT ALL
+#									- DOES DEFINITELY NOT WORK FOR DATA WITH DOUBLE WRAPS
 #
 #	- -x notes:
 #		- multiple perl expressions can be combined with ,
@@ -76,6 +77,11 @@
 #	Nov 15, 2016: - BUG: ensure_{DL,UL} routines did not negate heading data
 #	Jul 27, 2017: - began working on dealias()
 #	Dec  6, 2017: - cosmetics
+#	Oct 18, 2021: - played around with dealias() with data from BLT and came 
+#					to the conclusion that it does not work; although, the
+#				    problem with the BLT data is that there are probably lots
+#					of double wraps
+# END OF HISTORY
 
 use Getopt::Std;
 
@@ -120,7 +126,7 @@
 
 { my(@target);												# static scope, undef initially
 
-sub dealias_V0($)
+sub dealias_attempt($)
 {
 	my($WV) = @_;
 
@@ -133,10 +139,11 @@
 					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);
+#				printf(STDERR "ens=$e beam=$beam bin=$bin: target=$target[$beam] vel=$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] vd=$dealiased\n");
 				if (abs($target[$beam]-$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam]) >
 					abs($target[$beam]-$dealiased)) {
-						$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] = undef; #$dealiased;
-						$dealiased++;
+#						print(STDERR "DEALIASED\n");
+						$dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam] = $dealiased;
 				}
 			}
     	}
@@ -145,12 +152,17 @@
     }
 
 	@target = (0,0,0,0);									# calc ref-lr average target for next ens
+	@nSamp  = (0,0,0,0);
 	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]);
+			next unless defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam]);
+			$target[$beam] += $dta{ENSEMBLE}[$e]->{VELOCITY}[$bin][$beam];
+			$nSamp[$beam]++;
 		}
     }
+	for (my($beam)=0; $beam<4; $beam++) {
+		$target[$beam] = $nSamp[$beam] ? ($target[$beam] / $nSamp[$beam]) : nan;
+	}
 
     return 1;
 }
--- a/listHdr
+++ b/listHdr
@@ -2,9 +2,9 @@
 #======================================================================
 #                    L I S T H D R 
 #                    doc: Sat Jan 18 18:41:49 2003
-#                    dlm: Wed Mar 17 22:53:25 2021
+#                    dlm: Mon Oct 18 11:05:30 2021
 #                    (c) 2003 A.M. Thurnherr
-#                    uE-Info: 50 35 NIL 0 0 72 10 2 4 NIL ofnI
+#                    uE-Info: 117 41 NIL 0 0 72 10 2 4 NIL ofnI
 #======================================================================
 
 # Print useful info from the RDI BB header
@@ -48,6 +48,7 @@
 #					with this utility any more
 #	Mar  6, 2021: - adapted to Nortek Signature data files
 #	Mar 17, 2021: - updated HISTORY
+#	Aug 23, 2021: - added power and bandwidth to -s output
 
 use Getopt::Std;
 
@@ -61,7 +62,8 @@
 if ($opt_s) {
 	print("#!/usr/bin/perl -S list\n");     
 	chmod(0777&~umask,*STDOUT);
-	print("#ANTS#FIELDS# {id} {serial_number} {transducer_frequency} {number_of_bins} {blanking_distance} {bin_length} {pulse_length}\n");
+	print("#ANTS#FIELDS# {id} {serial_number} {transducer_frequency} {max_power} {bandwidth}" .
+					   " {number_of_bins} {blanking_distance} {bin_length} {pulse_length}\n");
 }
 
 while ($ARGV[0] ne '') {
@@ -78,8 +80,9 @@
 			$id =~ s@^.*/([^/]+)@\1@;
 		}
 		if ($valid) {
-			printf("%s %d %.1f %d %g %d %.1f\n",
+			printf("%s %d %.1f %d %s %d %g %d %.1f\n",
 				$id,$hdr{SERIAL_NUMBER},$hdr{BEAM_FREQUENCY},
+				$hdr{TRANSMIT_POWER_MAX},($hdr{NARROW_BANDWIDTH} ? 'narrow' : 'wide'),
 				$hdr{N_BINS},$hdr{BLANKING_DISTANCE},$hdr{BIN_LENGTH},
 	            $hdr{TRANSMITTED_PULSE_LENGTH});
 	    } else {
@@ -111,7 +114,7 @@
 	printf("\tSPEED_OF_SOUND\t\t\t= %d m/s [%s]\n",
 				$hdr{SPEED_OF_SOUND},
 				$hdr{SSPEED_CALCULATED} ? 'using measurements' : 'from settings');
-	printf("\tAMBIGUITY_VELOCITY\t\t= %.1f m/s\n",
+	printf("\tAMBIGUITY_VELOCITY\t\t= %.2f m/s\n",
 				ambiguity_velocity($hdr{BEAM_FREQUENCY},$hdr{BEAM_ANGLE},$hdr{SPEED_OF_SOUND},$hdr{TRANSMIT_LAG_DISTANCE}))
 					unless ($hdr{INSTRUMENT_TYPE} eq 'Signature');
 
--- a/mkProfile
+++ b/mkProfile
@@ -2,9 +2,9 @@
 #======================================================================
 #                    M K P R O F I L E 
 #                    doc: Sun Jan 19 18:55:26 2003
-#                    dlm: Sat Apr 10 05:51:48 2021
+#                    dlm: Mon Oct 18 11:02:31 2021
 #                    (c) 2003 A.M. Thurnherr
-#                    uE-Info: 99 35 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 101 76 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 # Make an LADCP Profile by Integrating W (similar to Firing's scan*).
@@ -97,6 +97,8 @@
 #	Apr 24, 2018: - BUG: bin1 was used even with zero blanking
 #	Apr 18, 2019: - added coord-transformation %PARAMs
 #	Apr 10, 2021: - adapted to V2.4
+#	Oct 12, 2021: - added speed to output on -M
+#	Oct 18, 2021: - BUG: speed bombed with zero valid velocities in ensemble
 # HISTORY END
 
 # NOTES:
@@ -369,6 +371,8 @@
 		my(@sumsq) = (0,0);
 		$dta{ENSEMBLE}[$ens]->{U} /= $n;
 		$dta{ENSEMBLE}[$ens]->{V} /= $n;
+		$dta{ENSEMBLE}[$ens]->{SPEED} = sqrt($dta{ENSEMBLE}[$ens]->{U}**2 +
+											 $dta{ENSEMBLE}[$ens]->{V}**2);
 		for ($i=0; $i<$n; $i++) {
 			$sumsq[0] += ($dta{ENSEMBLE}[$ens]->{U}-$goodU[$i])**2;
 			$sumsq[1] += ($dta{ENSEMBLE}[$ens]->{V}-$goodV[$i])**2;
@@ -382,8 +386,10 @@
 }
 
 #----------------------------------------------------------------------
+# Create Progressive Vector Diagram
+#----------------------------------------------------------------------
 
-($x,$y) = (0,0);											# init
+($x,$y,$cAvg,$ngood) = (0,0,0,0);										# init
 
 $dta{ENSEMBLE}[$firstgood]->{X} = $dta{ENSEMBLE}[$firstgood]->{X_ERR} = 0;
 $dta{ENSEMBLE}[$firstgood]->{Y} = $dta{ENSEMBLE}[$firstgood]->{Y_ERR} = 0;
@@ -411,6 +417,8 @@
 	# The current ensemble has valid u/v
 	#-----------------------------------
 
+	$cAvg += $dta{ENSEMBLE}[$e]->{SPEED}; $ngood++;
+
 	$x -= $dta{ENSEMBLE}[$prevgood]->{U} * $dt;			# integrate
 	$xErr += ($dta{ENSEMBLE}[$prevgood]->{U_ERR} * $dt)**2;
 	$dta{ENSEMBLE}[$e]->{X} = $x;
@@ -442,6 +450,8 @@
 $firstgood++ if ($firstgood == 0);							# centered diff
 $lastgood-- if ($lastgood == $#{$dta{ENSEMBLE}});			# in step 6
 
+$cAvg = $ngood ? ($cAvg / $ngood) : nan;
+
 #======================================================================
 # Step 3: Calculate Uncertainties
 #======================================================================
@@ -718,7 +728,7 @@
 chmod(0777&~umask,*STDOUT);
 
 print("#ANTS# [] $USAGE\n");
-$uFields = "{u} {u_err} {v} {v_err} {x} {x_err} {y} {y_err}"
+$uFields = "{u} {u_err} {v} {v_err} {x} {x_err} {y} {y_err} {speed}"
 	if defined($opt_M);
 print("#ANTS#FIELDS# {ens} {time} {elapsed} {secno} {downcast} " .
 					"{w} {w_err} {err_vel} {depth} {depth_err} {seabed} " .
@@ -775,14 +785,15 @@
 	  );
 printf("#ANTS#PARAMS# magnetic_declination{$opt_M} " .
 							  "uv_gap_time{%d} " .
-								   "mean_u{%.4f} " .
-								 "stderr_u{%.4f} " .
+								   "u.mean{%.4f} " .
+								 "u.sterrr{%.4f} " .
 									   "dx{%d} " .
-								   "dx_err{%d} " .
-								   "mean_v{%.4f} " .
-								 "stderr_v{%.4f} " .
+								   "dx.err{%d} " .
+								   "v.mean{%.4f} " .
+								 "v.stderr{%.4f} " .
 									   "dy{%d} " .
-								   "dy_err{%d}\n",
+								   "dy.err{%d} " .
+							   "speed.mean{%.2f}\n",
 	$uv_gap_time,
 	$dta{ENSEMBLE}[$lastgood]->{X} /
 		$dta{ENSEMBLE}[$lastgood]->{ELAPSED_TIME},
@@ -790,6 +801,7 @@
 	$dta{ENSEMBLE}[$lastgood]->{Y} /
 		$dta{ENSEMBLE}[$lastgood]->{ELAPSED_TIME},
 	$vErr, $dta{ENSEMBLE}[$lastgood]->{Y}, $y_err,
+	$cAvg
 ) if defined ($opt_M);
 if ($dta{TIME_BETWEEN_PINGS} == 0) {
 	 print("#ANTS#PARAMS# pinging_rate{staggered}\n");
@@ -829,6 +841,7 @@
 		p($dta{ENSEMBLE}[$e]->{V}); p($dta{ENSEMBLE}[$e]->{V_ERR});
 		p($dta{ENSEMBLE}[$e]->{X}); p($dta{ENSEMBLE}[$e]->{X_ERR});
 		p($dta{ENSEMBLE}[$e]->{Y}); p($dta{ENSEMBLE}[$e]->{Y_ERR});
+		p($dta{ENSEMBLE}[$e]->{SPEED});
 	}
 	if (@f) {
 		foreach $f (@f) {
--- a/patchPD0
+++ b/patchPD0
@@ -2,14 +2,14 @@
 #======================================================================
 #                    P A T C H P D 0 
 #                    doc: Tue Aug 23 20:00:15 2016
-#                    dlm: Tue Apr 14 21:28:49 2020
+#                    dlm: Mon Jul 12 20:43:08 2021
 #                    (c) 2010 A.M. Thurnherr
-#                    uE-Info: 103 26 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 24 37 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 $antsSummary = 'patch TRDI PD0 file with external attitude data';
 
-# History:
+# HISTORY:
 #	Aug 23, 2016: - exported from IMP+LADCP
 #	Aug 25, 2016: - completed basic structure
 #	Nov 20, 2017: - major code cleanup
@@ -21,6 +21,7 @@
 #				  - BUG: ??? does -o handle pitch and roll ANOMALIES correctly?
 #	Jun 30, 2019: - -o did not work with single argument
 #	Apr 14, 2020: - adapted to use for moored ADCP data as well
+#	Jul 12, 2021: - beautified output
 
 # PATCH-FILE REQUIREMENTS (ANTS format)
 #	- %[L]ADCP_pitch.mu %[L]ADCP_roll.mu	mean ADCP pitch and roll
@@ -201,9 +202,16 @@
 
 writeData($outPD0,\%ADCP);													# write new PD0
 
-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);
+if ($pr_missing+$hdg_missing) {
+	my($verb) = $opt_k ? 'retained' : 'cleared';
+	printf(STDERR "$outPD0: ");
+	if ($pr_missing) {
+		printf(STDERR "%d pitch/roll ",$pr_missing);
+		printf(STDERR "and ") if ($hdg_missing);
+	}
+	printf(STDERR "%d heading ",$hdg_missing) if ($hdg_missing);
+	printf(STDERR "values $verb\n");
+}
         
 exit(0);