before debugging weird nsamp problem
authorA.M. Thurnherr <athurnherr@yahoo.com>
Thu, 20 Mar 2014 12:04:30 +0000
changeset 25 91bd907db97f
parent 24 40756f8aff85
child 26 d778b73f2a43
before debugging weird nsamp problem
LADCPintsh
LADCPproc
LADCPproc.UHcode
LADCPproc.bestLag
libdiscard_beam2.pl
libdiscard_beam3.pl
libdiscard_beam4.pl
--- a/LADCPintsh
+++ b/LADCPintsh
@@ -2,9 +2,9 @@
 #======================================================================
 #                    L A D C P I N T S H 
 #                    doc: Thu Oct 14 21:22:50 2010
-#                    dlm: Fri Jul 12 12:18:07 2013
+#                    dlm: Thu Mar 20 11:57:54 2014
 #                    (c) 2010 A.M. Thurnherr & E. Firing
-#                    uE-Info: 57 59 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 58 55 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 $antsSummary = 'integrate LADCP shear';
@@ -55,6 +55,7 @@
 #	Jun 28, 2013: - adapated to new :: convention
 #				  - make sure LADCP DUL metadata are dealt with correctly
 #	Jul 12, 2013: - clarified -u usage with better messages
+#	Mar 20, 2014: - fiddled while debugging [LADCPproc]
 
 ($ANTS) = (`which ANTSlib` =~ m{^(.*)/[^/]*$});
 require "$ANTS/ants.pl";
@@ -166,11 +167,12 @@
 	$uc_nsamp = $ants_[0][$uc_nshF];
 	if (defined($opt_u)) {
 		$dl_nsamp = $dc_nsamp + $uc_nsamp;
-		$ul_nsamp = $UL_[$dc_nshF] + $UL_[$uc_nshF];
 		$dc_nsamp += $UL_[$dc_nshF];
 		$uc_nsamp += $UL_[$uc_nshF];
+		$ul_nsamp = $dc_nsamp + $uc_nsamp;
 	}
-	$dc_nsamp[$r] = $dc_nsamp;
+	
+	$dc_nsamp[$r] = $dc_nsamp;								# save for each record
 	$uc_nsamp[$r] = $uc_nsamp;
 	$nsamp[$r] = $dc_nsamp + $uc_nsamp;
 	if (defined($opt_u)) {
@@ -261,6 +263,7 @@
 			$wz[$r] = $uc_wz[$r];
 			$elapsed[$r] = $uc_elapsed[$r];
         }
+		print(STDERR "uz[$r] := $uz[$r] [$dc_uz[$r]/$uc_uz[$r]] (dcf=$dcf ucf=$ucf)\n");
 	} else {
 		$uz[$r] = $vz[$r] = $wz[$r] = $elapsed[$r] = nan;
 	}
@@ -378,7 +381,7 @@
 
 my($refU,$refV,$refW,$dc_refU,$dc_refV,$dc_refW,$uc_refU,$uc_refV,$uc_refW);
 
