Coverage for pygeodesy/auxilats/auxLat.py: 87%

442 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-08-02 18:24 -0400

1 

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

3 

4u'''Class L{AuxLat} transcoded to Python from I{Karney}'s C++ class U{AuxLatitude 

5<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AuxLatitude.html>} 

6in I{GeographicLib version 2.2+}. 

7 

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

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

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

11 

12@see: U{Auxiliary latitudes<https:#GeographicLib.SourceForge.io/C++/doc/auxlat.html>} 

13 U{On auxiliary latitudes<https:#ArXiv.org/abs/2212.05818>}. 

14''' 

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

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

17 

18from pygeodesy.auxilats.auxAngle import AuxAngle, AuxBeta, AuxChi, _AuxClass, \ 

19 AuxMu, AuxPhi, AuxTheta, AuxXi 

20from pygeodesy.auxilats.auxily import Aux, _sc, _sn, _Ufloats, atan1 

21from pygeodesy.basics import _reverange, _xinstanceof, _passarg 

22from pygeodesy.constants import INF, MAX_EXP, MIN_EXP, NAN, PI_2, PI_4, _EPSqrt, \ 

23 _0_0, _0_0s, _0_1, _0_25, _0_5, _1_0, _2_0, _3_0, \ 

24 _360_0, isfinite, isinf, isnan, _log2, _over 

25from pygeodesy.datums import _ellipsoidal_datum, _WGS84, \ 

26 Ellipsoid, _name__, _EWGS84 

27# from pygeodesy.ellipsoids import Ellipsoid, _EWGS84 # from .datums 

28from pygeodesy.elliptic import Elliptic as _Ef 

29from pygeodesy.errors import AuxError, _xkwds_not, _xkwds_pop2, _Xorder 

30# from pygeodesy.fmath import cbrt # from .karney 

31from pygeodesy.fsums import Fsum, _Fsumf_, _sum 

32# from pygeodesy.internals import _passarg # from .basics 

33from pygeodesy.interns import NN, _DOT_, _not_scalar_, _UNDER_ 

34from pygeodesy.karney import _2cos2x, _polynomial, _ALL_DOCS, cbrt, _MODS 

35# from pygeodesy.lazily import _ALL_DOCS, _ALL_MODS as _MODS # from .karney 

36# from pygeodesy.named import _name__ # from .datums 

37from pygeodesy.props import Property, Property_RO, _update_all 

38from pygeodesy.units import _isDegrees, _isRadius, Degrees, Meter 

39# from pygeodesy.utily import atan1 # from .auxily 

40 

41from math import asinh, atan2, copysign, cosh, fabs, sin, sinh, sqrt 

42try: 

43 from math import exp2 as _exp2 

44except ImportError: # Python 3.11- 

45 

46 def _exp2(x): 

47 return pow(_2_0, x) 

48 

49__all__ = () 

50__version__ = '24.06.16' 

51 

52_TRIPS = 1024 # XXX 2 or 3? 

53 

54 

55class AuxLat(AuxAngle): 

56 '''Base class for accurate conversion between I{Auxiliary} latitudes 

57 on an ellipsoid. 

58 

59 Latitudes are represented by L{AuxAngle} instances in order to 

60 maintain precision near the poles, I{Authalic} latitude C{Xi}, 

61 I{Conformal} C{Chi}, I{Geocentric} C{Theta}, I{Geographic} C{Phi}, 

62 I{Parametric} C{Beta} and I{Rectifying} C{Mu}. 

63 

64 @see: I{Karney}'s C++ class U{AuxLatitude 

65 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1AuxLatitude.html>}. 

66 ''' 

67 _csc = dict() # global coeffs cache: [aL][k], upto max(k) * (4 + 6 + 8) floats 

68 _E = _EWGS84 

69# _Lmax = 0 # overwritten below 

70 _mAL = 6 # 4, 6 or 8 aka Lmax 

71 

72 def __init__(self, a_earth=_EWGS84, f=None, b=None, **ALorder_name): 

73 '''New L{AuxLat} instance on an ellipsoid or datum. 

74 

75 @arg a_earth: Equatorial radius, semi-axis (C{meter}) or an ellipsoid or 

76 datum (L{Datum}, L{Ellipsoid}, L{Ellipsoid2} or L{a_f2Tuple}). 

77 @kwarg f: Flattening: M{(a - b) / a} (C{float}, near zero for spherical), 

78 required if B{C{a_earth}} is C{scalar} and C{B{b}=None}. 

79 @kwarg b: Optional polar radius, semi-axis (C{meter}, required if B{C{a_earth}} 

80 is C{scalar} and C{B{f}=None}. 

81 @kwarg ALorder_name: Optional C{B{name}=NN} (C{str}) and optional order of 

82 this L{AuxLat} C{B{ALorder}=6}, see property C{ALorder}. 

83 ''' 

84 if ALorder_name: 

85 M = self._mAL 

86 m, name = _xkwds_pop2(ALorder_name, ALorder=M) 

87 if m != M: 

