1

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

3

4 u'''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>}

6 in I{GeographicLib version 2.2+}.

7

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

9 under 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

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

17

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

19 AuxMu, AuxPhi, AuxTheta, AuxXi

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

21 from pygeodesy.basics import _reverange, _xinstanceof, _passarg

22 from 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

25 from pygeodesy.datums import _ellipsoidal_datum, _WGS84, Ellipsoid

26# from pygeodesy.ellipsoids import Ellipsoid # from .datums

27 from pygeodesy.elliptic import Elliptic as _Ef

28 from pygeodesy.errors import AuxError, _xkwds, _xkwds_get, _Xorder

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

30 from pygeodesy.fsums import Fsum, _Fsumf_, _sum

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

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

33 from pygeodesy.interns import NN, _DOT_, _UNDER_ # _earth_

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

35 from pygeodesy.props import Property, Property_RO, _update_all

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

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

38

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

40 try:

41 from math import exp2 as _exp2

42 except ImportError: # Python 3.11-

43

44 def _exp2(x):

45 return pow(_2_0, x)

46

47__all__ = ()

48__version__ = '24.04.14'

49

50_TRIPS = 1024 # XXX 2 or 3?

51

52

53 class AuxLat(AuxAngle):

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

55 on an ellipsoid.

56

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

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

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

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

61

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

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

64 '''

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

66 _E = _WGS84.ellipsoid

67# _Lmax = 0 # overwritten below

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

69

70 def __init__(self, a_earth=_WGS84, f=None, b=None, name=NN, **ALorder):

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

72

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

74 ellipsoid or datum (L{Datum}, L{Ellipsoid},

75 L{Ellipsoid2} or L{a_f2Tuple}).

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

77 spherical), ignored if B{C{a_earth}} is not scalar.

78 @kwarg b: Optional polar radius, semi-axis (C{meter}, same

79 units as B{C{a_earth}}), ignored if B{C{a_earth}}

80 is not scalar.

81 @kwarg ALorder: Optional keyword arguments B{C{ALorder}} to

82 set L{AuxLat}'s C{order}, see property

83 C{ALorder}.

84 @kwarg name: Optional name (C{str}).

85 '''

86 if a_earth is not _WGS84:

87 n = name or AuxLat.__name__

88 try:

89 if b is f is None:

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

91 elif _isRadius(a_earth):

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

93 else:

94 raise ValueError()

95 except Exception as x:

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

97 self._E = E

98

99 if name:

100 self.name = name

101 if ALorder:

102 self.ALorder = _xkwds_get(ALorder, ALorder=AuxLat._mAL)

103

104 @Property_RO

105 def a(self):

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

107 '''

108 return self.ellipsoid.a

109

110 equatoradius = a

111

112 @Property

113 def ALorder(self):

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

115 '''

116 return self._mAL

117

118 @ALorder.setter # PYCHOK setter!

119 def ALorder(self, order):

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

121 '''

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

123 if self._mAL != m:

124 _update_all(self)

125 self._mAL = m

126

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

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

129 f = self.f

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

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

132 e = self._e

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

134 return s

135

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

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

138

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

140

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

142 '''

143 _xinstanceof(AuxAngle, Phi=Phi)

144 # assert Phi._AUX == Aux.PHI

145 tphi = fabs(Phi.tan)

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

147 y, x = Phi._yx_normalized

148 q = self._q

149 qv = self._qf(tphi)

150 Dq2 = self._Dq(tphi)

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

152 Xi = AuxXi(copysign(qv, Phi.y), x * sqrt(Dq2),

153 name=_xkwds_get(diff_name, name=Phi.name))

154

155 if _xkwds_get(diff_name, diff=False):

156 if isnan(tphi):

157 d = self._e2m1_sq2

158 else:

159 c = self.Parametric(Phi)._x_normalized

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

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

162 Xi._diff = d

163 else:

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

165 # assert Xi._AUX == Aux.XI

166 return Xi

167

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

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

170

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

172 the I{Taylor} series.

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

174

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

176 units as the ellipsoid axes).

177

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

179 exceeds C{f_max}.

180 '''

181 f = self.f

182 if exact or not f:

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

184 elif fabs(f) < f_max:

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

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

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

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

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

190 m = self.ALorder

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

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

193 else:

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

195 return c2 * _0_5

196

197 @Property_RO

198 def b(self):

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

