before A20
authorA.M. Thurnherr <athurnherr@yahoo.com>
Mon, 08 Mar 2021 12:07:26 -0500
changeset 54 828e5466391b
parent 53 b4374f1ebd60
child 55 2d8e1139acd5
before A20
LADCP_VKE
LADCP_w_CTD
LADCP_w_ocean
Makefile
default_output.pl
defaults.pl
plot_mean_residuals.pl
plot_wprof.pl
svel_corrections.pl
version.pl
write_residual_profs.pl
--- a/LADCP_VKE	Tue Oct 29 13:16:36 2019 -0400
+++ b/LADCP_VKE	Mon Mar 08 12:07:26 2021 -0500
@@ -2,9 +2,9 @@
 #======================================================================
 #                    L A D C P _ V K E 
 #                    doc: Tue Oct 14 11:05:16 2014 
-#                    dlm: Tue Jul 24 17:02:30 2018
+#                    dlm: Thu Sep 12 13:49:04 2019
 #                    (c) 2012 A.M. Thurnherr
-#                    uE-Info: 399 0 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 111 71 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 $antsSummary = 'calculate VKE from LADCP-derived vertical-velocity profiles';
@@ -108,6 +108,7 @@
 #	Dec  9, 2017: - added support for $antsSuppressCommonOptions
 #	Apr 24, 2018: - BUG: output was one field too wide (filled with nans) because antsBufNFields was not reset
 #	Apr 25, 2018: - added -y and removed spectral bins from default output
+#	Sep 12, 2019: - disabled default -l cut-off (used to be 5e-11 W/kg)
 
 ($ANTSLIB) = (`which ANTSlib` =~ m{^(.*)/[^/]*$});
 ($WCALC)   = ($0              =~ m{^(.*)/[^/]*$});
@@ -130,7 +131,7 @@
 #my($c) = 0.0215;						# Thurnherr et al. (GRL 2015)
 my($c) = 0.026;							# increased by 20% for V1.2beta7
 $opt_q = 3;								# Equatorial band: little more than a guess based on 2015 P16N
-$opt_l = 5e-11;							# [W/kg]; based on DIMES data sets, method works for eps >= 2e-10; 5e-11 allows for scatter in p0
+$opt_l = 0;								# [W/kg]; cutoff disabled Sep 12, 2019
 $opt_a = nan;							# assume background dissipation for samples that pass the tests but have eps below -l
 $opt_z = 1;								# number of w_ocean samples to require
 $opt_o = 0;								# remove mean before calculating spectra
@@ -573,11 +574,11 @@
 	#		p0fit.r <= -0.4
 	#	- in a plot of rms vs nsamp the limiting value of 0.4 is hit at 50 samples
 	#
-	#	=> FULL SET OF MUTUALLY CONSISTENT CRITERIA
+	#	=> SET OF MUTUALLY CONSISTENT CRITERIA
 	#
 	# Additional Empirical Filters:
 	#	- latitude > 3deg				guess based on Thurnherr et al., 2015, Gregg et al., 2003, 2015 GO-SHIP P16N
-	#   - eps >= 5e-11 W/kg				based on DIMES data, cutting where errors become > factor 2
+	#   - eps >= 5e-11 W/kg				based on DIMES data, cutting where errors become > factor 2; DISABLED SEPTEMBER 2019
 	#-----------------------------------------------------------------------------------------------------
 
 	if ($latM > $opt_q &&													# 	1) not (too) equatorial
--- a/LADCP_w_CTD	Tue Oct 29 13:16:36 2019 -0400
+++ b/LADCP_w_CTD	Mon Mar 08 12:07:26 2021 -0500
@@ -2,9 +2,9 @@
 #======================================================================
 #                    L A D C P _ W _ C T D 
 #                    doc: Mon Nov  3 17:34:19 2014
-#                    dlm: Wed Aug 28 17:47:08 2019
+#                    dlm: Fri Jun 26 13:07:13 2020
 #                    (c) 2014 A.M. Thurnherr
-#                    uE-Info: 296 0 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 144 14 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 $antsSummary = 'pre-process SBE 9plus CTD data for LADCP_w';
@@ -89,6 +89,10 @@
 #					min 2 samples per bin, allowing for max 12Hz sampling rate)
 #	Aug 27, 2019: - began adding correction for dropped CTD scans
 #	Aug 28, 2019: - made it work
+#				  - added -g
+#	Aug 30, 2019: - renamed -g to -m)odulo error correction (not)
+#				  - added -f)ill missing data
+#	Jun 26, 2020: - added salinity -b)ias correction
 
 # NOTES:
 #	w_CTD is positive during the downcast to make the sign of the apparent
@@ -110,20 +114,26 @@
 
 $antsParseHeader = 0;											# usage
 $antsSuppressCommonOptions = 1;
-&antsUsage('ac:i:l:orp:qs:v:w:',1,
+$IS = &antsLoadModel('`','.nminterp','linear');
+&antsUsage("ab:c:fgi:l:morp:qs:v:w:$IS_opts",1,
 	'[-v)erbosity <level[0]>]',
 	'[use -a)lternate sensor pair]',
+	'[correct -S)alinity <bias>]',
 	'[-r)etain all data (no editing)] [allow infinite -o)utliers]',
+	'[suppress CTD -m)odulo error correction]',
 	'[-s)ampling <rate[6Hz]>]',
 	'[lowpass w_CTD -c)utoff <limit[2s]>] [-w)inch-speed <granularity[10s]>]',
 	'[profile -i)d <id>] [station -l)ocation <lat/lon>]',
 	'[-p)lot_basenames <[%03d_w_CTD.ps],[%03d_sspd.ps]>]',
 	'[-q)uiet (no plots)]',
+	'[-f)ill gaps with linear interpolation]',
 	'<SBE CNV file>');
+&antsUsageError() unless defined($IS);
 
 &antsFloatOpt(\$opt_c,2);										# default low-pass cutoff for w_CTD
 &antsCardOpt(\$opt_s,6);										# default output sampling rate (Hz)
 &antsFloatOpt(\$opt_w,10);										# winch velocity granularity
+&antsFloatOpt(\$opt_b,0);										# salinity bias
 &antsCardOpt(\$opt_v,$ENV{VERB});								# support VERB env variable
 
 $CNVfile = $ARGV[0];											# open CNV file
@@ -131,6 +141,9 @@
 &antsAddDeps($CNVfile);
 &antsActivateOut();												# activate ANTS file
 
+&antsAddParams('salinity_bias_corrected',$opt_b)
+	if ($opt_b != 0);
+
 #----------------------------------------------------------------------
 # Read Data
 #----------------------------------------------------------------------
@@ -153,7 +166,7 @@
 		SBE_parseHeader(F,0,0); 									# SBE field names, no time check
 	
 	_croak("$CNVfile: insufficient time resolution ($sampint s) for ${opt_s}Hz time series\n")
-		if (round(1/$sampint/$opt_s) < 1);
+		if (1/$sampint/$opt_s < 1);
 
 	if (defined($opt_l)) {											# set/override station location with -l
 		my($slat,$slon) = split('[,/]',$opt_l);
@@ -194,14 +207,16 @@
 		}
 	}
 
-	$systimeF = &fnrNoErr('timeY');									# apply correction for dropped scans
-	$xmerrF  = &fnrNoErr('modError');
-	if (defined($systimeF) && defined($xmerrF)) {
-		$fill_gaps = 1;
-	} else {
-		print(STDERR "\n\n") if ($opt_v > 1);
-		print(STDERR "WARNING: timeY and/or modError missing from $CNVfile -- cannot correct for CTD modulo errors\n");
-    }	
+	unless ($opt_m) {												# set up correction for dropped scans
+		$systimeF = &fnrNoErr('timeY');
+		$xmerrF  = &fnrNoErr('modError');
+		if (defined($systimeF) && defined($xmerrF)) {
+			$fill_gaps = 1;
+		} else {
+			print(STDERR "\n\n") if ($opt_v > 1);
+			print(STDERR "WARNING: timeY and/or modError missing from $CNVfile -- cannot correct for CTD modulo errors\n");
+	    }
+	}
 	
 	$latF = &fnrNoErr('lat');										# GPS data if available (to make files useful for u/v processing)
 	$lonF = &fnrNoErr('lon');
@@ -248,7 +263,7 @@
 #----------------------------------------------------------------------
 
 if ($fill_gaps) {
-	print(STDERR "Filling CTD time-series gaps...") if ($opt_v);
+	print(STDERR "Correcting for CTD modulo errors and clock jitter/drift...") if ($opt_v);
 
 	my($scans_filled) = 0;
 	my($scans_replaced) = 0;
@@ -257,15 +272,19 @@
 	for (my($scan)=30; $scan<@ants_; $scan++) {										# start a bit more than 1 second into the cast
 		next if ($ants_[$scan][$systimeF] == $ants_[$scan-1][$systimeF]);			# skip forward to next systime second
 
-#		while ($ants_[$scan][$systimeF] > $ants_[$scan-1][$systimeF]+1) {			# gap spans full second
-#			my(@splicescan);														# scan to splice in 
-#			$splicescan[$systimeF] = $ants_[$scan-1][$systimeF] + 1;
-#			$splicescan[$xmerrF] = $ants_[$scan-1][$xmerrF];
-#			for (my($i)=0; $i<24; $i++) {
-#				splice(@ants_,$scan,0,\@splicescan);
-#				$scans_filled++;
-#			}
-#		}
+		if ($ants_[$scan][$systimeF] > $ants_[$scan-1][$systimeF]+1) {				# gap spans at least one full second
+			print(STDERR "\n\n") if ($opt_v > 1);
+			printf(STDERR "WARNING: %d full seconds of data missing at elapsed %d s\n",
+				$ants_[$scan][$systimeF]-$ants_[$scan-1][$systimeF]-1,
+				$ants_[$scan-1][$systimeF]-$ants_[0][$systimeF]);
+			my(@splicescan);														# scan to splice in 
+			$splicescan[$systimeF] = $ants_[$scan-1][$systimeF] + 1;
+			$splicescan[$xmerrF] = $ants_[$scan-1][$xmerrF];
+			for (my($i)=0; $i<24; $i++) {
+				splice(@ants_,$scan,0,\@splicescan);
+				$scans_filled++;
+			}
+	    }
 			
 		my($second_start) = $scan - 1;												# backtrack to beginning of second
 		my($scans_this_sec) = 1;
@@ -337,11 +356,13 @@
 #	my($sts) = 1;
 #	for (my($scan)=1; $scan<@ants_; $scan++) {
 #		$sts++,next if ($ants_[$scan][$systimeF] == $ants_[$scan-1][$systimeF]);
+#		printf(STDERR "%s seconds of data missing after scan %d\n",
+#			$ants_[$scan][$systimeF]-$ants_[$scan-1][$systimeF]-1,$scan-1)
+#				if ($ants_[$scan][$systimeF]-$ants_[$scan-1][$systimeF]-1 != 0);
 #		printf(STDERR "sts = $sts beginning at scan %d (st = $ants_[$scan-1][$systimeF])\n",$scan-$sts)
 #			unless ($sts == 24);
 #		$sts = 1;
 #	}
-		
 
 	&antsAddParams('gaps_scans_filled',$scans_filled,
 				   'scans_removed',$scans_deleted,
@@ -656,6 +677,42 @@
 printf(STDERR "\n") if ($opt_v);
 
 #----------------------------------------------------------------------
+# Fill Missing Data With Linear Interpolation
+#----------------------------------------------------------------------
+
+if ($opt_f) {
+	print(STDERR "Filling CTD time-series data gaps...") if ($opt_v);
+
+	for (my($f)=0; $f<@antsLayout; $f++) {
+		&ISInit($f,-1);
+	}
+
+	my($interpolated) = 0;
+	my($not_interpolated) = 0;
+
+	for (my($s)=0; $s<@ants_; $s++) {
+		for (my($f)=0; $f<@antsLayout; $f++) {
+			if (numberp($ants_[$s][$f])) {
+				$lastValid[$f] = $f;
+				next;
+			}
+			$not_interpolated++,next unless defined($lastValid[$f]);
+			my($iv) = interpolate(-1,100,$f,$lastValid[$f],$s);
+			$not_interpolated++,next unless numberp($iv);
+			$ants_[$s][$f] = $iv;
+			$interpolated++;
+		}
+	}
+
+	if ($opt_v > 1) {
+		print(STDERR "\n\t$interpolated values interpolated");
+		print(STDERR "\n\t$not_interpolated non-interpolatable missing values left");
+	}
+
+	printf(STDERR "\n") if ($opt_v);
+} # if ($opt_f 
+
+#----------------------------------------------------------------------
 # Binning data
 #----------------------------------------------------------------------
 
@@ -708,6 +765,7 @@
 	$depth[$r] = &depth($press[$r],$lat);
 #	print(STDERR "depth[$r] = depth($press[$r],$lat) = $depth[$r]\n");
 	$salin[$r] = &salin($cond[$r],$temp[$r],$press[$r]);
+	$salin[$r] -= $opt_b;
 	$sspd[$r]  = &sVel($salin[$r],$temp[$r],$press[$r]);
 	$min_sspd = $sspd[$r] if ($sspd[$r] < $min_sspd);
 	$max_sspd = $sspd[$r] if ($sspd[$r] > $max_sspd);
--- a/LADCP_w_ocean	Tue Oct 29 13:16:36 2019 -0400
+++ b/LADCP_w_ocean	Mon Mar 08 12:07:26 2021 -0500
@@ -2,9 +2,9 @@
 #======================================================================
 #                    L A D C P _ W _ O C E A N 
 #                    doc: Fri Dec 17 18:11:13 2010
-#                    dlm: Sun Apr 21 14:16:03 2019
+#                    dlm: Mon Jun 29 11:03:22 2020
 #                    (c) 2010 A.M. Thurnherr
-#                    uE-Info: 298 81 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 306 76 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 # TODO:
@@ -296,6 +296,14 @@
 #	Apr 12, 2019: - added logfile error message for non-existing file
 #	Apr 18, 2019: - added addtional RDI coordinate transformation %PARAMs
 #	Apr 21, 2019: - BUG: removed duplicate warning about lacking water-depth info
+#	Sep 10, 2019: - BUG: -i was erroneously listed as the initial CTD time offset
+#	Sep 12, 2019: - modified .prof output layout to be consistent with [LADCP_w_postproc]
+#	May 15, 2020: - BUG: re-binning on -r did not work for v12 and v34
+#				  - BUG: before re-binning, upcast v12 and v34 were actuall from DC data
+#				  - BUG: all beam-pair velocity stats were contaminated by missing values
+#				  - made bin residuals per beam pair
+#	May 16, 2020: - BUG: write_residual_profs() clobbered layout
+#	Jun 22, 2020: - sscorr_w renamed to remove conflict with [RDI_Coords.pl]
 # HISTORY END
 
 # CTD REQUIREMENTS
@@ -399,7 +407,7 @@
 	"[max -r)esidual <rms.max[,delta.max][$opt_r m/s]>]",
 	"[-h water <depth|filename>]",
 	"[max LADCP time-series -g)ap <length[$opt_g s]>]",
-	"[-i)nitial CTD time offset <guestimate> [-u)se as final]]",
+	"[-i)nitial LADCP time offset <guestimate> [-u)se as final]]",
 	"[calculate -n) <lags,lags[$opt_n]>] [lag -w)indow <sz,sz[$opt_w s]>] [lag-p)iece <CTD_elapsed_min|+[,...]>]",
 	"[require top-3) lags to account for <frac[$opt_3]> of all]",
 	"[disable time-l)ag filtering]",
@@ -1513,20 +1521,20 @@
 		$min_depth = $bindepth[$bin] if ($bindepth[$bin] < $min_depth);
 		$max_depth = $bindepth[$bin] if ($bindepth[$bin] > $max_depth);
 		$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin] =
-			sscorr_w($LADCP{ENSEMBLE}[$ens]->{W}[$bin],
+			sscorr_LADCP_w($LADCP{ENSEMBLE}[$ens]->{W}[$bin],
 					 $CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
 					 $LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND},
 					 $LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH},
 					 $bindepth[$bin]);
 		$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin] =
-			sscorr_w($LADCP{ENSEMBLE}[$ens]->{W12}[$bin],
+			sscorr_LADCP_w($LADCP{ENSEMBLE}[$ens]->{W12}[$bin],
 					 $CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
 					 $LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND},
 					 $LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH},
 					 $bindepth[$bin])
 				if numberp($LADCP{ENSEMBLE}[$ens]->{W12}[$bin]);
 		$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin] =
