
    R1iYN                        d dl mZ d dlZd dlZd dlmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z" d dl#m$Z$m%Z%m&Z& d dl'm(Z(m)Z)m*Z* d dl+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1m2Z2 d d	l3m4Z4 d d
l5m6Z6 e%e%z  e$z  e$z  e$z  Z7 G d de4      Z8 e9dgdz        Z:d Z;d Z<d Z=d Z>d Z?ej                  j                  ZBd ZC eC       ZD e eEdeDdz  d      D  cg c]  } ej                  |        c}       ZG e eEdeDdz  d      D  cg c]  } ej                  |        c}       ZH ed eDdz
        ZI ed       eeB      z   dz   ZJd ZKd ZLyc c} w c c} w )    )divisionN)absamaxaminarangearccosarctanarray
atleast_1dclipcopycopytocoscoshexp	full_likelogndarraynewaxispipowerrepeatsignsinsinhsqueezesqrtsumtantanh
zeros_like)AU_KMDAY_SDEG2RAD)dots	length_ofmxv)reify)OsculatingElementsnormpi)DistanceVelocity)VectorFunction)_crossc                       e Zd Z	 	 d
dZe	 	 d
d       Ze	 	 	 	 dd       Ze	 	 d
d       Zd Ze	d        Z
d Zd	 Zy)_KeplerOrbitNc                 f    || _         || _        || _        || _        || _        || _        d| _        y)ad   Calculates the position of an object using 2 body propagation

        Parameters
        ----------
        position : Distance
            Position vector at epoch with shape (3,)
        velocity : Velocity
            Velocity vector at epoch with shape (3,)
        epoch : Time
            Time corresponding to `position` and `velocity`
        mu_au_d : float
            Value of mu (G * M) in au^3/d^2
        center : int
            NAIF ID of the primary body, 399 for geocentric orbits, 10 for
            heliocentric orbits
        target : int
            NAIF ID of the secondary body
        N)position_at_epochvelocity_at_epochepoch	mu_au3_d2centertarget	_rotation)selfpositionvelocityr4   r5   r6   r7   s          V/home/cursorai/projects/iching/venv/lib/python3.12/site-packages/skyfield/keplerlib.py__init__z_KeplerOrbit.__init__   s7    4 "*!)
"    c
           	          |t         z  }
t        ||t        |z  t        |z  t        |z  d|
      \  }} | t        |      t	        |      ||
||	      S )zABuild a `KeplerOrbit` given its parameters and date of periapsis.g        )_CONVERT_GM
ele_to_vecr$   r+   r,   )clssemilatus_rectum_aueccentricityinclination_degrees#longitude_of_ascending_node_degreesargument_of_perihelion_degreest_periapsis	gm_km3_s2r6   r7   	gm_au3_d2posvels                r<   _from_periapsisz_KeplerOrbit._from_periapsis9   sl     +	))9944
S SMSM
 	
r>   c           	      (   |r|	s|s|	st        d      |	r|	t        dz  z  t        dz  z  }t        |j                  ||j
                  |j
                  |j
                  |j
                  |      \  }} | t        |      t        |      |||
|      S )a-   Creates a `KeplerOrbit` object from elements using true anomaly

        Parameters
        ----------
        p : Distance
            Semi-Latus Rectum
        e : float
             Eccentricity
        i : Angle
            Inclination
        Om : Angle
            Longitude of Ascending Node
        w : Angle
            Argument of periapsis
        v : Angle
            True anomaly
        epoch : Time
            Time corresponding to `position` and `velocity`
        mu_km_s : float
            Value of mu (G * M) in km^3/s^2
        mu_au3_d2 : float
            Value of mu (G * M) in au^3/d^2
        center : int
            NAIF ID of the primary body, 399 for geocentric orbits, 10 for
            heliocentric orbits
        target : int
            NAIF ID of the secondary body
        z8Either mu_km_s or mu_au3_d2 should be used, but not both      )km)km_per_s)r6   r7   )
ValueErrorr"   r#   rA   rQ   radiansr+   r,   )rB   peiOmwvr4   mu_km_sr5   r6   r7   r:   r;   s                 r<   _from_true_anomalyz_KeplerOrbit._from_true_anomalyZ   s    H 	79WXX%(*UAX5G'()()		(*

