RDI_Utils.pl
author A.M. Thurnherr <athurnherr@yahoo.com>
Tue, 29 Jun 2021 12:21:32 -0400
changeset 59 4f4530fa35da
parent 52 5b07a9b89aee
permissions -rw-r--r--
V2.4 - New Features: - support for Nortek PD0 files - patchPD0 support for moored ADCP data - bug fixes & minor improvements
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     1
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     2
#                    R D I _ U T I L S . P L 
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     3
#                    doc: Wed Feb 12 10:21:32 2003
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 49
diff changeset
     4
#                    dlm: Mon Apr 13 14:07:54 2020
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     5
#                    (c) 2003 A.M. Thurnherr
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 49
diff changeset
     6
#                    uE-Info: 68 53 NIL 0 0 72 0 2 4 NIL ofnI
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     7
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     8
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     9
# miscellaneous RDI-specific utilities
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    10
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    11
# History:
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    12
#	Feb 12, 2003: - created
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    13
#	Feb 14, 2003: - added check for short (bad) data files
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    14
#	Feb 26, 2004: - added Earth-coordinate support
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    15
#				  - added ensure_BT_RANGE()
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    16
#	Mar 17, 2004: - set bad BT ranges to undef in ensure_BT_RANGE
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    17
#				  - calc mean/stddev in ensure_BT_RANGE
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    18
#	Mar 20, 2004: - BUG: find_seabed() could bomb when not enough
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    19
#					bottom guesses passed the mode_width test
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    20
#	Mar 30, 2004: - added &soundSpeed()
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    21
#	Nov  8, 2005: - WATER_DEPTH => Z_BT
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    22
#	Dec  1, 2005: - renamed to RDI_Utils.pl
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    23
#				  - folded in mk_prof from [mkProfile]
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    24
#	Nov 30, 2007: - adapted ref_lr_w() to 3-beam solutions
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    25
#	Feb  1, 2008: - added comment
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    26
#	Feb 22, 2008: - added ssCorr()
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    27
#	Apr  9, 2008: - BUG: duplicate line of code (without effect) in find_seabed() 
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    28
#				  - BUG: seabed < max depth was possible
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    29
#	Jan     2010: - fiddled with seabed detection params (no conclusion)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    30
#	May 23, 2010: - renamed Z to DEPTH
2
065ea9ce12fc before DIMES UK2 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
    31
#	Sep 27, 2010: - made sure coord flags are changed correctly when data
065ea9ce12fc before DIMES UK2 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
    32
#					are transferred to earth coords in mk_prof
065ea9ce12fc before DIMES UK2 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
    33
#	Sep 29, 2010: - BUG: previous change was wrong, as ref_lr_w does
065ea9ce12fc before DIMES UK2 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
    34
#						 not overwrite velocities
3
ladcp@ladcp-mac.local
parents: 2
diff changeset
    35
#	Oct 20, 2010: - BUG: w is now not integrated any more across gaps longer than 5s
ladcp@ladcp-mac.local
parents: 2
diff changeset
    36
#	Dec  8, 2010: - changed missing w warning to happen only if gap is longer than 15s
ladcp@ladcp-mac.local
parents: 2
diff changeset
    37
#	Dec 10, 2010: - beautified gap warning
5
29faa9e6226c after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 4
diff changeset
    38
#	Dec 16, 2010: - BUG: gaps at end caused mk_prof to throw away profile
6
603221e51c6f pre IWISE
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 5
diff changeset
    39
#	May 12, 2011: - added code to skip ensembles with built-in-test errors in mk_prof()
603221e51c6f pre IWISE
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 5
diff changeset
    40
#				  - immediately disabled this code becasue it does appear to make matters worse
8
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
    41
#	Sep 21, 2011: - added calculation of RMS heave acceleration
10
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
    42
#	Mar 27, 2013: - BUG: 3-beam solutions were not used in ref_lr_w
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
    43
#				  - disabled apparently unused code
11
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 10
diff changeset
    44
#	Apr 12, 2013: - added $min_pctg as optional parameter to mk_prof
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
    45
#	May 14, 2013: - added incident-velocity, w12 & w34 to mkProfile
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
    46
#	Jun  5, 2013: - BUG: incident-flow warning was printed repeatedly
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
    47
#	Jun 20, 2013: - BUG: warning had used &antsInfo()
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
    48
#	Feb 13, 2014: - replaced {DEPTH_BT} by {seabed}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
    49