200 '''

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

202

203 polaradius = b

204

205 def _coeffs(self, auxout, auxin):

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

207 aL = self.ALorder # aka Lmax

208 if auxout == auxin:

209 return _0_0s(aL) # uncached

210

211 k = Aux._1d(auxout, auxin)

212 try: # cached

213 return AuxLat._csc[aL][k]

214 except KeyError:

215 pass

216

217 Cx = _CXcoeffs(aL)

218 try:

219 Cx = Cx[auxout][auxin]

220 except KeyError as x:

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

222

223 d = x = n = self._n

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

225 x = self._n2

226

227 def _m(aL):

228 for m in _reverange(aL):

229 yield m // 2

230 else:

231 _m = _reverange # PYCHOK expected

232

233 i = 0

234 cs = []

235 _c = cs.append

236 _p = _polynomial

237 for m in _m(aL):

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

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

240 d *= n

241 i = j

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

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

244 return cs

245

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

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

248

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

250

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

252 '''

253 _xinstanceof(AuxAngle, Phi=Phi)

254 # assert Phi._AUX == Aux.PHI

255 tphi = tchi = fabs(Phi.tan)

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

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

258 scsig = _sc(sig)

259 scphi = _sc(tphi)

260 if self.f > 0:

261 # The general expression for tchi is

262 # tphi * scsig - sig * scphi

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

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

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

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

267 sigtphi = sig / tphi

268 if sig < (tphi * _0_5):

269 t = tphi - sig

270 else:

271 def _asinh_2(x):

272 return asinh(x) * _0_5

273 # Still possibly dangerous cancellation in tphi - sig.

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

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

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

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

278 # Turn the crank on divided differences, substitute

279 # sphi = tphi / _sc(tphi)

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

281 e = self._e

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

283 # assert em1 != 0

284 scb = self._scbeta(tphi)

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

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

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

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

289 try:

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

291 except ValueError: # Fsum(NAN) exception

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

293 e *= atphib

294 t = atphi - e

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

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

297 t = float(Dg) # tphi - sig

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

299 scsig + scphi * sigtphi) if t else _0_0

300 else:

301 tchi = tphi * scsig - sig * scphi

302

303 n = _xkwds_get(diff_name, name=Phi.name)

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

305

306 if _xkwds_get(diff_name, diff=False):

307 if isinf(tphi): # PYCHOK np cover

308 d = self._conformal_diff

309 else:

310 d = self.Parametric(Phi)._x_normalized

311 if d:

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

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

314 Chi._diff = d

315 # assrt Chi._AUX == Aux.CHI

316 return Chi

317

318 @Property_RO

319 def _conformal_diff(self): # PYCHOK no cover

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

321 '''

322 e = self._e

323 if self.f > 0:

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

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

326 elif e:

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

328 d = _sc(ss) - ss

329 else:

330 d = _1_0

331 return d

332

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

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

335 Z = d = Zeta_d

336 if isinstance(Z, AuxAngle):

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

338 if auxin == auxout:

339 pass

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

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

342 elif exact:

343 p = Aux.power(auxout, auxin)

344 if p is None:

345 P = self._fromAux(Z) # Phi

346 Z = self._toAux(auxout, P)

347 Z._iter = P.iteration

348 else:

349 y, x = Z._yx

350 if p:

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

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

353 else:

354 cs = self._coeffs(auxout, auxin)

355 yx = Z._yx_normalized

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

357 # assert Z._yx == yx

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

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

360 # assert Z._AUX == auxout

361 return Z

362

363 elif _isDegrees(d):

364 Z = AuxPhi.fromDegrees(d)

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

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

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

368

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

370

371 def _Dq(self, tphi):

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

373 sphi = _sn(tphi)

374 if tphi > 0:

375 scphi = _sc(tphi)

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

377 if d:

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

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

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

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

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

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

384 if e2m1 and ed:

385 e2 = self._e2

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

387 scb = self._scbeta(tphi)

388 q = scphib = scphi / scb

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

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

391 else:

392 s2 = sphi * e2

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

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

395 else: # PYCHOK no cover

396 q = INF

397 else: # PYCHOK no cover

398 q = self._2_e2m12

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

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

401 return q

402

403 @Property_RO

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

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

406

407 @Property_RO

408 def _e1(self):

409 return sqrt(fabs(self._e12))

410

411 @Property_RO

412 def _e12(self):

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

414

415 @Property_RO

416 def _e12p1(self):

417 return _1_0 / self._e2m1

418

419 @Property_RO

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

421 return self.ellipsoid.e2

422

423 @Property_RO

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

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

426

427 @Property_RO

428 def _e2m1_sq2(self):

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

430

431 @Property_RO

432 def _2_e2m12(self):

433 return _2_0 / self._e2m1**2

434

435 @Property_RO

436 def _Ef_fRG_a2b2_PI_4(self):

437 E = self.ellipsoid

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

439

440 @Property_RO

441 def ellipsoid(self):

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

443 '''