88 self.ALorder = m 

89 else: 

90 name = NN 

91 try: 

92 if a_earth not in (_EWGS84, _WGS84): 

93 n = _name__(name, name__=AuxLat) 

94 if b is f is None: 

95 E = _ellipsoidal_datum(a_earth, name=n).ellipsoid # XXX raiser=_earth_ 

96 elif _isRadius(a_earth): 

97 E = Ellipsoid(a_earth, f=f, b=b, name=_UNDER_(NN, n)) 

98 else: 

99 raise ValueError(_not_scalar_) 

100 self._E = E 

101 elif not (b is f is None): 

102 # turn _UnexpectedError into AuxError 

103 name = _name__(name, **_xkwds_not(None, b=b, f=f)) 

104 

105 if name: 

106 self.name = name 

107 except Exception as x: 

108 raise AuxError(a_earth=a_earth, f=f, b=b, cause=x) 

109 

110 @Property_RO 

111 def a(self): 

112 '''Get the C{ellipsoid}'s equatorial radius (C{meter}, conventionally). 

113 ''' 

114 return self.ellipsoid.a 

115 

116 equatoradius = a 

117 

118 @Property 

119 def ALorder(self): 

120 '''Get the I{AuxLat} order (C{int}, 4, 6 or 8). 

121 ''' 

122 return self._mAL 

123 

124 @ALorder.setter # PYCHOK setter! 

125 def ALorder(self, order): 

126 '''Set the I{AuxLat} order (C{int}, 4, 6 or 8). 

127 ''' 

128 m = _Xorder(_AR2Coeffs, AuxError, ALorder=order) 

129 if self._mAL != m: 

130 _update_all(self) 

131 self._mAL = m 

132 

133 def _atanhee(self, tphi): # see Ellipsoid._es_atanh, .albers._atanhee 

134 # atanh(e * sphi) = asinh(e' * sbeta) 

135 f = self.f 

136 s = _sn(self._fm1 * tphi) if f > 0 else _sn(tphi) 

137 if f: # atanh(e * sphi) = asinh(e' * sbeta) 

138 e = self._e 

139 s = _over(atan1(e * s) if f < 0 else asinh(self._e1 * s), e) 

140 return s 

141 

142 def Authalic(self, Phi, **diff_name): 

143 '''Convert I{Geographic} to I{Aunthalic} latitude. 

144 

145 @arg Phi: Geographic latitude (L{AuxAngle}). 

146 @kwarg diff_name: Use C{B{diff}=True} to set C{diff} 

147 optional C{B{name}=NN}. 

148 

149 @return: Parametric latitude, C{Beta} (L{AuxAngle}). 

150 ''' 

151 _xinstanceof(AuxAngle, Phi=Phi) 

152 # assert Phi._AUX == Aux.PHI 

153 tphi = fabs(Phi.tan) 

154 if isfinite(tphi) and tphi and self.f: 

155 y, x = Phi._yx_normalized 

156 q = self._q 

157 qv = self._qf(tphi) 

158 Dq2 = self._Dq(tphi) 

159 Dq2 *= (q + qv) / (fabs(y) + _1_0) # _Dq(-tphi) 

160 d, n = _diff_name2(Phi, **diff_name) 

161 Xi = AuxXi(copysign(qv, Phi.y), x * sqrt(Dq2), name=n) 

162 

163 if d: 

164 if isnan(tphi): 

165 d = self._e2m1_sq2 

166 else: 

167 c = self.Parametric(Phi)._x_normalized 

168 d = _over(c, Xi._x_normalized)**3 

169 d *= _over(c, x) * _over(_2_0, q) 

170 Xi._diff = d 

171 else: 

172 Xi = AuxXi(*Phi._yx) # diff default 

173 # assert Xi._AUX == Aux.XI 

174 return Xi 

175 

176 def AuthalicRadius2(self, exact=False, f_max=_0_1): 

177 '''Get the I{Authalic} radius I{squared}. 

178 

179 @kwarg exact: If C{True}, use the exact expression, otherwise 

180 the I{Taylor} series. 

181 @kwarg f_max: C{Flattening} not to exceed (C{float}). 

182 

183 @return: Authalic radius I{squared} (C{meter} I{squared}, same 

184 units as the ellipsoid axes). 

185 

186 @raise AuxError: If C{B{exact}=False} and C{abs(flattening)} 

187 exceeds C{f_max}. 

188 ''' 

189 f = self.f 

190 if exact or not f: 

191 c2 = self.ellipsoid.b2 * self._q # == ellipsoid.c2x * 2 

192 elif fabs(f) < f_max: 

193 # Using a * (a + b) / 2 as the multiplying factor leads to a rapidly 

194 # converging series in n. Of course, using this series isn't really 

195 # necessary, since the exact expression is simple to evaluate. However, 

196 # we do it for consistency with RectifyingRadius and, presumably, the 

197 # roundoff error is smaller compared to that for the exact expression. 

198 m = self.ALorder 

199 c2 = _polynomial(self._n, _AR2Coeffs[m], 0, m) 