-			sscorr_w($LADCP{ENSEMBLE}[$ens]->{W34}[$bin],
+			sscorr_LADCP_w($LADCP{ENSEMBLE}[$ens]->{W34}[$bin],
 					 $CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
 					 $LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND},
 					 $LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH},
@@ -1534,17 +1542,23 @@
 				if numberp($LADCP{ENSEMBLE}[$ens]->{W34}[$bin]);
 		$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_W}[$bin] =
 			$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin] + $CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}];
-		$LADCP{ENSEMBLE}[$ens]->{V12}[$bin] *= $CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]/$LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND};
-		$LADCP{ENSEMBLE}[$ens]->{V34}[$bin] *= $CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]/$LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND};
+		$LADCP{ENSEMBLE}[$ens]->{V12}[$bin] *= $CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]/$LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND}
+			if defined($LADCP{ENSEMBLE}[$ens]->{V12}[$bin]);
+		$LADCP{ENSEMBLE}[$ens]->{V34}[$bin] *= $CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]/$LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND}
+			if defined($LADCP{ENSEMBLE}[$ens]->{V34}[$bin]);
 		my($bi) = $bindepth[$bin]/$opt_o;
 		push(@{$DNCAST{ENSEMBLE}[$bi]},$ens);
 		push(@{$DNCAST{ELAPSED}[$bi]},$CTD{ELAPSED}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]);
 		push(@{$DNCAST{DEPTH}[$bi]},$bindepth[$bin]);
 		push(@{$DNCAST{W}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin]);
-		push(@{$DNCAST{W12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin]);
-		push(@{$DNCAST{W34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin]);
-		push(@{$DNCAST{V12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V12}[$bin]);
-		push(@{$DNCAST{V34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V34}[$bin]);
+		push(@{$DNCAST{W12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin])
+			if defined($LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin]);
+		push(@{$DNCAST{W34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin])
+			if defined($LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin]);
+		push(@{$DNCAST{V12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V12}[$bin])
+			if defined($LADCP{ENSEMBLE}[$ens]->{V12}[$bin]);
+		push(@{$DNCAST{V34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V34}[$bin])
+			if defined($LADCP{ENSEMBLE}[$ens]->{V34}[$bin]);
 		push(@{$DNCAST{PITCH}[$bi]},$LADCP{ENSEMBLE}[$ens]->{GIMBAL_PITCH});
 		push(@{$DNCAST{ROLL}[$bi]},$LADCP{ENSEMBLE}[$ens]->{ROLL});
 	}
@@ -1576,20 +1590,20 @@
 		$min_depth = $bindepth[$bin] if ($bindepth[$bin] < $min_depth);
 		$max_depth = $bindepth[$bin] if ($bindepth[$bin] > $max_depth);
 		$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin] =
-			sscorr_w($LADCP{ENSEMBLE}[$ens]->{W}[$bin],
+			sscorr_LADCP_w($LADCP{ENSEMBLE}[$ens]->{W}[$bin],
 					 $CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
 					 $LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND},
 					 $LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH},
 					 $bindepth[$bin]);
 		$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin] =
-			sscorr_w($LADCP{ENSEMBLE}[$ens]->{W12}[$bin],
+			sscorr_LADCP_w($LADCP{ENSEMBLE}[$ens]->{W12}[$bin],
 					 $CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
 					 $LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND},
 					 $LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH},
 					 $bindepth[$bin])
 				if numberp($LADCP{ENSEMBLE}[$ens]->{W12}[$bin]);
 		$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin] =
-			sscorr_w($LADCP{ENSEMBLE}[$ens]->{W34}[$bin],
+			sscorr_LADCP_w($LADCP{ENSEMBLE}[$ens]->{W34}[$bin],
 					 $CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}],
 					 $LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND},
 					 $LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH},
@@ -1597,17 +1611,23 @@
 				if numberp($LADCP{ENSEMBLE}[$ens]->{W34}[$bin]);
 		$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_W}[$bin] =
 			$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin] + $CTD{W}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}];
-		$LADCP{ENSEMBLE}[$ens]->{V12}[$bin] *= $CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]/$LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND};
-		$LADCP{ENSEMBLE}[$ens]->{V34}[$bin] *= $CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]/$LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND};
+		$LADCP{ENSEMBLE}[$ens]->{V12}[$bin] *= $CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]/$LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND}
+			if defined($LADCP{ENSEMBLE}[$ens]->{V12}[$bin]);
+		$LADCP{ENSEMBLE}[$ens]->{V34}[$bin] *= $CTD{SVEL}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]/$LADCP{ENSEMBLE}[$ens]->{SPEED_OF_SOUND}
+			if defined($LADCP{ENSEMBLE}[$ens]->{V34}[$bin]);
 		my($bi) = $bindepth[$bin]/$opt_o;
 		push(@{$UPCAST{ENSEMBLE}[$bi]},$ens);
 		push(@{$UPCAST{ELAPSED}[$bi]},$CTD{ELAPSED}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]);
 		push(@{$UPCAST{DEPTH}[$bi]},$bindepth[$bin]);
 		push(@{$UPCAST{W}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin]);