444 return self._E

445

446 @Property_RO

447 def f(self):

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

449 '''

450 return self.ellipsoid.f

451

452 flattening = f

453

454 @Property_RO

455 def _fm1(self): # 1 - flattening

456 return self.ellipsoid.f1

457

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

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

460

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

462

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

464 '''

465 _xinstanceof(AuxAngle, Zeta=Zeta)

466 aux = Zeta._AUX

467 n = _xkwds_get(name, name=Zeta.name)

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

469 if f is None:

470 Phi = AuxPhi(NAN, name=n)

471 elif callable(f):

472 t = self._fm1

473 t *= f(t)

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

475 else: # assert isscalar(f)

476 y, x = Zeta._yx

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

478 # assert Phi._AUX == Aux.PHI

479 return Phi

480

481 @Property_RO

482 def _fromAuxCase(self):

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

484 '''

485 return {Aux.AUTHALIC: cbrt,

486 Aux.CONFORMAL: _passarg,

487 Aux.GEOCENTRIC: self._e2m1,

488 Aux.GEOGRAPHIC: _1_0,

489 Aux.PARAMETRIC: self._fm1,

490 Aux.RECTIFYING: sqrt}

491

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

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

494

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

496

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

498 '''

499 _xinstanceof(AuxAngle, Phi=Phi)

500 # assert Phi._AUX == Aux.PHI

501 Theta = AuxTheta(Phi.y * self._e2m1, Phi.x,

502 name=_xkwds_get(diff_name, name=Phi.name))

503 if _xkwds_get(diff_name, diff=False):

504 Theta._diff = self._e2m1

505 return Theta

506

507 def Geodetic(self, Phi, **diff_name): # PYCHOK no cover

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

509

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

511

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

513 '''

514 _xinstanceof(AuxAngle, Phi=Phi)

515 # assert Phi._AUX == Aux.PHI

516 return AuxPhi(Phi, name=_xkwds_get(diff_name, name=Phi.name))

517

518 @Property_RO

519 def _n(self): # 3rd flattening

520 return self.ellipsoid.n

521

522 @Property_RO

523 def _n2(self):

524 return self._n**2

525

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

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

528

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

530

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

532 '''

533 _xinstanceof(AuxAngle, Phi=Phi)

534 # assert Phi._AUX == Aux.PHI

535 Beta = AuxBeta(Phi.y * self._fm1, Phi.x,

536 name=_xkwds_get(diff_name, name=Phi.name))

537 if _xkwds_get(diff_name, diff=False):

538 Beta._diff = self._fm1

539 return Beta

540

541 Reduced = Parametric

542

543 @Property_RO

544 def _q(self): # constant _q

545 q, f = self._e12p1, self.f

546 if f:

547 e = self._e

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

549 else:

550 q += _1_0

551 return q

552

553 def _qf(self, tphi):

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

555 scb = self._scbeta(tphi)

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

557

558 def _qIntegrand(self, beta):

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

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

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

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

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

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

565 # then the integral is

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

567 # where

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

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

570 Beta = AuxBeta.fromRadians(beta)

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

572 Ax, _cv = Aux, self.convert

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

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

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

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

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

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

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

580 r = _0_0 # XXX 0 avoids NAN summation exceptions

581 return r

582

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

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

585

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

587

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

589 '''

590 Beta = self.Parametric(Phi)

591 # assert Beta._AUX == Aux.BETA

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

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

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

595 if self.f < 0:

596 a, b = b, a

597 ka, kb = kb, ka

598 ka1, kb1 = kb1, ka1

599 sb, cb = cb, sb

600 # now a, b = larger/smaller semiaxis

601 # Beta measured from larger semiaxis

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

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

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

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

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

607 sb2 = sb**2

608 cb2 = cb**2

609 da2 = ka1 + ka * sb2

610 db2 = _1_0 - kb * sb2

611 # DLMF Eq. 19.25.9

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

613 # DLMF Eq. 19.25.10 with complementary angles

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

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

616 ka * sb / sqrt(da2))

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

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

619 if self.f < 0:

620 a, b = b, a

621 my, mx = mx, my

622 mr = (my + mx) / PI_2

623 if mr:

624 my = sin(my / mr)

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

626 else: # zero Mu

627 my, mx = _0_0, _1_0

628 n = _xkwds_get(diff_name, name=Phi.name)

629 Mu = AuxMu(my, mx, # normalized

630 name=n).copyquadrant(Phi)

631

632 if _xkwds_get(diff_name, diff=False):

633 d, x = _0_0, Beta._x_normalized

634 if x and mr:

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

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

637 * (x / Phi._x_normalized)

638 else:

639 d = mr / a

640 Mu._diff = self._fm1 * d

641 return Mu

642

643 def RectifyingRadius(self, exact=False):

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

645

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

647 otherwise the I{Taylor} series.

648

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

650 as the ellipsoid axes).

651 '''

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

