author  A.M. Thurnherr <athurnherr@yahoo.com> 
Mon, 30 May 2016 11:03:57 0400  
changeset 35  7c394a2d1fc9 
parent 34  3b4bcd55e1ea 
child 36  515b06dae59c 
permissions  rwrr 
0  1 
#====================================================================== 
2 
# R D I _ C O O R D S . P L 

3 
# doc: Sun Jan 19 17:57:53 2003 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

4 
# dlm: Thu May 26 17:40:20 2016 
0  5 
# (c) 2003 A.M. Thurnherr 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

6 
# uEInfo: 530 14 NIL 0 0 72 2 2 4 NIL ofnI 
0  7 
#====================================================================== 
8 

9 
# RDI Workhorse Coordinate Transformations 

10 

11 
# HISTORY: 

12 
# Jan 19, 2003:  written 

13 
# Jan 21, 2003:  made it obey HEADING_BIAS (magnetic declination) 

14 
# Jan 22, 3003:  corrected magnetic declination 

15 
# Feb 16, 2003:  use pitch correction from RDI manual 

16 
# Oct 11, 2003:  BUG: return value of atan() had been interpreted 

17 
# as degrees instead of radians 

18 
# Feb 27, 2004:  added velApplyHdgBias() 

19 
#  changed nonzero HEADING_ALIGNMENT from error to warning 

20 
# Sep 16, 2005:  added deg() for [mkprofile] 

21 
# Aug 26, 2006:  BUG: incorrect transformation for uplookers 

22 
# Nov 30, 2007:  optimized &velInstrumentToEarth(), velBeamToInstrument() 

23 
#  added support for 3beam solutions 

24 
# Feb 12, 2008:  added threeBeamFlag 

25 
# Mar 18, 2009:  added &gimbal_pitch(), &angle_from_vertical() 

26 
# May 19, 2009:  added &velBeamToVertical() 

27 
# May 23, 2009:  debugged & renamed to &velBeamToBPEarth 

28 
# May 23, 2010:  changed prototypes of rad() & deg() to conform to ANTS 

5  29 
# Dec 20, 2010:  cosmetics 
6  30 
# Dec 23, 2010:  added &velBeamToBPInstrument 
31 
# Jan 22, 2011:  made velApplyHdgBias calculate sin/cos every time to allow 

32 
# perensemble corrections 

8  33 
# Jan 15, 2012:  replaced defined(@...) by (@...) to get rid of warning 
13
b176da8559b3
before implementing WBWens (PD0 writing)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
8
diff
changeset

34 
# Aug 7, 2013:  BUG: &velBeamToBPInstrument did not return any val unless 
b176da8559b3
before implementing WBWens (PD0 writing)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
8
diff
changeset

35 
# all beam velocities are defined 
14  36 
# Nov 27, 2013:  added &RDI_pitch(), &tilt_azimuth() 
19
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

37 
# Mar 4, 2014:  added support for ensembles with missing PITCH/ROLL/HEADING 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

38 
# May 29, 2014:  BUG: vertical velocity can be calculated even without 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

39 
# heading 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

40 
#  removed some old debug statements 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

41 
#  removed unused code from &velBeamToBPInstrument 
28  42 
# Jan 5, 2016:  added &velEarthToInstrument(@), &velInstrumentToBeam(@) 
31  43 
# Jan 9, 2016:  added &velEarthToBeam(), &velBeamToEarth() 
32  44 
# Feb 29, 2016:  debugged & verified velEarthToInstrument(), velInstrumentToBeam() 
45 
#  added velBeamToEarth() 

34  46 
# May 19, 2016:  begin implemeting bin interpolation 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

47 
# May 25, 2016:  continued 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

48 
# May 26, 2016:  made it work 
0  49 

50 
use strict; 

51 
use POSIX; 

52 

53 
my($PI) = 3.14159265358979; 

54 

55 
sub rad(@) { return $_[0]/180 * $PI; } 

56 
sub deg(@) { return $_[0]/$PI * 180; } 

57 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

58 
# 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

59 
# Tweakables 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

60 
# 
0  61 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

62 
$RDI_Coords::minValidVels = 3; # 3beam solutions ok (velBeamToInstrument) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