()		()		(/
( 8x(X.  
 	
r>   c           	      H   t         |z  }|t        z  }|dk  rt        ||      }t        ||      }n+|dkD  rt        ||      }t	        ||      }nt        |||      }t        ||t         |z  t         |z  t         |z  ||      \  }} | t        |      t        |      |||	|
      S )a-   Creates a `KeplerOrbit` object from elements using mean anomaly

        Parameters
        ----------
        p : Distance
            Semi-Latus Rectum
        e : float
             Eccentricity
        i : Angle
            Inclination
        Om : Angle
            Longitude of Ascending Node
        w : Angle
            Argument of periapsis
        M : Angle
            Mean anomaly
        epoch : Time
            Time corresponding to `position` and `velocity`
        mu_km_s : float
            Value of mu (G * M) in km^3/s^2
        mu_au3_d2 : float
            Value of mu (G * M) in au^3/d^2
        center : int
            NAIF ID of the primary body, 399 for geocentric orbits, 10 for
            heliocentric orbits
        target : int
            NAIF ID of the secondary body
              ?)	r$   r@   eccentric_anomalytrue_anomaly_closedtrue_anomaly_hyperbolictrue_anomaly_parabolicrA   r+   r,   )rB   rC   rD   rE   rF   rG   mean_anomaly_degreesr4   rI   r6   r7   MrJ   ErZ   rK   rL   s                    r<   _from_mean_anomalyz_KeplerOrbit._from_mean_anomaly   s    T **+	#!,2A#L!4AC!,2A'a8A&':IqIA))9944
S SMSM
 	
r>   c                 B   t        | j                  j                  | j                  j                  | j
                  j                  |j                  | j                        \  }}| j                  ,t        | j                  |      }t        | j                  |      }||ddfS )zPropagate the KeplerOrbit to the given Time object

        The Time object can contain one time, or an array of times
        N)
	propagater2   aur3   au_per_dr4   ttr5   r8   r'   )r9   timerK   rL   s       r<   _atz_KeplerOrbit._at   s    
 ""%%""++JJMMGGNN
S >>%dnnc*Cdnnc*CCt##r>   c                 ~    t        | j                  | j                  | j                  | j                  t
        z        S )N)r[   )r)   r2   r3   r4   r5   r@   r9   s    r<   elements_at_epochz_KeplerOrbit.elements_at_epoch   s3    !$"8"8"&"8"8"&**,0NN[,H
 	
r>   c           	         | j                   }| j                  r<dj                  | j                  | j                  | j
                  | j                        S | j                   }d}|j                  | j                  | j                  |j                  j                  |j                  |j                  j                  |j                  j                  |j                  j                        S )NzKeplerOrbit {0} {1} -> {2} {3}zJKeplerOrbit {0} {1} -> q={2:.2}au e={3:.3f} i={4:.1f} Om={5:.1f} w={6:.1f})rp   target_nameformatr6   center_namer7   periapsis_distanceri   rD   inclinationdegreeslongitude_of_ascending_nodeargument_of_periapsis)r9   elestrings      r<   __str__z_KeplerOrbit.__str__   s    $$3::4;;;?;K;K;?;;;?;K;K  ((CaF==!%!1!1!$!7!7!:!:!$!1!1!$!8!8!$!@!@!H!H!$!:!:!B!B r>   c                 6    dj                  t        |             S )Nz<{0}>)rs   strro   s    r<   __repr__z_KeplerOrbit.__repr__  s    ~~c$i((r>   )NN)NNNN)__name__
__module____qualname__r=   classmethodrM   r\   rf   rm   r(   rp   r|   r    r>   r<   r0   r0      s     !F  
 
@  #'$(!%!%7
 7
t  D
 D
L$$ 
 
*)r>   r0   
   c                    t        |      }t        |      }||z  }dt        z  | z  dz
  }dt        z  |z  t        |      t        d|| |z  |z  z  z         z  dz
  z  }t        D ]W  }d| t        |      z  z
  }| t        |      z  }||z
  |z
  }||z  ||z  d|z  |z  z
  z  }	||	z  }t        |	      dk  sR||z  c S  t        d      )zbIterate to solve Kepler's equation to find the eccentric anomaly.

    See arXiv:2108.03215.

    g      ?r^   g      ?   g+=z$eccentric anomaly failed to converge)	r*   r   r   r   _ten_iterationsr   r   r   rS   )
rV   rd   sign_Mebarre   _f1f2fdEs
             r<   r_   r_     s     	q	A!WFKA"9Q;Db44:QAdF4K-@(AACGHA1SV8^s1vXFQJrTRUSU2X%&	Rr7U?v:  ;