#				  - added set_range_lim()
17
591779f6df30 changed mkProfile gap heuristics & removed warning
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    50
#	Feb 22, 2014: - changed gap heuristic
591779f6df30 changed mkProfile gap heuristics & removed warning
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
    51
#			      - Earth coord beam-pair warning removed
19
e23a5fd2923a after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 17
diff changeset
    52
#	May 29, 2014: - removed unused code (disabled warning) from ref_lr_w
23
fb0c269b1eaa V1.2 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 19
diff changeset
    53
#	Mar 22, 2015: - BUG: mk_prof could bomb because of division-by-zero in return statement
31
b6ca27a1d19c pre Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    54
#	Jan  9, 2016: - renamed ref_lr_w to mk_prof_ref_lr_w because the old name conflicts
b6ca27a1d19c pre Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
    55
#				    with a sub in LADCP_w
34
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 31
diff changeset
    56
#   May 19, 2016: - adapted to new velBeamToInstrument() usage
39
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    57
#	Aug  7, 2017: - added abmiguity velocity
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
    58
#	Aug  8, 2017: - changed transducer frequency to kHz
41
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 39
diff changeset
    59
#	Nov 27, 2017: - BUG: profile-restart heuristic did not work with P6#001
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 41
diff changeset
    60
#	Mar 18, 2018: - added -ve dt consistency check
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 41
diff changeset
    61
#	Jun  9, 2018: - added support for ENV{NO_GAP_WARNINGS}
48
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
    62
#	Jun 16, 2019: - adapted (partiall) to support RTI ADCPs:
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
    63
#					- made ambiguity_velocity() return nan (missing TL_distance)
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
    64
#					- skip missing velocities in reference-layer calculation
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
    65
#					- disable %-good screening with $min_pctg == 0
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
    66
# 	Jun 26, 2019: - increased short-gap in mk_prof from 5s to 6s to allow processing
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
    67
#					of RTi test file with 5s ensembles
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 49
diff changeset
    68
#	Apr 13, 2020: - added $RDI_Utils::No_Gap_Warnings
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    69
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    70
use strict;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    71
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 49
diff changeset
    72
$RDI_Utils::No_Gap_Warnings = 0;		# set to 1 to suppress gap warnings
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 49
diff changeset
    73
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    74
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    75
# fake_BT_RANGE(dta ptr)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    76
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    77
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    78
# During cruise NBP0204 it was found that one of Visbeck's RDIs consistently
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    79
# returns zero as the bottom-track range, even thought the BT velocities
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    80
# are good. This functions calculates the ranges if they are missing.
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    81
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    82
sub cBTR($$$)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    83
{
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    84
	my($d,$e,$b) = @_;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    85
	my($maxamp) = -9e99;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    86
	my($maxi);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    87
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    88
	for (my($i)=0; $i<$d->{N_BINS}; $i++) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    89
		next unless ($d->{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$i][$b] > $maxamp);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    90
		$maxamp = $d->{ENSEMBLE}[$e]->{ECHO_AMPLITUDE}[$i][$b];
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    91
		$maxi = $i;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    92
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    93
	$d->{ENSEMBLE}[$e]->{BT_RANGE}[$b] =
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    94
		$d->{DISTANCE_TO_BIN1_CENTER} + $maxi * $d->{BIN_LENGTH};
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    95
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    96
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    97
sub ensure_BT_RANGE($)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    98
{
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    99
	my($d) = @_;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   100
	for (my($e)=0; $e<=$#{$d->{ENSEMBLE}}; $e++) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   101
		my($sum) = my($n) = 0;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   102
		if (defined($d->{ENSEMBLE}[$e]->{BT_VELOCITY}[0])) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   103
			for (my($b)=0; $b<4; $b++) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   104
				cBTR($d,$e,$b)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   105
					unless defined($d->{ENSEMBLE}[$e]->{BT_RANGE}[$b]);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   106
				$sum += $d->{ENSEMBLE}[$e]->{BT_RANGE}[$b]; $n++;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   107
			}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   108
		} else {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   109
			for (my($b)=0; $b<4; $b++) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   110
				$d->{ENSEMBLE}[$e]->{BT_RANGE}[$b] = undef;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   111
			}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   112
		}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   113
		$d->{ENSEMBLE}[$e]->{BT_MEAN_RANGE} = $sum/$n if ($n == 4);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   114
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   115
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   116
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   117
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   118
# (seabed depth, stddev) = find_seabed(dta ptr, btm ensno, coord flag)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   119
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   120
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   121
# NOTE FOR YOYOS:
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   122
#	- this routine only detects the BT around the depeest depth!
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   123
#	- this is on purpose, because it is used for [listBT]
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   124
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   125
# This is a PAIN:
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   126
# 	if the instrument is too close to the bottom, the BT_RANGE
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   127
#	readings are all out; the only solution is to have a sufficiently
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   128
#	large search width (around the max(depth) ensemble) so that
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   129
#	a part of the up (oops, I forgot to finish this comment one year
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   130
#	ago during A0304 and now I don't understand it any more :-)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   131
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   132
my($search_width) = 200;	# # of ensembles around bottom to search
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   133
my($mode_width) = 10;		# max range of bottom around mode
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   134
my($min_dist) = 20;			# min dist to seabed for good data
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   135
my($z_offset) = 10000;		# shift z to ensure +ve array indices
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   136
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   137
sub find_seabed($$$)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   138
{
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   139
	my($d,$be,$beamCoords) = @_;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   140
	my($i,$dd,$sd,$nd);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   141
	my(@guesses);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   142
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   143
	return undef unless ($be-$search_width >= 0 &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   144
						 $be+$search_width <= $#{$d->{ENSEMBLE}});
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   145
	for ($i=$be-$search_width; $i<=$be+$search_width; $i++) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   146
		next unless (defined($d->{ENSEMBLE}[$i]->{DEPTH}) &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   147
					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[0]) &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   148
					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[1]) &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   149
					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[2]) &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   150
					 defined($d->{ENSEMBLE}[$i]->{BT_RANGE}[3]));
