Coverage for pygeodesy/auxilats/auxLat.py: 90%
421 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-08-12 12:31 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2023-08-12 12:31 -0400
2# -*- coding: utf-8 -*-
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+}.
8Copyright (C) U{Charles Karney<mailto:Charles@Karney.com>} (2022-2023) and licensed
9under the MIT/X11 License. For more information, see the U{GeographicLib
10<https://GeographicLib.SourceForge.io>} documentation.
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
18from pygeodesy.auxilats.auxAngle import AuxAngle, AuxBeta, AuxChi, _AuxClass, \
19 AuxMu, AuxPhi, AuxTheta, AuxXi
20from pygeodesy.auxilats.auxily import Aux, _2cos2x, _sc, _sn, _Ufloats
21from pygeodesy.basics import isscalar, _reverange, _xinstanceof
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, Ellipsoid
26# from pygeodesy.ellipsoids import Ellipsoid # from .datums
27from pygeodesy.elliptic import Elliptic as _Ef
28from pygeodesy.errors import AuxError, _xkwds, _xkwds_get, _Xorder
29from pygeodesy.fmath import cbrt, Fsum
30# from pygeodesy.fsums import Fsum # from .fmath
31from pygeodesy.interns import NN, _DOT_, _UNDER_ # _earth_
32from pygeodesy.lazily import _ALL_DOCS, _ALL_MODS as _MODS
33from pygeodesy.props import Property, Property_RO, _update_all
34from pygeodesy.units import Degrees, Meter
36from math import asinh, atan, atan2, copysign, cosh, fabs, sin, sinh, sqrt
37try:
38 from math import exp2 as _exp2
39except ImportError: # Python 3.11-
41 def _exp2(x):
42 return pow(_2_0, x)
44__all__ = ()
45__version__ = '23.08.09'
47_TRIPS = 1024 # XXX 2 or 3?
50def _passarg(arg): # PYCHOK to .utily
51 return arg
54class AuxLat(AuxAngle):
55 '''Accurate conversion between I{Auxiliary} latitudes on an ellipsoid.
57 Latitudes are represented by instances of L{AuxAngle} in order to
58 maintain precision close to 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}.
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 _mAL = 6 # 4, 6 or 8 aka Lmax
69 def __init__(self, a_earth=_WGS84, f=None, b=None, name=NN, **ALorder):
70 '''New L{AuxLat} instance on an ellipsoid or datum.
72 @arg a_earth: Equatorial radius, semi-axis (C{meter}) or an
73 ellipsoid or datum (L{Datum}, L{Ellipsoid},
74 L{Ellipsoid2} or L{a_f2Tuple}).
75 @kwarg f: Flattening: M{(a - b) / a} (C{float}, near zero for
76 spherical), ignored if B{C{a_earth}} is not scalar.
77 @kwarg b: Optional polar radius, semi-axis (C{meter}, same
78 units as B{C{a_earth}}), ignored if B{C{a_earth}}
79 is not scalar.
80 @kwarg ALorder: Optional keyword arguments B{C{ALorder}} to
81 set L{AuxLat}'s C{order}, see property
82 C{ALorder}.
83 @kwarg name: Optional name (C{str}).
84 '''
85 if a_earth is not _WGS84:
86 n = name or AuxLat.__name__
87 try:
88 if b is f is None:
89 E = _ellipsoidal_datum(a_earth, name=n).ellipsoid # XXX raiser=_earth_
90 elif isscalar(a_earth):
91 E = Ellipsoid(a_earth, f=f, b=b, name=_UNDER_(n))
92 else:
93 raise ValueError()
94 except Exception as x:
95 raise AuxError(a_earth=a_earth, f=f, b=b, cause=x)
96 self._E = E
98 if name:
99 self.name = name
100 if ALorder:
101 self.ALorder = _xkwds_get(ALorder, ALorder=AuxLat._mAL)
103 @Property_RO
104 def a(self):
105 '''Get the equatorial radius (C{Meter}, conventionally).
106 '''
107 return self.ellipsoid.a
109 equatoradius = a
111 @Property
112 def ALorder(self):
113 '''Get the I{AuxLat} order (C{int}, 4, 6 or 8).
114 '''
115 return self._mAL
117 @ALorder.setter # PYCHOK setter!
118 def ALorder(self, order):
119 '''Set the I{AuxLat} order (C{int}, 4, 6 or 8).
120 '''
121 m = _Xorder(_AR2Coeffs, AuxError, ALorder=order)
122 if self._mAL != m:
123 _update_all(self)
124 self._mAL = m
126 def _atanhee(self, tphi): # see Ellipsoid._es_atanh, .albers._atanhee
127 # atanh(e * sphi) = asinh(e' * sbeta)
128 f = self.f
129 s = _sn(self._fm1 * tphi) if f > 0 else _sn(tphi)
130 if f: # atanh(e * sphi) = asinh(e' * sbeta)
131 e = self._e
132 s = (atan(e * s) if f < 0 else asinh(self._e1 * s)) / e
133 return s
135 def Authalic(self, Phi, **diff_name):
136 '''Convert I{Geographic} to I{Aunthalic} latitude.
138 @arg Phi: Geographic latitude (L{AuxAngle}).
140 @return: Parametric latitude, C{Beta} (L{AuxAngle}).
141 '''
142 _xinstanceof(AuxAngle, Phi=Phi)
143 # assert Phi._AUX == Aux.PHI
144 tphi = fabs(Phi.tan)
145 if isfinite(tphi) and tphi and self.f:
146 y, x = Phi._yx_normalized
147 q = self._q
148 qv = self._qf(tphi)
149 Dq2 = self._Dq(tphi)
150 Dq2 *= (q + qv) / (fabs(y) + _1_0) # _Dq(-tphi)
151 Xi = AuxXi(copysign(qv, Phi.y), x * sqrt(Dq2),
152 name=_xkwds_get(diff_name, name=Phi.name))
154 if _xkwds_get(diff_name, diff=False):
155 if isnan(tphi):
156 d = self._e2m1_sq2
157 else:
158 c = self.Parametric(Phi)._x_normalized
159 d = _over(c, Xi._x_normalized)**3
160 d *= _over(c, x) * _over(_2_0, q)
161 Xi._diff = d
162 else:
163 Xi = AuxXi(*Phi._yx) # diff default
164 # assert Xi._AUX == Aux.XI
165 return Xi
167 def AuthalicRadius2(self, exact=False, f_max=_0_1):
168 '''Get the I{Authalic} radius I{squared}.
170 @kwarg exact: If C{True}, use the exact expression, otherwise
171 the I{Taylor} series.
172 @kwarg f_max: C{Flattening} not to exceed (C{float}).
174 @return: Authalic radius I{squared} (C{meter} I{squared}, same
175 units as the ellipsoid axes).
177 @raise AuxError: If C{B{exact}=False} and C{abs(flattening)}
178 exceeds C{f_max}.
179 '''
180 f = self.f
181 if exact or not f:
182 c2 = self.ellipsoid.b2 * self._q # == ellipsoid.c2x * 2
183 elif fabs(f) < f_max:
184 # Using a * (a + b) / 2 as the multiplying factor leads to a rapidly
185 # converging series in n. Of course, using this series isn't really
186 # necessary, since the exact expression is simple to evaluate. However,
187 # we do it for consistency with RectifyingRadius and, presumably, the
188 # roundoff error is smaller compared to that for the exact expression.
189 m = self.ALorder
190 c2 = _MODS.karney._polynomial(self._n, _AR2Coeffs[m], 0, m)
191 c2 *= self.a * (self.a + self.b)
192 else:
193 raise AuxError(exact=exact, f=f, f_max=f_max)
194 return c2 * _0_5
196 @Property_RO
197 def b(self):
198 '''Get the polar radius (C{Meter}, conventionally).
199 '''
200 return self.ellipsoid.b # a * (_1_0 - f)
202 polaradius = b
204 def _coeffs(self, auxout, auxin):
205 # Get the polynomial coefficients as 4-, 6- or 8-tuple
206 aL = self.ALorder # aka Lmax
207 k = Aux._1d(auxout, auxin)
208 try:
209 cs = AuxLat._csc[aL][k]
210 except KeyError:
211 if auxout == auxin:
212 cs = _0_0s(aL) # not cached
213 else:
214 Cx = _CXcoeffs(aL)
215 try:
216 Cx = Cx[auxout][auxin]
217 except KeyError as x:
218 raise AuxError(auxout=auxout, auxin=auxin, cause=x)
220 d = x = n = self._n
221 if Aux.use_n2(auxin) and Aux.use_n2(auxout):
222 x = self._n2
224 def _m(m):
225 return m // 2
226 else:
227 def _m(m): # PYCHOK expected
228 return m
230 i = 0
231 cs = []
232 _p = _MODS.karney._polynomial
233 for r in _reverange(aL):
234 j = i + _m(r) + 1 # order m = j - i - 1
235 cs.append(_p(x, Cx, i, j) * d)
236 d *= n
237 i = j
238 # assert i == len(Cx) and len(cs) == aL
239 AuxLat._csc.setdefault(aL, {})[k] = tuple(cs)
240 return cs
242 def Conformal(self, Phi, **diff_name):
243 '''Convert I{Geographic} to I{Conformal} latitude.
245 @arg Phi: Geographic latitude (L{AuxAngle}).
247 @return: Conformal latitude, C{Chi} (L{AuxAngle}).
248 '''
249 _xinstanceof(AuxAngle, Phi=Phi)
250 # assert Phi._AUX == Aux.PHI
251 tphi = tchi = fabs(Phi.tan)
252 if isfinite(tphi) and tphi and self.f:
253 sig = sinh(self._e2 * self._atanhee(tphi))
254 scsig = _sc(sig)
255 scphi = _sc(tphi)
256 if self.f > 0:
257 # The general expression for tchi is
258 # tphi * scsig - sig * scphi
259 # This involves cancellation if f > 0, so change to
260 # (tphi - sig) * (tphi + sig) / (tphi * scsig + sig * scphi)
261 # To control overflow, write as (sigtphi = sig / tphi)
262 # (tphi - sig) * (1 + sigtphi) / (scsig + sigtphi * scphi)
263 sigtphi = sig / tphi
264 if sig < (tphi * _0_5):
265 tphimsig = tphi - sig
266 else:
267 def _asinh_2(x):
268 return asinh(x) * _0_5
269 # Still possibly dangerous cancellation in tphi - sig.
270 # Write tphi - sig = (1 - e) * Dg(1, e)
271 # Dg(x, y) = (g(x) - g(y)) / (x - y)
272 # g(x) = sinh(x * atanh(sphi * x))
273 # Note sinh(atanh(sphi)) = tphi
274 # Turn the crank on divided differences, substitute
275 # sphi = tphi/_sc(tphi)
276 # atanh(x) = asinh(x/sqrt(1-x^2))
277 e = self._e
278 em1 = self._e2m1 / (_1_0 + e)
279 # assert em1 != 0
280 scb = _sc(self._fm1 * tphi) # sec(beta)
281 scphib = _sc(tphi) / scb # sec(phi) / sec(beta)
282 atphib = _asinh_2(e * tphi / scb) # atanh(e * sphi)
283 atphi = _asinh_2(tphi) # atanh(sphi)
284 t = _asinh_2(em1 * (tphi * scphib)) / em1
285 try:
286 Dg = Fsum(atphi, atphib, t, e * t)
287 except ValueError: # Fsum(NAN) exception
288 Dg = sum((atphi, atphib, t, e * t))
289 e *= atphib
290 t = atphi - e
291 Dg *= cosh(atphi + e) * _over(sinh(t), t)
292 tphimsig = float(Dg) * em1 # tphi - sig
293 tchi = _over(tphimsig * (_1_0 + sigtphi),
294 scsig + scphi * sigtphi)
295 else:
296 tchi = tphi * scsig - sig * scphi
298 n = _xkwds_get(diff_name, name=Phi.name)
299 Chi = AuxChi(tchi, name=n).copyquadrant(Phi)
301 if _xkwds_get(diff_name, diff=False):
302 if isinf(tphi): # PYCHOK np cover
303 d = self._conformal_diff
304 else:
305 x = self.Parametric(Phi)._x_normalized
306 d = (self._e2m1 * _over(x, Chi._x_normalized)
307 * _over(x, Phi._x_normalized)) if x else _0_0
308 Chi._diff = d
309 # assrt Chi._AUX == Aux.CHI
310 return Chi
312 @Property_RO
313 def _conformal_diff(self):
314 '''(INTERNAL) Constant I{Conformal} diff.
315 '''
316 e = self._e
317 if self.f > 0:
318 ss = sinh(e * asinh(self._e1))
319 d = _over(_1_0, _sc(ss) + ss)
320 else:
321 ss = sinh(-atan(e) * e)
322 d = _sc(ss) - ss
323 return d
325 def convert(self, auxout, Zeta_d, exact=False):
326 # Convert I{Auxiliary} or I{scalar} latitude
327 Z = d = Zeta_d
328 if isinstance(Z, AuxAngle):
329 A, auxin = _AuxClass(auxout), Z._AUX
330 if auxin == auxout:
331 pass
332 elif not (isfinite(Z.tan) and Z.tan): # XXX
333 Z = A(*Z._yx, aux=auxout, name=Z.name)
334 elif exact:
335 p = Aux.power(auxout, auxin)
336 if p is None:
337 P = self._fromAux(Z) # Phi
338 Z = self._toAux(auxout, P)
339 Z._iter = P.iteration
340 else:
341 y, x = Z._yx
342 if p:
343 y *= pow(self._fm1, p)
344 Z = A(y, x, aux=auxout, name=Z.name)
345 else:
346 cs = self._coeffs(auxout, auxin)
347 yx = Z._yx_normalized
348 Z = A(*yx, aux=auxout, name=Z.name)
349 # assert Z._yx == yx
350 r = _Clenshaw(True, Z, cs, self.ALorder)
351 Z += AuxAngle.fromRadians(r, aux=auxout)
352 # assert Z._AUX == auxout
353 return Z
355 elif isinstance(d, Degrees) or isscalar(d):
356 Z = AuxPhi.fromDegrees(d)
357 d = round((d - Z.toDegrees) / _360_0) * _360_0
358 d += self.convert(auxout, Z, exact).toDegrees
359 return Degrees(d, name=Aux.Greek(auxout))
361 raise AuxError(auxout=auxout, Zeta_d=Zeta_d, exact=exact)
363 def _Dq(self, tphi):
364 # I{Divided Difference} of (q(1) - q(sphi)) / (1 - sphi).
365 sphi = _sn(tphi)
366 if tphi > 0:
367 scphi = _sc(tphi)
368 d = _1_0 / (scphi**2 * (_1_0 + sphi)) # XXX - sphi
369 if d:
370 # General expression for _Dq(1, sphi) is
371 # atanh(e * d / (1 - e2 * sphi)) / (e * d) +
372 # (1 + e2 * sphi) / ((1 - e2 * sphi * sphi) * e2m1);
373 # atanh( e * d / (1 - e2 * sphi))
374 # = atanh( e * d * scphi/(scphi - e2 * tphi))
375 e2m1, ed = self._e2m1, self._e * d
376 if e2m1 and ed:
377 e2 = self._e2
378 if e2 > 0: # assert self.f > 0
379 scb = _sc(self._fm1 * tphi)
380 q = scphi / scb
381 q *= (scphi + tphi * e2) / (e2m1 * scb)
382 q += asinh(self._e1 * d * scphi / scb) / ed
383 else:
384 s2 = sphi * e2
385 q = (_1_0 + s2) / ((_1_0 - sphi * s2) * e2m1)
386 q += (atan2(ed, _1_0 - s2) / ed) if e2 < 0 else _1_0
387 else:
388 q = INF
389 else:
390 q = self._2_e2m12
391 else: # not reached, open-coded in .Authalic
392 q = _over(self._q - self._qf(tphi), _1_0 - sphi)
393 return q
395 @Property_RO
396 def _e(self): # unsigned, (1st) eccentricity
397 return self.ellipsoid.e # sqrt(fabs(self._e2))
399 @Property_RO
400 def _e1(self):
401 return sqrt(fabs(self._e12))
403 @Property_RO
404 def _e12(self):
405 return _over(self._e2, _1_0 - self._e2)
407 @Property_RO
408 def _e12p1(self):
409 return _1_0 / self._e2m1
411 @Property_RO
412 def _e2(self): # signed, (1st) eccentricity squared
413 return self.ellipsoid.e2
415 @Property_RO
416 def _e2m1(self): # 1 less 1st eccentricity squared
417 return self.ellipsoid.e21 # == ._fm1**2
419 @Property_RO
420 def _e2m1_sq2(self):
421 return self._e2m1 * sqrt(self._q * _0_5)
423 @Property_RO
424 def _2_e2m12(self):
425 return _2_0 / self._e2m1**2
427 @Property_RO
428 def _Ef_fRG_a2b2_PI_4(self):
429 E = self.ellipsoid
430 return _Ef.fRG(E.a2, E.b2) / PI_4
432 @Property_RO
433 def ellipsoid(self):
434 '''Get the ellipsoid (L{Ellipsoid}).
435 '''
436 return self._E
438 @Property_RO
439 def f(self):
440 '''Get the flattening (C{float}).
441 '''
442 return self.ellipsoid.f
444 flattening = f
446 @Property_RO
447 def _fm1(self): # 1 - flattening
448 return self.ellipsoid.f1
450 def _fromAux(self, Zeta, **name):
451 '''Convert I{Auxiliary} to I{Geographic} latitude.
453 @arg Zeta: Auxiliary latitude (L{AuxAngle}).
455 @return: Geographic latitude, I{Phi} (L{AuxAngle}).
456 '''
457 _xinstanceof(AuxAngle, Zeta=Zeta)
458 aux = Zeta._AUX
459 n = _xkwds_get(name, name=Zeta.name)
460 f = self._fromAuxCase.get(aux, None)
461 if f is None:
462 Phi = AuxPhi(NAN, name=n)
463 elif callable(f):
464 t = self._fm1
465 t *= f(t)
466 Phi = _Newton(t, Zeta, self._toZeta(aux), name=n)
467 else: # assert isscalar(f)
468 y, x = Zeta._yx
469 Phi = AuxPhi(y / f, x, name=n)
470 # assert Phi._AUX == Aux.PHI
471 return Phi
473 @Property_RO
474 def _fromAuxCase(self):
475 '''(INTERNAL) switch(auxin): ...
476 '''
477 return {Aux.AUTHALIC: cbrt,
478 Aux.CONFORMAL: _passarg,
479 Aux.GEOCENTRIC: self._e2m1,
480 Aux.GEOGRAPHIC: _1_0,
481 Aux.PARAMETRIC: self._fm1,
482 Aux.RECTIFYING: sqrt}
484 def Geocentric(self, Phi, **diff_name):
485 '''Convert I{Geographic} to I{Geocentric} latitude.
487 @arg Phi: Geographic latitude (L{AuxAngle}).
489 @return: Geocentric latitude, C{Phi} (L{AuxAngle}).
490 '''
491 _xinstanceof(AuxAngle, Phi=Phi)
492 # assert Phi._AUX == Aux.PHI
493 Theta = AuxTheta(Phi.y * self._e2m1, Phi.x,
494 name=_xkwds_get(diff_name, name=Phi.name))
495 if _xkwds_get(diff_name, diff=False):
496 Theta._diff = self._e2m1
497 return Theta
499 def Geodetic(self, Phi, **diff_name):
500 '''Convert I{Geographic} to I{Geodetic} latitude.
502 @arg Phi: Geographic latitude (L{AuxAngle}).
504 @return: Geodetic latitude, C{Phi} (L{AuxAngle}).
505 '''
506 _xinstanceof(AuxAngle, Phi=Phi)
507 # assert Phi._AUX == Aux.PHI
508 return AuxPhi(Phi, name=_xkwds_get(diff_name, name=Phi.name))
510 @Property_RO
511 def _n(self): # 3rd flattening
512 return self.ellipsoid.n
514 @Property_RO
515 def _n2(self):
516 return self._n**2
518 def Parametric(self, Phi, **diff_name):
519 '''Convert I{Geographic} to I{Parametric} latitude.
521 @arg Phi: Geographic latitude (L{AuxAngle}).
523 @return: Parametric latitude, C{Beta} (L{AuxAngle}).
524 '''
525 _xinstanceof(AuxAngle, Phi=Phi)
526 # assert Phi._AUX == Aux.PHI
527 Beta = AuxBeta(Phi.y * self._fm1, Phi.x,
528 name=_xkwds_get(diff_name, name=Phi.name))
529 if _xkwds_get(diff_name, diff=False):
530 Beta._diff = self._fm1
531 return Beta
533 Reduced = Parametric
535 @Property_RO
536 def _q(self): # constant _q
537 q, f = self._e12p1, self.f
538 if f:
539 e = self._e
540 q += (asinh(self._e1) if f > 0 else atan(e)) / e
541 else:
542 q += _1_0
543 return q
545 def _qf(self, tphi):
546 # function _q: atanh(e * sphi) / e + sphi / (1 - (e * sphi)^2)
547 scb = _sc(self._fm1 * tphi)
548 return self._atanhee(tphi) + (tphi / scb) * (_sc(tphi) / scb)
550 def _qIntegrand(self, beta):
551 # pbeta(beta) = integrate(q(beta), beta), with beta in radians
552 # q(beta) = (1-f) * (sin(xi) - sin(chi)) / cos(phi)
553 # = (1-f) * (cos(chi) - cos(xi)) / cos(phi) *
554 # (cos(xi) + cos(chi)) / (sin(xi) + sin(chi))
555 # Fit q(beta)/cos(beta) with Fourier transform
556 # q(beta)/cos(beta) = sum(c[k] * sin((2*k+1)*beta), k, 0, K-1)
557 # then the integral is
558 # pbeta = sum(d[k] * cos((2*k+2)*beta), k, 0, K-1)
559 # where
560 # d[k] = -1/(4*(k+1)) * (c[k] + c[k+1]) for k in 0..K-2
561 # d[K-1] = -1/(4*K) * c[K-1]
562 Beta = AuxBeta.fromRadians(beta)
563 if Beta.x: # and self._fm1:
564 Ax, _cv = Aux, self.convert
565 Phi = _cv(Ax.PHI, Beta, exact=True)
566 schi, cchi = _cv(Ax.CHI, Phi, exact=True)._yx_normalized
567 sxi, cxi = _cv(Ax.XI, Phi, exact=True)._yx_normalized
568 r = (sxi - schi) if fabs(schi) < fabs(cchi) else \
569 _over(_2cos2x(cchi, cxi), (sxi + schi) * _2_0)
570 r *= _over(self._fm1, Phi._x_normalized * Beta._x_normalized)
571 else: # beta == PI_2, PI3_2, ...
572 r = _0_0 # XXX 0 avoids NAN summation exceptions
573 return r
575 def Rectifying(self, Phi, **diff_name):
576 '''Convert I{Geographic} to I{Rectifying} latitude.
578 @arg Phi: Geographic latitude (L{AuxAngle}).
580 @return: Rectifying latitude, C{Mu} (L{AuxAngle}).
581 '''
582 Beta = self.Parametric(Phi)
583 # assert Beta._AUX == Aux.BETA
584 sb, cb = map(fabs, Beta._yx_normalized)
585 a, ka, ka1 = _1_0, self._e2, self._e2m1
586 b, kb, kb1 = self._fm1, -self._e12, self._e12p1
587 if self.f < 0:
588 a, b = b, a
589 ka, kb = kb, ka
590 ka1, kb1 = kb1, ka1
591 sb, cb = cb, sb
592 # now a, b = larger/smaller semiaxis
593 # Beta measured from larger semiaxis
594 # kb, ka = modulus-squared for distance from Beta = 0, pi/2
595 # NB kb <= 0; 0 <= ka <= 1
596 # sa = b*E(Beta, sqrt(kb))
597 # sb = a*E(Beta',sqrt(ka))
598 # 1 - ka * (1 - sb2) = 1 - ka + ka*sb2
599 sb2 = sb**2
600 cb2 = cb**2
601 da2 = ka1 + ka * sb2
602 db2 = _1_0 - kb * sb2
603 # DLMF Eq. 19.25.9
604 my = b * sb * (_Ef.fRF(cb2, db2, _1_0) -
605 kb * sb2 * _Ef.fRD(cb2, db2, _1_0, _3_0))
606 # DLMF Eq. 19.25.10 with complementary angles
607 mx = a * cb * (_Ef.fRF(sb2, da2, _1_0) * ka1 +
608 ka * cb2 * _Ef.fRD(sb2, _1_0, da2, _3_0) * ka1 +
609 ka * sb / sqrt(da2))
610 # my + mx = 2*_Ef.fRG(a*a, b*b) = a*E(e) = b*E(i*e')
611 # mr = a*E(e)*(2/pi) = b*E(i*e')*(2/pi)
612 if self.f < 0:
613 a, b = b, a
614 my, mx = mx, my
615 mr = (my + mx) / PI_2
616 if mr:
617 my = sin(my / mr)
618 mx = sin(mx / mr) # XXX zero?
619 else: # zero Mu
620 my, mx = _0_0, _1_0
621 n = _xkwds_get(diff_name, name=Phi.name)
622 Mu = AuxMu(my, mx, # normalized
623 name=n).copyquadrant(Phi)
625 if _xkwds_get(diff_name, diff=False):
626 d, x = _0_0, Beta._x_normalized
627 if x and mr:
628 if Mu.x and Phi.x and not isinf(Phi.tan):
629 d = b / mr * (x / Mu.x)**2 \
630 * (x / Phi._x_normalized)
631 else:
632 d = mr / a
633 Mu._diff = self._fm1 * d
634 return Mu
636 def RectifyingRadius(self, exact=False):
637 '''Get the I{Rectifying} radius.
639 @arg exact: If C{True}, use the exact expression,
640 otherwise the I{Taylor} series.
642 @return: Rectifying radius (L{Meter}, same units
643 as the ellipsoid axes).
644 '''
645 r = self._Ef_fRG_a2b2_PI_4 if exact else self._RectifyingR
646 return Meter(r, name=self.RectifyingRadius.__name__)
648 @Property_RO
649 def _RectifyingR(self):
650 m = self.ALorder
651 d = _MODS.karney._polynomial(self._n2, _RRCoeffs[m], 0, m // 2)
652 return d * (self.a + self.b) * _0_5
654 def _toAux(self, auxout, Phi, **diff_name):
655 '''Convert I{Geographic} to I{Auxiliary} latitude.
657 @arg auxout: I{Auxiliary} kind (C{Aux.KIND}).
658 @arg Phi: Geographic latitude (L{AuxLat}).
660 @return: Auxiliary latitude, I{Eta} (L{AuxLat}).
661 '''
662 _xinstanceof(AuxAngle, Phi=Phi)
663 # assert Phi._AUX == Aux.PHI
664 n = _xkwds_get(diff_name, name=Phi.name)
665 m = _toAuxCase.get(auxout, None)
666 if m: # callable
667 A = m(self, Phi, **_xkwds(diff_name, name=n))
668 elif auxout == Aux.GEODETIC: # .GEOGRAPHIC
669 A = AuxPhi(Phi, name=n)
670 else: # auxout?
671 A = AuxPhi(NAN, name=n)
672 # assert A._AUX == auxout
673 return A
675 def _toZeta(self, zetaux):
676 '''Return a (lean) function to create C{AuxPhi(tphi)} and
677 convert that into C{AuxAngle} of (fixed) kind C{zetaux}
678 for use only inside the C{_Newton} loop.
679 '''
680 class _AuxPhy(AuxPhi):
681 # lean C{AuxPhi} instance.
682 # _diff = _1_0
683 # _x = _1_0
685 def __init__(self, tphi): # PYCHOK signature
686 self._y = tphi
688 m = _toAuxCase.get(zetaux, None)
689 if m: # callable
691 def _toZeta(tphi):
692 return m(self, _AuxPhy(tphi), diff=True)
694 elif zetaux == Aux.GEODETIC: # GEOGRAPHIC
695 _toZeta = _AuxPhy
697 else: # zetaux?
699 def _toZeta(unused): # PYCHOK expected
700 return _AuxPhy(NAN)
702 return _toZeta
705# switch(auxout): ...
706_toAuxCase = {Aux.AUTHALIC: AuxLat.Authalic,
707 Aux.CONFORMAL: AuxLat.Conformal,
708 Aux.GEOCENTRIC: AuxLat.Geocentric,
709 Aux.PARAMETRIC: AuxLat.Parametric,
710 Aux.RECTIFYING: AuxLat.Rectifying}
713def _Clenshaw(sinp, Zeta, cs, K):
714 sz, cz = Zeta._yx # isnormal
715 # Evaluate sum(c[k] * sin((2*k+2) * Zeta)) if sinp else
716 # sum(c[k] * cos((2*k+2) * Zeta))
717 x = _2cos2x(cz, sz) # 2 * cos(2*Zeta)
718 if isfinite(x):
719 U0, U1 = Fsum(), Fsum()
720 # assert len(cs) == K
721 for r in _reverange(K):
722 U1 -= U0 * x + cs[r]
723 U1, U0 = U0, -U1
724 # u0*f0(Zeta) - u1*fm1(Zeta)
725 # f0 = sin(2*Zeta) if sinp else cos(2*Zeta)
726 # fm1 = 0 if sinp else 1
727 if sinp:
728 U0 *= sz * cz * _2_0
729 else:
730 U0 *= x * _0_5
731 U0 -= U1
732 x = float(U0)
733 return x
736def _CXcoeffs(aL): # PYCHOK in .auxilats.__main__
737 '''(INTERNAL) Get the C{CX_4}, C{_6} or C{_8} coefficients.
738 '''
739 try: # from pygeodesy.auxilats._CX_x import _coeffs_x as _coeffs
740 _CX_x = _DOT_(_MODS.auxilats.__name__, _UNDER_('_CX', aL))
741 _coeffs = _MODS.getattr(_CX_x, _UNDER_('_coeffs', aL))
742 except (AttributeError, ImportError, KeyError, TypeError) as x:
743 raise AuxError(ALorder=aL, cause=x)
744 # assert _coeffs.ALorder == aL
745 # assert _coeffs.n == Aux.len(aL)
746 return _coeffs
749def _Newton(tphi, Zeta, _toZeta, **name):
750 # Newton's method fro AuxLat._fromAux
751 try:
752 _lg2 = _log2
753 _abs = fabs
754 tz = _abs(Zeta.tan)
755 tphi = tz / tphi # **)
756 ltz = _lg2(tz) # **)
757 ltphi = _lg2(tphi) # **)
758 ltmin = min(ltphi, MIN_EXP)
759 ltmax = max(ltphi, MAX_EXP)
760# auxin = Zeta._AUX
761 s, n, __2 = 0, 3, _0_5 # n = i + 2
762 _TOL, _xp2 = _EPSqrt, _exp2
763 for i in range(1, _TRIPS): # up to 1 Ki!
764 # _toAux(auxin, AuxPhi(tphi), diff=True)
765 Z = _toZeta(tphi)
766 # assert Z._AUX == auxin
767 t, s_ = Z.tan, s
768 if t > tz:
769 ltmax, s = ltphi, +1
770 elif t < tz:
771 ltmin, s = ltphi, -1
772 else:
773 break
774 # derivative dtan(Z)/dtan(Phi)
775 # to dlog(tan(Z))/dlog(tan(Phi))
776 d = (ltz - _lg2(t)) * t # **)
777 if d:
778 d = d / (Z.diff * tphi) # **)
779 ltphi += d
780 tphi = _xp2(ltphi)
781 if _abs(d) < _TOL:
782 i += 1
783 # _toAux(auxin, AuxPhi(tphi), diff=True)
784 Z = _toZeta(tphi)
785 tphi -= _over(Z.tan - tz, Z.diff)
786 break
787 if (i > n and (s * s_) < 0) or not ltmin < ltphi < ltmax:
788 s, n = 0, (i + 2)
789 ltphi = (ltmin + ltmax) * __2
790 tphi = _xp2(ltphi)
791 else:
792 i = _TRIPS
793 Phi = AuxPhi(tphi, **name).copyquadrant(Zeta)
794 Phi._iter = i
795 except (ValueError, ZeroDivisionError): # **) zero t, tphi, tz or Z.diff
796 Phi = AuxPhi(Zeta, **name) # diff as-as
797 Phi._iter = 0
798 # assert Phi._AUX == Aux.PHI
799 return Phi
802_f, _u = float, _Ufloats()
803_1__f3 = -1 / _f(3) # XXX +1 / _f(3)
804_AR2Coeffs = {4: _u(4 / _f(315), 4 / _f(105), 4 / _f(15), _1__f3),
805 6: _u(4 / _f(1287), 4 / _f(693), 4 / _f(315), 4 / _f(105),
806 4 / _f(15), _1__f3),
807 8: _u(4 / _f(3315), 4 / _f(2145), 4 / _f(1287), 4 / _f(693),
808 4 / _f(315), 4 / _f(105), 4 / _f(15), _1__f3)}
809_RRCoeffs = {4: _u(1 / _f(64), _0_25),
810 6: _u(1 / _f(256), 1 / _f(64), _0_25),
811 8: _u(25 / _f(16384), 1 / _f(256), 1 / _f(64), _0_25)} # PYCHOK used!
812del _f, _u, _Ufloats, _1__f3
813# assert set(_AR2Coeffs.keys()) == set(_RRCoeffs.keys())
815__all__ += _ALL_DOCS(AuxLat)
817# **) MIT License
818#
819# Copyright (C) 2023-2023 -- mrJean1 at Gmail -- All Rights Reserved.
820#
821# Permission is hereby granted, free of charge, to any person obtaining a
822# copy of this software and associated documentation files (the "Software"),
823# to deal in the Software without restriction, including without limitation
824# the rights to use, copy, modify, merge, publish, distribute, sublicense,
825# and/or sell copies of the Software, and to permit persons to whom the
826# Software is furnished to do so, subject to the following conditions:
827#
828# The above copyright notice and this permission notice shall be included
829# in all copies or substantial portions of the Software.
830#
831# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
832# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
833# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
834# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
835# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
836# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
837# OTHER DEALINGS IN THE SOFTWARE.