200 c2 *= self.a * (self.a + self.b) 

201 else: 

202 raise AuxError(exact=exact, f=f, f_max=f_max) 

203 return c2 * _0_5 

204 

205 @Property_RO 

206 def b(self): 

207 '''Get the C{ellipsoid}'s polar radius (C{meter}, conventionally). 

208 ''' 

209 return self.ellipsoid.b # a * (_1_0 - f) 

210 

211 polaradius = b 

212 

213 def _coeffs(self, auxout, auxin): 

214 # Get the polynomial coefficients as 4-, 6- or 8-tuple 

215 aL = self.ALorder # aka Lmax 

216 if auxout == auxin: 

217 return _0_0s(aL) # uncached 

218 

219 k = Aux._1d(auxout, auxin) 

220 try: # cached 

221 return AuxLat._csc[aL][k] 

222 except KeyError: 

223 pass 

224 

225 Cx = _CXcoeffs(aL) 

226 try: 

227 Cx = Cx[auxout][auxin] 

228 except KeyError as x: 

229 raise AuxError(auxout=auxout, auxin=auxin, cause=x) 

230 

231 d = x = n = self._n 

232 if Aux.use_n2(auxin) and Aux.use_n2(auxout): 

233 x = self._n2 

234 

235 def _m(aL): 

236 for m in _reverange(aL): 

237 yield m // 2 

238 else: 

239 _m = _reverange # PYCHOK expected 

240 

241 i = 0 

242 cs = [] 

243 _c = cs.append 

244 _p = _polynomial 

245 for m in _m(aL): 

246 j = i + m + 1 # order m = j - i - 1 

247 _c(_p(x, Cx, i, j) * d) 

248 d *= n 

249 i = j 

250 # assert i == len(Cx) and len(cs) == aL 

251 AuxLat._csc.setdefault(aL, {})[k] = cs = tuple(cs) 

252 return cs 

253 

254 def Conformal(self, Phi, **diff_name): 

255 '''Convert I{Geographic} to I{Conformal} latitude. 

256 

257 @arg Phi: Geographic latitude (L{AuxAngle}). 

258 @kwarg diff_name: Use C{B{diff}=True} to set C{diff} 

259 and an optional C{B{name}=NN}. 

260 

261 @return: Conformal latitude, C{Chi} (L{AuxAngle}). 

262 ''' 

263 _xinstanceof(AuxAngle, Phi=Phi) 

264 # assert Phi._AUX == Aux.PHI 

265 tphi = tchi = fabs(Phi.tan) 

266 if isfinite(tphi) and tphi and self.f: 

267 sig = sinh(self._atanhee(tphi) * self._e2) 

268 scsig = _sc(sig) 

269 scphi = _sc(tphi) 

270 if self.f > 0: 

271 # The general expression for tchi is 

272 # tphi * scsig - sig * scphi 

273 # This involves cancellation if f > 0, so change to 

274 # (tphi - sig) * (tphi + sig) / (tphi * scsig + sig * scphi) 

275 # To control overflow, write as (sigtphi = sig / tphi) 

276 # (tphi - sig) * (1 + sigtphi) / (scsig + sigtphi * scphi) 

277 sigtphi = sig / tphi 

278 if sig < (tphi * _0_5): 

279 t = tphi - sig 

280 else: 

281 def _asinh_2(x): 

282 return asinh(x) * _0_5 

283 # Still possibly dangerous cancellation in tphi - sig. 

284 # Write tphi - sig = (1 - e) * Dg(1, e) 

285 # Dg(x, y) = (g(x) - g(y)) / (x - y) 

286 # g(x) = sinh(x * atanh(sphi * x)) 

287 # Note sinh(atanh(sphi)) = tphi 

288 # Turn the crank on divided differences, substitute 

289 # sphi = tphi / _sc(tphi) 

290 # atanh(x) = asinh(x / sqrt(1 - x^2)) 

291 e = self._e 

292 em1 = self._e2m1 / (_1_0 + e) 

293 # assert em1 != 0 

294 scb = self._scbeta(tphi) 

295 scphib = scphi / scb # sec(phi) / sec(beta) 

296 atphib = _asinh_2(tphi * e / scb) # atanh(e * sphi) 

297 atphi = _asinh_2(tphi) # atanh(sphi) 

298 t = _asinh_2(em1 * (tphi * scphib)) / em1 

299 try: 

300 Dg = _Fsumf_(atphi, atphib, t, e * t) 

301 except ValueError: # Fsum(NAN) exception 

302 Dg = _sum((atphi, atphib, t, e * t)) 

303 e *= atphib 

304 t = atphi - e 

305 if t: # sinh(0) == 0 

306 Dg *= sinh(t) / t * cosh(atphi + e) * em1 

307 t = float(Dg) # tphi - sig 

308 tchi = _over(t * (_1_0 + sigtphi), 

309 scsig + scphi * sigtphi) if t else _0_0 

310 else: 

311 tchi = tphi * scsig - sig * scphi 

312 