34
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 31
diff changeset
   151
		my(@BT) = $beamCoords ? velBeamToEarth($d,$i,@{$d->{ENSEMBLE}[$i]->{BT_VELOCITY}})
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 31
diff changeset
   152
							  : velApplyHdgBias($d,$i,@{$d->{ENSEMBLE}[$i]->{BT_VELOCITY}});
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   153
		next unless (abs($BT[3]) < 0.05);
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   154
		$d->{ENSEMBLE}[$i]->{seabed} =
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   155
			 $d->{ENSEMBLE}[$i]->{BT_RANGE}[0]/4 +
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   156
			 $d->{ENSEMBLE}[$i]->{BT_RANGE}[1]/4 +
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   157
 			 $d->{ENSEMBLE}[$i]->{BT_RANGE}[2]/4 +
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   158
			 $d->{ENSEMBLE}[$i]->{BT_RANGE}[3]/4;
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   159
		next
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   160
			unless ($d->{ENSEMBLE}[$i]->{seabed} >= $min_dist);
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   161
		$d->{ENSEMBLE}[$i]->{seabed} *= -1
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   162
			if ($d->{ENSEMBLE}[$i]->{XDUCER_FACING_UP});
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   163
		$d->{ENSEMBLE}[$i]->{seabed} += $d->{ENSEMBLE}[$i]->{DEPTH};
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   164
		if ($d->{ENSEMBLE}[$i]->{seabed} > $d->{ENSEMBLE}[$be]->{DEPTH}) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   165
			$guesses[int($d->{ENSEMBLE}[$i]->{seabed})+$z_offset]++;
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   166
			$nd++;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   167
		} else {
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   168
			undef($d->{ENSEMBLE}[$i]->{seabed});
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   169
		}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   170
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   171
	return undef unless ($nd>5);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   172
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   173
	my($mode,$nmax);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   174
	for ($i=0; $i<=$#guesses; $i++) {			# find mode
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   175
	$nmax=$guesses[$i],$mode=$i-$z_offset
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   176
			if ($guesses[$i] > $nmax);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   177
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   178
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   179
	$nd = 0;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   180
	for ($i=$be-$search_width; $i<=$be+$search_width; $i++) {
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   181
		next unless defined($d->{ENSEMBLE}[$i]->{seabed});
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   182
		if (abs($d->{ENSEMBLE}[$i]->{seabed}-$mode) <= $mode_width) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   183
			$dd += $d->{ENSEMBLE}[$i]->{seabed};
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   184
			$nd++;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   185
		} else {
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   186
			$d->{ENSEMBLE}[$i]->{seabed} = undef;
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   187
		}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   188
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   189
	return undef unless ($nd >= 2);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   190
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   191
	$dd /= $nd;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   192
	for ($i=$be-$search_width; $i<=$be+$search_width; $i++) {
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   193
		next unless defined($d->{ENSEMBLE}[$i]->{seabed});
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   194
		$sd += ($d->{ENSEMBLE}[$i]->{seabed}-$dd)**2;
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   195
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   196
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   197
	return ($dd, sqrt($sd/($nd-1)));
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   198
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   199
14
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   200
#----------------------------------------------------------------------
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   201
# set_range_lim(d)
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   202
#	- set field range_lim
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   203
#----------------------------------------------------------------------
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   204
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   205
sub set_range_lim($)
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   206
{
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   207
	my($d) = @_;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   208
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   209
	for (my($e)=0; $e<=$#{$d->{ENSEMBLE}}; $e++) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   210
		my($lastGood) = 1; my($b);
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   211
		for ($b=0; $b<$d->{N_BINS}; $b++) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   212
			if (defined($d->{ENSEMBLE}[$e]->{VELOCITY}[$b][0]) &&
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   213
				defined($d->{ENSEMBLE}[$e]->{VELOCITY}[$b][1]) &&
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   214
				defined($d->{ENSEMBLE}[$e]->{VELOCITY}[$b][2]) &&
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   215
				defined($d->{ENSEMBLE}[$e]->{VELOCITY}[$b][3])) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   216
					$lastGood = 1;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   217
			} elsif ($lastGood) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   218
				$lastGood = 0;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   219
			} else {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   220
				last;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   221
			}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   222
	    }
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   223
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   224
		next unless ($b>=2) && defined($d->{ENSEMBLE}[$e]->{DEPTH});
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   225
	    $d->{ENSEMBLE}[$e]->{range_lim} =
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   226
			$d->{DISTANCE_TO_BIN1_CENTER} + ($b-2) * $d->{BIN_LENGTH};
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   227
		$d->{ENSEMBLE}[$e]->{range_lim} *= -1
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   228
			if ($d->{ENSEMBLE}[$e]->{XDUCER_FACING_UP});
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   229
		$d->{ENSEMBLE}[$e]->{range_lim} += $d->{ENSEMBLE}[$e]->{DEPTH};
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   230
	}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   231
}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 12
diff changeset
   232
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   233
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   234
# c = soundSpeed($salin,$temp,$depth)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   235
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   236
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   237
# Taken from the RDI BroadBand primer manual. The reference given there
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   238
# is Urick (1983).
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   239
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   240
sub soundSpeed($$$)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   241
{
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   242
	my($salin,$temp,$depth) = @_;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   243
	die("ERROR: soundSpeed($salin,$temp,$depth): non-numeric parameter\n")
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   244
		unless numberp($salin) && numberp($temp) && numberp($depth);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   245
	return 1449.2 + 4.6*$temp -0.055*$temp**2  + 0.00029*$temp**3 +
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   246
				(1.34 - 0.01*$temp) * ($salin - 35) + 0.016*$depth;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   247
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   248
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   249
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   250
# fac = ssCorr($eRef,$salin,$temp,$depth)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   251
#	$eRef :	reference to current ensemble
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   252
#	$salin: * -> use instrument salinity
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   253
#	$temp : * -> use instrument temperature
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   254
#	$depth: * -> use instrument PRESSURE(!)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   255
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   256
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   257
{ my($warned);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   258
	sub ssCorr($$$$)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   259
	{
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   260
		my($eRef,$S,$T,$D) = @_;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   261
		$S = $eRef->{SALINITY} if ($S eq '*');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   262
		$T = $eRef->{TEMPERATURE} if ($T eq '*');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   263
		if ($D eq '*') {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   264
			print(STDERR "WARNING: soundspeed correction using instrument pressure instead of depth!\n")
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   265
				unless ($warned);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   266
			$warned = 1;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   267
			$D = $eRef->{PRESSURE};
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   268
		}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   269
		return soundSpeed($S,$T,$D) / $eRef->{SPEED_OF_SOUND};
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   270
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   271
}
39
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   272
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   273
#======================================================================
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   274
# ambiguity_velocity(transducer_freq,beam_angle,sound_speed,transmit_lag_dist)
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   275
# 	- recipe provied by Jerry Mullison in August 2017
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   276
#	- transducer_freq in kHz
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   277
#	- sound speed can vary with ensemble
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   278
#======================================================================
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   279
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   280
sub ambiguity_velocity($$$$)
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   281
{
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   282
	my($xd_freq,$beam_angle,$speed_of_sound,$TL_distance) = @_;
47
494a76548e94 before adapting to read RTI PD0 files
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   283
	return 'nan' unless ($TL_distance > 0);
39
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   284
	my($lambda) = $speed_of_sound / (1000*$xd_freq);
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   285
	my($D) = $speed_of_sound * cos(rad($beam_angle)) / 2;
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   286
	return $lambda * $D / (4 * $TL_distance);
3bddaa514ef5 before Hamburg
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 36
diff changeset
   287
}
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   288
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   289
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   290
# ($firstgood,$lastgood,$atbottom,$w_gap_time,$zErr,$maxz) =
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   291
#	mk_prof($dta,$check,$filter,$lr_b0,$lr_b1,$min_corr,$max_e,$max_gap);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   292
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   293
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   294
# calculate reference-layer vertical and incident velocities
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   295
31
b6ca27a1d19c pre Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
   296
sub mk_prof_ref_lr_w($$$$$$$)
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   297
{
11
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 10
diff changeset
   298
	my($dta,$ens,$rl_b0,$rl_b1,$min_corr,$max_e,$min_pctg) = @_;
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   299
	my($i,@n,@bn,@v,@vi,@vel,@veli,@bv,@w);
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   300
	my($w,$e,$nvi,$vi12,$vi43,@vbp,@velbp,@nbp,$w12,$w34,@w12,@w34);
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   301
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   302
	for ($i=$rl_b0; $i<=$rl_b1; $i++) {
48
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
   303
		next unless defined($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i]);
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   304
		undef($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][0])
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   305
			if ($dta->{ENSEMBLE}[$ens]->{CORRELATION}[$i][0] < $min_corr);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   306
		undef($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][1])
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   307
			if ($dta->{ENSEMBLE}[$ens]->{CORRELATION}[$i][1] < $min_corr);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   308
		undef($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][2])
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   309
			if ($dta->{ENSEMBLE}[$ens]->{CORRELATION}[$i][2] < $min_corr);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   310
		undef($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][3])
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   311
			if ($dta->{ENSEMBLE}[$ens]->{CORRELATION}[$i][3] < $min_corr);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   312
		if ($dta->{BEAM_COORDINATES}) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   313
			undef($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][0])