-if (defined($opt_r)) {											# reference to bottom-track profile
+if (defined($opt_r)) {											# reference using velocity profile
 	print(STDERR "Loading reference-velocity data from $opt_r...\n")
 		if ($opt_d);
 	open(BTF,$opt_r) || croak("$opt_r: $!\n");
@@ -503,7 +506,7 @@
 			$dc_refV = $dc_sumV/$dc_nSumVel - $dc_wSumBTv/$dc_sumVarBTv;
 			$dc_refW = $dc_sumW/$dc_nSumVel - $dc_wSumBTw/$dc_sumVarBTw if (@BT_w);
 		} else {
-			&antsInfo("$opt_r: insufficient reference-velocity data to constrain dc profile --- baroclinic profile only");
+			&antsInfo("$opt_r: insufficient reference-velocity data to constrain DC profile --- baroclinic profile only");
 		}
 	    
 		if ($uc_nSumVel > 0) {
@@ -511,7 +514,7 @@
 			$uc_refV = $uc_sumV/$uc_nSumVel - $uc_wSumBTv/$uc_sumVarBTv;
 		    $uc_refW = $uc_sumW/$uc_nSumVel - $uc_wSumBTw/$uc_sumVarBTw if (@BT_w);
 		} else {
-			&antsInfo("$opt_r: insufficient reference-velocity data to constrain dc profile --- baroclinic profile only");
+			&antsInfo("$opt_r: insufficient reference-velocity data to constrain UC profile --- baroclinic profile only");
 		}
 	} else {
 		&antsInfo("$opt_r: no valid reference-velocity data --- baroclinic profiles only");
--- a/LADCPproc
+++ b/LADCPproc
@@ -2,29 +2,33 @@
 #======================================================================
 #                    L A D C P P R O C 
 #                    doc: Thu Sep 16 20:36:10 2010
-#                    dlm: Wed Sep 25 13:09:21 2013
+#                    dlm: Wed Mar 19 23:41:55 2014
 #                    (c) 2010 A.M. Thurnherr
-#                    uE-Info: 84 61 NIL 0 0 72 10 2 4 NIL ofnI
+#                    uE-Info: 91 55 NIL 0 0 72 10 2 4 NIL ofnI
 #======================================================================
 
 # NOTES:
 #   - the shear-method editing in this code is based on Eric Firing's merge.c
 #   - as described in [LADCPproc.backscatter], there are three different codes 
 #     for correcting echo amplitudes for attenuation loss & beam spreading
-#   - comments starting with ## are taken verbatim from the original implementations
+#   - comments starting with ## are taken verbatim from the original
+#	  implementations
 #   - the basic idea of the time lagging implemented in this code is similar
 #     to the one implemented in Martin Visbeck's bestlag.m
 #   - for SeaBird files:
 #       - CTD elapsed time is estimated from recno * CTD{sampint}
 #       - first elapsed time is added on output
-#   - output elapsed time is from CTD to allow yoyo processing without loss of
-#     time information
+#   - output time:
+#		- is from CTD to allow yoyo processing without loss of time information
+#		- to obtain LADCP time, add %LADCP_time_lag to CTD time
+#		- %LADCP_time_lag can be used for -l
+#		- -i is different: it should be set to the number that is added to
+#						   LADCP_elapsed to get the CTD time, i.e.
+#						   -%LADCP_time_lag
 #   - CTD{elapsed} is undefined for records before instrument is in the water
 #   - ITS-90 temp field in degC required
 #   - salin field prequired
 #   - pressure field in dbar required
-#   - -i should be set to the number that's added to LADCP_elapsed to make the two
-#     time series overplot nicely
 
 # HISTORY:
 #   Sep 16, 2010: - incepted
@@ -82,6 +86,9 @@
 #	Sep 25, 2013: - BUG: %PARAM magnetic_declination did not have LADCPproc:: prefix
 #				  - added CTD lat/lon info to most output files (but not BT)
 #				  - BUG: moved %water_depth to common %PARAMs
+#	Mar 19, 2014: - moved code to set LADCP_time_lag %PARAM into main prog so it is
+#				    set, even when -l is used
+#				  - added pitch, roll, hdg to -t output
 
 ($ANTS)    = (`which ANTSlib` =~ m{^(.*)/[^/]*$});
 ($PERL_TOOLS) = (`which mkProfile` =~ m{^(.*)/[^/]*$});
@@ -405,6 +412,7 @@
 print(STDERR "\n") if defined($opt_l);
 
 $opt_l = defined($opt_l) ? -$opt_l : &lagLADCP2CTD();
+&antsAddParams('LADCPproc::LADCP_time_lag',-$opt_l);
 
 print(STDERR "Associating CTD data with LADCP ensembles...");
 
@@ -757,7 +765,7 @@
 if (defined($opt_t)) {
 	print(STDERR "Writing time series to <$opt_t>...");
 	
-	@antsNewLayout = ('ens','elapsed','depth','CTD_lat','CTD_lon','CTD_w','LADCP_w');
+	@antsNewLayout = ('ens','elapsed','depth','CTD_lat','CTD_lon','CTD_w','LADCP_w','pitch','roll','hdg');
 	&antsOut('EOF');
 	$antsCurParams = $commonParams;
 	close(STDOUT);
@@ -770,7 +778,11 @@
 				 $LADCP{ENSEMBLE}[$ens]->{CTD_LAT},
 				 $LADCP{ENSEMBLE}[$ens]->{CTD_LON},
 				 $LADCP{ENSEMBLE}[$ens]->{CTD_W},
-				 $LADCP{ENSEMBLE}[$ens]->{W});
+				 $LADCP{ENSEMBLE}[$ens]->{W},
+				 $LADCP{ENSEMBLE}[$ens]->{PITCH},
+				 $LADCP{ENSEMBLE}[$ens]->{ROLL},
+				 $LADCP{ENSEMBLE}[$ens]->{HEADING},
+		);
 	}
 	print(STDERR "\n");
 }
--- a/LADCPproc.UHcode
+++ b/LADCPproc.UHcode
@@ -1,9 +1,9 @@
 #======================================================================
 #                    L A D C P P R O C . U H C O D E 
 #                    doc: Fri Sep 17 20:27:53 2010
-#                    dlm: Thu Nov 21 10:13:04 2013
+#                    dlm: Tue Mar  4 13:44:48 2014
 #                    (c) 2010 A.M. Thurnherr & E. Firing