313 d, n = _diff_name2(Phi, **diff_name) 

314 Chi = AuxChi(tchi, name=n).copyquadrant(Phi) 

315 

316 if d: 

317 if isinf(tphi): # PYCHOK np cover 

318 d = self._conformal_diff 

319 else: 

320 d = self.Parametric(Phi)._x_normalized 

321 if d: 

322 d = _over(d, Chi._x_normalized) * \ 

323 _over(d, Phi._x_normalized) * self._e2m1 

324 Chi._diff = d 

325 # assrt Chi._AUX == Aux.CHI 

326 return Chi 

327 

328 @Property_RO 

329 def _conformal_diff(self): # PYCHOK no cover 

330 '''(INTERNAL) Constant I{Conformal} diff. 

331 ''' 

332 e = self._e 

333 if self.f > 0: 

334 ss = sinh(asinh(self._e1) * e) 

335 d = _over(_1_0, _sc(ss) + ss) 

336 elif e: 

337 ss = sinh(-atan1(e) * e) 

338 d = _sc(ss) - ss 

339 else: 

340 d = _1_0 

341 return d 

342 

343 def convert(self, auxout, Zeta_d, exact=False): 

344 # Convert I{Auxiliary} or I{scalar} latitude 

345 Z = d = Zeta_d 

346 if isinstance(Z, AuxAngle): 

347 A, auxin = _AuxClass(auxout), Z._AUX 

348 if auxin == auxout: 

349 pass 

350 elif not (isfinite(Z.tan) and Z.tan): # XXX 

351 Z = A(*Z._yx, aux=auxout, name=Z.name) 

352 elif exact: 

353 p = Aux.power(auxout, auxin) 

354 if p is None: 

355 P = self._fromAux(Z) # Phi 

356 Z = self._toAux(auxout, P) 

357 Z._iter = P.iteration 

358 else: 

359 y, x = Z._yx 

360 if p: 

361 y *= pow(self._fm1, p) 

362 Z = A(y, x, aux=auxout, name=Z.name) 

363 else: 

364 cs = self._coeffs(auxout, auxin) 

365 yx = Z._yx_normalized 

366 Z = A(*yx, aux=auxout, name=Z.name) 

367 # assert Z._yx == yx 

368 r = _Clenshaw(True, Z, cs, self.ALorder) 

369 Z += AuxAngle.fromRadians(r, aux=auxout) 

370 # assert Z._AUX == auxout 

371 return Z 

372 

373 elif _isDegrees(d): 

374 Z = AuxPhi.fromDegrees(d) 

375 d = round((d - Z.toDegrees) / _360_0) * _360_0 

376 d += self.convert(auxout, Z, exact).toDegrees 

377 return Degrees(d, name=Aux.Greek(auxout)) 

378 

379 raise AuxError(auxout=auxout, Zeta_d=Zeta_d, exact=exact) 

380 

381 def _Dq(self, tphi): 

382 # I{Divided Difference} of (q(1) - q(sphi)) / (1 - sphi). 

383 sphi = _sn(tphi) 

384 if tphi > 0: 

385 scphi = _sc(tphi) 

386 d = _1_0 / (scphi**2 * (_1_0 + sphi)) # XXX - sphi 

387 if d: 

388 # General expression for _Dq(1, sphi) is 

389 # atanh(e * d / (1 - e2 * sphi)) / (e * d) + 

390 # (1 + e2 * sphi) / ((1 - e2 * sphi * sphi) * e2m1) 

391 # with atanh(e * d / (1 - e2 * sphi)) = 

392 # atanh(e * d * scphi / (scphi - e2 * tphi)) 

393 e2m1, ed = self._e2m1, (self._e * d) 

394 if e2m1 and ed: 

395 e2 = self._e2 

396 if e2 > 0: # assert self.f > 0 

397 scb = self._scbeta(tphi) 

398 q = scphib = scphi / scb 

399 q *= (scphi + tphi * e2) / (e2m1 * scb) 

400 q += asinh(self._e1 * d * scphib) / ed 

401 else: 

402 s2 = sphi * e2 

403 q = (_1_0 + s2) / ((_1_0 - sphi * s2) * e2m1) 

404 q += (atan2(ed, _1_0 - s2) / ed) if e2 < 0 else _1_0 

405 else: # PYCHOK no cover 

406 q = INF 

407 else: # PYCHOK no cover 

408 q = self._2_e2m12 

409 else: # not reached, open-coded in .Authalic 

410 q = _over(self._q - self._qf(tphi), _1_0 - sphi) 

411 return q 

412 

413 @Property_RO 

414 def _e(self): # unsigned, (1st) eccentricity 

415 return self.ellipsoid.e # sqrt(fabs(self._e2)) 

416 

417 @Property_RO 

418 def _e1(self): 

419 return sqrt(fabs(self._e12)) 

420 

421 @Property_RO 

422 def _e12(self): 

423 return _over(self._e2, _1_0 - self._e2) 

424 

425 @Property_RO 

426 def _e12p1(self): 