63 
$RDI_Coords::binMapping = 'linterp'; # 'linterp' or 'none' (earthVels, BPearthVels) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

64 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

65 
# 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

66 
# beam to earth transformation (from RDI coord trans manual) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

67 
# 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

68 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

69 
$RDI_Coords::threeBeam_1 = 0; # stats from velBeamToInstrument 
0  70 
$RDI_Coords::threeBeam_2 = 0; 
71 
$RDI_Coords::threeBeam_3 = 0; 

72 
$RDI_Coords::threeBeam_4 = 0; 

73 
$RDI_Coords::fourBeam = 0; 

74 

75 
$RDI_Coords::threeBeamFlag = 0; # flag last transformation 

76 

77 
{ # STATIC SCOPE 

78 
my(@B2I); 

79 

80 
sub velBeamToInstrument(@) 

81 
{ 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

82 
my($ADCP,$ens,$v1,$v2,$v3,$v4) = @_; 
0  83 
return undef unless (defined($v1) + defined($v2) + 
84 
defined($v3) + defined($v4) 

85 
>= $RDI_Coords::minValidVels); 

86 

8  87 
unless (@B2I) { 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

88 
my($a) = 1 / (2 * sin(rad($ADCP>{BEAM_ANGLE}))); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

89 
my($b) = 1 / (4 * cos(rad($ADCP>{BEAM_ANGLE}))); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

90 
my($c) = $ADCP>{CONVEX_BEAM_PATTERN} ? 1 : 1; 
0  91 
my($d) = $a / sqrt(2); 
92 
@B2I = ([$c*$a, $c*$a, 0, 0 ], 

93 
[0, 0, $c*$a, $c*$a], 

94 
[$b, $b, $b, $b ], 

95 
[$d, $d, $d, $d ]); 

96 
} 

97 

98 
if (!defined($v1)) { # 3beam solutions 

99 
$RDI_Coords::threeBeamFlag = 1; 

100 
$RDI_Coords::threeBeam_1++; 

101 
$v1 = ($v2*$B2I[3][1]+$v3*$B2I[3][2]+$v4*$B2I[3][3])/$B2I[3][0]; 

102 
} elsif (!defined($v2)) { 

103 
$RDI_Coords::threeBeamFlag = 1; 

104 
$RDI_Coords::threeBeam_2++; 

105 
$v2 = ($v1*$B2I[3][0]+$v3*$B2I[3][2]+$v4*$B2I[3][3])/$B2I[3][1]; 

106 
} elsif (!defined($v3)) { 

107 
$RDI_Coords::threeBeamFlag = 1; 

108 
$RDI_Coords::threeBeam_3++; 

109 
$v3 = ($v1*$B2I[3][0]+$v2*$B2I[3][1]+$v4*$B2I[3][3])/$B2I[3][2]; 

110 
} elsif (!defined($v4)) { 

111 
$RDI_Coords::threeBeamFlag = 1; 

112 
$RDI_Coords::threeBeam_4++; 

113 
$v4 = ($v1*$B2I[3][0]+$v2*$B2I[3][1]+$v3*$B2I[3][2])/$B2I[3][3]; 

114 
} else { 

115 
$RDI_Coords::threeBeamFlag = 0; 

116 
$RDI_Coords::fourBeam++; 

117 
} 

118 

119 
return ($v1*$B2I[0][0]+$v2*$B2I[0][1], 

120 
$v3*$B2I[1][2]+$v4*$B2I[1][3], 

121 
$v1*$B2I[2][0]+$v2*$B2I[2][1]+$v3*$B2I[2][2]+$v4*$B2I[2][3], 

122 
$v1*$B2I[3][0]+$v2*$B2I[3][1]+$v3*$B2I[3][2]+$v4*$B2I[3][3]); 

123 
} 

124 
} # STATIC SCOPE 

125 