-#                    uE-Info: 44 0 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 44 66 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 # PERLified functions from Eric's [merge.c]; with mods
@@ -41,6 +41,7 @@
 #				         solutions
 #				  - BUG: set_shear_flag() calculated shdev (slightly?)
 #						 wrongly
+#	Mar  4, 2014: - added support for missing PITCH/ROLL (TILT) & HEADING
 
 #======================================================================
 # VELOCITY EDITING
@@ -98,7 +99,7 @@
 
 	## if upward (=negative) velocity greater than minimum, calculate wake
 	## 		heading and inclination
-	if ($wref < -$min_wake_w) {
+	if (defined($LADCP{ENSEMBLE}[$ens]->{HEADING}) && $wref<-$min_wake_w) {
 		my($wake_hd) = 180 / 3.14159265358979 * atan2($uref,$vref);
 		my($speed) 	 = sqrt($uref*$uref + $vref*$vref);
 		my($wake_ang)= abs(180 / 3.14159265358979 * atan($speed/$wref));
@@ -212,6 +213,7 @@
 sub set_PPI_flags($$)
 {
 	my($ens,$De) = @_;
+	my($clip_z0,$clip_z1);
 
 	my($dt_ping) = $LADCP{ENSEMBLE}[$ens]->{UNIX_TIME} - $LADCP{ENSEMBLE}[$ens-1]->{UNIX_TIME};
 
@@ -239,7 +241,7 @@
 		next if ($edit_flags[$ens][$bin]);
 
 		my($dob) = depthOfBin($ens,$bin);
-		if ($dob >= $clip_z0 && $dob <= $clip_z1) {
+		if (!defined($LADCP{ENSEMBLE}[$ens]->{TILT}) || ($dob>=$clip_z0 && $dob<=$clip_z1)) {
 			$edit_flags[$ens][$bin] |= $PPI_BIT;
 			$flag_count{$PPI_BIT}++;
 		}
@@ -264,7 +266,8 @@
 			}
 			next;
 		}
-		if ($LADCP{ENSEMBLE}[$ens]->{TILT} > $max_tilt) {				# get rid ensembles with large tilt
+		if (!defined($LADCP{ENSEMBLE}[$ens]->{TILT}) ||
+			 $LADCP{ENSEMBLE}[$ens]->{TILT}>$max_tilt) {				# get rid ensembles with large tilt
 			for (my($bin)=0; $bin<$LADCP{N_BINS}; $bin++) {
 				$edit_flags[$ens][$bin] |= $TILT_BIT;
 				$flag_count{$TILT_BIT}++;
@@ -279,12 +282,14 @@
 			}
 			next;
 		}																# get rid ensembles after large rotation
-		if (abs($LADCP{ENSEMBLE}[$ens]->{TILT}-$LADCP{ENSEMBLE}[$ens-$De]->{TILT}) > $max_delta_tilt) {
-			for (my($bin)=0; $bin<$LADCP{N_BINS}; $bin++) {
-				$edit_flags[$ens][$bin] |= $DELTA_TILT_BIT;
-				$flag_count{$DELTA_TILT_BIT}++;
-			}
-			next;
+		if (defined($LADCP{ENSEMBLE}[$ens]->{TILT}) &&
+			defined($LADCP{ENSEMBLE}[$ens-$De]->{TILT}) &&
+			abs($LADCP{ENSEMBLE}[$ens]->{TILT}-$LADCP{ENSEMBLE}[$ens-$De]->{TILT}) > $max_delta_tilt) {
+				for (my($bin)=0; $bin<$LADCP{N_BINS}; $bin++) {
+					$edit_flags[$ens][$bin] |= $DELTA_TILT_BIT;
+					$flag_count{$DELTA_TILT_BIT}++;
+				}
+	            next;
 		}
 		for (my($bin)=$shbin_start-1; $bin<$shbin_end; $bin++) {		# flag bad velocities
 			$edit_flags[$ens][$bin] |= $BADVEL_BIT,$flag_count{$BADVEL_BIT}++
--- a/LADCPproc.bestLag
+++ b/LADCPproc.bestLag
@@ -1,9 +1,9 @@
 #======================================================================
 #                    L A D C P P R O C . B E S T L A G 
 #                    doc: Tue Sep 28 21:58:48 2010
-#                    dlm: Tue Jun 25 14:40:05 2013
+#                    dlm: Wed Mar 19 21:33:30 2014
 #                    (c) 2010 A.M. Thurnherr
-#                    uE-Info: 193 31 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 28 45 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 # TODO:
@@ -25,6 +25,7 @@
 #	May 18, 2012: - BUG: window start index was not always calculated correctly
 #	Oct 19, 2012: - BUG: opt_i had wrong sign!
 #	Jun 25, 2013: - adapted to :: %PARAM convention
+#	Mar 19, 2014: - moved %PARAM to LADCPproc
 
 sub interp_LADCP_w($$)
 {
@@ -190,7 +191,6 @@
 		printf(STDERR "\n\t\tmost popular lag = %ds\n",($first_guess_lag+$best_lag)*$CTD{sampint});
 	}
 
-	&antsAddParams('LADCPproc::LADCP_time_lag',($first_guess_lag + $best_lag) * $CTD{sampint});
 	return ($first_guess_lag + $best_lag) * $CTD{sampint};
 }
 
new file mode 100644
--- /dev/null
+++ b/libdiscard_beam2.pl
@@ -0,0 +1,23 @@
+#======================================================================
+#                    L I B D I S C A R D _ B E A M 2 . P L 
+#                    doc: Thu Mar 28 21:57:14 2013
+#                    dlm: Tue Mar  4 18:15:42 2014
+#                    (c) 2013 A.M. Thurnherr
+#                    uE-Info: 15 60 NIL 0 0 72 2 2 4 NIL ofnI
+#======================================================================
+
+sub edit_LADCP_vels()
+{
+    print(STDERR "\t\tDISCARDING beam-2 velocities...\n");
+    
+    for (my($ens)=$LADCP_start; $ens<=$LADCP_end; $ens++) {
+        for (my($bin)=0; $bin<$LADCP{N_BINS}; $bin++) {
+            undef($LADCP{ENSEMBLE}[$ens]->{BEAM_VEL}[$bin][1]);
+            @{$LADCP{ENSEMBLE}[$ens]->{VELOCITY}[$bin]} =
+            	velInstrumentToEarth(\%LADCP,$ens,velBeamToInstrument(\%LADCP,@{$LADCP{ENSEMBLE}[$ens]->{BEAM_VEL}[$bin]}));
+        }
+    }
+}
+
+1;
+
new file mode 100644
--- /dev/null
+++ b/libdiscard_beam3.pl
@@ -0,0 +1,23 @@
+#======================================================================
+#                    L I B D I S C A R D _ B E A M 3 . P L 
+#                    doc: Thu Mar 28 21:57:14 2013
+#                    dlm: Tue Mar  4 18:16:24 2014
+#                    (c) 2013 A.M. Thurnherr
+#                    uE-Info: 15 60 NIL 0 0 72 2 2 4 NIL ofnI
+#======================================================================
+
+sub edit_LADCP_vels()
+{
+    print(STDERR "\t\tDISCARDING beam-3 velocities...\n");
+    
+    for (my($ens)=$LADCP_start; $ens<=$LADCP_end; $ens++) {
+        for (my($bin)=0; $bin<$LADCP{N_BINS}; $bin++) {
+            undef($LADCP{ENSEMBLE}[$ens]->{BEAM_VEL}[$bin][2]);
+            @{$LADCP{ENSEMBLE}[$ens]->{VELOCITY}[$bin]} =
+            	velInstrumentToEarth(\%LADCP,$ens,velBeamToInstrument(\%LADCP,@{$LADCP{ENSEMBLE}[$ens]->{BEAM_VEL}[$bin]}));
+        }
+    }
+}
+
+1;
+
new file mode 100644
--- /dev/null
+++ b/libdiscard_beam4.pl
@@ -0,0 +1,23 @@
+#======================================================================
+#                    L I B D I S C A R D _ B E A M 4 . P L 
+#                    doc: Thu Mar 28 21:57:14 2013
+#                    dlm: Tue Mar  4 18:16:45 2014
+#                    (c) 2013 A.M. Thurnherr
+#                    uE-Info: 11 39 NIL 0 0 72 2 2 4 NIL ofnI
+#======================================================================
+
+sub edit_LADCP_vels()
+{
+    print(STDERR "\t\tDISCARDING beam-4 velocities...\n");
+    
+    for (my($ens)=$LADCP_start; $ens<=$LADCP_end; $ens++) {
+        for (my($bin)=0; $bin<$LADCP{N_BINS}; $bin++) {
+            undef($LADCP{ENSEMBLE}[$ens]->{BEAM_VEL}[$bin][3]);
+            @{$LADCP{ENSEMBLE}[$ens]->{VELOCITY}[$bin]} =
+            	velInstrumentToEarth(\%LADCP,$ens,velBeamToInstrument(\%LADCP,@{$LADCP{ENSEMBLE}[$ens]->{BEAM_VEL}[$bin]}));
+        }
+    }
+}
+
+1;
+