
    R1iOZ                        d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZ d d	lZd
 Zd Zed   Z  e ed            Z! e ed            Z"e"d   Z#e!d   Z$e!d   Z%e!d   Z& ed       d6dZ'd Z(d7dZ)d7dZ*d Z+d Z,d Z-d Z.d Z/d Z0d Z1d  Z2d! Z3d" Z4d# Z5d$ Z6d% Z7 eg d&      ez  Z8 eg d'      ez  Z9d( Z:d) Z;d* Z<d+ Z=d, Z>d- Z?d. Z@d/ ZAd0 ZBd1 ZCd2 ZDd3 ZEd4 ZFd5 ZGy	)8    )loadTime	load_file)inertial_frames)GM_dict)DistanceAngleVelocity)DAY_S)OsculatingElementsnormpiosculating_elements_of)
ele_to_vec)arraypindarrayfloat64repeatseterrinflinspaceNc               #   8   K   t        j                          y w)N)r   	timescale     c/home/cursorai/projects/iching/venv/lib/python3.12/site-packages/skyfield/tests/test_elementslib.pytsr      s     
..
s   c                     t         j                  j                  t         j                  j                  t              d|       S )Ndata)ospathjoindirname__file__)filenames    r   
_data_pathr&      s&    77<<168DDr   
ECLIPJ2000zde430-2015-03-02.bspzjup310-2015-03-02.bspiomoonearthsunraise)allc                 
   |rt        | |z
        }n| |z
  }t        | d      st        |d      rEt        |d      rt        |      |k  j                         sJ t	        t        |            |k  sJ yyt        |      |k  sJ y)zCompares value to expected value, and works if one or both are arrays.

    Also allows epsilon to be an array.

    If mod==True, then compare(0, tau, 0) is True.
    __len__N)r   hasattrabsr-   max)valueexpected_valueepsilonmoddiffs        r   comparer8   !   s     en,-~%ui GNI$F7I&I(--///s4y>W,,, 0 4yG###r   c                 |   dD ];  }t        | |      }t        |t              sJ |j                  j                  |k(  r;J  dD ]7  }t        | |      }t        |t
        t        f      sJ |j                  |k(  r7J  dD ]:  }t        | |      }t        |t
        t        f      sJ |d   j                  |k(  r:J  dD ]|  }t        | |      }t        |t              sJ |j                  j                  |k(  sJ t        | j                  t              sJ | j                  j                  j                  |k(  r|J  y)zRaise an assertion error for any problems with orbital `elements`.

    Checks that all of the attributes in the OsculatingElements object
    `elements` are present, have the correct type, and have the expected
    size.

    )inclinationlongitude_of_ascending_nodeargument_of_periapsistrue_anomalyargument_of_latitudeeccentric_anomalymean_anomalymean_longitudetrue_longitudelongitude_of_periapsismean_motion_per_day)eccentricityperiod_in_days)_n_vec_e_vec_h_vecr   )semi_latus_rectumapoapsis_distanceperiapsis_distancesemi_major_axissemi_minor_axisN)getattr
