
    R1i                     p    d Z ddlmZmZ ddlmZ ddlmZ dZdZ	d Z
 G d	 d
e      Z G d de      Zd Zy)zxCompute things from a NASA SPICE binary PCK kernel file.

ftp://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/req/pck.html

    )arrayrollaxis   )DAF)target_namesg   ,BAg     @c                 "    t         | t        z  z   S )z9Convert a number of seconds since J2000 to a Julian Date.)T0	S_PER_DAY)secondss    P/home/cursorai/projects/iching/venv/lib/python3.12/site-packages/jplephem/pck.pyjdr      s    )###    c                   8    e Zd ZdZd Zed        Zd Zd Zd Z	y)PCKap  A JPL binary PCK (extension ``.bcp``) kernel.

    You can load a binary PCK file by specifying its filename::

        kernel = BinaryPCK.open('moon_pa_de421_1900-2050.bpc')

    Run ``print(kernel)`` see which segments are inside and iterate
    across ``kernel.segments`` to access them each in turn.

    To see the text comments, call ``kernel.comments()``.

    c           	          || _         | j                   j                         D cg c]  \  }}t        | j                   ||       c}}| _        y c c}}w N)daf	summariesSegmentsegmentsselfr   source
descriptors       r   __init__zPCK.__init__   sO    37883E3E3GI3G/VZ !6:>3GI Is   !Ac                 8     | t        t        |d                  S )z9Open the file at `path` and return a binary PCK instance.rb)r   open)clspaths     r   r   zPCK.open#   s     3tD$'())r   c                     | j                   j                  j                          | j                  D ]  }t	        |d      s|` y)zClose this file._dataN)r   filecloser   hasattrr"   )r   segments     r   r$   z	PCK.close(   s3    }}Gw(M %r   c                     | j                   }d }d | j                  D        }dj                   ||j                         ||j                        t        | j                        dj                  |            S )Nc                 $    | j                  d      S )Nzlatin-1)decode)bs    r   <lambda>zPCK.__str__.<locals>.<lambda>1   s    ahhy)r   c              3   2   K   | ]  }t        |        y wr   )str).0r&   s     r   	<genexpr>zPCK.__str__.<locals>.<genexpr>2   s     ;]'W]s   z3File type {0} and format {1} with {2} segments:
{3}
)r   r   formatlocidwlocfmtlenjoin)r   r   dliness       r   __str__zPCK.__str__/   s^    hh);T]];ELLcjjM1SZZ=#dmm*<dii>NP 	Pr   c                 6    | j                   j                         S )z&Return the file comments, as a string.)r   commentsr   s    r   r:   zPCK.comments6   s    xx  ""r   N)
__name__
__module____qualname____doc__r   classmethodr   r$   r8   r:    r   r   r   r      s1    I
 * *"P#r   r   c                   2    e Zd ZdZd Zd ZddZd ZddZy)	r   az  A single segment of a binary PCK file.

    There are several items of information about each segment that are
    loaded from the underlying PCK file, and made available as object
    attributes:

    segment.source - official ephemeris name, like 'DE-0430LE-0430'
    segment.initial_second - initial epoch, as seconds from J2000
    segment.final_second - final epoch, as seconds from J2000
    segment.body - integer body identifier
    segment.frame - integer frame identifier
    segment.data_type - integer data type identifier
    segment.start_i - index where segment starts
    segment.end_i - index where segment ends

    c                     || _         || _        |\  | _        | _        | _        | _        | _        | _        | _        t        | j                        | _
        t        | j                        | _        d | _        y r   )r   r   initial_secondfinal_secondbodyframe	data_typestart_iend_ir   
initial_jdfinal_jdr"   r   s       r   r   zSegment.__init__K   sb    5?	3	d/DJ	tzT0014,,-
r   c                 &    | j                  d      S )NF)verbose)describer;   s    r   r8   zSegment.__str__T   s    }}U}++r   c                     t        t        j                  | j                  d            }dj	                  | |      }|r.|dj	                  | | j
                  j                  d            z  }|S )z,Return a textual description of the segment.zUnknown bodyzD{0.initial_jd:.2f}..{0.final_jd:.2f} frame={0.frame}  {1} ({0.body})z%
  data_type={0.data_type} source={1}ascii)	titlecaser   getrF   r1   r   r)   )r   rN   rF   texts       r   rO   zSegment.describeW   sd    ))$))^DE##)6$#5 	=fT4;;#5#5g#>?ADr   c                    | j                   dk(  rd}nt        d      | j                  j                  | j                  dz
  | j                        \  }}}}t        |dz
        |z  }| j                  j                  | j                  | j                  dz
        }t        |      t        |      f|_        |ddddf   }t        |      ||f|_        t        |d      }t        |d      }|ddd   }|||fS )z?Map the coefficients into memory using a NumPy array.

              z(only binary PCK data type 2 is supported   Nr   )
rH   
ValueErrorr   
read_arrayrJ   int	map_arrayrI   shaper   )r   component_countinitintlenrsizencoefficient_countcoefficientss           r   _loadzSegment._loada   s     >>QOGHH!%!4!4TZZ!^TZZ!PfeQ	No=xx))$,,

QG!!fc%j1#AabD)!!fo7HIa0a0#DbD)V\))r   c                 2   t        |dd       xr t        |dd       }|rt        |f      }| j                  }|| j                         x| _        }|\  }}}|j                  \  }	}
}|t
        z
  t        z  |z
  |t        z  z   }t        ||      \  }}|j                  t              }|dk  j                         s||kD  j                         r|||z  z   }t        d||fz        ||k(  }||xx   dz  cc<   ||xx   |z  cc<   |dddd|f   }d|z  |z  dz
  }d|z  }dx}x}x}}|dd	 D ]&  }|}|}|||z  |z
  z   }|s|}|}d|z  ||z  z   |z
  }( |d	   ||z  |z
  z   }|r	|dddf   }|s|S |||z  z   |z
  }||z  }|dz  }|r	|dddf   }||fS )
zGenerate angles and derivatives for time `tdb` plus `tdb2`.

        If ``derivative`` is true, return a tuple containing both the
        angle and its derivative; otherwise simply return the angles.

        r^   r   Nz+segment only covers dates %.1f through %.1fr   g       @g      ?g        rY   )getattrr   r"   rf   r^   r	   r
   divmodastyper\   anyrZ   )r   tdbtdb2
derivativescalardatar`   ra   re   rd   r_   rc   r   indexoffsetfinal_epochomegasss2w0w1dw0dw1coefficientw2dw2
componentsratess                               r   computezSegment.computew   s#    S'1--OgdGQ6O2O-Czz< $

,DJ%)"fl0<0B0B-?A 8y(4/$2BBw/vS!AI?? 1!+KJ#[12 3 3 1*fv& #AaI. &L6!C'1W!!!R!#',KBBR"-BBhr)C/ - ""%R"5
#AaCJ QWs"!A#JE5  r   N)T)	r<   r=   r>   r?   r   r8   rO   rf   r   rA   r   r   r   r   :   s!     ,*,C!r   r   c                 H    | j                  d      r| S | j                         S )z1Title-case body `name` if it looks safe to do so.)1zC/zDSS-)
startswithtitle)names    r   rR   rR      s    ??#674ITZZ\Ir   N)r?   numpyr   r   r   r   namesr   r	   r
   r   objectr   r   rR   rA   r   r   <module>r      sF   
 "  	$'#& '#R@!f @!DJr   