126 
{ # STATIC SCOPE 

127 
my($hdg,$pitch,$roll,@I2E); 

128 

129 
sub velInstrumentToEarth(@) 

130 
{ 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

131 
my($ADCP,$ens,$v1,$v2,$v3,$v4) = @_; 
0  132 
return undef unless (defined($v1) && defined($v2) && 
18  133 
defined($v3) && defined($v4) && 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

134 
defined($ADCP>{ENSEMBLE}[$ens]>{PITCH}) && 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

135 
defined($ADCP>{ENSEMBLE}[$ens]>{ROLL})); 
0  136 

137 
unless (@I2E && 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

138 
$pitch == $ADCP>{ENSEMBLE}[$ens]>{PITCH} && 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

139 
$roll == $ADCP>{ENSEMBLE}[$ens]>{ROLL}) { 
0  140 
printf(STDERR "$0: warning HEADING_ALIGNMENT == %g ignored\n", 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

141 
$ADCP>{HEADING_ALIGNMENT}) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

142 
if ($ADCP>{HEADING_ALIGNMENT}); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

143 
$hdg = $ADCP>{ENSEMBLE}[$ens]>{HEADING}  $ADCP>{HEADING_BIAS} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

144 
if defined($ADCP>{ENSEMBLE}[$ens]>{HEADING}); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

145 
$pitch = $ADCP>{ENSEMBLE}[$ens]>{PITCH}; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

146 
$roll = $ADCP>{ENSEMBLE}[$ens]>{ROLL}; 
0  147 
my($rad_gimbal_pitch) = atan(tan(rad($pitch)) * cos(rad($roll))); 
19
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

148 
my($sh,$ch) = (sin(rad($hdg)),cos(rad($hdg))) 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

149 
if defined($hdg); 
0  150 
my($sp,$cp) = (sin($rad_gimbal_pitch),cos($rad_gimbal_pitch)); 
151 
my($sr,$cr) = (sin(rad($roll)), cos(rad($roll))); 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

152 
@I2E = $ADCP>{ENSEMBLE}[$ens]>{XDUCER_FACING_UP} 
0  153 
? ( 
154 
[$ch*$cr$sh*$sp*$sr, $sh*$cp,$ch*$sr+$sh*$sp*$cr], 

155 
[$ch*$sp*$sr+$sh*$cr, $ch*$cp, $sh*$sr+$ch*$sp*$cr], 

156 
[+$cp*$sr, $sp, $cp*$cr, ], 

157 
) : ( 

158 
[$ch*$cr+$sh*$sp*$sr, $sh*$cp, $ch*$sr$sh*$sp*$cr], 

159 
[$ch*$sp*$sr$sh*$cr, $ch*$cp,$sh*$sr$ch*$sp*$cr], 

160 
[$cp*$sr, $sp, $cp*$cr, ], 

161 
); 

162 
} 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

163 
return defined($ADCP>{ENSEMBLE}[$ens]>{HEADING}) 
19
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

164 
? ($v1*$I2E[0][0]+$v2*$I2E[0][1]+$v3*$I2E[0][2], 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

165 
$v1*$I2E[1][0]+$v2*$I2E[1][1]+$v3*$I2E[1][2], 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

166 
$v1*$I2E[2][0]+$v2*$I2E[2][1]+$v3*$I2E[2][2], 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

167 
$v4) 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

168 
: (undef,undef, 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

169 
$v1*$I2E[2][0]+$v2*$I2E[2][1]+$v3*$I2E[2][2], 
e23a5fd2923a
after adapting RDI_Coords to calc w even without valid heading
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
18
diff
changeset

170 
$v4); 
0  171 
} 
172 
} # STATIC SCOPE 

173 

32  174 

175 
sub velBeamToEarth(@) 

176 
{ 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

177 
my($ADCP,$e,@v) = @_; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

178 
return velInstrumentToEarth($ADCP,$e,velBeamToInstrument($ADCP,$e,@v)); 
32  179 
} 
180 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

181 

28  182 
# 
183 
# velEarthToInstrument() transforms earth to instrument coordinates 

184 
#  based on manually inverted rotation matrix M (Sec 5.6 in coordtrans manual) 

32  185 
#  code was verified for both down and uplookers 
28  186 
#  missing heading data (IMP) causes undef beam velocities 
187 
# 

188 