427 return _1_0 / self._e2m1 

428 

429 @Property_RO 

430 def _e2(self): # signed, (1st) eccentricity squared 

431 return self.ellipsoid.e2 

432 

433 @Property_RO 

434 def _e2m1(self): # 1 less 1st eccentricity squared 

435 return self.ellipsoid.e21 # == ._fm1**2 

436 

437 @Property_RO 

438 def _e2m1_sq2(self): 

439 return self._e2m1 * sqrt(self._q * _0_5) 

440 

441 @Property_RO 

442 def _2_e2m12(self): 

443 return _2_0 / self._e2m1**2 

444 

445 @Property_RO 

446 def _Ef_fRG_a2b2_PI_4(self): 

447 E = self.ellipsoid 

448 return _Ef.fRG(E.a2, E.b2) / PI_4 

449 

450 @Property_RO 

451 def ellipsoid(self): 

452 '''Get the ellipsoid (L{Ellipsoid}). 

453 ''' 

454 return self._E 

455 

456 @Property_RO 

457 def f(self): 

458 '''Get the C{ellipsoid}'s flattening (C{scalar}). 

459 ''' 

460 return self.ellipsoid.f 

461 

462 flattening = f 

463 

464 @Property_RO 

465 def _fm1(self): # 1 - flattening 

466 return self.ellipsoid.f1 

467 

468 def _fromAux(self, Zeta, **name): 

469 '''Convert I{Auxiliary} to I{Geographic} latitude. 

470 

471 @arg Zeta: Auxiliary latitude (L{AuxAngle}). 

472 @kwarg name: Optional C{B{name}=NN} (C{str}). 

473 

474 @return: Geographic latitude, I{Phi} (L{AuxAngle}). 

475 ''' 

476 _xinstanceof(AuxAngle, Zeta=Zeta) 

477 aux = Zeta._AUX 

478 n = _name__(name, _or_nameof=Zeta) 

479 f = self._fromAuxCase.get(aux, None) 

480 if f is None: 

481 Phi = AuxPhi(NAN, name=n) 

482 elif callable(f): 

483 t = self._fm1 

484 t *= f(t) 

485 Phi = _Newton(t, Zeta, self._toZeta(aux), name=n) 

486 else: # assert isscalar(f) 

487 y, x = Zeta._yx 

488 Phi = AuxPhi(y / f, x, name=n) 

489 # assert Phi._AUX == Aux.PHI 

490 return Phi 

491 

492 @Property_RO 

493 def _fromAuxCase(self): 

494 '''(INTERNAL) switch(auxin): ... 

495 ''' 

496 return {Aux.AUTHALIC: cbrt, 

497 Aux.CONFORMAL: _passarg, 

498 Aux.GEOCENTRIC: self._e2m1, 

499 Aux.GEOGRAPHIC: _1_0, 

500 Aux.PARAMETRIC: self._fm1, 

501 Aux.RECTIFYING: sqrt} 

502 

503 def Geocentric(self, Phi, **diff_name): 

504 '''Convert I{Geographic} to I{Geocentric} latitude. 

505 

506 @arg Phi: Geographic latitude (L{AuxAngle}). 

507 @kwarg diff_name: Use C{B{diff}=True} to set C{diff} 

508 and an optional C{B{name}=NN}. 

509 

510 @return: Geocentric latitude, C{Phi} (L{AuxAngle}). 

511 ''' 

512 _xinstanceof(AuxAngle, Phi=Phi) 

513 # assert Phi._AUX == Aux.PHI 

514 d, n = _diff_name2(Phi, **diff_name) 

515 Theta = AuxTheta(Phi.y * self._e2m1, Phi.x, name=n) 

516 if d: 

517 Theta._diff = self._e2m1 

518 return Theta 

519 

520 def Geodetic(self, Phi, **name): # PYCHOK no cover 

521 '''Convert I{Geographic} to I{Geodetic} latitude. 

522 

523 @arg Phi: Geographic latitude (L{AuxAngle}). 

524 @kwarg name: Optional C{B{name}=NN} (C{str}). 

525 

526 @return: Geodetic latitude, C{Phi} (L{AuxAngle}). 

527 ''' 

528 _xinstanceof(AuxAngle, Phi=Phi) 

529 # assert Phi._AUX == Aux.PHI 

530 _, n = _diff_name2(Phi, **name) 

531 return AuxPhi(Phi, name=n) 

532 

533 @Property_RO 

534 def _n(self): # 3rd flattening 

535 return self.ellipsoid.n 

536 

537 @Property_RO 

538 def _n2(self): 

539 return self._n**2 

540 

541 def Parametric(self, Phi, **diff_name): 

542 '''Convert I{Geographic} to I{Parametric} latitude. 

543 

544 @arg Phi: Geographic latitude (L{AuxAngle}). 

545 @kwarg diff_name: Use C{B{diff}=True} to set C{diff} 

546 and an optional C{B{name}=NN}. 

547 

548 @return: Parametric latitude, C{Beta} (L{AuxAngle}). 

549 ''' 

