Coverage for pygeodesy/auxilats/auxily.py: 94%

105 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-01-06 12:20 -0500

1 

2# -*- coding: utf-8 -*- 

3 

4u'''(INTERNAL) I{Auxiliary} latitudes' classes, constants and functions. 

5 

6Class L{AuxAngle} transcoded to Python from I{Karney}'s C++ class U{AuxAngle 

7<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AuxAngle.html>} 

8in I{GeographicLib version 2.2+}. 

9 

10Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2022-2024) and licensed 

11under the MIT/X11 License. For more information, see the U{GeographicLib 

12<https://GeographicLib.SourceForge.io>} documentation. 

13''' 

14# make sure int/int division yields float quotient, see .basics 

15from __future__ import division as _; del _ # PYCHOK semicolon 

16 

17# from pygeodesy import auxilats # _MODS 

18from pygeodesy.constants import INF, NAN, isinf, isnan, _0_0, _0_5, _1_0, \ 

19 _copysign_1_0, _over, _1_over 

20from pygeodesy.errors import AuxError 

21from pygeodesy.fmath import hypot1 as _sc, hypot2_ 

22from pygeodesy.interns import NN, _DOT_, _UNDER_ # PYCHOK used! 

23from pygeodesy.lazily import _ALL_DOCS, _ALL_MODS as _MODS # PYCHOK used! 

24from pygeodesy.utily import atan1 

25 

26from math import asinh, copysign 

27 

28__all__ = () 

29__version__ = '24.09.03' 

30 

31 

32class Aux(object): 

33 '''Enum-style Aux names. 

34 ''' 

35 _coeffs = {} 

36 GEOGRAPHIC = PHI = GEODETIC = 0 

37 PARAMETRIC = BETA = REDUCED = 1 

38 GEOCENTRIC = THETA = 2 # all ... 

39 RECTIFYING = MU = 3 # use n^2 

40 CONFORMAL = CHI = 4 # use n 

41 AUTHALIC = XI = 5 # use n 

42 N = 6 

43 N2 = 36 

44 

45 def __index__(self, aux): 

46 # throws KeyError, not IndexError 

47 return _Aux2Greek[aux] 

48 

49 def __len__(self): 

50 return Aux.N 

51 

52 def _CXcoeffs(self, aL): # in .auxLat.AuxLat._CXcoeffs 

53 '''(INTERNAL) Get the C{_CX_4._coeffs_4}, C{_CX_6._coeffs_6} 

54 or C{_CS_8._coeffs_8} coefficients, once. 

55 ''' 

56 try: 

57 _coeffs = Aux._coeffs[aL] 

58 except KeyError: 

59 try: # from pygeodesy.auxilats._CX_x import _coeffs_x as _coeffs 

60 _CX_x = _DOT_(_MODS.auxilats.__name__, _UNDER_('_CX', aL)) 

61 _coeffs = _MODS.getattr(_CX_x, _UNDER_('_coeffs', aL)) 

62 except (AttributeError, ImportError, KeyError, TypeError) as x: 

63 raise AuxError(ALorder=aL, cause=x) 

64 Aux._coeffs[aL] = _coeffs._validate(aL, Aux.len(aL)) 

65 return _coeffs 

66 

67 def _1d(self, auxout, auxin): 

68 '''Get the 1-d index into N^2 coeffs. 

69 ''' 

70 N = Aux.N 

71 if 0 <= auxout < N and 0 <= auxin < N: 

72 return N * auxout + auxin 

73 raise AuxError(auxout=auxout, auxin=auxin, N=N) 

74 

75 def Greek(self, aux): 

76 '''Get an angle's name (C{str}). 

77 ''' 

78 return _Aux2Greek.get(aux, NN) 

79 

80 def len(self, ALorder): # PYCHOK no cover 

81 aL = ALorder # aka Lmax 

82 mu = Aux.MU * (Aux.MU + 1) 

83 nu = Aux.N2 - Aux.N - mu 