<<r>   c                 `    dt        t        | dz   | dz
  z        t        |dz        z        z  S )zCalculates true anomaly from eccentricity and eccentric anomaly.

    Valid for hyperbolic orbits. Equations from the relevant Wikipedia entries.

           @r^   rP   )r	   r   r    rV   re   s     r<   ra   ra   (  s3     a#g!c'23d1Q3i?@@@r>   c                 `    dt        t        d| z   d| z
  z        t        |dz        z        z  S )zCalculates true anomaly from eccentricity and eccentric anomaly.

    Valid for closed orbits. Equations from the relevant Wikipedia entries.

    r   r^   rP   )r	   r   r   r   s     r<   r`   r`   1  s3     cAg#'23c!A#h>???r>   c                     t        d| dz  z  |z        |z  }| dz  }dt        |d|dz  z  z        z  |z  }|||z  dz   z   dz  }dt        |d|z  z
        z  S )zCalculates true anomaly from semi-latus rectum, gm, and mean anomaly.

    Valid for parabolic orbits. Equations from
    https://en.wikipedia.org/wiki/Parabolic_trajectory.

    rP   rO         ?r   gUUUUUU?)r   r	   )rU   gmrd   delta_tru   ABs          r<   rb   rb   :  sz     1q!t8b=!A%GQR11144566@A	
acAg#Ava!A#gr>   c                    t        |t              rEt        |t              r5|dkD  }||   t        d||   z        kD  j                         rt	        d      t        |t              rBt        |t              s2|dkD  }|t        d||   z        kD  j                         r{t	        d      t        |t              r?t        |t              s/|dkD  rK|t        d|z        kD  j                         r,t	        d      |dkD  r|t        d|z        kD  rt	        d      t        |t              r(|dk\  |t
        k  z  j                         s1t	        d      d|cxk  rt
        k  st	        d       t	        d      | d|t        |      z  z   z  }t        | |z        }	||z   }
|t        |      t        |
      z  t        |      t        |
      z  t        |      z  z
  z  }|t        |      t        |
      z  t        |      t        |
      z  t        |      z  z   z  }|t        |      t        |
      z  z  }||	z  |z  || z  z  t        |      z  |	|z  t        |      t        |
      z  t        |      t        |
      z  t        |      z  z   z  z
  }||	z  |z  || z  z  t        |      z  |	|z  t        |      t        |
      z  t        |      t        |
      z  t        |      z  z
  z  z
  }||	z  |z  || z  z  t        |      z  |	|z  t        |      z  t        |
      z  z   }|j                  |j                  k7  r,t        ||j                        }t        ||j                        }t        |||g      t        |||g      fS )zCalculates state vectors from orbital elements. Also checks for invalid
    sets of elements.

    Based on equations from this document:

    https://web.archive.org/web/*/http://ccar.colorado.edu/asen5070/handouts/kep2cart_2002.doc
    r   zIIf eccentricity is >1, abs(true anomaly) cannot be more than arccos(-1/e)r   z-Inclination outside the range [0, pi] radians)
isinstancer   r   anyrS   r   allr   r   r   sizer   r
   )rU   rV   rW   rX   rY   rZ   muindsrhuXYZX_dotY_dotZ_dots                    r<   rA   rA   H  s    !W*Q"8!dGF2ag:&&++-hii	Aw	
1g(>!fR$Z  %%'hii	Aw	
1g(>Q3AfRTlN'')hiiQ31VBqD\>hii !WA!r'"'')LMMA||LMM LMM	1qQx<AQrT
A	!A	3r73q6>CGCFN3q6112A	3r73q6>CGCFN3q6112A	3q6#a&=AaCE1Q3KA1c"gc!fns2ws1v~c!f7L&L!MMEaCE1Q3KA1c"gc!fns2ws1v~c!f7L&L!MMEaCE1Q3KA1SVCF!22E 	vvqvv~1affuaff%!QUE5%#8999r>   c                  x    d} d}d}d| z  }d|z   dkD  r&| d|z   z  d|z   z  } |dz   }|dz   }d| z  }d|z   dkD  r&|S )NrP   r   r   )denomfactrtruncxs       r<   
find_truncr     sk    EEE	E	A
A#'5!QuW-		I	 A#'
 Lr>   rO   rP   r   c                 8   | j                         t        k  rt        d      t        t	        |             }t        |       }t        |       }t        |       }t        |       }| dk  }t        ||         ||<   t        ||         ||   z  ||<   | dkD  }t        ||         ||<   t        ||         ||   z  ||<   ||z   }t        |      rt        | |   ddt        f   t        dz
  d      }	|	dddddfxx   dz  cc<   t        t        |	t              t         z  d      ||<   t        t        |	t              t"        z  d      ||<   d| |   ||   z  z
  ||<   d| |   ||   z  z
  ||<   | }
