
    R1i3                        d Z ddlmZmZmZmZmZmZ ddlm	Z	m
Z
 ddlmZmZmZmZ ddlmZmZmZmZmZmZ ddlmZmZ ddlmZ dd	lmZ  ed
      Z G d de      Z G d de       Z!ddZ" ed      Z# ed      Z$ ed      Z%d Z&ddZ'y)z>An interface between Skyfield and the Python ``sgp4`` library.    )arrayconcatenateidentitymultiply	ones_likerepeat)SGP4_ERRORSSatrec   )AU_KMDAY_ST0tau)_Tmxmmxvrot_xrot_yrot_z)_find_discretefind_maxima)compute_calendar_date)VectorFunction   c                   f    e Zd ZdZdZdZddZd Zed        Z	d Z
ed        Zd	 Zd
 Zd ZddZy)EarthSatelliteu.
  An Earth satellite loaded from a TLE file and propagated with SGP4.

    An earth satellite object is a Skyfield vector function, so you can
    either call its ``at()`` method to generate its position in the sky
    or else use addition and subtraction to combine it with other
    vectors.

    Satellite parameters are generally only accurate for a week or two
    around the *epoch* of the parameters, the date for which they were
    generated, which is available as an attribute:

    ``epoch``
        A Skyfield :class:`~skyfield.timelib.Time` giving the exact
        epoch moment for these satellite orbit parameters.
    ``name``
        Satellite name

    When building a satellite, use the arguments ``line1`` and ``line2``
    to provide the two data lines from a TLE file as separate strings.
    Optional ``name`` lets you give a name to the satellite, accessible
    later through the ``name`` attribute.  ``ts`` is a
    :class:`~skyfield.timelib.Timescale` object, used to generate the
    ``epoch`` value; if it is not provided, the satellite will use a
    built in ``Timescale`` object.

    If you are interested in the catalog entry details, the SGP4 model
    parameters for a particular satellite can be accessed through its
    ``model`` attribute:

    ``model.satnum``
        The unique satellite NORAD catalog number given in the TLE file.
    ``model.classification``
        Satellite classification, or else ``'U'`` for “Unknown”
    ``model.intldesg``
        International designator
    ``model.epochyr``
        Full four-digit year of this element set's epoch moment.
    ``model.epochdays``
        Fractional days into the year of the epoch moment.
    ``model.jdsatepoch``
        Julian date of the epoch (computed from ``epochyr`` and ``epochdays``).
    ``model.ndot``
        First time derivative of the mean motion (ignored by SGP4).
    ``model.nddot``
        Second time derivative of the mean motion (ignored by SGP4).
    ``model.bstar``
        Ballistic drag coefficient B* in inverse earth radii.
    ``model.ephtype``
        Ephemeris type (ignored by SGP4 as determination now automatic)
    ``model.elnum``
        Element number
    ``model.inclo``
        Inclination in radians.
    ``model.nodeo``
        Right ascension of ascending node in radians.
    ``model.ecco``
        Eccentricity.
    ``model.argpo``
        Argument of perigee in radians.
    ``model.mo``
        Mean anomaly in radians.
    ``model.no_kozai``
        Mean motion in radians per minute.
    ``model.revnum``
        Revolution number at epoch [Revs]

    i  Nc                 p   |/| j                   }|!ddlm} |j                         x}t        _         |d n|j                         | _        t        j                  ||      }|| _	        |j                  }|dk  r|dz   }n|dz   }|j                  |d|j                        | _        | j                  |       y )Nr   )load9   i  il  )tsapir   	timescaler   stripnamer
   