653 return Meter(r, name=self.RectifyingRadius.__name__)

654

655 @Property_RO

656 def _RectifyingR(self):

657 m = self.ALorder

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

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

660

661 def _scbeta(self, tphi):

662 return _sc(self._fm1 * tphi)

663

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

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

666

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

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

669

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

671 '''

672 _xinstanceof(AuxAngle, Phi=Phi)

673 # assert Phi._AUX == Aux.PHI

674 n = _xkwds_get(diff_name, name=Phi.name)

675 m = _toAuxCase.get(auxout, None)

676 if m: # callable

677 A = m(self, Phi, **_xkwds(diff_name, name=n))

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

679 A = AuxPhi(Phi, name=n)

680 else: # auxout?

681 A = AuxPhi(NAN, name=n)

682 # assert A._AUX == auxout

683 return A

684

685 def _toZeta(self, zetaux):

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

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

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

689 '''

690 class _AuxPhy(AuxPhi):

691 # lean C{AuxPhi} instance.

692 # _diff = _1_0

693 # _x = _1_0

694

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

696 self._y = tphi

697

698 m = _toAuxCase.get(zetaux, None)

699 if m: # callable

700

701 def _toZeta(tphi):

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

703

704 elif zetaux == Aux.GEODETIC: # GEOGRAPHIC

705 _toZeta = _AuxPhy

706

707 else: # zetaux?

708

709 def _toZeta(unused): # PYCHOK expected

710 return _AuxPhy(NAN)

711

712 return _toZeta

713

714

715# switch(auxout): ...

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

717 Aux.CONFORMAL: AuxLat.Conformal,

718 Aux.GEOCENTRIC: AuxLat.Geocentric,

719 Aux.PARAMETRIC: AuxLat.Parametric,

720 Aux.RECTIFYING: AuxLat.Rectifying}

721

722

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

724 sz, cz = Zeta._yx # isnormal

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

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

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

728 if isfinite(x):

729 U0, U1 = Fsum(), Fsum()

730 # assert len(cs) == K

731 for r in _reverange(K):

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

733 U1, U0 = U0, -U1

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

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

736 # fm1 = 0 if sinp else 1

737 if sinp:

738 U0 *= sz * cz * _2_0

739 else:

740 U0 *= x * _0_5

741 U0 -= U1

742 x = float(U0)

743 return x

744

745

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

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

748 '''

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

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

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

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

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

754 # assert _coeffs.ALorder == aL

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

756 return _coeffs

757

758

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

760 # Newton's method fro AuxLat._fromAux

761 try:

762 _lg2 = _log2

763 _abs = fabs

764 tz = _abs(Zeta.tan)

765 tphi = tz / tphi # **)

766 ltz = _lg2(tz) # **)

767 ltphi = _lg2(tphi) # **)

768 ltmin = min(ltphi, MIN_EXP)

769 ltmax = max(ltphi, MAX_EXP)

770# auxin = Zeta._AUX

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

772 _TOL, _xp2 = _EPSqrt, _exp2

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

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

775 Z = _toZeta(tphi)

776 # assert Z._AUX == auxin

777 t, s_ = Z.tan, s

778 if t > tz:

779 ltmax, s = ltphi, +1

780 elif t < tz:

781 ltmin, s = ltphi, -1

782 else:

783 break

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

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

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

787 if d:

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

789 ltphi += d

790 tphi = _xp2(ltphi)

791 if _abs(d) < _TOL:

792 i += 1

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

794 Z = _toZeta(tphi)

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

796 break

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

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

799 ltphi = (ltmin + ltmax) * __2

800 tphi = _xp2(ltphi)

801 else:

802 i = _TRIPS

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

804 Phi._iter = i

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

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

807 Phi._iter = 0

808 # assert Phi._AUX == Aux.PHI

809 return Phi

810

811

812_f, _u = float, _Ufloats()

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

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

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

816 4 / _f(15), _1__f3),

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

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

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

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

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

822 del _f, _u, _Ufloats, _1__f3

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

824

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

826

827__all__ += _ALL_DOCS(AuxLat)

828

829# **) MIT License

830#

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

832#

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

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

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

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

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

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

839#

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

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

842#

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

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

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

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

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

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

849# OTHER DEALINGS IN THE SOFTWARE.