550 _xinstanceof(AuxAngle, Phi=Phi) 

551 # assert Phi._AUX == Aux.PHI 

552 d, n = _diff_name2(Phi, **diff_name) 

553 Beta = AuxBeta(Phi.y * self._fm1, Phi.x, name=n) 

554 if d: 

555 Beta._diff = self._fm1 

556 return Beta 

557 

558 Reduced = Parametric 

559 

560 @Property_RO 

561 def _q(self): # constant _q 

562 q, f = self._e12p1, self.f 

563 if f: 

564 e = self._e 

565 q += _over(asinh(self._e1) if f > 0 else atan1(e), e) 

566 else: 

567 q += _1_0 

568 return q 

569 

570 def _qf(self, tphi): 

571 # function _q: atanh(e * sphi) / e + sphi / (1 - (e * sphi)^2) 

572 scb = self._scbeta(tphi) 

573 return self._atanhee(tphi) + (tphi / scb) * (_sc(tphi) / scb) 

574 

575 def _qIntegrand(self, beta): 

576 # pbeta(beta) = integrate(q(beta), beta), with beta in radians 

577 # q(beta) = (1-f) * (sin(xi) - sin(chi)) / cos(phi) 

578 # = (1-f) * (cos(chi) - cos(xi)) / cos(phi) * 

579 # (cos(xi) + cos(chi)) / (sin(xi) + sin(chi)) 

580 # Fit q(beta)/cos(beta) with Fourier transform 

581 # q(beta)/cos(beta) = sum(c[k] * sin((2*k+1)*beta), k, 0, K-1) 

582 # then the integral is 

583 # pbeta = sum(d[k] * cos((2*k+2)*beta), k, 0, K-1) 

584 # where 

585 # d[k] = -1/(4*(k+1)) * (c[k] + c[k+1]) for k in 0..K-2 

586 # d[K-1] = -1/(4*K) * c[K-1] 

587 Beta = AuxBeta.fromRadians(beta) 

588 if Beta.x: # and self._fm1: 

589 Ax, _cv = Aux, self.convert 

590 Phi = _cv(Ax.PHI, Beta, exact=True) 

591 schi, cchi = _cv(Ax.CHI, Phi, exact=True)._yx_normalized 

592 sxi, cxi = _cv(Ax.XI, Phi, exact=True)._yx_normalized 

593 r = (sxi - schi) if fabs(schi) < fabs(cchi) else \ 

594 _over(_2cos2x(cchi, cxi), (sxi + schi) * _2_0) 

595 r *= _over(self._fm1, Phi._x_normalized * Beta._x_normalized) 

596 else: # beta == PI_2, PI3_2, ... 

597 r = _0_0 # XXX 0 avoids NAN summation exceptions 

598 return r 

599 

600 def Rectifying(self, Phi, **diff_name): 

601 '''Convert I{Geographic} to I{Rectifying} latitude. 

602 

603 @arg Phi: Geographic latitude (L{AuxAngle}). 

604 @kwarg diff_name: Use C{B{diff}=True} to set C{diff} 

605 and an optional C{B{name}=NN}. 

606 

607 @return: Rectifying latitude, C{Mu} (L{AuxAngle}). 

608 ''' 

609 Beta = self.Parametric(Phi) 

610 # assert Beta._AUX == Aux.BETA 

611 sb, cb = map(fabs, Beta._yx_normalized) 

612 a, ka, ka1 = _1_0, self._e2, self._e2m1 

613 b, kb, kb1 = self._fm1, -self._e12, self._e12p1 

614 if self.f < 0: 

615 a, b = b, a 

616 ka, kb = kb, ka 

617 ka1, kb1 = kb1, ka1 

618 sb, cb = cb, sb 

619 # now a, b = larger/smaller semiaxis 

620 # Beta measured from larger semiaxis 

621 # kb, ka = modulus-squared for distance from Beta = 0, pi/2 

622 # NB kb <= 0; 0 <= ka <= 1 

623 # sa = b*E(Beta, sqrt(kb)) 

624 # sb = a*E(Beta',sqrt(ka)) 

625 # 1 - ka * (1 - sb2) = 1 - ka + ka*sb2 

626 sb2 = sb**2 

627 cb2 = cb**2 

628 da2 = ka1 + ka * sb2 

629 db2 = _1_0 - kb * sb2 

630 # DLMF Eq. 19.25.9 

631 my = b * sb * _Ef._RFRD(cb2, db2, _1_0, kb * sb2) 

632 # DLMF Eq. 19.25.10 with complementary angles 

633 mx = a * cb * (_Ef.fRF(sb2, da2, _1_0) * ka1 + 

634 ka * cb2 * _Ef.fRD(sb2, _1_0, da2, _3_0) * ka1 + 

635 ka * sb / sqrt(da2)) 

636 # my + mx = 2*_Ef.fRG(a*a, b*b) = a*E(e) = b*E(i*e') 

637 # mr = a*E(e)*(2/pi) = b*E(i*e')*(2/pi) 