189 
{ # STATIC SCOPE 

190 
my($hdg,$pitch,$roll,@E2I); 

191 

192 
sub velEarthToInstrument(@) 

193 
{ 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

194 
my($ADCP,$ens,$u,$v,$w,$ev) = @_; 
28  195 

32  196 
unless (@E2I && 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

197 
$pitch == $ADCP>{ENSEMBLE}[$ens]>{PITCH} && 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

198 
$roll == $ADCP>{ENSEMBLE}[$ens]>{ROLL}) { 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

199 
$hdg = $ADCP>{ENSEMBLE}[$ens]>{HEADING}  $ADCP>{HEADING_BIAS} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

200 
if defined($ADCP>{ENSEMBLE}[$ens]>{HEADING}); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

201 
$pitch = $ADCP>{ENSEMBLE}[$ens]>{PITCH}; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

202 
$roll = $ADCP>{ENSEMBLE}[$ens]>{ROLL}; 
28  203 
my($rad_gimbal_pitch) = atan(tan(rad($pitch)) * cos(rad($roll))); 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

204 
my($useRoll) = ($ADCP>{ENSEMBLE}[$ens]>{XDUCER_FACING_UP}) ? $roll+180 : $roll; 
28  205 
my($sh,$ch) = (sin(rad($hdg)),cos(rad($hdg))) 
206 
if defined($hdg); 

207 
my($sp,$cp) = (sin($rad_gimbal_pitch),cos($rad_gimbal_pitch)); 

32  208 
my($sr,$cr) = (sin(rad($useRoll)), cos(rad($useRoll))); 
209 
@E2I = ([$ch*$cr+$sh*$sp*$sr, $ch*$sp*$sr$sh*$cr, $cp*$sr], # M^1 = R^1 * P^1 * R^1 

210 
[$sh*$cp, $ch*$cp, $sp ], 

211 
[$ch*$sr$sh*$sp*$cr, $sh*$sr$ch*$sp*$cr, $cp*$cr]); 

28  212 
} 
213 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

214 
return defined($ADCP>{ENSEMBLE}[$ens]>{HEADING}) 
28  215 
? ($u*$E2I[0][0]+$v*$E2I[0][1]+$w*$E2I[0][2], 
216 
$u*$E2I[1][0]+$v*$E2I[1][1]+$w*$E2I[1][2], 

217 
$u*$E2I[2][0]+$v*$E2I[2][1]+$w*$E2I[2][2], 

218 
$ev) 

219 
: (undef,undef,undef,undef); 

220 

221 
} 

222 
} # STATIC SCOPE 

223 

224 
# 

225 
# velInstrumentToBeam() transforms instrument to beam coordinates 

226 
#  based on manually solved eq system in sec 5.3 of coord manual 

227 
#  does not implement binremapping 

228 
#  does not work for 3beam solutions, as it is not known which 

229 
# beam was bad 

230 
# 

231 

232 
{ # STATIC SCOPE 

233 
my($a,$b,$c,$d); 

234 

235 
sub velInstrumentToBeam(@) 

236 
{ 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

237 
my($ADCP,$x,$y,$z,$ev) = @_; 
28  238 
return undef unless (defined($x) + defined($y) + 
239 
defined($z) + defined($ev) == 4); 

240 

241 
unless (defined($a)) { 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

242 
$a = 1 / (2 * sin(rad($ADCP>{BEAM_ANGLE}))); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

243 
$b = 1 / (4 * cos(rad($ADCP>{BEAM_ANGLE}))); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

244 
$c = $ADCP>{CONVEX_BEAM_PATTERN} ? 1 : 1; 
28  245 
$d = $a / sqrt(2); 
246 
} 

247 

248 
return ( $x/(2*$a*$c) + $z/(4*$b) + $ev/(4*$d), 

249 
$x/(2*$a*$c) + $z/(4*$b) + $ev/(4*$d), 

250 
$y/(2*$a*$c) + $z/(4*$b)  $ev/(4*$d), 

251 
$y/(2*$a*$c) + $z/(4*$b)  $ev/(4*$d)); 

252 

253 
} 

254 
} # STATIC SCOPE 

255 

31  256 
# 
257 
# velEarthToBeam() combines velEarthToInstrument and velInstrumentToBeam 

258 
# 

259 

260 
sub velEarthToBeam(@) 

261 
{ 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

262 
my($ADCP,$ens,$u,$v,$w,$ev) = @_; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

263 
return velInstrumentToBeam($ADCP, 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

264 
velEarthToInstrument($ADCP,$ens,$u,$v,$w,$ev)); 
31  265 
} 
266 