isinstancer	   radianssizer   r   r   auperiapsis_timer   tt)elementslengthitemelements       r   check_typesrZ   6   s<   (
 (D)'5)))##v---( 3(D)'GW#5666||v%%% 3
 /(D)'GW#5666qz&((( /
M(D)'8,,,zz&((((114888&&))..&888Mr   c                    i }| j                   |d<   |dk(  s|dk(  r| j                  j                  |d<   n|dk(  r| j                  j                  |d<   | j                  j
                  |d<   | j                  j
                  |d<   | j                  j
                  |d<   | j                  j                  |d	<   |dk(  s|dk(  r| j                  j
                  |d
<   n%|dk(  r | j                  j
                  t        z  |d
<   | j                  j
                  |d<   | j                  j
                  |d<   |dk(  s|dk(  r3| j                  j                  |d<   | j                  j                  |d<   n7|dk(  r2| j                  j                  |d<   | j                  j                  |d<   |dk(  s|dk(  r| j                   |d<   |S |dk(  r| j                   t        z  |d<   |S )a  Return a dictionary with keys that match labels used by Horizons.

    The data from this method is exactly the same as the data in the
    elements object and differs from it only in units.

    Returns an dictionary whose keys match the labels from Horizons:
        EC = eccentricity
        QR = periapsis distance
        IN = inclination
        OM = longitude of ascending node
        W  = argument of periapsis
        Tp = time of closest periapsis
        N  = mean motion
        MA = mean anomaly
        TA = true anomaly
        A  = semi-major axis
        AD = apoapsis distance
        PR = period

    Just like in Horizons, all angle values are in degrees, but the
    distance and time units can be specified with the `units` keyword,
    and must be one of the following:
        'km_s' for kilometers and seconds
        'km_d' for kilometers and days
        'au_d' for au and days

    ECkm_skm_dQRau_dINOMWTpNMATAAADPR)rE   rL   kmrS   r:   degreesr;   r<   rT   tdbrD   r   r@   r=   rM   rK   rF   )elemunitsr   s      r   horizons_dictrp   [   s   8 D""DJf}v,,//T
	,,//T
!!))DJ1199DJ**22DI$$((DJf},,44S		,,44U:S	""**DJ""**DJf}v((++S	++..T
	((++S	++..T
f}((T
 K 
((.T
Kr   c                     t        | |      }t        |d   |d   |d   |d   |d   |d   |d   |d	   |d
   |d   |d   |d   g      }|S )a  Return a numpy array containing data in the same order as horizons.

    The data from this method is exactly the same as the data in the
    elements object and differs from it only in units.

    Just like in Horizons, all angle values are in degrees, but the
    distance and time units can be specified with the `units` keyword,
    and must be one of the following:
        'km_s' for kilometers and seconds
        'km_d' for kilometers and days
        'au_d' for au and days

    The shape of the array is ``(12,)`` if the time used to construct
    the position is a float, and ``(12, n)`` if the time is an array of
    length n.

    ro   r\   r_   ra   rb   rc   rd   re   rf   rg   rh   ri   rj   )rp   r   )rn   ro   dict_array_s       r   horizons_arrayru      sy    $ $e,EE$Ktttststtstt F Mr   c           
          t        t        t        z
  j                  | j	                  dddd                  }t        |      dk(  sJ y )N           z<Elements 1 sets>)r   r)   r*   atutcreprr   rV   s     r   	test_reprr      s;    %tEzoobffT1a6L&MNH>0000r   c           
          t        t        t        z
  j                  | j	                  dddd                  }t        |d       y)zQTests creation of an OsculatingElements object with a single set of elements
    rw   rx   ry   rz      N)r   r)   r*   r{   r|   rZ   r~   s     r   test_single_timer      s4     &tEzoobffT1a6L&MNH!r   c                     | j                  dddg d      }t        t        t        z
  j	                  |            }t        |t        |             y)zRTests creation of an OsculatingElements object with multiple sets of elements
    rw   rx   ry   )rz            N)r|   r   r)   r*   r{   rZ   len)r   timerV   s      r   test_multiple_timesr      s>     66$1./D%tEzood&;<H#d)$r   c                     t         t        z
  j                  | j                  dddd            }t	        t        |            }t        g d      }t        g d      }t        |||       y)zUTests against data from Horizons in km and days, with equatorial reference plane
    rw   rx   ry   )5׃?Ո$A/8S2@Qv@狫Yt@-*BA|Gh*@#Cb@