638 if self.f < 0: 

639 a, b = b, a 

640 my, mx = mx, my 

641 mr = (my + mx) / PI_2 

642 if mr: 

643 my = sin(my / mr) 

644 mx = sin(mx / mr) # XXX zero? 

645 else: # zero Mu 

646 my, mx = _0_0, _1_0 

647 d, n = _diff_name2(Phi, **diff_name) 

648 Mu = AuxMu(my, mx, # normalized 

649 name=n).copyquadrant(Phi) 

650 if d: 

651 d, x = _0_0, Beta._x_normalized 

652 if x and mr: 

653 if Mu.x and Phi.x and not isinf(Phi.tan): 

654 d = b / mr * (x / Mu.x)**2 \ 

655 * (x / Phi._x_normalized) 

656 else: 

657 d = mr / a 

658 Mu._diff = self._fm1 * d 

659 return Mu 

660 

661 def RectifyingRadius(self, exact=False): 

662 '''Get the I{Rectifying} radius. 

663 

664 @arg exact: If C{True}, use the exact expression, 

665 otherwise the I{Taylor} series. 

666 

667 @return: Rectifying radius (L{Meter}, same units 

668 as the ellipsoid axes). 

669 ''' 

670 r = self._Ef_fRG_a2b2_PI_4 if exact else self._RectifyingR 

671 return Meter(r, name__=self.RectifyingRadius) 

672 

673 @Property_RO 

674 def _RectifyingR(self): 

675 m = self.ALorder 

