
    R1i'                     h    d Z ddlZddlZddlZddlmZ ddlmZm	Z	 dZ
dddZd	Z G d
 de      ZeZy)zAccess a NASA JPL SPICE Double Precision Array File (DAF).

http://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/req/daf.html

    N)Struct)arrayndarrays   FTPSTR::
:
: :::ENDFTP><)s   BIG-IEEEs   LTL-IEEE   c                   X    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)DAFa2  Access to NASA SPICE Double Precision Array Files (DAF).

    Provide the constructor with a ``file_object`` for full access to
    both the segment summaries and to the numeric arrays.  If you pass a
    ``StringIO`` instead, then you can fetch the summary information but
    not access the arrays.

    c                     t        |dd       rt        d      | _        d  _        d  _         j                  d       fd}d d j                         j                          _         j                  dk(  rIt        j                         D ]'  \   _         _         |         j                  dk(  s' n t        d       j                  j                  d	      rd
d j                  d      t         k7  rt        d      dd  _        t        j#                   j                         _         j                  $t        dj%                   j                               |        n$t        dj%                   j                               j&                  j                          _        d j                  z  d j*                  z  z   }t-         j                  dz          _        t-         j                  |z         x _        }|j2                  x _        }|| dz  z    _        d j6                  z   _        y )Nencodingz-file_object must be opened in binary "b" mode   c                      j                   dz   } t        |       _        j                  j                        \  }_        _        _        _        _        _	        }_
        _        _        y )N8sII60sIII8s603s28s297s)endianr   file_record_structunpackndnilocifnfwardbwardfreeprenulftpstrpstnul)fmtlocidwlocfmtfile_recordselfs      P/home/cursorai/projects/iching/venv/lib/python3.12/site-packages/jplephem/daf.pyr   zDAF.__init__.<locals>.unpack#   sb    ++ 99C&,SkD# ''..{;VTWdgt{DJ
YT[$+       s   NAIF/DAF   zOneither a big- nor a little-endian scan of this file produces the expected ND=2s   DAF/i        zthis SPK file has been damagedX   `   zunknown format {0!r}z0file starts with {0!r}, not "NAIF/DAF" or "DAF/"diddd)getattr
ValueErrorfile_map_arrayread_recordupperrstripr   LOCFMTitemsr   r   r   
startswithstripFTPSTRgetformatr   locifn_textr   r   summary_control_structsummary_structsizesummary_lengthsummary_stepsummaries_per_record)r    file_objectr   summary_formatstructlengthr   s   `     @r!   __init__zDAF.__init__   s   ;
D1LMM		&&q)	< ""1o++-446;;+%,2LLN(T[77a< -;
 ! "L M M[[##G,3t$**51V; !ABB%b,DK **T[[1DK{{" !7!>!>t{{!KLLHO$fT[[13 3  ;;--/twwtww6&,T[[5-@&A#'-dkkN.J'KKf'-{{2f"vgk2%1d6G6G$G!r"   c                     | j                   j                  |t        z  t        z
         | j                   j                  t              S )z=Return record `n` as 1,024 bytes; records are indexed from 1.)r.   seekKread)r    ns     r!   r1   zDAF.read_recordJ   s-    		q1uqy!yy~~a  r"   c                     | j                   j                  |t        z  t        z
         | j                   j                  |      S )z<Write `data` to file record `n`; records are indexed from 1.)r.   rH   rI   write)r    rK   datas      r!   write_recordzDAF.write_recordO   s-    		q1uqy!yyt$$r"   c                 n   | j                   j                  | j                  j                  dd      | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        }| j                  d|       y )Nr#       r   )r   packr   ljustr   r   r   r   r   r   r   r   r   r   rO   )r    rN   s     r!   write_file_recordzDAF.write_file_recordT   s|    &&++KKa&$++JJ

DIIt{{KKdkk

 	!T"r"   c                     d|z  dz
  d|z  }}	 | j                   j                         }|t        j                  z  }t        j                  }	 t        j                  |||z
  |z   |||z
        }|;d}| j                   j                  |       | j                   j                  ||z
        }t        j                  dkD  rt        |      }|fS # t
        $ r d}Y lw xY w# t        t        j                  f$ r d}Y w xY w)a  Return a memory-map of the elements `start` through `end`.

        The memory map will offer the 8-byte double-precision floats
        ("elements") in the file from index `start` through to the index
        `end`, inclusive, both counting the first float as element 1.
        Memory maps must begin on a page boundary, so `skip` returns the
        number of extra bytes at the beginning of the return value.

        r#   )rE   accessoffsetNr   )   )r.   filenommapALLOCATIONGRANULARITYACCESS_READOSErrorAttributeErrorioUnsupportedOperationrH   rJ   sysversion_info
memoryview)	r    startendr*   jrY   skiprms	            r!   	map_wordszDAF.map_words\   s     5y1}a#g1
	YY%%'F t111D  AIIfQqSXa$O 9DIINN1		q1u%Ad"1A$w    7 78 	A	s#   C "C CCC=<C=c                 (    t        d j                        }|sydj                   fd|D              }	 |d|j                  d       j	                  d      j                  dd	      S # t        $ r t        d
      t        $ r t        d      w xY w)z4Return the text inside the comment area of the file.r$    r"   c              3   F   K   | ]  }j                  |      d d   yw)r   r%   N)r1   ).0rK   r    s     r!   	<genexpr>zDAF.comments.<locals>.<genexpr>   s$     L^((+Ad3^s   !N   ascii 