11
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 10
diff changeset
   314
				if ($dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][0] < $min_pctg);
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   315
			undef($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][1])
11
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 10
diff changeset
   316
				if ($dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][1] < $min_pctg);
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   317
			undef($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][2])
11
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 10
diff changeset
   318
				if ($dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][2] < $min_pctg);
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   319
			undef($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][3])
11
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 10
diff changeset
   320
	            if ($dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][3] < $min_pctg);
34
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 31
diff changeset
   321
	        @vi = velBeamToInstrument($dta,$ens,@{$dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i]});
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   322
			@v = velInstrumentToEarth($dta,$ens,@vi);
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   323
			@vbp = velBeamToBPEarth($dta,$ens,@{$dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i]});
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   324
		} else {
48
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
   325
			if ($min_pctg > 0) {
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
   326
				next if ($dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][0] > 0 ||
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
   327
						 $dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][1] > 0 ||
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
   328
						 $dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][2] > 0 ||
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
   329
	                     $dta->{ENSEMBLE}[$ens]->{PERCENT_GOOD}[$i][3] < $min_pctg);
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
   330
            }
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   331
			@v = @{$dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i]};
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   332
		}
10
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   333
		next if (defined($v[3]) && abs($v[3]) > $max_e);		# allow 3-beam solutions
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   334
19
e23a5fd2923a after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 17
diff changeset
   335
		if (defined($v[2])) {									# valid vertical velocity
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   336
			$vel[2] += $v[2]; $n[2]++;							# vertical velocity
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   337
			$vel[3] += $v[3], $n[3]++ if defined($v[3]);		# error velocity
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   338
			push(@w,$v[2]); 									# save for stderr calculation
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   339
		}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   340
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   341
		if (defined($vbp[1])) {									# beam-pair vertical velocities
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   342
			$velbp[0] += $vbp[1]; $nbp[0]++;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   343
			push(@w12,$vbp[1]);
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   344
		}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   345
		if (defined($vbp[3])) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   346
			$velbp[1] += $vbp[3]; $nbp[1]++;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   347
			push(@w34,$vbp[1]);
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   348
		}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   349
		
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   350
		if (defined($vi[0])) { 									# incident velocity
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   351
			$veli[0] += $vi[0];
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   352
			$veli[1] += $vi[1];
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   353
			$nvi++;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   354
		}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   355