676 d = _polynomial(self._n2, _RRCoeffs[m], 0, m // 2) 

677 return d * (self.a + self.b) * _0_5 

678 

679 def _scbeta(self, tphi): 

680 return _sc(self._fm1 * tphi) 

681 

682 def _toAux(self, auxout, Phi, **diff_name): 

683 '''Convert I{Geographic} to I{Auxiliary} latitude. 

684 

685 @arg auxout: I{Auxiliary} kind (C{Aux.KIND}). 

686 @arg Phi: Geographic latitude (L{AuxLat}). 

687 @kwarg diff_name: Use C{B{diff}=True} to set C{diff} 

688 and an optional C{B{name}=NN}. 

689 

690 @return: Auxiliary latitude, I{Eta} (L{AuxLat}). 

691 ''' 

692 _xinstanceof(AuxAngle, Phi=Phi) 

693 # assert Phi._AUX == Aux.PHI 

694 d, n = _diff_name2(Phi, **diff_name) 

695 m = _toAuxCase.get(auxout, None) 

696 if m: # callable 

697 A = m(self, Phi, diff=d, name=n) 

698 elif auxout == Aux.GEODETIC: # == GEOGRAPHIC 

699 A = AuxPhi(Phi, name=n) 

700 else: # auxout? 

701 A = AuxPhi(NAN, name=n) 

702 # assert A._AUX == auxout 

703 return A 

704 

705 def _toZeta(self, zetaux): 

706 '''Return a (lean) function to create C{AuxPhi(tphi)} and 

707 convert that into C{AuxAngle} of (fixed) kind C{zetaux} 

708 for use only inside the C{_Newton} loop. 

709 ''' 

710 class _AuxPhy(AuxPhi): 

711 # lean C{AuxPhi} instance. 

712 # _diff = _1_0 

713 # _x = _1_0 

714 

715 def __init__(self, tphi): # PYCHOK signature 

716 self._y = tphi 

717 

718 m = _toAuxCase.get(zetaux, None) 

719 if m: # callable 

720 

721 def _toZeta(tphi): 

722 return m(self, _AuxPhy(tphi), diff=True) 

723 

724 elif zetaux == Aux.GEODETIC: # GEOGRAPHIC 

725 _toZeta = _AuxPhy 

726 

727 else: # zetaux? 

728 

729 def _toZeta(unused): # PYCHOK expected 

730 return _AuxPhy(NAN) 

731 

732 return _toZeta 

733 

734 

735# switch(auxout): ... 

736_toAuxCase = {Aux.AUTHALIC: AuxLat.Authalic, 

737 Aux.CONFORMAL: AuxLat.Conformal, 

738 Aux.GEOCENTRIC: AuxLat.Geocentric, 

739 Aux.PARAMETRIC: AuxLat.Parametric, 

740 Aux.RECTIFYING: AuxLat.Rectifying} 

741 

742 

743def _Clenshaw(sinp, Zeta, cs, K): 

744 sz, cz = Zeta._yx # isnormal 

745 # Evaluate sum(c[k] * sin((2*k+2) * Zeta)) if sinp else 

746 # sum(c[k] * cos((2*k+2) * Zeta)) 

747 x = _2cos2x(cz, sz) # 2 * cos(2*Zeta) 

748 if isfinite(x): 

749 U0, U1 = Fsum(), Fsum() 

750 # assert len(cs) == K 

751 for r in _reverange(K): 

752 U1 -= U0 * x + cs[r] 

753 U1, U0 = U0, -U1 

754 # u0*f0(Zeta) - u1*fm1(Zeta) 

755 # f0 = sin(2*Zeta) if sinp else cos(2*Zeta) 

756 # fm1 = 0 if sinp else 1 

757 if sinp: 

758 U0 *= sz * cz * _2_0 

759 else: 

760 U0 *= x * _0_5 

761 U0 -= U1 

762 x = float(U0) 

763 return x 

764 

765 

766def _CXcoeffs(aL): # PYCHOK in .auxilats.__main__ 

767 '''(INTERNAL) Get the C{CX_4}, C{_6} or C{_8} coefficients. 

768 ''' 

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

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

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

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

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

774 # assert _coeffs.ALorder == aL 

775 # assert _coeffs.n == Aux.len(aL) 

776 return _coeffs 

777 

778 

779def _diff_name2(Phi, diff=False, **name): 

780 '''(INTERNAL) Get C{{Bdiff}=False} and C{B{name}=NN}. 

781 ''' 

782 n = _name__(name, _or_nameof=Phi) # if name else Phi.name 

783 return diff, n 

784 

785 

786def _Newton(tphi, Zeta, _toZeta, **name): 

787 # Newton's method from AuxLat._fromAux 

788 try: 

789 _lg2 = _log2 

790 _abs = fabs 

791 tz = _abs(Zeta.tan) 

792 tphi = tz / tphi # **) 

793 ltz = _lg2(tz) # **) 

794 ltphi = _lg2(tphi) # **) 

795 ltmin = min(ltphi, MIN_EXP) 

796 ltmax = max(ltphi, MAX_EXP) 

797# auxin = Zeta._AUX 

798 s, n, __2 = 0, 3, _0_5 # n = i + 2 

799 _TOL, _xp2 = _EPSqrt, _exp2 

800 for i in range(1, _TRIPS): # up to 1 Ki! 

801 # _toAux(auxin, AuxPhi(tphi), diff=True) 

802 Z = _toZeta(tphi) 

803 # assert Z._AUX == auxin 

804 t, s_ = Z.tan, s 

805 if t > tz: 

806 ltmax, s = ltphi, +1 

807 elif t < tz: 

808 ltmin, s = ltphi, -1 

809 else: 

810 break 

811 # derivative dtan(Z)/dtan(Phi) 

812 # to dlog(tan(Z))/dlog(tan(Phi)) 

813 d = (ltz - _lg2(t)) * t # **) 

814 if d: 

815 d = d / (Z.diff * tphi) # **) 

816 ltphi += d 

817 tphi = _xp2(ltphi) 

818 if _abs(d) < _TOL: 

819 i += 1 

820 # _toAux(auxin, AuxPhi(tphi), diff=True) 

821 Z = _toZeta(tphi) 

822 tphi -= _over(Z.tan - tz, Z.diff) 

823 break 

824 if (i > n and (s * s_) < 0) or not ltmin < ltphi < ltmax: 

825 s, n = 0, (i + 2) 

826 ltphi = (ltmin + ltmax) * __2 

827 tphi = _xp2(ltphi) 

828 else: 

829 i = _TRIPS 

830 Phi = AuxPhi(tphi, **name).copyquadrant(Zeta) 

831 Phi._iter = i 

832 except (ValueError, ZeroDivisionError): # **) zero t, tphi, tz or Z.diff 

833 Phi = AuxPhi(Zeta, **name) # diff as-as 

834 Phi._iter = 0 

835 # assert Phi._AUX == Aux.PHI 

836 return Phi 

837 

838 

839_f, _u = float, _Ufloats() 

840_1__f3 = -1 / _f(3) # XXX +1 / _f(3) 

841_AR2Coeffs = {4: _u(4 / _f(315), 4 / _f(105), 4 / _f(15), _1__f3), 

842 6: _u(4 / _f(1287), 4 / _f(693), 4 / _f(315), 4 / _f(105), 

843 4 / _f(15), _1__f3), 

844 8: _u(4 / _f(3315), 4 / _f(2145), 4 / _f(1287), 4 / _f(693), 

845 4 / _f(315), 4 / _f(105), 4 / _f(15), _1__f3)} 

846_RRCoeffs = {4: _u(1 / _f(64), _0_25), 

847 6: _u(1 / _f(256), 1 / _f(64), _0_25), 

848 8: _u(25 / _f(16384), 1 / _f(256), 1 / _f(64), _0_25)} # PYCHOK used! 

849del _f, _u, _Ufloats, _1__f3 

850# assert set(_AR2Coeffs.keys()) == set(_RRCoeffs.keys()) 

851 

852# AuxLat._Lmax = max(_AR2Coeffs.keys()) # == max(ALorder) 

853 

854__all__ += _ALL_DOCS(AuxLat) 

855 

856# **) MIT License 

857# 

858# Copyright (C) 2023-2024 -- mrJean1 at Gmail -- All Rights Reserved. 

859# 

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

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

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

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

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

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

866# 

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

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

869# 

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

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

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

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

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

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

876# OTHER DEALINGS IN THE SOFTWARE.