0  267 
#====================================================================== 
5  268 
# velBeamToBPEarth(@) calculates the vertical and horizontal vels 
0  269 
# from the two beam pairs separately. Note that (w1+w2)/2 is 
270 
# identical to the w estimated according to RDI without 3beam 

271 
# solutions. 

272 
#====================================================================== 

273 

274 
{ # STATIC SCOPE 

275 
my($TwoCosBAngle,$TwoSinBAngle); 

276 

277 
sub velBeamToBPEarth(@) 

278 
{ 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

279 
my($ADCP,$ens,$b1,$b2,$b3,$b4) = @_; 
0  280 
my($v12,$w12,$v34,$w34); 
281 

18  282 
return (undef,undef,undef,undef) 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

283 
unless (defined($ADCP>{ENSEMBLE}[$ens]>{PITCH}) && 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

284 
defined($ADCP>{ENSEMBLE}[$ens]>{ROLL})); 
18  285 

0  286 
unless (defined($TwoCosBAngle)) { 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

287 
$TwoCosBAngle = 2 * cos(rad($ADCP>{BEAM_ANGLE})); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

288 
$TwoSinBAngle = 2 * sin(rad($ADCP>{BEAM_ANGLE})); 
0  289 
} 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

290 
my($roll) = rad($ADCP>{ENSEMBLE}[$ens]>{ROLL}); 
0  291 
my($sr) = sin($roll); my($cr) = cos($roll); 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

292 
my($pitch) = atan(tan(rad($ADCP>{ENSEMBLE}[$ens]>{PITCH})) * $cr); # gimbal pitch 
0  293 
my($sp) = sin($pitch); my($cp) = cos($pitch); 
294 

295 
# Sign convention: 

296 
#  refer to Coord manual Fig. 3 

297 
#  v12 is horizontal velocity from beam1 to beam2, i.e. westward for upwardlooking ADCP 

298 
# with beam 3 pointing north (heading = 0) 

299 
#  w is +ve upward, regardless of instrument orientation 

300 

301 
my($v12_ic) = ($b1$b2)/$TwoSinBAngle; # instrument coords with w vertical up 

302 
my($w12_ic) = ($b1+$b2)/$TwoCosBAngle; 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

303 
$w12_ic *= 1 if ($ADCP>{ENSEMBLE}[$ens]>{XDUCER_FACING_UP}); 
0  304 
my($v34_ic) = ($b3$b4)/$TwoSinBAngle; 
305 
my($w34_ic) = ($b3+$b4)/$TwoCosBAngle; 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

306 
$w34_ic *= 1 if ($ADCP>{ENSEMBLE}[$ens]>{XDUCER_FACING_UP}); 
0  307 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

308 
if ($ADCP>{ENSEMBLE}[$ens]>{XDUCER_FACING_UP}) { # beampair Earth coords 
0  309 
$w12 = $w12_ic*$cr + $v12_ic*$sr  $v34_ic*$sp; 
310 
$v12 = $v12_ic*$cr  $w12_ic*$sr + $w34_ic*$sp; 

311 
$w34 = $w34_ic*$cp  $v34_ic*$sp + $v12_ic*$sr; 

312 
$v34 = $v34_ic*$cp + $w34_ic*$sp  $w12_ic*$sr; 

313 
} else { 

314 
$w12 = $w12_ic*$cr  $v12_ic*$sr  $v34_ic*$sp; 

315 
$v12 = $v12_ic*$cr + $w12_ic*$sr + $w34_ic*$sp; 

316 
$w34 = $w34_ic*$cp  $v34_ic*$sp  $v12_ic*$sr; 

317 
$v34 = $v34_ic*$cp + $w34_ic*$sp + $w12_ic*$sr; 

318 
} 

319 

13
b176da8559b3
before implementing WBWens (PD0 writing)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
8
diff
changeset

320 
$v12=$w12=undef unless (defined($b1) && defined($b2)); 
b176da8559b3
before implementing WBWens (PD0 writing)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
8
diff
changeset

321 
$v34=$w34=undef unless (defined($b3) && defined($b4)); 
b176da8559b3
before implementing WBWens (PD0 writing)
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
8
diff
changeset

322 

0  323 
return ($v12,$w12,$v34,$w34); 
324 
} 

325 
} 