d||
   z
  | |
   z  ||
<   d||
   z
  | |
   z  ||
<   ||||fS )zCalculates Stumpff functions

    Based on the function toolkit/src/spicelib/stmp03.f from the SPICE toolkit,
    which can be downloaded from naif.jpl.nasa.gov/naif/toolkit_FORTRAN.html
    zArgument below lower boundr   r   NaxisrP   )minstumpff_boundrS   r   r   r!   r   r   r   r   r   r   r   r   r   	exponentsodd_factorialseven_factorials)r   zc0c1c2c3lowhighmid
numeratorsnot_mids              r<   stumpffr     s    	uuw566SVA	AB	AB	AB	AB
b&C1S6lBsG1S6l1S6!BsGq5D1T7|BtH1T7|AdG#BtHH+C
3xAcF1g:.aa@
1add7r!eJ	2>AJ3eJ	2?BK3afRWn$3afRWn$3dGr'{?AgJ.BwKr'{?AgJ.BwKr2r>r>   c                   3456 t        |      }|dk  j                         rt        d      t        |      j                         dk(  rt        d      t        |       j                         dk(  rt        d      | j                  dk(  r| ddt
        f   } |j                  dk(  r|ddt
        f   }t        |       }t        | |      }t        | |      }t        ||      }|dk(  j                         rt        d      t        ||      |z  |  |z  z   }	t        |	      }
||d|
z   z  z  }d|
z
  6t        ||z        }||z  5||z  |z  3||z  4||z  }t        t        t        5      t        3      t        4      t        |4z        g      d      }6dk  }t        6      }t        t        d	z        t        ||         z
  }t        6|          }t        6|          }t        t        ||z  |d
|z  z   |z  g      d      ||<   t        d
      t        t              z   t        ||          z
  dz  }t!        |      || <   6ddt
        f   64ddt
        f   43ddt
        f   35ddt
        f   5|ddt
        f   }|ddt
        f   }3456fd}3456fd}t        |      }t        |      }t#        |      dk(  rt%        || j&                  d         }||ddt
        f   z
  }|4z  }t)        || || k         t)        ||||kD          ||      }|dk  }|dkD  }t        |d      }t        |d      }t        |d      }t)        |||       t)        |||       ||   ||   kD  j                         rt)        |||       ||xx   d	z  cc<   t)        |||       t+        |d      }t-        ||   t%        | |      t%        ||            ||<   ||   ||   k(  j                         r)t        dj/                  | ||        ||                   |||   |      ||<   ||   ||   kD  j                         r||   ||   k  j                         rt)        |||       ||xx   d	z  cc<   t)        |||       t+        |d      }t-        ||   t%        | |      t%        ||            ||<   ||   ||   k(  j                         r)t        dj/                  | ||        ||                   |||   |      ||<   ||   ||   k  j                         rt1        |      }t)        |||z   d	z  ||k         t        |      } t3        |d      }!||k  ||k  z  }"|"j                         rt+        |"d      }# |||"   |#      ||"<   ||kD  |"z  }$||k  |"z  }%|$ |% z  |"z  }&t)        |||$|&z         t)        |||%|&z         |"|!dkD  z  |dk7  z  |dk7  z  }'d|!|'<   d| |'<   t)        |||"||kD  z         t)        |||z   d	z  |"||k  z         | dz  } ||k  ||k  z  | |!k  z  }"|"j                         rt5        6|z  |z        \  }(})}*}+5|(z  |3|)z  |4z  |*z  z   z  z   },d||z  |z  |*z  z
  }-|4|dz  z  |+z  z
  }.| |,z  |z  |)z  }/d4|,z  |z  |z  |*z  z
  }0|-t
        ddddf   | ddddt
        f   z  |.t
        ddddf   |ddddt
        f   z  z   }1|/t
        ddddf   | ddddt
        f   z  |0t
        ddddf   |ddddt
        f   z  z   }2t7        |1      t7        |2      fS )a^  Propagates a position and velocity vector with an array of times.

    Based on the function toolkit/src/spicelib/prop2b.f from the SPICE toolkit,
    which can be downloaded from naif.jpl.nasa.gov/naif/toolkit_FORTRAN.html

    Parameters
    ----------
    position : ndarray
       Position vector with shape (3,)
    velocity : ndarray
        Velocity vector with shape (3,)
    t0 : float
        Time corresponding to `position` and `velocity`
    t1 : float or ndarray
        Time or times to propagate to
    gm : float
        Gravitational parameter in units that match the other arguments
    r   z'gm' should be positivez"Velocity vector has zero magnitudez"Position vector has zero magnituder   NzMotion is not conicalr   rP   r   rO   c                 d    t        | z  | z        \  }}}}| |z  | |z  | z  |z  z   z  z   z  S N)r   )	r   r   r   r   r   b2rvbqbr0r   s	        r<   keplerzpropagate.<locals>.kepler	  sF    !A2r2#b&1d2g"R/0011r>   c           
          t        | | z  t        	|      z        \  }}}}| |t        |      z  | |t        |      z  | |t        |      z  z  z   z  z   z  S r   )r   r   )