áb@$NsAϻrA8cC;@)绽|=-C6?Hz>ư>r   :0yE>&.>r   r   r   r   r   Nr)   r*   r{   rm   ru   r   r   r8   r   geocentric_pos	calc_datahorizons_datar5   s        r   test_equatorial_km_dr      s_     Ul&&rvvdAq!'<=N5nEFI 2 3M ]^GI}g.r   c                     t         t        z
  j                  | j                  dddd            }t	        t        |      d      }t        g d      }t        g d      }t        |||       y)	zXTests against data from Horizons in km and seconds, with equatorial reference plane
    rw   rx   ry   r]   rr   )r   r   r   r   r   r   򀱧'$?r   r   r   r   m*AA)r   r   r   r   r   r   +=r   r   r   r   MbP?Nr   r   s        r   test_equatorial_km_sr      sa     Ul&&rvvdAq!'<=N5nEVTI 2 3M ^_GI}g.r   c                     t         t        z
  j                  | j                  dddd            }t	        t        |      d      }t        g d      }t        g d      }t        |||       y)	zUTests against data from Horizons in au and days, with equatorial reference plane
    rw   rx   ry   r`   rr   )r   Vc?r   r   r   r   r   r   r   S	e?hi@O5f?r   )r   -q=r   r   r   r   r   r   r   vIh%<=r   r   Nr   r   s        r   test_equatorial_au_dr      sa     Ul&&rvvdAq!'<=N5nEVTI 2 3M `aGI}g.r   c                     t         t        z
  j                  | j                  dddd            }t	        t        |t              d      }t        g d      }t        g d      }t        |||       y)	zSTests against data from Horizons in km and days, with ecliptic reference plane
    rw   rx   ry   r^   rr   )r   r   D{ڷ@1AU	g@ba@r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   N	r)   r*   r{   rm   ru   r   ECLIPTICr   r8   r   s        r   test_ecliptic_km_dr     sd     Ul&&rvvdAq!'<=N5nhOW]^I 1 2M ]^GI}g.r   c                     t         t        z
  j                  | j                  dddd            }t	        t        |t              d      }t        g d      }t        g d      }t        |||       y)	zVTests against data from Horizons in km and seconds, with ecliptic reference plane
    rw   rx   ry   r]   rr   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   Nr   r   s        r   test_ecliptic_km_sr   %  sd     Ul&&rvvdAq!'<=N5nhOW]^I 2 3M ^_GI}g.r   c                     t         t        z
  j                  | j                  dddd            }t	        |t
              }t        |d      }t        g d      }t        g d      }t        |||       y)	zSTests against data from Horizons in au and days, with ecliptic reference plane
    rw   rx   ry   r`   rr   )r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   N)	r)   r*   r{   rm   r   r   ru   r   r8   )r   r   rn   r   r   r5   s         r   test_ecliptic_au_dr   :  sg     Ul&&rvvdAq!'<=N!.(;Dt62I 2 3M `aGI}g.r   c           	          t         t        z
  j                  | j                  ddddd            }t	        t        |      d      }t        g d      }t        g d	      }t        |||       y
)zUTests against data from Horizons for an orbit with eccentricity just less than 1
    rw   rx   ry         r]   rr   )gB4?g_>)AgbX5@gF@g{հSv@g,84BAg|e;=gx7d?g!`@g2
DBgEOTBgX9mrB)r   皙?r   r   r   r   gؗҜ<r   r        j@r   g    .AN)r(   r+   r{   rm   ru   r   r   r8   r   s        r   test_extreme_ellipser   P  sa     3h]]266$1b"#=>N5nEVTI 2 3M [\GI}g.r   c           	      8   t         t        z
  j                  | j                  ddddd            }t	        t        |      d      }t        g d      }t        g d	      }t        |d
d ||       |dd
 t        t        t        g      k(  j                         sJ y
)zPTests against data from Horizons for an orbit with eccentricity just over 1
    rw   rx   ry   r      r]   rr   )
gD?g\F	AgL.5@gi+@g=Yv@g'"4BAg_R5=ggVgb?g:WH`@gQ?y[)
dy=g{Gz?r   r   r   r   gFFg<r   r   g     @N)
r(   r+   r{   rm   ru   r   r   r8   r   r-   r   s        r   test_slightly_hyperbolicr   e  s     3h]]266$1b"#=>N5nEVTI 	3 	4M TUGIcrNM73bcNeS#J//44666r   c                     | j                  t        ddd            }t        t        t        z
  j                  |            }|j                  j                   }d|j                   z  dz   }t        ||d       y)	a  This tests the moment when the eccentricity of Io orbiting the sun
    transitions from just below 1 to just above 1. Periapsis time is
    calculated using M/n, and both M and n go to 0 as e goes to 1.
    Periapsis time of Io around the sun should change linearly over a very
    small time interval interval (a fraction of one second) during this
    transition, but instead periapsis time deviated from linear within .005s
    of e=1.

    One source of the deviation was the fact that M was being remapped to
    -pi to pi when e>1. Using M without remapping reduced the deviation when
    e>1. The second thing that reduced the deviation was widening the
    tolerance for the use of the parabolic periapsis time equation.

    This test makes sure these fixes don't regress by checking that the
    periapsis times don't deviate from linear by more than 1e-5 days within
    .005s of e=1.
    g