326 

5  327 
#=================================================================== 
328 
# velBeamToBPInstrument(@) calculates the instrumentcoordinate vels 

329 
# from the two beam pairs separately. 

330 
#=================================================================== 

331 

332 
{ # STATIC SCOPE 

333 
my($TwoCosBAngle,$TwoSinBAngle); 

334 

335 
sub velBeamToBPInstrument(@) 

336 
{ 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

337 
my($ADCP,$ens,$b1,$b2,$b3,$b4) = @_; 
5  338 
my($v12,$w12,$v34,$w34); 
339 

18  340 
return (undef,undef,undef,undef) 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

341 
unless (defined($ADCP>{ENSEMBLE}[$ens]>{PITCH}) && 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

342 
defined($ADCP>{ENSEMBLE}[$ens]>{ROLL})); 
18  343 

5  344 
unless (defined($TwoCosBAngle)) { 
35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

345 
$TwoCosBAngle = 2 * cos(rad($ADCP>{BEAM_ANGLE})); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

346 
$TwoSinBAngle = 2 * sin(rad($ADCP>{BEAM_ANGLE})); 
5  347 
} 
348 

349 
# Sign convention: 

350 
#  refer to Coord manual Fig. 3 

351 
#  v12 is horizontal velocity from beam1 to beam2 

352 
#  w is +ve upward, regardless of instrument orientation 

353 

354 
if (defined($b1) && defined($b2)) { 

355 
$v12 = ($b1$b2)/$TwoSinBAngle; 

356 
$w12 = ($b1+$b2)/$TwoCosBAngle; 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

357 
$w12 *= 1 if ($ADCP>{ENSEMBLE}[$ens]>{XDUCER_FACING_UP}); 
5  358 
} 
359 
if (defined($b3) && defined($b4)) { 

360 
$v34 = ($b3$b4)/$TwoSinBAngle; 

361 
$w34 = ($b3+$b4)/$TwoCosBAngle; 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

362 
$w34 *= 1 if ($ADCP>{ENSEMBLE}[$ens]>{XDUCER_FACING_UP}); 
5  363 
} 
364 

365 
return ($v12,$w12,$v34,$w34); 

366 
} 

367 
} 

368 

0  369 
#====================================================================== 
370 
# velApplyHdgBias() applies the heading bias, which is used to correct 

371 
# for magnetic declination for data recorded in Earthcoordinates ONLY. 

372 
# Bias correction for beamcoordinate data is done in velInstrumentToEarth() 

373 
#====================================================================== 

374 

6  375 
sub velApplyHdgBias(@) 
376 
{ 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

377 
my($ADCP,$ens,$v1,$v2,$v3,$v4) = @_; 
18  378 
return (undef,undef,undef,undef) 
379 
unless (defined($v1) && defined($v2) && 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

380 
defined($ADCP>{ENSEMBLE}[$ens]>{HEADING})); 
0  381 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

382 
my($sh) = sin(rad($ADCP>{HEADING_BIAS})); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

383 
my($ch) = cos(rad($ADCP>{HEADING_BIAS})); 
0  384 

6  385 
return ( $v1*$ch + $v2*$sh, 
386 
$v1*$sh + $v2*$ch, 

387 
$v3 , 

388 
$v4 ); 

389 
} 

0  390 

391 
# 

392 
# Pitch/Roll Functions 

393 
# 

394 

395 
sub gimbal_pitch($$) # RDI coord trans manual 

396 
{ 

5  397 
my($RDI_pitch,$RDI_roll) = @_; 
18  398 
return 'nan' unless defined($RDI_pitch) && defined($RDI_roll); 
5  399 
return deg(atan(tan(rad($RDI_pitch)) * cos(rad($RDI_roll)))); 
0  400 
} 
401 

14  402 
sub RDI_pitch($$) 
403 
{ 

404 
my($gimbal_pitch,$roll) = @_; 

18  405 
return 'nan' unless defined($gimbal_pitch) && defined($roll); 
14  406 
return deg(atan(tan(rad($gimbal_pitch))/cos(rad($roll)))); 
407 
} 

408 

409 
sub tilt_azimuth($$) 