-		push(@{$UPCAST{W12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin]);
-		push(@{$UPCAST{W34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin]);
-		push(@{$UPCAST{V12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V12}[$bin]);
-		push(@{$UPCAST{V34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V34}[$bin]);
+		push(@{$UPCAST{W12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin])
+			if defined($LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin]);
+		push(@{$UPCAST{W34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin])
+			if defined($LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin]);
+		push(@{$UPCAST{V12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V12}[$bin])
+			if defined($LADCP{ENSEMBLE}[$ens]->{V12}[$bin]);
+		push(@{$UPCAST{V34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V34}[$bin])
+			if defined($LADCP{ENSEMBLE}[$ens]->{V34}[$bin]);
 		push(@{$UPCAST{PITCH}[$bi]},$LADCP{ENSEMBLE}[$ens]->{GIMBAL_PITCH});
 		push(@{$UPCAST{ROLL}[$bi]},$LADCP{ENSEMBLE}[$ens]->{ROLL});
 	}
@@ -1619,8 +1639,8 @@
 	$UPCAST{MEDIAN_W}[$bi] 	  	= median(@{$UPCAST{W}[$bi]});
 	$UPCAST{MEDIAN_W12}[$bi]  	= median(@{$UPCAST{W12}[$bi]});
 	$UPCAST{MEDIAN_W34}[$bi]  	= median(@{$UPCAST{W34}[$bi]});
-	$UPCAST{MEDIAN_V12}[$bi]  	= median(@{$DNCAST{V12}[$bi]});
-	$UPCAST{MEDIAN_V34}[$bi]  	= median(@{$DNCAST{V34}[$bi]});
+	$UPCAST{MEDIAN_V12}[$bi]  	= median(@{$UPCAST{V12}[$bi]});
+	$UPCAST{MEDIAN_V34}[$bi]  	= median(@{$UPCAST{V34}[$bi]});
 	$UPCAST{MEAN_PITCH}[$bi]  	= avg(@{$UPCAST{PITCH}[$bi]});
 	$UPCAST{MEAN_ROLL}[$bi]  	= avg(@{$UPCAST{ROLL}[$bi]});
 	$UPCAST{MAD_W}[$bi]		  	= mad2($UPCAST{MEDIAN_W}[$bi],@{$UPCAST{W}[$bi]});
@@ -1751,8 +1771,16 @@
 			push(@{$DNCAST{ELAPSED}[$bi]},$CTD{ELAPSED}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]);
 			push(@{$DNCAST{DEPTH}[$bi]},$bindepth[$bin]);
 			push(@{$DNCAST{W}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin]);
-			push(@{$DNCAST{W12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin]);
-			push(@{$DNCAST{W34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin]);
+			push(@{$DNCAST{W12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin])
+				if defined($LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin]);
+			push(@{$DNCAST{W34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin])
+				if defined($LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin]);
+			push(@{$DNCAST{V12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V12}[$bin])
+				if defined($LADCP{ENSEMBLE}[$ens]->{V12}[$bin]);
+			push(@{$DNCAST{V34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V34}[$bin])
+				if defined($LADCP{ENSEMBLE}[$ens]->{V34}[$bin]);
+			push(@{$DNCAST{PITCH}[$bi]},$LADCP{ENSEMBLE}[$ens]->{GIMBAL_PITCH});
+			push(@{$DNCAST{ROLL}[$bi]},$LADCP{ENSEMBLE}[$ens]->{ROLL});
 		}
 	}
 	for (my($bi)=0; $bi<=$#{$DNCAST{ENSEMBLE}}; $bi++) {							# bin data into profile
@@ -1761,6 +1789,10 @@
 		$DNCAST{MEDIAN_W}[$bi]		= median(@{$DNCAST{W}[$bi]});
 		$DNCAST{MEDIAN_W12}[$bi]	= median(@{$DNCAST{W12}[$bi]});
 		$DNCAST{MEDIAN_W34}[$bi]	= median(@{$DNCAST{W34}[$bi]});
+		$DNCAST{MEDIAN_V12}[$bi]  	= median(@{$DNCAST{V12}[$bi]});
+		$DNCAST{MEDIAN_V34}[$bi]  	= median(@{$DNCAST{V34}[$bi]});
+		$DNCAST{MEAN_PITCH}[$bi]  	= avg(@{$DNCAST{PITCH}[$bi]});
+		$DNCAST{MEAN_ROLL}[$bi]  	= avg(@{$DNCAST{ROLL}[$bi]});
 		$DNCAST{MAD_W}[$bi] 		= mad2($DNCAST{MEDIAN_W}[$bi],@{$DNCAST{W}[$bi]});
 		$DNCAST{N_SAMP}[$bi]		= @{$DNCAST{W}[$bi]};
 	}
@@ -1778,8 +1810,16 @@
 			push(@{$UPCAST{ELAPSED}[$bi]},$CTD{ELAPSED}[$LADCP{ENSEMBLE}[$ens]->{CTD_SCAN}]);
 			push(@{$UPCAST{DEPTH}[$bi]},$bindepth[$bin]);
 			push(@{$UPCAST{W}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin]);
-			push(@{$UPCAST{W12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin]);
-			push(@{$UPCAST{W34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin]);
+			push(@{$UPCAST{W12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin])
+				if defined($LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W12}[$bin]);
+			push(@{$UPCAST{W34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin])
+				if defined($LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W34}[$bin]);
+			push(@{$UPCAST{V12}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V12}[$bin])
+				if defined($LADCP{ENSEMBLE}[$ens]->{V12}[$bin]);
+			push(@{$UPCAST{V34}[$bi]},$LADCP{ENSEMBLE}[$ens]->{V34}[$bin])
+				if defined($LADCP{ENSEMBLE}[$ens]->{V34}[$bin]);
+			push(@{$UPCAST{PITCH}[$bi]},$LADCP{ENSEMBLE}[$ens]->{GIMBAL_PITCH});
+			push(@{$UPCAST{ROLL}[$bi]},$LADCP{ENSEMBLE}[$ens]->{ROLL});
 		}
 	}
 	for (my($bi)=0; $bi<=$#{$UPCAST{ENSEMBLE}}; $bi++) {
@@ -1788,6 +1828,10 @@
 		$UPCAST{MEDIAN_W}[$bi]		= median(@{$UPCAST{W}[$bi]});
 		$UPCAST{MEDIAN_W12}[$bi]	= median(@{$UPCAST{W12}[$bi]});
 		$UPCAST{MEDIAN_W34}[$bi]	= median(@{$UPCAST{W34}[$bi]});
+		$UPCAST{MEDIAN_V12}[$bi]  	= median(@{$UPCAST{V12}[$bi]});
+		$UPCAST{MEDIAN_V34}[$bi]  	= median(@{$UPCAST{V34}[$bi]});
+		$UPCAST{MEAN_PITCH}[$bi]  	= avg(@{$UPCAST{PITCH}[$bi]});
+		$UPCAST{MEAN_ROLL}[$bi]  	= avg(@{$UPCAST{ROLL}[$bi]});
 		$UPCAST{MAD_W}[$bi] 		= mad2($UPCAST{MEDIAN_W}[$bi],@{$UPCAST{W}[$bi]});
 		$UPCAST{N_SAMP}[$bi]		= @{$UPCAST{W}[$bi]};
 	}
@@ -1851,18 +1895,23 @@
 # Calculate and Output ADCP-Bin-Averaged Residuals
 #-------------------------------------------------
 
-if (@out_BR) {
+#if (@out_BR) {
 	progress("Binning vertical-velocity residuals wrt. ADCP bin number...");
 
-	local(@dc_bres,@uc_bres);
+	local(@dc_bres12,@uc_bres12,@dc_bres34,@uc_bres34);
 	for ($ens=$firstGoodEns; $ens<$LADCP_atbottom; $ens++) {						# DOWNCAST
 		next unless numberp($LADCP{ENSEMBLE}[$ens]->{CTD_DEPTH});
 		my(@bindepth) = binDepths($ens);
 		for ($bin=$LADCP_firstBin-1; $bin<=$LADCP_lastBin-1; $bin++) {
 			next unless numberp($LADCP{ENSEMBLE}[$ens]->{W}[$bin]);
-			push(@{$dc_bres[$bin]},
+			push(@{$dc_bres12[$bin]},
 					$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin]
-					- $DNCAST{MEDIAN_W}[$bindepth[$bin]/$opt_o]);
+					- $DNCAST{MEDIAN_W12}[$bindepth[$bin]/$opt_o])
+						if numberp($DNCAST{MEDIAN_W12}[$bindepth[$bin]/$opt_o]);
+			push(@{$dc_bres34[$bin]},
+					$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin]
+					- $DNCAST{MEDIAN_W34}[$bindepth[$bin]/$opt_o])
+						if numberp($DNCAST{MEDIAN_W34}[$bindepth[$bin]/$opt_o]);
 		}
 	}
 	for ($ens=$LADCP_atbottom; $ens<=$lastGoodEns; $ens++) {						# UPCAST 
@@ -1870,60 +1919,106 @@
 		my(@bindepth) = binDepths($ens);
 		for ($bin=$LADCP_firstBin-1; $bin<=$LADCP_lastBin-1; $bin++) {
 			next unless numberp($LADCP{ENSEMBLE}[$ens]->{W}[$bin]);
-			push(@{$uc_bres[$bin]},
+			push(@{$uc_bres12[$bin]},
 					$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin]
-					- $UPCAST{MEDIAN_W}[$bindepth[$bin]/$opt_o]);
+					- $UPCAST{MEDIAN_W12}[$bindepth[$bin]/$opt_o])
+						if numberp($UPCAST{MEDIAN_W12}[$bindepth[$bin]/$opt_o]);
+			push(@{$uc_bres34[$bin]},
+					$LADCP{ENSEMBLE}[$ens]->{SSCORRECTED_OCEAN_W}[$bin]
+					- $UPCAST{MEDIAN_W34}[$bindepth[$bin]/$opt_o])
+						if numberp($UPCAST{MEDIAN_W34}[$bindepth[$bin]/$opt_o]);
 		}
 	}
-	local(@dc_avg_bres,@uc_avg_bres,@dc_avg_bres_nsamp,@uc_avg_bres_nsamp);			# calc means/stddevs
+	local(@dc_avg_bres12,@uc_avg_bres12,@dc_avg_bres12_nsamp,@uc_avg_bres12_nsamp);	# means/stddevs
+	local(@dc_avg_bres34,@uc_avg_bres34,@dc_avg_bres34_nsamp,@uc_avg_bres34_nsamp);	
 	for (my($bin)=$LADCP_firstBin-1; $bin<=$LADCP_lastBin-1; $bin++) {		
-		$dc_bres_nsamp[$bin] = @{$dc_bres[$bin]};
-		if ($dc_bres_nsamp[$bin] > 0) {
-			$dc_avg_bres[$bin] = avg(@{$dc_bres[$bin]}); 
-			$dc_sig_bres[$bin] = stddev2($dc_avg_bres[$bin],@{$dc_bres[$bin]});
+		$dc_bres12_nsamp[$bin] = @{$dc_bres12[$bin]};
+		if ($dc_bres12_nsamp[$bin] > 0) {
+			$dc_avg_bres12[$bin] = avg(@{$dc_bres12[$bin]}); 
+			$dc_sig_bres12[$bin] = stddev2($dc_avg_bres12[$bin],@{$dc_bres12[$bin]});
+		} else {
+			$dc_avg_bres12[$bin] = nan;
+			$dc_sig_bres12[$bin] = nan;
+		}
+		$uc_bres12_nsamp[$bin] = @{$uc_bres12[$bin]};
+		if ($uc_bres12_nsamp[$bin] > 0) {
+			$uc_avg_bres12[$bin] = avg(@{$uc_bres12[$bin]}); 
+			$uc_sig_bres12[$bin] = stddev2($uc_avg_bres12[$bin],@{$uc_bres12[$bin]});
 		} else {
-			$dc_avg_bres[$bin] = nan;
-			$dc_sig_bres[$bin] = nan;
+			$uc_avg_bres12[$bin] = nan;
+			$uc_sig_bres12[$bin] = nan;
 		}
-		$uc_bres_nsamp[$bin] = @{$uc_bres[$bin]};
-		if ($uc_bres_nsamp[$bin] > 0) {
-			$uc_avg_bres[$bin] = avg(@{$uc_bres[$bin]}); 
-			$uc_sig_bres[$bin] = stddev2($uc_avg_bres[$bin],@{$uc_bres[$bin]});
+		$dc_bres34_nsamp[$bin] = @{$dc_bres34[$bin]};
+		if ($dc_bres34_nsamp[$bin] > 0) {
+			$dc_avg_bres34[$bin] = avg(@{$dc_bres34[$bin]}); 
+			$dc_sig_bres34[$bin] = stddev2($dc_avg_bres34[$bin],@{$dc_bres34[$bin]});
 		} else {
-			$uc_avg_bres[$bin] = nan;
-			$uc_sig_bres[$bin] = nan;
+			$dc_avg_bres34[$bin] = nan;
+			$dc_sig_bres34[$bin] = nan;
+		}
+		$uc_bres34_nsamp[$bin] = @{$uc_bres34[$bin]};
+		if ($uc_bres34_nsamp[$bin] > 0) {
+			$uc_avg_bres34[$bin] = avg(@{$uc_bres34[$bin]}); 
+			$uc_sig_bres34[$bin] = stddev2($uc_avg_bres34[$bin],@{$uc_bres34[$bin]});
+		} else {
+			$uc_avg_bres34[$bin] = nan;
+			$uc_sig_bres34[$bin] = nan;
 		}
 	}
 
-	local($uc_bres_max_nsamp,$dc_bres_max_nsamp) = (0,0);							# calc rms in block of well-determined bins
+	local($uc_bres12_max_nsamp,$dc_bres12_max_nsamp) = (0,0);							# calc rms in block of well sampled bins
+	local($uc_bres34_max_nsamp,$dc_bres34_max_nsamp) = (0,0);
 	for (my($bin)=$LADCP_firstBin; $bin<=$LADCP_lastBin-1; $bin++) {				# SKIP 1ST BIN!!!
 		next if ($bin+1<$outGrid_firstBin || $bin+1>$outGrid_lastBin);				# skip bins not included in gridded output
-		$dc_bres_max_nsamp = $dc_bres_nsamp[$bin]									# nsamp in best sampled bin
-			if ($dc_bres_nsamp[$bin] > $dc_bres_max_nsamp);
-		$uc_bres_max_nsamp = $uc_bres_nsamp[$bin]
-			if ($uc_bres_nsamp[$bin] > $uc_bres_max_nsamp);
+		$dc_bres12_max_nsamp = $dc_bres12_nsamp[$bin]									# nsamp in best sampled bin
+			if ($dc_bres12_nsamp[$bin] > $dc_bres12_max_nsamp);
+		$uc_bres12_max_nsamp = $uc_bres12_nsamp[$bin]
+			if ($uc_bres12_nsamp[$bin] > $uc_bres12_max_nsamp);
+		$dc_bres34_max_nsamp = $dc_bres34_nsamp[$bin]									# nsamp in best sampled bin
+			if ($dc_bres34_nsamp[$bin] > $dc_bres34_max_nsamp);
+		$uc_bres34_max_nsamp = $uc_bres34_nsamp[$bin]
+			if ($uc_bres34_nsamp[$bin] > $uc_bres34_max_nsamp);
 	}
-	my($dc_sumsq,$uc_sumsq,$dc_n,$uc_n) = (0,0,0,0);								# calc rms residual
+	my($dc_sumsq12,$uc_sumsq12,$dc_n12,$uc_n12) = (0,0,0,0);						# calc rms residuals
+	my($dc_sumsq34,$uc_sumsq34,$dc_n34,$uc_n34) = (0,0,0,0);
 	for (my($bin)=$LADCP_firstBin; $bin<=$LADCP_lastBin-1; $bin++) {				# SKIP 1ST BIN
 		next if ($bin+1<$outGrid_firstBin || $bin+1>$outGrid_lastBin);				# skip bins not included in gridded output
-		if ($dc_bres_nsamp[$bin] >= $dc_bres_max_nsamp/3) {							# skip bins with < 1/3 max(nsamp)
-			$dc_sumsq += $dc_avg_bres[$bin]**2;
-			$dc_n++;
+		if ($dc_bres12_nsamp[$bin] >= $dc_bres12_max_nsamp/3) {						# skip bins with < 1/3 max(nsamp)
+			$dc_sumsq12 += $dc_avg_bres12[$bin]**2;
+			$dc_n12++;
+		}
+		if ($uc_bres12_nsamp[$bin] >= $uc_bres12_max_nsamp/3) {
+			$uc_sumsq12 += $uc_avg_bres12[$bin]**2;
+			$uc_n12++;
 		}
-		if ($uc_bres_nsamp[$bin] >= $uc_bres_max_nsamp/3) {
-			$uc_sumsq += $uc_avg_bres[$bin]**2;
-			$uc_n++;
+		if ($dc_bres34_nsamp[$bin] >= $dc_bres34_max_nsamp/3) {						# skip bins with < 1/3 max(nsamp)
+			$dc_sumsq34 += $dc_avg_bres34[$bin]**2;
+			$dc_n34++;
+		}
+		if ($uc_bres34_nsamp[$bin] >= $uc_bres34_max_nsamp/3) {
+			$uc_sumsq34 += $uc_avg_bres34[$bin]**2;
+			$uc_n34++;
 		}
 	}
-	local($dc_bres_rms) = ($dc_n > 0) ? sqrt($dc_sumsq/$dc_n) : nan;
-	local($uc_bres_rms) = ($uc_n > 0) ? sqrt($uc_sumsq/$uc_n) : nan;
+	$dc_bres12_rms = ($dc_n12 > 0) ? sqrt($dc_sumsq12/$dc_n12) : nan;
+	$uc_bres12_rms = ($uc_n12 > 0) ? sqrt($uc_sumsq12/$uc_n12) : nan;
+	$dc_bres34_rms = ($dc_n34 > 0) ? sqrt($dc_sumsq34/$dc_n34) : nan;
+	$uc_bres34_rms = ($uc_n34 > 0) ? sqrt($uc_sumsq34/$uc_n34) : nan;
+
+	&antsAddParams('dc_bin_residuals12.rms',$dc_bres12_rms,
+				   'dc_bin_residuals34.rms',$dc_bres34_rms,
+				   'uc_bin_residuals12.rms',$uc_bres12_rms,
+				   'uc_bin_residuals34.rms',$uc_bres34_rms);
 	
 	progress("\n\twriting binned residuals to ");
 
 	my($saveParams) = $antsCurParams;
-	@antsNewLayout = ('bin','dc_residual','dc_residual.sig','dc_residual.nsamp',
-						   ,'uc_residual','uc_residual.sig','uc_residual.nsamp');
-	&antsAddParams('BR_max_bin',max(scalar(@dc_bres),scalar(@uc_bres)));
+	@antsNewLayout = ('bin','dc_residual12','dc_residual12.sig','dc_residual12.nsamp',
+						    'uc_residual12','uc_residual12.sig','uc_residual12.nsamp',
+						    'dc_residual34','dc_residual34.sig','dc_residual34.nsamp',
+						    'uc_residual34','uc_residual34.sig','uc_residual34.nsamp');
+	&antsAddParams('BR_max_bin',max(scalar(@dc_bres12),scalar(@uc_bres12),
+									scalar(@dc_bres34),scalar(@uc_bres34)));
 
 	foreach my $of (@out_BR) {
 	    progress("<$of> ");
@@ -1936,15 +2031,17 @@
 	    $of = ">$of" unless ($of =~ /^$|^\s*\|/);
 		open(STDOUT,$of) || error("$of: $!\n");
 		undef($antsActiveHeader) unless ($ANTS_TOOLS_AVAILABLE);
-		for (my($bin)=0; $bin<max(scalar(@dc_bres),scalar(@uc_bres)); $bin++) {
-			&antsOut($bin+1,$dc_avg_bres[$bin],$dc_sig_bres[$bin],$dc_bres_nsamp[$bin],
-							$uc_avg_bres[$bin],$uc_sig_bres[$bin],$uc_bres_nsamp[$bin]);
+		for (my($bin)=0; $bin<max(scalar(@dc_bres12),scalar(@uc_bres12)); $bin++) {
+			&antsOut($bin+1,$dc_avg_bres12[$bin],$dc_sig_bres12[$bin],$dc_bres12_nsamp[$bin],
+							$uc_avg_bres12[$bin],$uc_sig_bres12[$bin],$uc_bres12_nsamp[$bin],
+							$dc_avg_bres34[$bin],$dc_sig_bres34[$bin],$dc_bres34_nsamp[$bin],
+							$uc_avg_bres34[$bin],$uc_sig_bres34[$bin],$uc_bres34_nsamp[$bin]);
 		}
 	    &antsOut('EOF'); open(STDOUT,">&2");
 	}
 	$antsCurParams = $saveParams;
 	progress("\n");
-}
+#}
 
 #--------------------------------------------------
 # Calculate BT-referenced vertical-velocity profile
@@ -1977,11 +2074,6 @@
 
 if (@out_wsamp) {
 	progress("Writing vertical-velocity data to ");
-	@antsNewLayout = ('ensemble','bin','elapsed','depth','CTD_depth','downcast',
-					  'w','w12','w34','v12','v34','residual','residual12','residual34',
-					  'CTD_w','CTD_w_t','CTD_w_tt','LADCP_w','LADCP_reflr_w','winch_w',
-					  'errvel','correlation','echo_amplitude','Sv',
-					  'pitch','roll','tilt','heading','3_beam','svel');
 
 	foreach my $of (@out_wsamp) {
 	    progress("<$of> ");
@@ -1993,6 +2085,12 @@
 			next;
 		}
 			
+		@antsNewLayout = ('ensemble','bin','elapsed','depth','CTD_depth','downcast',
+						  'w','w12','w34','v12','v34','residual','residual12','residual34',
+						  'CTD_w','CTD_w_t','CTD_w_tt','LADCP_w','LADCP_reflr_w','winch_w',
+						  'errvel','correlation','echo_amplitude','Sv',
+	                      'pitch','roll','tilt','heading','3_beam','svel');
+
 	    $of = ">$of" unless ($of =~ /^$|^\s*\|/);
 		open(STDOUT,$of) || error("$of: $!\n");
 		undef($antsActiveHeader) unless ($ANTS_TOOLS_AVAILABLE);
@@ -2094,9 +2192,13 @@
 
 if (@out_profile) {
 	progress("Writing vertical-velocity profiles to ");
-	@antsNewLayout = ('depth','hab','dc_depth','dc_elapsed','dc_w','dc_w.mad','dc_w.nsamp','dc_w12','dc_w34','dc_v12','dc_v34',
-							  		'uc_depth','uc_elapsed','uc_w','uc_w.mad','uc_w.nsamp','uc_w12','uc_w34','uc_v12','uc_v34',
-	                          		'BT_w','BT_w.mad','BT_w.nsamp');
+	undef(@antsNewLayout);
+	push(@antsNewLayout,'depth','hab',											# consistent with [LADCP_w_postproc]
+						'dc_elapsed','dc_w','dc_w.mad','dc_w.nsamp',
+						'uc_elapsed','uc_w','uc_w.mad','uc_w.nsamp'); 
+	push(@antsNewLayout,'dc_depth','dc_w12','dc_w34','dc_v12','dc_v34',			# additional fields (inconsistent w postproc)
+						'uc_depth','uc_w12','uc_w34','uc_v12','uc_v34',
+	                    'BT_w','BT_w.mad','BT_w.nsamp');
 
 	foreach my $of (@out_profile) {
 	    progress("<$of> ");
@@ -2115,14 +2217,16 @@
 		for (my($bi)=0; $bi<=max($#{$DNCAST{ENSEMBLE}},$#{$UPCAST{ENSEMBLE}},$#{$BT{NSAMP}}); $bi++) {
 			&antsOut(($bi+0.5)*$opt_o,												# nominal depth
 					 defined($water_depth)?$water_depth-($bi+0.5)*$opt_o:nan,		# nominal hab
-					 $DNCAST{MEAN_DEPTH}[$bi],$DNCAST{MEAN_ELAPSED}[$bi],			# dc data
+					 $DNCAST{MEAN_ELAPSED}[$bi],									# dc data consistent w postproc
 					 $DNCAST{N_SAMP}[$bi]>=$opt_k?$DNCAST{MEDIAN_W}[$bi]:nan,
 					 $DNCAST{MAD_W}[$bi],$DNCAST{N_SAMP}[$bi],
+					 $UPCAST{MEAN_ELAPSED}[$bi],									# uc data consistent w postproc
+					 $UPCAST{N_SAMP}[$bi]>=$opt_k?$UPCAST{MEDIAN_W}[$bi]:nan,
+					 $UPCAST{MAD_W}[$bi],$UPCAST{N_SAMP}[$bi],
+					 $DNCAST{MEAN_DEPTH}[$bi],										# remaining dc data
 					 $DNCAST{MEDIAN_W12}[$bi],$DNCAST{MEDIAN_W34}[$bi],
 					 $DNCAST{MEDIAN_V12}[$bi],$DNCAST{MEDIAN_V34}[$bi],
-					 $UPCAST{MEAN_DEPTH}[$bi],$UPCAST{MEAN_ELAPSED}[$bi],			# uc data
-					 $UPCAST{N_SAMP}[$bi]>=$opt_k?$UPCAST{MEDIAN_W}[$bi]:nan,
-					 $UPCAST{MAD_W}[$bi],$UPCAST{N_SAMP}[$bi],
+					 $UPCAST{MEAN_DEPTH}[$bi],										# remaining uc data
 					 $UPCAST{MEDIAN_W12}[$bi],$UPCAST{MEDIAN_W34}[$bi],
 					 $UPCAST{MEDIAN_V12}[$bi],$UPCAST{MEDIAN_V34}[$bi],
 					 $BT{N_SAMP}[$bi]>=$opt_k?$BT{MEDIAN_W}[$bi]:nan,				# BT data
--- a/Makefile	Tue Oct 29 13:16:36 2019 -0400
+++ b/Makefile	Mon Mar 08 12:07:26 2021 -0500
@@ -1,21 +1,9 @@
 #======================================================================
-#                    M A K E F I L E 
+#                    L A D C P _ W _ S O F T W A R E / M A K E F I L E 
 #                    doc: Mon Oct 17 13:29:27 2011
-#                    dlm: Wed Oct 31 10:19:00 2018
+#                    dlm: Wed Jul  1 14:30:06 2020
 #                    (c) 2011 A.M. Thurnherr
-#                    uE-Info: 22 33 NIL 0 0 72 0 2 4 NIL ofnI
-#======================================================================
-
-# GO_SHIP archive target
-
-PROGS 	= LADCP_w_CTD LADCP_w_ocean LADCP_w_postproc LADCP_wspec LADCP_VKE
-LIBS  	= *.pl
-ANTSLIB	= ANTSlib/.[ln]* ANTSlib/* 
-A_TOOLS	= ADCP_tools/RDI*pl
-
-LADCP_w_Software.tgz: ${PROGS} ${LIBS} ${ANTSLIB} ${A_TOOLS}
-	tar cvfz $@ $^
-
+#                    uE-Info: 9 0 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 MAKE_DIR = /Data/Makefiles
--- a/default_output.pl	Tue Oct 29 13:16:36 2019 -0400
+++ b/default_output.pl	Mon Mar 08 12:07:26 2021 -0500
@@ -1,9 +1,9 @@
 #======================================================================
 #                    D E F A U L T _ O U T P U T . P L 
 #                    doc: Wed Jun  1 19:21:19 2016
-#                    dlm: Wed Jun  1 19:26:01 2016
+#                    dlm: Sat May 16 12:19:26 2020
 #                    (c) 2016 A.M. Thurnherr
-#                    uE-Info: 106 1 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 71 0 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 # NOTES:
@@ -12,6 +12,10 @@
 
 # HISTORY:
 #	Jun  1, 2016: - created from [default_paths.pl]
+#	May 15, 2020: - removed a couple of time-depth plots from level 1
+#				  - changed semmantics so that pushing to plot adds,
+#				    instead of overwwriting
+#				  - BUG: one of the plots did not repect plotting level
 
 #----------------------------------------------------------------------
 # Processing log (diagnostic messages) output
@@ -20,7 +24,6 @@
 $out_log = "$log_dir/$out_basename.log"
 	unless defined($out_log);
 
-
 #----------------------------------------------------------------------
 # Vertical-velocity profile output and plots:
 #
@@ -31,11 +34,9 @@
 # 	*_wprof.ps			vertical velocity profiles (main output plot)
 #----------------------------------------------------------------------
 
-unless (@out_profile) {
-	push(@out_profile,"$data_dir/$out_basename.wprof");
-	push(@out_profile,"plot_wprof($plot_dir/${out_basename}_wprof.ps)")
-		if ($plotting_level > 0);
-}
+push(@out_profile,"$data_dir/$out_basename.wprof");
+push(@out_profile,"plot_wprof($plot_dir/${out_basename}_wprof.ps)")
+	if ($plotting_level > 0);
 
 #--------------------------------------------------------------------------------------------------
 # Vertical-velocity sample data output and plots:
@@ -44,34 +45,38 @@
 #	*.wsamp							w sample data
 #	residuals/<prof>/<ens>.rprof	OPTIONAL: per-ensemble residuals
 #						
-# Plots (in $plot_dir):
-#	*_wsamp.ps						1: vertical velocity time-depth plot
-#	*_residuals.ps					1: residual vertical velocity time-depth plot
+# Plots (in $plot_dir):				plotting level: contents
+#	*_wprof.ps						1: summary plot
+#	*_bin_residuals.ps				1: residuals binned wrt bin number
 #	*_residual_profs.ps				1: residuals binned in depth
-#	*_backscatter.ps				1: volume scattering coefficient time-depth plot
-#	*_attitude_res.ps				2: residuals binned wrt. pitch/roll
-#	*_residuals12.ps				2: beampair <1,2> residual vertical velocity time-depth plot
-#	*_residuals34.ps				2: beampair <3,4> residual vertical velocity time-depth plot
-#	*_attitude_res.ps				2: residuals binned wrt. package attitude
-#	*_acceleration_res.ps			2: residuals binned wrt. package acceleration derivative
-#	*_correlation.ps				3: correlation time-depth plot
+#	*_wsamp.ps						2: vertical velocity time-depth plot
+#	*_residuals.ps					2: residual vertical velocity time-depth plot
+#	*_backscatter.ps				2: volume scattering coefficient time-depth plot
+#	*_attitude_res.ps				3: residuals binned wrt. pitch/roll
+#	*_residuals12.ps				3: beampair <1,2> residual vertical velocity time-depth plot
+#	*_residuals34.ps				3: beampair <3,4> residual vertical velocity time-depth plot
+#	*_attitude_res.ps				3: residuals binned wrt. package attitude
+#	*_acceleration_res.ps			3: residuals binned wrt. package acceleration derivative
+#	*_correlation.ps				4: correlation time-depth plot
 #--------------------------------------------------------------------------------------------------
 
-unless (@out_wsamp) {
-	push(@out_wsamp,"$data_dir/$out_basename.wsamp");
-	#push(@out_wsamp,sprintf('dump_residual_profiles(%s/residuals/%03d)',$data_dir,$PROF));
+push(@out_wsamp,"$data_dir/$out_basename.wsamp");
 
-	if ($plotting_level > 0) {
+#push(@out_wsamp,sprintf('dump_residual_profiles(%s/residuals/%03d)',$data_dir,$PROF));
+#push(@out_wsamp,"write_residual_profs('$data_dir/$out_basename.resprof')");
+
+if ($plotting_level > 0) {
+	push(@out_wsamp,"plot_residual_profs($plot_dir/${out_basename}_residual_profs.ps)");
+	if ($plotting_level > 1) {
 		push(@out_wsamp,"plot_wsamp($plot_dir/${out_basename}_wsamp.ps)");
 		push(@out_wsamp,"plot_residuals($plot_dir/${out_basename}_residuals.ps)");
-		push(@out_wsamp,"plot_residual_profs($plot_dir/${out_basename}_residual_profs.ps)");
 		push(@out_wsamp,"plot_backscatter($plot_dir/${out_basename}_backscatter.ps)");
-		if ($plotting_level > 1) {
+		if ($plotting_level > 2) {
 			push(@out_wsamp,"plot_residuals12($plot_dir/${out_basename}_residuals12.ps)");
 			push(@out_wsamp,"plot_residuals34($plot_dir/${out_basename}_residuals34.ps)");
 			push(@out_wsamp,"plot_attitude_residuals($plot_dir/${out_basename}_attitude_res.ps)");
 			push(@out_wsamp,"plot_acceleration_residuals($plot_dir/${out_basename}_acceleration_res.ps)");
-			if ($plotting_level > 2) {
+			if ($plotting_level > 3) {
 				push(@out_wsamp,"plot_correlation($plot_dir/${out_basename}_correlation.ps)");
 			}
 		}
@@ -85,16 +90,14 @@
 #					package- and LADCP reference layer w
 #----------------------------------------------------------------------
 
-push(@out_timeseries,"$data_dir/$out_basename.tis")
-	unless (@out_timeseries);
+push(@out_timeseries,"$data_dir/$out_basename.tis");
 
 #----------------------------------------------------------------------
 # Per-bin vertical-velocity residuals (plot only)
 #----------------------------------------------------------------------
 
 push(@out_BR,"plot_mean_residuals($plot_dir/${out_basename}_bin_residuals.ps)")
-	unless (@out_BR);
-
+	if ($plotting_level > 0);
 
 #----------------------------------------------------------------------
 # Time-lagging correlation statistics (plot only)
--- a/defaults.pl	Tue Oct 29 13:16:36 2019 -0400
+++ b/defaults.pl	Mon Mar 08 12:07:26 2021 -0500
@@ -1,9 +1,9 @@
 #======================================================================
 #                    D E F A U L T S . P L 
 #                    doc: Tue Oct 11 17:11:21 2011
-#                    dlm: Wed Apr 17 11:30:31 2019
+#                    dlm: Sat May 16 10:53:34 2020
 #                    (c) 2011 A.M. Thurnherr
-#                    uE-Info: 343 41 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 90 31 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 # HISTORY:
@@ -87,6 +87,7 @@
 #	Apr 24, 2018: - added $water_depth_db_cmd
 #	May  2, 2018: - added max_hspeed
 #				  - replaced $PPI_seabed_editing_required by &PPI_seabed_editing_required
+#	May 16, 2020: - updated doc
 
 #======================================================================
 # Output Log Files
@@ -104,16 +105,16 @@
 
 #======================================================================
 # Output Plots
-#	- there are 3 plotting levels
+#	- there are 4 plotting levels
 #		0 : suppress all plots
 #		1* : produce default plots; *DEFAULT
 #		2 : produce default and diagnostic plots
-#		>2: produce all plots, including useless ones
+#		3 : produce debugging diagnostic plots
+#		>3: produce all plots, including useless ones
 #======================================================================
 
 $plotting_level = 1;
 
-
 #======================================================================
 # Input Data 
 #======================================================================
--- a/plot_mean_residuals.pl	Tue Oct 29 13:16:36 2019 -0400
+++ b/plot_mean_residuals.pl	Mon Mar 08 12:07:26 2021 -0500
@@ -1,9 +1,9 @@
 #======================================================================
 #                    P L O T _ M E A N _ R E S I D U A L S . P L 
 #                    doc: Tue Jul 28 13:21:09 2015
-#                    dlm: Wed May 18 12:55:09 2016
+#                    dlm: Fri May 15 19:06:51 2020
 #                    (c) 2015 A.M. Thurnherr
-#                    uE-Info: 19 0 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 122 47 NIL 0 0 72 2 2 4 NIL ofnI
 #======================================================================
 
 # HISTORY:
@@ -16,6 +16,9 @@
 #	Jan 25, 2016: - added return on no data
 #	Mar 16, 2016: - adapted to gmt5
 #   May 18, 2016: - added version
+#	May 15, 2020: - adapted to bin-residuals separate per beam pair
+#				  - added orange range
+#				  - slightly relaxed green range
 
 require "$ANTS/libGMT.pl";
 
@@ -52,33 +55,59 @@
 	GMT_psxy('-W1');																# plot zero line
 	printf(GMT "0 $ymin\n0 $ymax\n");
 
-	GMT_psxy('-W2,coral');
-		for (my($bin)=$LADCP_firstBin; $bin<@dc_bres; $bin++) {						# SKIP FIRST BIN
+	GMT_psxy('-W2,coral,8_2:0');
+		for (my($bin)=$LADCP_firstBin; $bin<@dc_bres12; $bin++) {			
 			next if ($bin+1<$outGrid_firstBin || $bin+1>$outGrid_lastBin);
-			next unless ($dc_bres_nsamp[$bin] >= $dc_bres_max_nsamp/3);
-			printf(GMT "%f %d\n",$dc_avg_bres[$bin],$bin+1);
+			next unless ($dc_bres12_nsamp[$bin] >= $dc_bres12_max_nsamp/3);
+			printf(GMT "%f %d\n",$dc_avg_bres12[$bin],$bin+1);
+        }
+	GMT_psxy('-W2,coral,1_1:0');
+		for (my($bin)=$LADCP_firstBin; $bin<@dc_bres34; $bin++) {			
+			next if ($bin+1<$outGrid_firstBin || $bin+1>$outGrid_lastBin);
+			next unless ($dc_bres34_nsamp[$bin] >= $dc_bres34_max_nsamp/3);
+			printf(GMT "%f %d\n",$dc_avg_bres34[$bin],$bin+1);
         }
 	GMT_psxy('-Ex0.2/2,coral');
-		for (my($bin)=$LADCP_firstBin-1; $bin<@dc_bres; $bin++) {
+		for (my($bin)=$LADCP_firstBin-1; $bin<@dc_bres12; $bin++) {
 			printf(GMT "%f %d %f\n",
-							$dc_avg_bres[$bin],
+							$dc_avg_bres12[$bin],
+							$bin+1,
+							($dc_bres12_nsamp[$bin] > 1) ?
+								$dc_sig_bres12[$bin]/sqrt($dc_bres12_nsamp[$bin]-1) : 0);
+		}
+		for (my($bin)=$LADCP_firstBin-1; $bin<@dc_bres34; $bin++) {
+			printf(GMT "%f %d %f\n",
+							$dc_avg_bres34[$bin],
 							$bin+1,
-							($dc_bres_nsamp[$bin] > 1) ?
-								$dc_sig_bres[$bin]/sqrt($dc_bres_nsamp[$bin]-1) : 0);
+							($dc_bres34_nsamp[$bin] > 1) ?
+								$dc_sig_bres34[$bin]/sqrt($dc_bres34_nsamp[$bin]-1) : 0);
         }
-	GMT_psxy('-W2,SeaGreen');
-		for (my($bin)=$LADCP_firstBin; $bin<@uc_bres; $bin++) {						# SKIP FIRST BIN
+	GMT_psxy('-W2,SeaGreen,8_2:0');
+		for (my($bin)=$LADCP_firstBin; $bin<@uc_bres12; $bin++) {			
 			next if ($bin+1<$outGrid_firstBin || $bin+1>$outGrid_lastBin);
-			next unless ($uc_bres_nsamp[$bin] >= $uc_bres_max_nsamp/3);
-			printf(GMT "%f %d\n",$uc_avg_bres[$bin],$bin+1);
+			next unless ($uc_bres12_nsamp[$bin] >= $uc_bres12_max_nsamp/3);
+			printf(GMT "%f %d\n",$uc_avg_bres12[$bin],$bin+1);
+        }
+	GMT_psxy('-W2,SeaGreen,1_1:0');
+		for (my($bin)=$LADCP_firstBin; $bin<@uc_bres34; $bin++) {			
+			next if ($bin+1<$outGrid_firstBin || $bin+1>$outGrid_lastBin);
+			next unless ($uc_bres34_nsamp[$bin] >= $uc_bres34_max_nsamp/3);
+			printf(GMT "%f %d\n",$uc_avg_bres34[$bin],$bin+1);
         }
 	GMT_psxy('-Ex0.2/2,SeaGreen');
-		for (my($bin)=$LADCP_firstBin-1; $bin<@uc_bres; $bin++) {
+		for (my($bin)=$LADCP_firstBin-1; $bin<@uc_bres12; $bin++) {
 			printf(GMT "%f %d %f\n",
-							$uc_avg_bres[$bin],
+							$uc_avg_bres12[$bin],
 							$bin+1,
-							($uc_bres_nsamp[$bin] > 1) ?
-								$uc_sig_bres[$bin]/sqrt($uc_bres_nsamp[$bin]-1) : 0);
+							($uc_bres12_nsamp[$bin] > 1) ?
+								$uc_sig_bres12[$bin]/sqrt($uc_bres12_nsamp[$bin]-1) : 0);
+		}
+		for (my($bin)=$LADCP_firstBin-1; $bin<@uc_bres34; $bin++) {
+			printf(GMT "%f %d %f\n",
+							$uc_avg_bres34[$bin],
+							$bin+1,
+							($uc_bres34_nsamp[$bin] > 1) ?
+								$uc_sig_bres34[$bin]/sqrt($uc_bres34_nsamp[$bin]-1) : 0);
         }
 
 	GMT_unitcoords();																	# LABELS
@@ -88,19 +117,37 @@
 	GMT_pstext('-F+f14,Helvetica,blue+jBL -N');											# profile id
 		print(GMT "0.0 -0.03 $P{out_basename} $P{run_label}\n");
 
+	GMT_pstext('-F+f12,Helvetica-Bold,black+jBL -Gwhite'); 							# rms residuals
+		print(GMT "0.1 0.88 beams <1,2>\n");
+	GMT_pstext('-F+f12,Helvetica-Bold,black+jBL -Gwhite');
+		print(GMT "0.7 0.88 beams <3,4>\n");
+
 	GMT_pstext('-F+f12,Helvetica,coral+jBL');											# rms residuals
 		print(GMT "0.01 0.93 dc\n");
-	if ($dc_bres_rms >= 0.005) { 		GMT_pstext('-F+f12,Helvetica,white+jBL -Gred'); }
-	elsif ($dc_bres_rms >= 0.001) { 	GMT_pstext('-F+f12,Helvetica,black+jBL -Gyellow'); }
+	if ($dc_bres12_rms >= 0.005) { 		GMT_pstext('-F+f12,Helvetica,white+jBL -Gred'); }
+	elsif ($dc_bres12_rms >= 0.003) { 	GMT_pstext('-F+f12,Helvetica,black+jBL -Gorange'); }
+	elsif ($dc_bres12_rms >= 0.0015) { 	GMT_pstext('-F+f12,Helvetica,black+jBL -Gyellow'); }
 	else {								GMT_pstext('-F+f12,Helvetica,black+jBL -Ggreen'); }
-		printf(GMT "0.10 0.93 %.1f mm/s rms\n",1000*$dc_bres_rms);
+		printf(GMT "0.10 0.93 %.1f mm/s rms\n",1000*$dc_bres12_rms);
+
+	if ($dc_bres34_rms >= 0.005) { 		GMT_pstext('-F+f12,Helvetica,white+jBL -Gred'); }
+	elsif ($dc_bres34_rms >= 0.003) { 	GMT_pstext('-F+f12,Helvetica,black+jBL -Gorange'); }
+	elsif ($dc_bres34_rms >= 0.0015) { 	GMT_pstext('-F+f12,Helvetica,black+jBL -Gyellow'); }
+	else {								GMT_pstext('-F+f12,Helvetica,black+jBL -Ggreen'); }
+		printf(GMT "0.70 0.93 %.1f mm/s rms\n",1000*$dc_bres34_rms);
 
 	GMT_pstext('-F+f12,Helvetica,SeaGreen+jBL');
 		print(GMT "0.01 0.98 uc\n");
-	if ($uc_bres_rms >= 0.005) { 		GMT_pstext('-F+f12,Helvetica,white+jBL -Gred'); }
-	elsif ($uc_bres_rms >= 0.001) { 	GMT_pstext('-F+f12,Helvetica,black+jBL -Gyellow'); }
+	if ($uc_bres12_rms >= 0.005) { 		GMT_pstext('-F+f12,Helvetica,white+jBL -Gred'); }
+	elsif ($uc_bres12_rms >= 0.003) { 	GMT_pstext('-F+f12,Helvetica,black+jBL -Gorange'); }
+	elsif ($uc_bres12_rms >= 0.0015) { 	GMT_pstext('-F+f12,Helvetica,black+jBL -Gyellow'); }
 	else {								GMT_pstext('-F+f12,Helvetica,black+jBL -Ggreen'); }
-		printf(GMT "0.10 0.98 %.1f mm/s rms\n",1000*$uc_bres_rms);
+		printf(GMT "0.10 0.98 %.1f mm/s rms\n",1000*$uc_bres12_rms);
+	if ($uc_bres34_rms >= 0.005) { 		GMT_pstext('-F+f12,Helvetica,white+jBL -Gred'); }
+	elsif ($uc_bres34_rms >= 0.003) { 	GMT_pstext('-F+f12,Helvetica,black+jBL -Gorange'); }
+	elsif ($uc_bres34_rms >= 0.0015) { 	GMT_pstext('-F+f12,Helvetica,black+jBL -Gyellow'); }
+	else {								GMT_pstext('-F+f12,Helvetica,black+jBL -Ggreen'); }
+		printf(GMT "0.70 0.98 %.1f mm/s rms\n",1000*$uc_bres34_rms);
 
 	my($bin_tics) = ($ymax <= 20) ? 'f1a1' : 'f1a2';
 	GMT_setR($R);																		# FINISH PLOT
--- a/plot_wprof.pl	Tue Oct 29 13:16:36 2019 -0400
+++ b/plot_wprof.pl	Mon Mar 08 12:07:26 2021 -0500
@@ -1,9 +1,9 @@
 #======================================================================
 #                    P L O T _ W P R O F . P L 
 #                    doc: Sun Jul 26 11:08:50 2015
-#                    dlm: Tue Mar 20 15:26:21 2018
+#                    dlm: Sat May 23 11:11:42 2020
 #                    (c) 2015 A.M. Thurnherr
-#                    uE-Info: 22 53 NIL 0 0 72 2 2 4 NIL ofnI
+#                    uE-Info: 89 108 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 # HISTORY:
@@ -20,6 +20,8 @@
 #	May 26, 2016: - added instrument coord system to plot labels
 #	Mar 20, 2018: - BUG: units of vertical package acceleration were wrong
 #				  - added blue background for likely in-ice package accelerations
+#	May 16, 2020: - added residual profile data to background
+#	May 23, 2020: - BUG: windows without samples made program bomb
 
 # Tweakables:
 #
@@ -70,6 +72,47 @@
     }
 }
 
+sub plotRes()
+{
+	my($last_depth,$dc_sumsq_res,$dc_n,$uc_sumsq_res,$uc_n);
+	for (my($bi)=0; $bi<=$#{$DNCAST{MEDIAN_W}}; $bi++) {
+		my($depth) = ($bi+0.5) * $opt_o;
+		if ($depth > $last_depth+100 || $bi == $#{$DNCAST{MEDIAN_W}}) {
+			if ($dc_n==0 || sqrt($dc_sumsq_res/$dc_n) > 0.002) {
+				my($green) = $dc_n ? round(100*max(0.01-max(sqrt($dc_sumsq_res/$dc_n)-0.002,0),0) * 255) : 0;
+				GMT_psxy("-Gp300/12:F255/$green/${green}B-");
+				printf(GMT "%g %g\n%g %g\n%g %g\n%g %g\n",
+								-0.1,$last_depth,0,$last_depth,
+								0,$depth,-0.1,$depth);
+			}
+			if ($uc_n==0 || sqrt($uc_sumsq_res/$uc_n) > 0.002) {
+				my($green) = $uc_n ? round(100*max(0.01-max(sqrt($uc_sumsq_res/$uc_n)-0.002,0),0) * 255) : 0;
+				GMT_psxy("-Gp300/9:F255/$green/${green}B-");
+				printf(GMT "%g %g\n%g %g\n%g %g\n%g %g\n",
+								0,$last_depth,0.07,$last_depth,
+								0.07,$depth,0,$depth);
+			}
+			$dc_sumsq_res = $dc_n = $uc_sumsq_res = $uc_n = 0;
+			$last_depth = $depth;
+		}
+		if (numberp($DNCAST{MEAN_RESIDUAL12}[$bi])) {
+			$dc_sumsq_res += $DNCAST{MEAN_RESIDUAL12}[$bi]**2;
+			$dc_n++;
+		}
+		if (numberp($UPCAST{MEAN_RESIDUAL12}[$bi])) {
+			$uc_sumsq_res += $UPCAST{MEAN_RESIDUAL12}[$bi]**2;
+			$uc_n++;
+		}
+		if (numberp($DNCAST{MEAN_RESIDUAL34}[$bi])) {
+			$dc_sumsq_res += $DNCAST{MEAN_RESIDUAL34}[$bi]**2;
+			$dc_n++;
+		}
+		if (numberp($UPCAST{MEAN_RESIDUAL34}[$bi])) {
+			$uc_sumsq_res += $UPCAST{MEAN_RESIDUAL34}[$bi]**2;
+			$uc_n++;
+		}
+	}
+}
 
 sub plot_wprof($)
 {
@@ -93,11 +136,12 @@
 
 	if ($P{water_depth} > 0) {															# SEABED
 		GMT_psxy('-G204/153/102');
-		print(GMT "$plot_wprof_xmin $plot_wprof_ymax\n0.35 $plot_wprof_ymax\n0.35 $P{water_depth}\n $plot_wprof_xmin $P{water_depth}\n");
+		print(GMT "$plot_wprof_xmin $plot_wprof_ymax\n0.07 $plot_wprof_ymax\n0.07 $P{water_depth}\n $plot_wprof_xmin $P{water_depth}\n");
 	}
 
-	setR1();																			# FRAME
-	GMT_psxy('-W0.5');
+	setR1();	
+	plotRes();																			# RESIDUAL PROFILES
+	GMT_psxy('-W0.5');																	# FRAME
 		print(GMT "0 0\n 0 $plot_wprof_ymax\n");
 	setR2();
 	GMT_psxy('-W0.5');
@@ -112,37 +156,82 @@
 	GMT_psxy('-W1,SeaGreen,1_1:0'); 	plotUC('MEDIAN_W34',$opt_k);
 	GMT_psxy('-W1,black');				plotBT('MEDIAN_W',$opt_k);
 
-	GMT_psxy('-Sc0.1c -Gcoral');		plotDC('MAD_W',0);								# MEAN ABSOLUTE DEVIATIONS
-	GMT_psxy('-Sc0.1c -GSeaGreen');		plotUC('MAD_W',0);	
-	GMT_psxy('-Sc0.1c -Gblack');		plotBT('MAD_W',0);	
+	GMT_psxy('-Sc0.1c -Gcoral');		plotDC('MAD_W',1);								# MEAN ABSOLUTE DEVIATIONS
+	GMT_psxy('-Sc0.1c -GSeaGreen');		plotUC('MAD_W',1);	
+	GMT_psxy('-Sc0.1c -Gblack');		plotBT('MAD_W',1);	
 
 	setR2();																			# SAMPLES
 	GMT_psxy('-W0.7,coral');			plotDC('N_SAMP',1);
 	GMT_psxy('-W0.7,SeaGreen');			plotUC('N_SAMP',1);	
-	GMT_psxy('-W0.7,black');			plotBT('N_SAMP',1);	
+	GMT_psxy('-W0.7,black');			plotBT('N_SAMP',1);
+
+	GMT_unitcoords();																	# QUALITY SEMAPHORE
+	GMT_psxy('-Ggray90');
+	print(GMT "0.895 0.895\n0.985 0.895\n0.985 0.985\n0.895 0.985\n");
+	if ($dc_bres12_rms >= 0.005) { 		GMT_psxy('-Gred -N'); }
+	elsif ($dc_bres12_rms >= 0.003) { 	GMT_psxy('-Gorange -N'); }
+	elsif ($dc_bres12_rms >= 0.0015) { 	GMT_psxy('-Gyellow -N'); }
+	else {								GMT_psxy('-Ggreen -N'); }
+		print(GMT "0.90 0.90\n0.935 0.90\n0.935 0.935\n");							
+	if ($dc_bres34_rms >= 0.005) { 		GMT_psxy('-Gred -N'); }
+	elsif ($dc_bres34_rms >= 0.003) { 	GMT_psxy('-Gorange -N'); }
+	elsif ($dc_bres34_rms >= 0.0015) { 	GMT_psxy('-Gyellow -N'); }
+	else {								GMT_psxy('-Ggreen -N'); }
+		print(GMT "0.945 0.90\n0.98 0.90\n0.945 0.935\n");							
+	if ($uc_bres12_rms >= 0.005) { 		GMT_psxy('-Gred -N'); }
+	elsif ($uc_bres12_rms >= 0.003) { 	GMT_psxy('-Gorange -N'); }
+	elsif ($uc_bres12_rms >= 0.0015) { 	GMT_psxy('-Gyellow -N'); }
+	else {								GMT_psxy('-Ggreen -N'); }
+		print(GMT "0.90 0.98\n0.935 0.98\n0.935 0.945\n");							
+	if ($uc_bres34_rms >= 0.005) { 		GMT_psxy('-Gred -N'); }
+	elsif ($uc_bres34_rms >= 0.003) { 	GMT_psxy('-Gorange -N'); }
+	elsif ($uc_bres34_rms >= 0.0015) { 	GMT_psxy('-Gyellow -N'); }
+	else {								GMT_psxy('-Ggreen -N'); }
+		print(GMT "0.945 0.98\n0.98 0.98\n0.945 0.945\n");							
+if (0) {
+	if ($dc_bres12_rms >= 0.005) { 		GMT_psxy('-Gred -N -Sc0.3'); }
+	elsif ($dc_bres12_rms >= 0.003) { 	GMT_psxy('-Gorange -N -Sc0.3'); }
+	elsif ($dc_bres12_rms >= 0.0015) { 	GMT_psxy('-Gyellow -N -Sc0.3'); }
+	else {								GMT_psxy('-Ggreen -N -Sc0.3'); }
+		print(GMT "0.92 0.96\n");							
+	if ($dc_bres34_rms >= 0.005) { 		GMT_psxy('-Gred -N -Sc0.3'); }
+	elsif ($dc_bres34_rms >= 0.003) { 	GMT_psxy('-Gorange -N -Sc0.3'); }
+	elsif ($dc_bres34_rms >= 0.0015) { 	GMT_psxy('-Gyellow -N -Sc0.3'); }
+	else {								GMT_psxy('-Ggreen -N -Sc0.3'); }
+		print(GMT "0.96 0.96\n");							
+	if ($uc_bres12_rms >= 0.005) { 		GMT_psxy('-Gred -N -Sc0.3'); }
+	elsif ($uc_bres12_rms >= 0.003) { 	GMT_psxy('-Gorange -N -Sc0.3'); }
+	elsif ($uc_bres12_rms >= 0.0015) { 	GMT_psxy('-Gyellow -N -Sc0.3'); }
+	else {								GMT_psxy('-Ggreen -N -Sc0.3'); }
+		print(GMT "0.92 0.92\n");							
+	if ($uc_bres34_rms >= 0.005) { 		GMT_psxy('-Gred -N -Sc0.3'); }
+	elsif ($uc_bres34_rms >= 0.003) { 	GMT_psxy('-Gorange -N -Sc0.3'); }
+	elsif ($uc_bres34_rms >= 0.0015) { 	GMT_psxy('-Gyellow -N -Sc0.3'); }
+	else {								GMT_psxy('-Ggreen -N -Sc0.3'); }
+		print(GMT "0.96 0.92\n");
+} 		
 	
-	GMT_unitcoords();																	# LABELS
-	GMT_pstext('-F+f14,Helvetica,blue+jTL -N');
+	GMT_pstext('-F+f14,Helvetica,blue+jTL -N');											# LABELS
 		print(GMT "0.01 -0.06 $P{out_basename} [$P{run_label}]\n");
-	GMT_pstext('-F+f12,Helvetica+jBR');
-		print(GMT "0.6 0.98 m.a.d.\n");
+	GMT_pstext('-F+f12,Helvetica+jTR');
+		print(GMT "0.61 0.02 m.abs.dev.\n");
 	GMT_pstext('-F -N');
 		print(GMT "0.32 1.12 Vertical Velocity [m/s]\n");
-	GMT_pstext('-F+f9,Helvetica,orange+jTR -N -Gwhite');
+	GMT_pstext('-F+f9,Helvetica,LightSkyBlue+jTR -N -Gwhite');
 		print(GMT "0.99 0.01 V$VERSION\n");
 
 	GMT_pstext('-F+f12,Helvetica,coral+jTL -Gwhite');
-		print(GMT "0.02 0.01 dc\n");
+		print(GMT "0.02 0.02 downcast\n");
 	GMT_pstext('-F+f12,Helvetica,SeaGreen+jTL -Gwhite');
-		print(GMT "0.02 0.06 uc\n");
+		print(GMT "0.24 0.02 upcast\n");
 	if ($have_BT) {
-		GMT_pstext('-F+f12,Helvetica,black+jTL -Gwhite');
-			print(GMT "0.02 0.10 BT\n");
+		GMT_pstext('-F+f12,Helvetica,black+jBL -Gwhite');
+			print(GMT "0.02 0.98 b.track\n");
 	}
 
 	GMT_pstext('-F+f9,Helvetica,CornFlowerBlue+jTL -N');
-		printf(GMT "0.64 1.020 $LADCP{BEAM_FREQUENCY}kHz $LADCP{INSTRUMENT_TYPE} $P{ADCP_orientation}\n");
-#		printf(GMT "0.64 1.055 %s\n		0.77 1.055 : %.1fm/%1.fm/%1.fm\n",
+		printf(GMT "0.64 1.020 %d kHz $LADCP{INSTRUMENT_TYPE} $P{ADCP_orientation}\n",
+				round($LADCP{BEAM_FREQUENCY},100));
 		printf(GMT "0.64 1.055 %s [%.1fm/%1.fm/%1.fm]\n",
 			$LADCP{BEAM_COORDINATES} ? 'beam vels' : 'Earth vels',
 			$LADCP{BLANKING_DISTANCE},$LADCP{TRANSMITTED_PULSE_LENGTH},$LADCP{BIN_LENGTH});
--- a/svel_corrections.pl	Tue Oct 29 13:16:36 2019 -0400
+++ b/svel_corrections.pl	Mon Mar 08 12:07:26 2021 -0500
@@ -1,9 +1,9 @@
 #======================================================================
 #                    S V E L _ C O R R E C T I O N S . P L 
 #                    doc: Thu Dec 30 01:35:18 2010
-#                    dlm: Tue May 24 16:44:07 2016
+#                    dlm: Mon Jun 29 11:02:08 2020
 #                    (c) 2010 A.M. Thurnherr
-#                    uE-Info: 116 64 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 22 74 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 # HISTORY:
@@ -19,6 +19,7 @@
 #				  - made sscorr_w return nan on undef'd input vel
 #   May 24, 2016: - calc_binDepths() -> binDepths()
 #				  - added caching to binDepths
+#	Jun 29, 2020: - renamed sscorr_w to remove conflict with RDI_Coords.pl
 
 # NOTES:
 #	In an effort to track down the scale bias, NBP0901 stn 160 was reprocessed with various
@@ -27,7 +28,7 @@
 #		2) simplified (dBin/dADCP): very similar to full correction, esp. when dBin is used
 #		3) hacked correction (1450m/s vs 1500m/s assumed soundspeed) => bias largely disappears
 
-sub sscorr_w($$$$$)												# sound-speed correction for w
+sub sscorr_LADCP_w($$$$$)										# sound-speed correction for w
 {																# see RDI Coord. Trans. manual sec. 4.1, ...
 	my($wObs,$wCTD,$ssADCP,$dADCP,$dBin) = @_;					# but there is an error: the ^2 applies to the []
 	return nan unless numberp($wObs);
--- a/version.pl	Tue Oct 29 13:16:36 2019 -0400
+++ b/version.pl	Mon Mar 08 12:07:26 2021 -0500
@@ -1,9 +1,9 @@
 #======================================================================
 #                    V E R S I O N . P L 
 #                    doc: Tue Oct 13 10:40:57 2015
-#                    dlm: Tue Nov 27 13:15:48 2018
+#                    dlm: Thu Sep 12 12:27:38 2019
 #                    (c) 2015 A.M. Thurnherr
-#                    uE-Info: 35 29 NIL 0 0 72 0 2 4 NIL ofnI
+#                    uE-Info: 36 29 NIL 0 0 72 0 2 4 NIL ofnI
 #======================================================================
 
 # HISTORY:
@@ -25,11 +25,13 @@
 #				  - added 1; to the end
 #	Nov 27, 2018: - updated ANTS lib to V7.1
 #			      - updated ADCP tools to V2.2
+#	Sep 12, 2019: - updated to V1.5 because of CTD gap correction
 
 #$VERSION = '1.1';				# Jan  4, 2016
 #$VERSION = '1.2';				# May 12, 2016
 #$VERSION = '1.3';				# Mar 15, 2017
-$VERSION = '1.4';				# Nov 28, 2017
+#$VERSION = '1.4';				# Nov 28, 2017
+$VERSION = '1.5';				# Sep 12, 2018
 
 $antsMinLibVersion 		= 7.1;
 $ADCP_tools_minVersion 	= 2.2;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/write_residual_profs.pl	Mon Mar 08 12:07:26 2021 -0500
@@ -0,0 +1,37 @@
+#======================================================================
+#                    W R I T E _ R E S I D U A L _ P R O F S . P L 
+#                    doc: Fri May 15 20:22:54 2020
+#                    dlm: Fri May 15 20:56:06 2020
+#                    (c) 2020 A.M. Thurnherr
+#                    uE-Info: 21 53 NIL 0 0 72 2 2 4 NIL ofnI
+#======================================================================
+
+# HISTORY:
+#   May 25, 2020: - created from [plot_residual_profs.pl]
+
+sub write_residual_profs($)
+{
+	my($ofn) = @_;
+
+	@antsNewLayout = ('depth','dc_residual12','dc_residual34','uc_residual12','uc_residual34');
+
+	open(STDOUT,">$ofn") || error("$ofn: $!\n");
+	undef($antsActiveHeader) unless ($ANTS_TOOLS_AVAILABLE);
+
+	for (my($bi)=0; $bi<=$#{$DNCAST{MEAN_RESIDUAL12}}; $bi++) {
+		my(@out);
+		push(@out,($bi+0.5) * $opt_o);									# depth
+		push(@out,(numberp($DNCAST{MEAN_RESIDUAL12}[$bi]) && $DNCAST{N_SAMP}[$bi]>=$minsamp) ?
+				  	$DNCAST{MEAN_RESIDUAL12}[$bi] : nan);
+		push(@out,(numberp($DNCAST{MEAN_RESIDUAL34}[$bi]) && $DNCAST{N_SAMP}[$bi]>=$minsamp) ?
+				  	$DNCAST{MEAN_RESIDUAL34}[$bi] : nan);
+		push(@out,(numberp($UPCAST{MEAN_RESIDUAL12}[$bi]) && $UPCAST{N_SAMP}[$bi]>=$minsamp) ?
+				  	$UPCAST{MEAN_RESIDUAL12}[$bi] : nan);
+		push(@out,(numberp($UPCAST{MEAN_RESIDUAL34}[$bi]) && $UPCAST{N_SAMP}[$bi]>=$minsamp) ?
+				  	$UPCAST{MEAN_RESIDUAL34}[$bi] : nan);
+		&antsOut(@out);
+	}
+    &antsOut('EOF'); open(STDOUT,'>&2');
+}
+
+1; # return true on require