r   orb_indsr   r   r   r   r   r   r   r   s
         r<   	kepler_1dzpropagate.<locals>.kepler_1d  sk    !F1h$7 782r2"VC**Q6$3I0IArRXY[]eRfOfLg0g-hhiir>   )wherefloat64)dtypezThe input delta time (dt) has a value of {0}.This is beyond the range of DT for which we can reliably propagate states. The limits for this GM and initial state are from {1}to {2}.zThe input delta time (dt) has a value of {0}.This is beyond the range of DT for which we can reliably propagate states. The limits for this GM and initial state are from {1} to {2}.i  @   )r   r   rS   r&   ndimr   r%   r.   r   r   r
   r   r!   r   dpmaxr   r   lenr   shaper   r   r   rs   r   r   r   r   )7r:   r;   t0t1r   r0rvhvech2eqvecrV   qbqovr0maxc
hyperbolicboundfixedrootflogflogboundr   r   dtr   kfunpastfutureupperloweroldxorb_indlcountmostcnot_doner   r   r   same	conditionr   r   r   r   brpcvcpcdotvcdotposition_propvelocity_propr   r   r   r   s7                                                      @@@@r<   rh   rh     s   & 
BB
a}}233(  "a'=>>(  "a'=>>}}AwJ'}}AwJ'	8	B	h	!B(H%D	dD	B
a}}0118T"2%	"4E%A
bAaCjA	AAQrT
A
b&Cq52:D	
QBFE s3x4y2w58}  &'(D
 A#JqMEaL3tJ/00E!J- E*~DUE%K%#d(2BE1I#JKRSTE*C3u:%D*,=(>>!CHXE:+ 	
!W*A	AwJB7
D
aj/C!W*E!W*E2j
 
BB	BB
2w!|Bq)* 
bGn	B
2A
1ufQvX'
1eAeG%!9D6D!VFr+Er+Eb	*D
5!4 
5!6":4 
%
%
'ue4(dqtQd#d#uT{FE67$;VE7=ST$dGtDz!&&( ' (.vb&%.&-'P	R R
 qw0T
 :4 
%
%
' <"V*$
)
)
+ue6*ftQf%f1%vvw(?wAWX&	fIf%**, ' (.vb&%.&-'P	R R
 !6G4V <"V*$
)
)
+ 	UA
1uU{AoeUl4^Fb$E	a%i(H
,,.xa("1X;9Xr	X%byH$(uaT	+uaD*
+uz:eqjI	iyq%E%K 8:q5;/(eUl*CE!AI!e),?' ,,.* QqSU^NBB	R!T"WqtBw&'	'B	
UQY]R	B	b1a4i"n	BFRK!Ob ER!a"$$Ew1}%hq!W}&==7Aq=@QRZ[\^_ah[hRi@iiM'1a-(!Q-)@@5RSUVCWX`abdegnanXoCooM=!7=#999r>   )M
__future__r   sysmathnumpyr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   skyfield.constantsr"   r#   r$   skyfield.functionsr%   r&   r'   skyfield.descriptorlibr(   skyfield.elementslibr)   r*   skyfield.unitsr+   r,   skyfield.vectorlibr-   skyfield.sgp4libr.   r@   r0   tupler   r_   ra   r`   rb   rA   
float_infomaxr   r   r   range	factorialr   r   r   r   r   rh   )rW   s   0r<   <module>r     si    
         5 4 3 3 ( ; - - #eme#e+e3x)> x)t $=.A@2:j 	
 	5E!GQ3GH3Gaq)3GHIE!U1Wa4HI4Hq*4HIJ1eAg	a&3u:%))&Pr:[ IIs   *E!E&