410 
{ 

411 
my($gimbal_pitch,$roll) = @_; 

18  412 
return 'nan' unless defined($gimbal_pitch) && defined($roll); 
14  413 
return angle(deg(atan2(sin(rad($gimbal_pitch)),sin(rad($roll))))); 
414 
} 

415 

18  416 
#  angle from vertical is home grown 
0  417 
#  angle between two unit vectors given by acos(v1 dot v2) 
418 
#  vertical unit vector v1 = (0 0 1) => dot product = zcomponent of v2 

419 
#  when vertical unit vector is pitched in x direction, followed by 

420 
# roll in y direction: 

421 
# x = sin(pitch) 

422 
# y = cos(pitch) * sin(roll) 

423 
# z = cos(pitch) * cos(roll) 

424 
# has been checked with sqrt(x^2+y^2+z^2) == 1 

425 
#  for small angles, this is very similar to sqrt(pitch^2+roll^2) 

426 

427 
sub angle_from_vertical($$) 

428 
{ 

5  429 
my($RDI_pitch,$RDI_roll) = @_; 
18  430 
return 'nan' unless defined($RDI_pitch) && defined($RDI_roll); 
5  431 
my($rad_pitch) = atan(tan(rad($RDI_pitch)) * cos(rad($RDI_roll))); 
432 
return deg(acos(cos($rad_pitch) * cos(rad($RDI_roll)))); 

0  433 
} 
434 

35
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

435 
# 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

436 
# alongBeamDZ(ADCP_dta,ens,beam) => (dz_to_bin1_center,bin_dz) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

437 
#  calculate vertical distances: 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

438 
#  between transducer and bin1 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

439 
#  between adjacent bins 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

440 
#  no soundspeed correction 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

441 
#  for UL (Fig. 3 Coord Manual): 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

442 
# b1 = phi + roll b2 = phi  roll 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

443 
# b3 = phi  pitch b4 = phi + pitch 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

444 
#  for DL: 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

445 
# b1 = phi + roll b2 = phi  roll 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

446 
# b3 = phi + pitch b4 = phi  pitch 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

447 
# 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

448 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

449 
sub alongBeamDZ($$$) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

450 
{ 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

451 
my($ADCP,$ens,$beam) = @_; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

452 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

453 
my($tilt); # determine tilt of given beam 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

454 
my($pitch) = $ADCP>{ENSEMBLE}[$ens]>{PITCH}; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

455 
my($roll) = $ADCP>{ENSEMBLE}[$ens]>{ROLL}; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

456 
if ($beam == 0) { # beam 1 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

457 
$tilt = &angle_from_vertical($pitch,$ADCP>{BEAM_ANGLE}+$roll); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

458 
} elsif ($beam == 1) { # beam 2 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

459 
$tilt = &angle_from_vertical($pitch,$ADCP>{BEAM_ANGLE}$roll); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

460 
} elsif ($beam == 2) { # beam 3 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

461 
$tilt = $ADCP>{ENSEMBLE}[$ens]>{XDUCER_FACING_UP} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

462 
? &angle_from_vertical($ADCP>{BEAM_ANGLE}$pitch,$roll) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

463 
: &angle_from_vertical($ADCP>{BEAM_ANGLE}+$pitch,$roll); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

464 
} else { # beam 4 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

465 
$tilt = $ADCP>{ENSEMBLE}[$ens]>{XDUCER_FACING_UP} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

466 
? &angle_from_vertical($ADCP>{BEAM_ANGLE}+$pitch,$roll) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

467 
: &angle_from_vertical($ADCP>{BEAM_ANGLE}$pitch,$roll); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

468 
} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

469 
return ($ADCP>{DISTANCE_TO_BIN1_CENTER}*cos(rad($tilt)), 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

470 
$ADCP>{BIN_LENGTH}*cos(rad($tilt))); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

471 
} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

472 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

473 
# 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

474 
# binterp(ADCP_dta,ens,bin,ADCP_field) => @interpolated_vals 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

475 
#  interpolate beam velocities to nominal bin center 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

476 
#  field can be VELOCITY, ECHO_AMPLITUDE, ... 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

477 
# 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

478 
# earthVels(ADCP_dta,ens,bin) => (u,v,w,err_vel) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

479 
# BPEarthVels(ADCP_dta,ens,bin) => (v12,w12,v34,w34) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