BAg
BAi  jdgv5,ngRx=Agh㈵>N)rm   r   r   r(   r+   r{   rT   r8   )r   trn   rd   lines        r   test_periapsis_timer   y  sg    $ 	(,.?EFA!28--"23D				 	 Bquu$'88DBdr   c           	      ,   d}| |||||fD ]$  }t        |t        t        f      st        |      }& d}	t	        | ||||||	      \  }
}|j                  d      j                  }|j                  t        ||
d   j                              }t        t        |
      t        |      ||	      }t        ||       t        ||j                  d       t        |j                  j                   | d	       t        |j"                  |d
       t        |j$                  j&                  |d
d       t        |j(                  j&                  |d
d       t        |j*                  j&                  |d
d       t        |j,                  j&                  |d
d       y)aI  Checks that the given set of elements are calculated properly by
    elementslib.py

    Converts the given elements to state vectors using ele_to_vec, then uses
    those state vectors to create an OsculatingElements object, and then
    checks that the data in the OsculatingElements object matches the input
    elements.
    r   g 'Ai  r   r   )rk   )km_per_sr   r   r   T)r6   N)rP   listr   r   r   r|   rU   r   rR   r   r   r
   rZ   r8   r   rJ   rk   rE   r:   rQ   r;   r<   r=   )peiOmwvr   rW   rX   mupos_vecvel_vectime_ttr   rV   s                  r   check_orbitr     sU    F1aQ!dT7O,YF " 
B!!Q2q!R8GWffTlooG55F7GAJOO455D!(g"6"*G"<"&,H &!D(--&H&&))1d3H!!1e,H  ((!U=H0088"eNH**22Au$GH!!))1e>r   )r         ?      ?      ?r   g      ?g      ?g      ?)g      g      g      пr   r   r   r   c           
          d}d}t        d|dt        |d|        t        d|dd|t        |        t        D ]$  }t        d|d||d|        t        d|dd|||        & y )Nr    r   r   r   angles1)r   r   r   angles       r   test_circularr     sf    	A	A2w1b12q!Wb1FAr5!Q3FAr1a3 r   c           
      v    d}d}d}d}t        d||||t        |        t        D ]  }t        d||||||         y )Nr   r   r   )r   r   r   r   r   r   s         r   test_circular_equatorialr     sH    	A	A	A	
B1b!Wb1FAq"a3 r   c           
          d}d}t         dz  }t        d||t        |d|        t        d||d|t        |        t        D ]$  }t        d||||d|        t        d||d|||        & y )Nr   ry   r   rx   r   r   r   r   )r   r   r   r   r   s        r   test_circular_polarr     so    	A	A
1A1gq!R01aGR0FAq%Ar2FAq!Qr2 r   c           
      l   t        ddt        d d ddd|        t        dddt        dd|        t        ddddt        d|        t        dddddt        |        t        d d D ]  }t        dd|ddd|         t        D ]5  }t        ddd|dd|        t        dddd|d|        t        ddddd||        7 y )Nr   333333?   r      r   ry   r   )r   r   s     r   test_ellipticalr     s    GBQKAq"5BAr2B7Ar2B1gr2!FBq!Q3 FBE1a4FBAua4FBAq%4 r   c           
          d}d}t        dd||t        d|        t        dd||dt        |        t        D ]$  }t        dd|||d|        t        dd||d||        & y )Nr   r   r   r   r   r   )r   r   r   r   s       r   test_elliptical_equatorialr     sf    	A	
BAr7Ar2Ar1gr2FB2ua4FB2q%4 r   c           
         t         dz  }t        dd|t        dd|        t        dd|dt        d|        t        dd|ddt        |        t        D ]5  }t        dd||dd|        t        dd|d|d|        t        dd|dd||        7 y )Nry   r   皙?rx   r   r   )r   r   r   s      r   test_elliptical_polarr     s    