10
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   356
#	The following code calculates beam-averaged ref-lr velocities.
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   357
#	I do not recall what this was implemented for. Disabled Mar 27, 2013.
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   358
#
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   359
#		if ($dta->{BEAM_COORDINATES}) {
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   360
#			$bv[0] += $dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][0], $bn[0]++
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   361
#				if defined($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][0]);
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   362
#			$bv[1] += $dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][1], $bn[1]++
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   363
#				if defined($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][1]);
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   364
#			$bv[2] += $dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][2], $bn[2]++
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   365
#				if defined($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][2]);
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   366
#			$bv[3] += $dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][3], $bn[3]++
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   367
#	            if defined($dta->{ENSEMBLE}[$ens]->{VELOCITY}[$i][3]);
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   368
#	    }
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   369
	} # loop over ref-lr bins
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   370
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   371
	$w = ($n[2] > 0) ? $vel[2]/$n[2] : undef;					# calc means
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   372
	$e = ($n[3] > 0) ? $vel[3]/$n[3] : undef;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   373
	if ($nvi > 0) {						
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   374
		$vi12 = $veli[0] / $nvi;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   375
		$vi43 = $veli[1] / $nvi;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   376
	} else {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   377
		$vi12 = $vi43 = undef;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   378
	}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   379
	$w12 = ($nbp[0] > 0) ? $velbp[0]/$nbp[0] : undef;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   380
	$w34 = ($nbp[1] > 0) ? $velbp[1]/$nbp[1] : undef;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   381
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   382
	if (@w12) {													# w uncertainty
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   383
		my($sumsq) = 0;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   384
		for ($i=0; $i<=$#w12; $i++) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   385
			$sumsq += ($w12-$w12[$i])**2;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   386
		}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   387
		$dta->{ENSEMBLE}[$ens]->{W12} = $w12;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   388
		$dta->{ENSEMBLE}[$ens]->{W12_ERR} = sqrt($sumsq)/($nbp[0]-1)
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   389
			if ($nbp[0]>=2);
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   390
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   391
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   392
	if (@w34) {													# w uncertainty
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   393
		my($sumsq) = 0;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   394
		for ($i=0; $i<=$#w34; $i++) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   395
			$sumsq += ($w34-$w34[$i])**2;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   396
		}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   397
		$dta->{ENSEMBLE}[$ens]->{W34} = $w34;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   398
		$dta->{ENSEMBLE}[$ens]->{W34_ERR} = sqrt($sumsq)/($nbp[1]-1)
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   399
			if ($nbp[1]>=2);
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   400
	}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   401
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   402
	my($sumsq) = 0;												# w uncertainty
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   403
	for ($i=0; $i<=$#w; $i++) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   404
		$sumsq += ($w-$w[$i])**2;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   405
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   406
	my($stderr) = $n[2]>=2 ? sqrt($sumsq)/($n[2]-1) : undef;
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   407
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   408
#	The following stderr test introduces a huge gap near the bottom of
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   409
#	the profiles. Without it, they seem more reasonable.
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   410
#	next if ($stderr > 0.05);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   411
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   412
	if (defined($w)) {											# valid velocity
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   413
		$dta->{ENSEMBLE}[$ens]->{W} = $w;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   414
		$dta->{ENSEMBLE}[$ens]->{W_ERR} = $stderr;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   415
	}
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   416
	$dta->{ENSEMBLE}[$ens]->{ERR_VEL} = $e if (defined($e));
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   417
	
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   418
	$dta->{ENSEMBLE}[$ens]->{W12} = $w12 if (defined($w12));
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   419
	$dta->{ENSEMBLE}[$ens]->{W34} = $w34 if (defined($w34));
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   420
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   421
	if (defined($vi12)) {
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   422
		$dta->{ENSEMBLE}[$ens]->{INCIDENT_VEL_T12} = $vi12;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   423
		$dta->{ENSEMBLE}[$ens]->{INCIDENT_VEL_T43} = $vi43;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   424
	}
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   425
10
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   426
#	The following code calculates beam-averaged ref-lr velocities.
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   427
#	I do not recall what this was implemented for. Disabled Mar 27, 2013.
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   428
#
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   429
#	if ($dta->{BEAM_COORDINATES}) {
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   430
#		$dta->{ENSEMBLE}[$ens]->{V1} = $bn[0]>=2 ? $bv[0]/$bn[0] : undef;
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   431
#		$dta->{ENSEMBLE}[$ens]->{V2} = $bn[1]>=2 ? $bv[1]/$bn[1] : undef;
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   432
#		$dta->{ENSEMBLE}[$ens]->{V3} = $bn[2]>=2 ? $bv[2]/$bn[2] : undef;
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   433
#	    $dta->{ENSEMBLE}[$ens]->{V4} = $bn[3]>=2 ? $bv[3]/$bn[3] : undef;
c835cd613f3e before EGU Vienna
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 8
diff changeset
   434
#	}
12
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 11
diff changeset
   435
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   436
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   437
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   438
11
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 10
diff changeset
   439
sub mk_prof(...)											# make profile
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   440
{
11
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 10
diff changeset
   441
	my($dta,$check,$filter,$lr_b0,$lr_b1,$min_corr,$max_e,$max_gap,$min_pctg) = @_;
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   442
	my($firstgood,$lastgood,$atbottom,$w_gap_time,$zErr,$maxz);
8
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   443
	my($rms_heave_accel_ssq,$rms_heave_accel_nsamp);
11
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 10
diff changeset
   444
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 10
diff changeset
   445
	$min_pctg = 100 unless defined($min_pctg);
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   446
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   447
	for (my($z)=0,my($e)=0; $e<=$#{$dta->{ENSEMBLE}}; $e++) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   448
		checkEnsemble($dta,$e) if ($check);
6
603221e51c6f pre IWISE
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 5
diff changeset
   449
###		The following line of code, which can only have an effect if check is disabled,
603221e51c6f pre IWISE
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 5
diff changeset
   450
###		seems reasonable but has been found to make matters worse with one particular
603221e51c6f pre IWISE
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 5
diff changeset
   451
###		data file from a BB150. 
603221e51c6f pre IWISE
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 5
diff changeset
   452
###		next if ($dta->{ENSEMBLE}[$e]->{BUILT_IN_TEST_ERROR});
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   453
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   454
		filterEnsemble($dta,$e)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   455
			if (defined($filter) &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   456
				$dta->{ENSEMBLE}[$e]->{PERCENT_GOOD}[0][0] > 0);
31
b6ca27a1d19c pre Tampa
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 23
diff changeset
   457
		mk_prof_ref_lr_w($dta,$e,$lr_b0,$lr_b1,$min_corr,$max_e,$min_pctg);	# ref. layer w
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   458
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   459
		if (defined($firstgood)) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   460
			$dta->{ENSEMBLE}[$e]->{ELAPSED_TIME} =			# time since start
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   461
				$dta->{ENSEMBLE}[$e]->{UNIX_TIME} -
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   462
				$dta->{ENSEMBLE}[$firstgood]->{UNIX_TIME};
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   463
		} else {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   464
			if (defined($dta->{ENSEMBLE}[$e]->{W})) {		# start of prof.
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   465
				$firstgood = $lastgood = $e;		    
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   466
				$dta->{ENSEMBLE}[$e]->{ELAPSED_TIME} = 0;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   467
				$dta->{ENSEMBLE}[$e]->{DEPTH} = $dta->{ENSEMBLE}[$e]->{DEPTH_ERR} = 0;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   468
			}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   469
			next;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   470
		}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   471
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   472
		#--------------------------------------------------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   473
		# within profile: both $firstgood and $lastgood set
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   474
		#--------------------------------------------------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   475
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   476
		if (!defined($dta->{ENSEMBLE}[$e]->{W})) {			# gap
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   477
			$w_gap_time += $dta->{ENSEMBLE}[$e]->{UNIX_TIME} -
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   478
						   $dta->{ENSEMBLE}[$e-1]->{UNIX_TIME};
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   479
			next;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   480
		}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   481
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   482
		my($dt) = $dta->{ENSEMBLE}[$e]->{UNIX_TIME} -		# time step since
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   483
				  $dta->{ENSEMBLE}[$lastgood]->{UNIX_TIME}; # ... last good ens
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 41
diff changeset
   484
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 41
diff changeset
   485
		die(sprintf("PANIC: negative dt = $dt between ensembles %d and %d\n",
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 41
diff changeset
   486
			$dta->{ENSEMBLE}[$lastgood]->{NUMBER},$dta->{ENSEMBLE}[$e]->{NUMBER}))
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 41
diff changeset
   487
				if ($dt < 0);
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   488
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   489
		if ($dt > $max_gap) {
41
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 39
diff changeset
   490
			# 2nd heuristic test added Nov 2017 for P06 profile #001
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 39
diff changeset
   491
			if ((@{$dta->{ENSEMBLE}}-$e < @{$dta->{ENSEMBLE}}/2) &&
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 39
diff changeset
   492
				($maxz > 25 && $z < $maxz/2)) {
17
591779f6df30 changed mkProfile gap heuristics & removed warning
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 14
diff changeset
   493
					printf(STDERR "WARNING: %.1f-s gap in 2nd half of profile is too long; profile ended at ensemble $lastgood\n",$dt);
41
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 39
diff changeset
   494
#					printf(STDERR "\t[#ens = %d, end-of-gap = $e]\n",scalar(@{$dta->{ENSEMBLE}}));
5
29faa9e6226c after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 4
diff changeset
   495
					last;
29faa9e6226c after DIMES UK2
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents: 4
diff changeset
   496
			}
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 41
diff changeset
   497
			printf(STDERR "WARNING: %.1f-s gap beginning at ens#%d in first half of profile is too long; profile restarted at ensemble %d\n",
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 41
diff changeset
   498
				$dt,$dta->{ENSEMBLE}[$lastgood+1]->{NUMBER},$dta->{ENSEMBLE}[$e]->{NUMBER});
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   499
			$firstgood = $lastgood = $e;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   500
			$dta->{ENSEMBLE}[$e]->{ELAPSED_TIME} = 0;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   501
			$z = $zErr = $maxz = 0;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   502
			$dta->{ENSEMBLE}[$e]->{DEPTH} = $dta->{ENSEMBLE}[$e]->{DEPTH_ERR} = 0;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   503
			$w_gap_time = 0;
8
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   504
			$rms_heave_accel_ssq = $rms_heave_accel_nsamp = 0;
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   505
			next;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   506
		}
2
065ea9ce12fc before DIMES UK2 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
   507
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   508
		#-----------------------------------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   509
		# The current ensemble has a valid w
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   510
		#-----------------------------------
8
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   511
48
cdc74ebada81 after partial adaptation to RTI files
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 43
diff changeset
   512
		if ($dt < 6) {												# no or short gap
8
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   513
			$z += $dta->{ENSEMBLE}[$lastgood]->{W} * $dt;			# integrate w to get depth
2
065ea9ce12fc before DIMES UK2 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
   514
			$zErr += ($dta->{ENSEMBLE}[$lastgood]->{W_ERR} * $dt)**2;
8
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   515
			$rms_heave_accel_ssq += (($dta->{ENSEMBLE}[$e]->{W}-$dta->{ENSEMBLE}[$lastgood]->{W})/$dt)**2;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 6
diff changeset
   516
			$rms_heave_accel_nsamp++;
3
ladcp@ladcp-mac.local
parents: 2
diff changeset
   517
		} elsif ($dt > 15) {
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 41
diff changeset
   518
	       	printf(STDERR "WARNING: long-ish w gap at ens#%d-%d (dt=%.1fs)\n",
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 41
diff changeset
   519
				$dta->{ENSEMBLE}[$lastgood+1]->{NUMBER},$dta->{ENSEMBLE}[$e-1]->{NUMBER},$dt)
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 49
diff changeset
   520
					unless defined($ENV{NO_GAP_WARNINGS}) || $RDI_Utils::No_Gap_Warnings;
2
065ea9ce12fc before DIMES UK2 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
   521
		}
065ea9ce12fc before DIMES UK2 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
   522
	
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   523
		$dta->{ENSEMBLE}[$e]->{DEPTH} = $z;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   524
		$dta->{ENSEMBLE}[$e]->{DEPTH_ERR} = sqrt($zErr);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   525
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   526
		$atbottom = $e, $maxz = $z if ($z > $maxz); 
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   527
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   528
		$lastgood = $e;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   529
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   530
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   531
	filterEnsembleStats() if defined($filter);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   532
23
fb0c269b1eaa V1.2 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 19
diff changeset
   533
	return ($firstgood,$lastgood,$atbottom,$w_gap_time,$zErr,$maxz,
fb0c269b1eaa V1.2 release candidate
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 19
diff changeset
   534
			($rms_heave_accel_nsamp>0) ? sqrt($rms_heave_accel_ssq/$rms_heave_accel_nsamp) : 'nan');
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   535
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   536
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   537
#----------------------------------------------------------------------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   538
# (true|false) = numberp(var)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   539
#----------------------------------------------------------------------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   540
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   541
sub numberp(@)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   542
{ return  $_[0] =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/; }
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   543
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   544
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   545
1;
2
065ea9ce12fc before DIMES UK2 cruise
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
   546