480 
#  new interface (V1.7) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

481 
# 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

482 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

483 
sub binterp1($$$$$) # interpolate along a single beam 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

484 
{ 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

485 
my($ADCP,$ens,$target_dz,$ADCP_field,$beam) = @_; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

486 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

487 
my($dz2bin1,$bin_dz) = &alongBeamDZ($ADCP,$ens,$beam); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

488 
my($floor_bin) = int(($target_dz$dz2bin1) / $bin_dz); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

489 
$floor_bin if ($floor_bin == $ADCP>{N_BINS}1); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

490 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

491 
my($y1) = $ADCP>{ENSEMBLE}[$ens]>{$ADCP_field}[$floor_bin][$beam]; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

492 
my($y2) = $ADCP>{ENSEMBLE}[$ens]>{$ADCP_field}[$floor_bin+1][$beam]; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

493 
$y2 = $y1 unless defined($y2); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

494 
$y1 = $y2 unless defined($y1); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

495 
return undef unless defined($y1); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

496 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

497 
my($dz1) = $dz2bin1 + $floor_bin * $bin_dz; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

498 
my($dz2) = $dz1 + $bin_dz; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

499 
my($ifac) = ($target_dz  $dz1) / ($dz2  $dz1); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

500 
die("assertion failed\nifac = $ifac (target_dz = $target_dz, dz1 = $dz1, dz2 = $dz2)") 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

501 
unless ($ifac>= 0.5 && $ifac<=2); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

502 
return $y1 + $ifac*($y2$y1); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

503 
} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

504 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

505 
sub binterp($$$$) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

506 
{ 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

507 
my($ADCP,$ens,$target_bin,$ADCP_field) = @_; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

508 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

509 
my($crt) = cos(rad($ADCP>{ENSEMBLE}[$ens]>{TILT})); # calc center depth of target bin 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

510 
my($target_dz) = ($ADCP>{DISTANCE_TO_BIN1_CENTER} + $target_bin*$ADCP>{BIN_LENGTH}) * $crt; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

511 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

512 
return (&binterp1($ADCP,$ens,$target_dz,$ADCP_field,0), # interpolate all four beams 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

513 
&binterp1($ADCP,$ens,$target_dz,$ADCP_field,1), 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

514 
&binterp1($ADCP,$ens,$target_dz,$ADCP_field,2), 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

515 
&binterp1($ADCP,$ens,$target_dz,$ADCP_field,3)); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

516 
} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

517 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

518 
sub earthVels($$$) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

519 
{ 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

520 
my($ADCP,$ens,$bin) = @_; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

521 
if ($RDI_Coords::binMapping eq 'linterp') { 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

522 
return velInstrumentToEarth($ADCP,$ens, 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

523 
velBeamToInstrument($ADCP,$ens, 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

524 
binterp($ADCP,$ens,$bin,'VELOCITY'))); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

525 
} elsif ($RDI_Coords::binMapping eq 'none') { 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

526 
return velInstrumentToEarth($ADCP,$ens, 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

527 
velBeamToInstrument($ADCP,$ens, 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

528 
@{$ADCP>{ENSEMBLE}[$ens]>{VELOCITY}[$bin]})); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

529 
} else { 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

530 
die("earthVels(): unknown bin mapping '$RDI_Coords::binMapping '\n"); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

531 
} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

532 
} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

533 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

534 
sub BPEarthVels($$$) 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

535 
{ 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

536 
my($ADCP,$ens,$bin) = @_; 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

537 
if ($RDI_Coords::binMapping eq 'linterp') { 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

538 
return velBeamToBPEarth($ADCP,$ens,binterp($ADCP,$ens,$bin,'VELOCITY')); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

539 
} elsif ($RDI_Coords::binMapping eq 'none') { 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

540 
return velBeamToBPEarth($ADCP,$ens,@{$ADCP>{ENSEMBLE}[$ens]>{VELOCITY}[$bin]}); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

541 
} else { 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

542 
die("BPEarthVels(): unknown bin mapping '$RDI_Coords::binMapping '\n"); 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

543 
} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

544 
} 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

545 

7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

546 
# 
7c394a2d1fc9
before implementing 2nd order coord trans
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
34
diff
changeset

547 

0  548 
1; 