Oct_11_2011/find_seabed.pl
author A.M. Thurnherr <ant@ldeo.columbia.edu>
Sun, 05 Apr 2015 22:34:59 -0400
changeset 21 cd001648ccba
parent 3 9c021fdea1ff
permissions -rw-r--r--
.prof layout change re-implemented
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     1
#======================================================================
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     2
#                    F I N D _ S E A B E D . P L 
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     3
#                    doc: Sun May 23 20:26:11 2010
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     4
#                    dlm: Thu Dec 30 21:45:02 2010
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     5
#                    (c) 2010 A.M. Thurnherr
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     6
#                    uE-Info: 12 0 NIL 0 0 72 0 2 4 NIL ofnI
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     7
#======================================================================
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     8
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
     9
# HISTORY:
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    10
#	May 23, 2010: - adapted from [perl-tools/RDI_Utils.pl]
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    11
#	Dec 25, 2010: - adapted to changes in [LADCP_w]
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    12
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    13
# NOTES:
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    14
#	1) BT range is corrected for sound speed at the transducer. This is not
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    15
#	   accurate, but unlikely to be very wrong, at least for deep casts, 
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    16
#	   because the vertical sound speed variability near the seabed tends
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    17
#	   to be small. The seabed depth is only used for sidelobe editing,
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    18
#	   which is done with a generous safety margin (from the UH shear
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    19
#	   method implementation).
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    20
#	2) Acquisition sound speed of 1500m/s assumed.
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    21
#	3) To be reasonably accurate, DEPTH must be from the CTD at this stage.
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    22
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    23
#======================================================================
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    24
# (seabed median depth, mad) = find_seabed(dta ptr, btm ensno, coord flag)
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    25
#======================================================================
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    26
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    27
my($search_width) = 200;	# # of ensembles around bottom to search
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    28
my($mode_width) = 10;		# max range of bottom around mode
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    29
my($min_dist) = 20;			# min dist to seabed for good data
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    30
my($z_offset) = 10000;		# shift z to ensure +ve array indices
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    31
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    32
sub find_seabed($$$)
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    33
{
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    34
	my($d,$be,$beamCoords) = @_;
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    35
	my($i,$dd,$sd,$nd);
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    36
	my(@guesses);
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    37
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    38
	return undef unless ($be-$search_width >= 0 &&
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    39
						 $be+$search_width <= $#{$d->{ENSEMBLE}});
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    40
	for ($i=$be-$search_width; $i<=$be+$search_width; $i++) {
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    41
		next unless (defined($d->{ENSEMBLE}[$i]->{CTD_DEPTH}) &&
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    42
					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[0]) &&
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    43
					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[1]) &&
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    44
					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[2]) &&
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    45
					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[3]));
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    46
		my(@BT) = $beamCoords
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    47
				? velInstrumentToEarth($d,$i,
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    48
					velBeamToInstrument($d,
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    49
						@{$d->{ENSEMBLE}[$i]->{BT_VELOCITY}}))
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    50
				: velApplyHdgBias($d,$i,@{$d->{ENSEMBLE}[$i]->{BT_VELOCITY}});
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    51
		next unless (abs($BT[3]) < 0.05);
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    52
		$d->{ENSEMBLE}[$i]->{DEPTH_BT} =
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    53
			 $d->{ENSEMBLE}[$i]->{BT_RANGE}[0]/4 +
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    54
			 $d->{ENSEMBLE}[$i]->{BT_RANGE}[1]/4 +
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    55
 			 $d->{ENSEMBLE}[$i]->{BT_RANGE}[2]/4 +
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    56
			 $d->{ENSEMBLE}[$i]->{BT_RANGE}[3]/4;
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    57
		$d->{ENSEMBLE}[$i]->{DEPTH_BT} *= cos(rad($d->{BEAM_ANGLE}));
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    58
		$d->{ENSEMBLE}[$i]->{DEPTH_BT} *= $CTD{SVEL}[$d->{ENSEMBLE}[$i]->{CTD_SCAN}]/1500;
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    59
		next unless ($d->{ENSEMBLE}[$i]->{DEPTH_BT} >= $min_dist);
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    60
		$d->{ENSEMBLE}[$i]->{DEPTH_BT} *= -1
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    61
			if ($d->{ENSEMBLE}[$i]->{XDUCER_FACING_UP});
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    62
		$d->{ENSEMBLE}[$i]->{DEPTH_BT} += $d->{ENSEMBLE}[$i]->{CTD_DEPTH};
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    63
		if ($d->{ENSEMBLE}[$i]->{DEPTH_BT} > $d->{ENSEMBLE}[$be]->{CTD_DEPTH}) {
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    64
			$guesses[int($d->{ENSEMBLE}[$i]->{DEPTH_BT})+$z_offset]++;
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    65
			$nd++;
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    66
		} else {
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    67
			undef($d->{ENSEMBLE}[$i]->{DEPTH_BT});
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    68
		}
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    69
	}
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    70
	return undef unless ($nd>5);
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    71
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    72
	my($mode,$nmax);
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    73
	for ($i=0; $i<=$#guesses; $i++) {			# find mode
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    74
		$nmax=$guesses[$i],$mode=$i-$z_offset
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    75
			if ($guesses[$i] > $nmax);
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    76
	}
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    77
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    78
	$nd = 0;
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    79
	for ($i=$be-$search_width; $i<=$be+$search_width; $i++) {
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    80
		next unless defined($d->{ENSEMBLE}[$i]->{DEPTH_BT});
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    81
		if (abs($d->{ENSEMBLE}[$i]->{DEPTH_BT}-$mode) <= $mode_width) {
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    82
			$dd += $d->{ENSEMBLE}[$i]->{DEPTH_BT};
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    83
			$nd++;
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    84
		} else {
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    85
			undef($d->{ENSEMBLE}[$i]->{DEPTH_BT});
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    86
		}
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    87
	}
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    88
	return undef unless ($nd >= 2);
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    89
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    90
	$dd /= $nd;
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    91
	for ($i=$be-$search_width; $i<=$be+$search_width; $i++) {
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    92
		next unless defined($d->{ENSEMBLE}[$i]->{DEPTH_BT});
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    93
		$sd += ($d->{ENSEMBLE}[$i]->{DEPTH_BT}-$dd)**2;
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    94
	}
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    95
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    96
	return ($dd, sqrt($sd/($nd-1)));
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    97
}
9c021fdea1ff Before replacing command-line options by default variables.
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
diff changeset
    98