1AAw1b1Aq'1b1Aq!Wb1FB5!Q3FB1eQ3FB1a3 r   c           
         d}t        d|t        d d ddd|        t        d|dt        dd|        t        d|ddt        d|        t        d|dddt        |        t        d d D ]  }t        d||ddd|         t        D ]$  }t        d|d|dd|        t        d|dd|d|        & t        D ]  }t        d|ddd||         y )Nr   r   r   r   rx   ry   r   r   angles2r   r   r   s      r   test_parabolicr     s    	A72A;1a41gq!R01a!R01aGR0!FAuaAr2 FAq%Ar2FAq!UAr2  FAq!Qr2 r   c           
          d}d}d}t        d|||t        d|        t        d|||dt        |        t        D ]  }t        d||||d|         t        D ]  }t        d|||d||         y )Nr   r   r   ry   r   r   r   r   r   r   r   s        r   test_parabolic_equatorialr   	  su    	A	A	
B1b'1b11b!Wb1FAq"eQ3 FAq"a3 r   c           
      *   d}t         dz  }t        d||t        dd|        t        d||dt        d|        t        d||ddt        |        t        D ]$  }t        d|||dd|        t        d||d|d|        & t        D ]  }t        d||dd||         y )Nr   ry   r   rx   r   r   r   r   r   r   r   r   s       r   test_parabolic_polarr     s    	A
1A1gq!R01a!R01aGR0FAq%Ar2FAq!UAr2  FAq!Qr2 r   c           
         d}t        d|t        d d ddd|        t        d|dt        dd|        t        d|ddt        d|        t        d|dddt        |        t        d d D ]  }t        d||ddd|         t        D ]$  }t        d|d|dd|        t        d|dd|d|        & t        D ]  }t        d|ddd||         y )N?r   r   r   r   ry   rx   r   r   s      r   test_hyperbolicr   "  s    A72A;1b"51gq"b11a"b11aGR0!FAuaB3 FAq%B3FAq!UB3  FAq!Qr2 r   c           
          d}d}d}t        d|||t        d|        t        d|||dt        |        t        D ]  }t        d||||d|         t        D ]  }t        d|||d||         y )Nr   r   r   r   r   r   r   s        r   test_hyperbolic_equatorialr  2  su    A	A	
B1b'2r21b!Wb1FAq"eR4 FAq"a3 r   c           
      *   d}t         dz  }t        d||t        dd|        t        d||dt        d|        t        d||ddt        |        t        D ]$  }t        d|||dd|        t        d||d|d|        & t        D ]  }t        d||dd||         y )Nr   ry   r   r   r   r   r   s       r   test_hyperbolic_polarr  ?  s    A
1A1gq"b11a"b11aGR0FAq%B3FAq!UB3  FAq!Qr2 r   c                     t        t        dgdz        t        g d      t        ddt        dz  ddt        dz  ddt        dz  ddt        dz  g      t        g d      t        g d	      t        g d
      |        y )Nr   rz   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   ry   r   )r   r   r   ry   r   r   rx   r   r   rx   r   r   )r   r   r   r   r   ry   r   r   ry   r   r   ry   )r   r   rx   r   r   rx   r   r   rx   r   r   r   )r   r   r   r   r   r   r   )r   r   r   )r   s    r   test_all_types_at_oncer  M  so    %$OPQ1b1bdAq"Q$!q"Q$OPPQOPOPr   c                    t         t        z
  j                  | j                  dddd            }t	        |t
              }|j                  t        d   k(  sJ t        j                  | j                  dddd            }t	        |t
              }|j                  t        d   k(  sJ t	        |t
        t        d         j                  |j                  k(  sJ y )Nrw   rx   ry   r   )r)   r*   r{   rm   r   r   _mur   )r   r   geocentric_elementsbarycentric_posbarycentric_elementss        r   test_gm_calculationr  W  s    Ul&&rvvdAq!'<=N0J""gaj000hhrvvdAq!45O1/8L##wqz111!/8WQZHLLPdPhPhhhhr   )F)r^   )Hskyfield.apir   r   r   skyfield.data.spicer   &skyfield.data.gravitational_parametersr   skyfield.unitsr   r	   r
   skyfield.constantsr   skyfield.elementslibr   r   r   skyfield.keplerlibr   numpyr   r   r   r   r   r   r   r   r    r   r&   r   ephem	jup_ephemr(   r)   r*   r+   r8   rZ   rp   ru   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  r   r   r   <module>r     sZ   . . / : 4 4 $ 
 * L L L 	E <(*345j!89:	t_V}gEl 7 $*"9J=@D1
%/*/*/*/*/*/,/*7(4?F 5
6r
9
2
3B
644	355	43
433 
43	ir   