z-DAF file comment area is missing its EOT bytez'DAF file comment area is not ASCII text)	ranger   joinfinddecodereplace
IndexErrorr-   UnicodeDecodeError)r    record_numbersrN   s   `  r!   commentszDAF.commentsz   s    q$**-xxL^LL	H)5)*11':BB4NN 	NLMM! 	HFGG	Hs   2A) )(Bc                     | j                   }|j                  d|dz
  z         d|z   |z
  }|j                  d|z        }t        || j                  dz   |      S )a  Return floats from `start` to `end` inclusive, indexed from 1.

        The entire range of floats is immediately read into memory from
        the file, making this efficient for small sequences of floats
        whose values are all needed immediately.

        r#   r   r)   )r.   rH   rJ   r   r   )r    rd   re   frE   rN   s         r!   
read_arrayzDAF.read_array   sX     II	qEAIS5vva&j!vt{{S0$77r"   c                    | j                   d| j                  d| j                  dz
        \  | _        }|dk(  sJ t	        | j                  dz
  | j
                  dz   | j                        | _         | j                   |dz
  | S )a  Return floats from `start` to `end` inclusive, indexed from 1.

        Instead of pausing to load all of the floats into RAM, this
        routine creates a memory map which will load data from the file
        only as it is accessed, and then will let it expire back out to
        disk later.  This is very efficient for large data sets to which
        you need random access.

        r   r   r)   )r0   rj   r   r/   r   r   )r    rd   re   rg   s       r!   	map_arrayzDAF.map_array   sr     ;;"nnQ		A>ODIt199!$))a-s1BDIINDK{{519s++r"   c              #      K   | j                   }| j                  j                  }|r6| j                  |      } ||dd       \  }}}|||f t	        |      }|r5yyw)zYield (record_number, n_summaries, record_data) for each record.

        Readers will only use the second two values in each tuple.
        Writers can update the record using the `record_number`.

        N   )r   r<   r   r1   int)r    record_numberr   rN   next_numberprevious_numbern_summariess          r!   summary_recordszDAF.summary_records   sh      

,,33##M2D8>tCRy8I5K+d22,M	 s   AAAc              #     K   | j                   }| j                  }| j                         D ]  \  }}}| j                  |dz         }t	        dt        |      |z  |      D ]Z  }| j                  j                  |z   }||||z    j                         }	||||z    }
| j                  j                  |
      }|	|f \  yw)z?Yield (name, (value, value, ...)) for each summary in the file.r   r   N)r?   r@   r   r1   rt   r   r<   r>   r7   r=   r   )r    rE   stepr   r   summary_data	name_datar*   rf   namerN   valuess               r!   	summarieszDAF.summaries   s     $$  8<8L8L8N4M;(():;I1c+.5t<//44q8 1T6*002#Aah/,,33D9Fl" = 9Os   B=B?c                 2    | j                  |d   |d         S )az  Return the array of floats described by a summary.

        Instead of pausing to load all of the floats into RAM, this
        routine creates a memory map which will load data from the file
        only as it is accessed, and then will let it expire back out to
        disk later.  This is very efficient for large data sets to which
        you need random access.

        )r   )r    summary_valuess     r!   mapzDAF.map   s     ~~nR0.2DEEr"   c                 `   | j                   }| j                  }| j                  }t        | j	                  |            }|j                  |dd       \  }}	}
|
| j                  k  r3|}|dz   }|j                  ||	|
dz         |dd | j                  ||       n| j                  dz
  dz  dz   dz  dz   }|dz   }|dz   }d}
|j                  ||	|
      |dd | j                  ||       |j                  d|d      j                  dd	      }d
}| j                  ||       | j                  ||       || _        |dz
  dz  dz  dz   | _	        t        || j                  dz         }| j                  }|j                  |dz
  dz         |j                  |j                                |j!                         dz  }|dz   | _	        | j#                          |d| j$                  | j&                  z   dz
   ||fz   }d|dz
  z  }t)        |
      | j*                  z  }|j                  ||j,                  z   |z          |j                   | j.                  j                  |        |j                  |dz   |z          |j                  |d| j0                   j                  | j*                  d             y)zAdd a new array to the DAF file.

        The summary will be initialized with the `name` and `values`,
        and will have its start word and end word fields set to point to
        where the `array` of floats has been appended to the file.

        Nr   r   r#   i  r   r$   r   r&   s                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   f8rQ   )r.   r<   r   	bytearrayr1   r   rA   rR   rO   r   rS   numpy_arrayr   rH   rM   viewtellrT   r   r   r   r@   r>   r=   r?   )r    r   r   r   r~   scsr   rN   next_recordprevious_recordr   summary_recordname_recordfree_recordr   names
start_wordend_wordbaserW   s                       r!   	add_arrayzDAF.add_array   s    II))

))-8947JJtCRy4I1_k222*N(1,Ko{QOD"Ind3#yy1}1D8TAAEN(1,K(1,KK+ND"ImT2M15;;D%HI Eni8k51'DJ$qD0A59DIE4;;#56YY
	
Q!#$	

668q=qL	 .477*Q./:x2HH~)*[!D$5$55	tchh'(	(##((&12	td{V#$	)d))*001B1BDIJr"   N)__name__
__module____qualname____doc__rF   r1   rO   rT   rj   r|   r   r   r   r   r   r    r"   r!   r
   r
      sH    /Hb!
%
#<H8, -#
F6Kr"   r
   )r   r_   rZ   ra   rD   r   numpyr   r   r   r8   r4   rI   objectr
   NAIF_DAFr   r"   r!   <module>r      sG   
 
  
  /	9	-sK& sKl r"   