twoline2rvmodelepochyrutc	epochdaysepoch_setup)	selfline1line2r$   r    r   satrectwo_digit_yearyears	            T/home/cursorai/projects/iching/venv/lib/python3.12/site-packages/skyfield/sgp4lib.py__init__zEarthSatellite.__init__X   s    :Bz%)-)99^& LDdjjl	""5%0
B!D(D!D(DVVD!V%5%56
F    c                 ,    d|j                   z
  | _        y )Ni`y)satnumtarget)r,   r/   s     r2   r+   zEarthSatellite._setupm   s    
 -r4   c                    | j                  |       }||_        d|_        t        |j                  d      \  }}t        |      \  }}}|dz  }|j                  ||||z   |j                  z         |_        |j                  |       |S )zBuild an EarthSatellite from a raw sgp4 Satrec object.

        This lets you provide raw numeric orbital elements instead of
        the text of a TLE set.  See :ref:`from-satrec` for detais.

        N      ?      ?)
__new__r&   r$   divmod
jdsatepochr   r(   jdsatepochFr*   r+   )	clsr/   r    r,   wholefractionr1   monthdays	            r2   from_satreczEarthSatellite.from_satrect   s     {{3
	
 !!2!2C8x07eSs
VVD%x&:L:L)LM
Fr4   c                     | j                   S N)target_namer,   s    r2   __str__zEarthSatellite.__str__   s    r4   c                     dj                  | j                  xs d| j                  rdnd| j                  j                  | j                  j                               S )Nz{0}{1}catalog #{2} epoch {3}  )formatr$   r&   r6   r*   utc_strftimerH   s    r2   rG   zEarthSatellite.target_name   sH    -44IIO99C"JJJJ##%	
 	
r4   c                    | j                   }|j                  }|j                  |j                         t        z  z
  }t        |dd      rI|j                  ||      \  }}}|D cg c]  }|r	t        |   nd }	}|j                  |j                  |	fS |j                  ||      \  }}
}|r	t        |   nd}t        |
      t        |      |fS c c}w )a!  Return the raw true equator mean equinox (TEME) vectors from SGP4.

        Returns a tuple of NumPy arrays ``([x y z], [xdot ydot zdot])``
        expressed in kilometers and kilometers per second.  Note that we
        assume the TLE epoch to be a UTC date, per AIAA 2006-6753.

        shapeN)r&   r@   tai_fraction_leap_secondsr   getattr
sgp4_arrayr	   Tsgp4r   )r,   tsatjdrA   erverrormessagespositionvelocitymessages                r2   _position_and_velocity_TEME_kmz-EarthSatellite._position_and_velocity_TEME_km   s     jjWW>>AOO$5$==2w%nnR2GAq!KLM1%eE*=1HM33X%%(+X(>%E8X,1k%(tG?E(OW<< Ns   $Cc                     | j                  |      \  }}}|t        z  }|t        z  }|t        z  }t        |j                  ||dd|j
                        \  }}|||fS )8Deprecated: use the TEME and ITRS frame objects instead.        )rb   r   r   TEME_to_ITRFr@   ut1_fraction)r,   rW   rTEMEvTEMEr]   rITRFvITRFs          r2   ITRF_position_velocity_errorz+EarthSatellite.ITRF_position_velocity_error   sf     #AA!Due#AGGUE3$%NN4ueU""r4   c                     | j                  |      \  }}}|t        z  }|t        z  }|t        z  }t        t        j                  |            }t        ||      }t        ||      }||d|fS )z@Compute this satellite's GCRS position and velocity at time `t`.N)rb   r   r   r   TEMErotation_atr   )r,   rW   r[   r\   r]   Rs         r2   _atzEarthSatellite._at   sm    99!<1e	U
	U
	U
t"#1I1I!T5  r4   c                    |j                   }| |z
  j                  dt        z  }| j                  j                  t
        z  }d|z  }dt        |d      z  }	|	dkD  rd}	d fd}
|	|
_        t        |||
|d      \  }}|s|t        |      fS |k\  }|j                  |   }t        |d	      }fd
}t        t        |j                  f||j                  ff      d      }|dd |dd z   dz  }t        |j                   |||d      \  }}t        ||j                  f      }t        ||dz  f      }|j                         }|j                  ||         ||   fS )u  Return the times at which the satellite rises, culminates, and sets.

        Searches between ``t0`` and ``t1``, which should each be a
        Skyfield :class:`~skyfield.timelib.Time` object, for passes of
        this satellite above the location ``topos`` that reach at least
        ``altitude_degrees`` above the horizon.

        Returns a tuple ``(t, events)`` whose first element is a
        :class:`~skyfield.timelib.Time` array and whose second element
        is an array of events:

        * 0 — Satellite rose above ``altitude_degrees``.
        * 1 — Satellite culminated and started to descend again.
        * 2 — Satellite fell below ``altitude_degrees``.

        Note that multiple culminations in a row are possible when,
        without setting, the satellite reaches a second peak altitude
        after descending partway down the sky from the first one.

        r:   i  g?r9   g      ?c                 P    | j                   dz  | _        t        x| _        | _        y)z8Avoid computing expensive values that cancel out anyway.re   N)ttgast	_identityMMT)rW   s    r2   cheatz)EarthSatellite.find_events.<locals>.cheat   s    TTCZAF""AC!$r4   c                 Z     |         |       j                         d   j                  S Nr   altazdegrees)rW   atry   s    r2   altitude_atz/EarthSatellite.find_events.<locals>.altitude_at   s%    !Ha5;;=#+++r4      uint8c                 `     |         |       j                         d   j                  k  S r{   r|   )rW   altitude_degreesr   ry   s    r2   below_horizon_atz4EarthSatellite.find_events.<locals>.below_horizon_at  s+    !Ha5;;=#++.>>>r4      Nr   g       @   )r    r   r   r&   no_kozair   max	step_daysr   r   rt   r   r   r   argsorttt_jd)r,   topost0t1r   r    half_secondorbits_per_minuteorbits_per_dayr   r   tmaxaltitudekeepersjdmaxonesr   doubletsjdotrsrsrY   r\   ir   ry   s       `                   @@r2   find_eventszEarthSatellite.find_events   sv   2 UUUlEk JJ//#5 #44 3~s33	 tI	#
	, !*$R[+rJh4(( .. (	? +x&ABAF}x|+s2 -={ANR%)rAv'JJLxx1!$$r4   )NNre   )__name__
__module____qualname____doc__centerr    r3   r+   classmethodrD   rI   propertyrG   rb   rl   rq   r    r4   r2   r   r      sa    BF F	B*.  ,  
 
=*#	!U%r4   r   c                        e Zd ZdZed        Zy)rn   u   The SGP4-specific True Equator Mean Equinox frame of reference.

    Described in AIAA 2006-6753 Appendix C.  See :ref:`reference_frames`
    for a guide to using Skyfield reference frames like this one.

    c                     t        | j                  | j                        \  }}|| j                  dz  t        z  z
  }t        t        |      | j                        S )Ng      8@)theta_GMST1982r@   rg   ru   r   r   r   rw   )rW   theta	theta_dotangles       r2   ro   zTEME.rotation_at#  sG    )!''1>>By++5<%%r4   N)r   r   r   r   staticmethodro   r   r4   r2   rn   rn     s     & &r4   rn   c                     | t         z
  |z   dz  }dddd|z  z   |z  z   |z  z   }ddd|z  z   |z  z   }| dz  |z   |t        z  dz  z   dz  t        z  }d|t        dz  z  z   t        z  }||fS )	a  Return the angle of Greenwich Mean Standard Time 1982 given the JD.

    This angle defines the difference between the idiosyncratic True
    Equator Mean Equinox (TEME) frame of reference used by SGP4 and the
    more standard Pseudo Earth Fixed (PEF) frame of reference.  The UT1
    time should be provided as a Julian date.  Theta is returned in
    radians, and its velocity in radians per day of UT1 time.

    From AIAA 2006-6753 Appendix C.

    g    @gmIn@gz`Agt շ?g3ھgt ?gZmDr9   )r   r   r   )jd_ut1fraction_ut1rW   gdgr   r   s          r2   r   r   -  s     
"|	#w.A~WM)AQ(FF!KKA	>]a,??1D	DBc\L(1u9s?:cACGErUW_--4I)r4   )re   re   g      )r   r   r   )r   r   r   c                 P    | t            |t           z  | t           |t            z  z
  S rF   )	_cross120	_cross201)abs     r2   _crossr   D  s'    Y<!I,&9))DDDr4   c                    t        | |      \  }}t        j                  t        |      }t	        |       }	t        |j                        dk(  r0|	j                  |      }
|	j                  |      t        ||
      z   }n%t        |	|      }
t        |	|      t        ||
      z   }|dk(  r|dk(  r|
}|}||fS t        |      j                  t        |            }|j                  |
      }|j                  |      }||fS )rd   r   re   )r   r   outer_zero_zero_minus_oner   lenrP   dotr   r   r   r   )r   rh   ri   xpypr   r   r   angular_velocityrp   rPEFvPEFrj   rk   Ws                  r2   rf   rf   H  s    %fl;E9~~&:IFufA
5;;1wwu~wwu~'7 >>1e}1e}v&6==	SyR3Y
 %< 2YOOE"I&%<r4   Nr   )re   re   re   )(r   numpyr   r   r   r   r   r   sgp4.apir	   r
   	constantsr   r   r   r   	functionsr   r   r   r   r   r   	searchlibr   r   timelibr   	vectorlibr   rv   r   objectrn   r   r   r   r   r   rf   r   r4   r2   <module>r      s    D  ) , , 8 8 2 * %QK	I%^ I%V&6 &"& -. 'N	'N	Er4   