84 return (mu * (aL * (aL + 3) - (aL // 2) * 2) // 4 + 

85 nu * (aL * (aL + 1)) // 2) 

86 

87 def power(self, auxout, auxin): 

88 '''Get the C{convert} exponent (C{int} or C{None}). 

89 ''' 

90 self._1d(auxout, auxin) # validate 

91 return (auxout - auxin) if max(auxin, auxout) < Aux.MU else None 

92 

93 def use_n2(self, aux): 

94 return aux not in (Aux.CHI, Aux.XI) 

95 

96Aux = Aux() # PYCHOK singleton 

97 

98_Aux2Greek = {Aux.AUTHALIC: 'Xi', 

99 Aux.CONFORMAL: 'Chi', 

100 Aux.GEOCENTRIC: 'Theta', 

101 Aux.GEODETIC: 'Phi', # == .GEOGRAPHIC 

102 Aux.PARAMETRIC: 'Beta', # == .REDUCED 

103 Aux.RECTIFYING: 'Mu'} 

104_Greek2Aux = dict(map(reversed, _Aux2Greek.items())) # PYCHOK exported 

105# _Greek2Aux.update((_g.upper(), _x) for _g, _x in _Greek2Aux.items()) 

106 

107 

108def _Dasinh(x, y): 

109 d = y - x 

110 if isinf(d): # PYCHOK no cover 

111 r = _0_0 

112 elif isnan(d): # PYCHOK no cover 

113 r = NAN 

114 elif d: 

115 xy = x * y 

116 if xy > 0: 

117 hx, hy = _sc(x), _sc(y) 

118 if xy < 1: 

119 hx, hy = hy, hx 

120 else: 

121 x = _1_0 / x 

122 y = _1_0 / y 

123 r = _over(x + y, hx * x + hy * y) 

124 r = asinh(r * d) / d 

125 else: 

126 r = (asinh(y) - asinh(x)) / d 

127 else: 

128 r = _1_over(_sc(x)) 

129 return r 

130 

131 

132def _Datan(x, y): 

133 xy = x * y 

134 r = xy + _1_0 

135 if isnan(r): # PYCHOK no cover 

136 pass 

137 elif x == y: 

138 r = _1_over(r) 

139 elif x > 0 and isinf(xy): # PYCHOK no cover 

140 r = _0_0 

141 else: 

142 d = y - x 

143 if (r + xy) > 0: 

144 r = atan1(d, r) / d # atan(d / r) / d 

145 else: 

146 r = (atan1(y) - atan1(x)) / d 

147 return r 

148 

149 

150def _Dh(x, y): 

151 r = x + y 

152 if isnan(r): 

153 pass # N.B. NAN for inf-inf 

154 elif isinf(x): # PYCHOK no cover 

155 r = copysign(_0_5, x) 

156 elif isinf(y): # PYCHOK no cover 

157 r = copysign(_0_5, y) 

158 else: 

159 snx, sny = _sn(x), _sn(y) 

160 dy = sny * y 

161 dx = snx * x 

162 d = dy + dx 

163 if (d * _0_5): 

164 if (x * y) > 0: 

165 r *= hypot2_(snx / _sc(y), snx * sny, 

166 sny / _sc(x)) / (d + d) 

167 else: 

168 r = _over((dy - dx) * _0_5, y - x) 

169 else: # underflow and x == y == d == 0 

170 r *= _0_5 # PYCHOK no cover 

171 return r 

172 

173 

174def _Dlam(x, y): # Chi1.tan, Chi2.tan 

175 # I{Divided difference} of the isometric latitude 

176 # with respect to the conformal latitude 

177 if isnan(x) or isnan(y): # PYCHOK no cover 

178 r = NAN 

179 elif isinf(x) or isinf(y): # PYCHOK no cover 

180 r = INF 

181 elif x == y: 

182 r = _sc(x) 

183 else: 

184 r = _over(_Dasinh(x, y), _Datan(x, y)) 

185 return r 

186 

187 

188def _Dm(X, Y, s): # in .auxDLat, .auxDST 

189 # Return M{(X - Y) * s}, inplace X 

190 X -= Y 

191 X *= s 

192 return X # Fsum 

193 

194 

195def _Dp0Dpsi(x, y): # Chi1.tan, Chi2.tan 

196 # I{Divided difference} of the spherical rhumb area 

197 # term with respect to the isometric latitude 

198 r = x + y 

199 if isnan(r): # PYCHOK no cover 

200 pass # NAN for inf-inf 

201 elif isinf(x): # PYCHOK no cover 

202 r = _copysign_1_0(x) 

203 elif isinf(y): # PYCHOK no cover 

204 r = _copysign_1_0(y) 

205 elif x == y: 

206 r = _sn(x) 

207 else: 

208 r = _Dasinh(_h(x), _h(y)) 

209 r = _over(_Dh(x, y) * r, _Dasinh(x, y)) 

210 return r 

211 

212 

213def _h(tx): 

214 '''(INTERNAL) M{h(tan(x)) = tan(x) * sin(x) / 2} 

215 ''' 

216 if tx: 

217 tx *= _sn(tx) * _0_5 

218 return tx # preserve signed-0 

219 

220 

221def _sn(tx): 

222 '''(INTERNAL) M{sin(x) = tan(x) / sqrt(tan(x)**2 + 1)}. 

223 ''' 

224 if tx: 

225 tx = _copysign_1_0(tx) if isinf(tx) else ( 

226 NAN if isnan(tx) else (tx / _sc(tx))) 

227 return tx # preserve signed-0 

228 

229 

230__all__ += _ALL_DOCS(Aux.__class__) 

231 

232# **) MIT License 

233# 

234# Copyright (C) 2023-2025 -- mrJean1 at Gmail -- All Rights Reserved. 

235# 

236# Permission is hereby granted, free of charge, to any person obtaining a 

237# copy of this software and associated documentation files (the "Software"), 

238# to deal in the Software without restriction, including without limitation 

239# the rights to use, copy, modify, merge, publish, distribute, sublicense, 

240# and/or sell copies of the Software, and to permit persons to whom the 

241# Software is furnished to do so, subject to the following conditions: 

242# 

243# The above copyright notice and this permission notice shall be included 

244# in all copies or substantial portions of the Software. 

245# 

246# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 

247# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

248# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 

249# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 

250# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 

251# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

252# OTHER DEALINGS IN THE SOFTWARE.