
    HR-eaA                        d dl Z d dlZd dlZd dlmZ d dlZd dlmZ	 d dl
mZ d dlmZmZmZmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d
dlmZmZmZm Z  d
dlm!Z" d
dl#m$Z$m%Z% d
dl&m'Z' d
dl(m)Z) d
dl(m*Z+ d
dl,m-Z- g dZ.dd/                    e$          z   dz   Z0d Z1d Z2d Z3	 	 	 	 	 	 d dZ4d Z5d!dZ6 ej7        dee4            ej8        dee6            ej9        dee2           dS )"    N)deepcopy)units)registry)ColumnMaskedColumnTablemeta	serialize)Time)serialize_context_as)AstropyDeprecationWarningAstropyUserWarning)NOT_OVERWRITING_MSG   )BinTableHDU	GroupsHDUHDUListTableHDU)append)KEYWORD_NAMES_fortran_to_python_format)table_to_hdu)FITS_SIGNATURE)fitsopen)first)	XTENSIONBITPIXNAXISNAXIS1NAXIS2PCOUNTGCOUNTTFIELDSTHEAP(|z)[0-9]+c                 :    t          j        t          |           d uS N)rematchCOLUMN_KEYWORD_REGEXP)keywords    7lib/python3.11/site-packages/astropy/io/fits/connect.pyis_column_keywordr.   ,   s    8)7334??    c                 F   |I|                                 }|                    d          }|                    |           |t          k    S |'|                                                    d          S t          |d         t          t          t          t          f          S )a  
    Determine whether `origin` is a FITS file.

    Parameters
    ----------
    origin : str or readable file-like
        Path or file object containing a potential FITS file.

    Returns
    -------
    is_fits : bool
        Returns `True` if the given file is a FITS file.
    N   )z.fitsz.fits.gzz.fitz.fit.gzz.ftsz.fts.gzr   )tellreadseekr   lowerendswith
isinstancer   r   r   r   )originfilepathfileobjargskwargspossigs          r-   is_fitsr?   0   s     llnnll2Sn$$		~~((G
 
 	
 d1g;	JKKKr/   c                    	 | j         d                             d          }| j         d                             d          }n# t          t          f$ r | cY S w xY wd}g }| j         d         |dz   |         D ]K}|r|d         |dd         z   |d<   n|                    |dd                    t          |          d	k    }L| j         d         ||dz   = | j         d         s| j         d= t          j        |          }|d
         d         | j         d<   |d         D ]3}dD ].}||v r(t          | |d                  j        |||                    /4t          j
        |           } | S )zDecode a Table ``tbl`` that has astropy Columns + appropriate meta-data into
    the corresponding table with mixin columns (as appropriate).
    comments$--BEGIN-ASTROPY-SERIALIZED-COLUMNS--"--END-ASTROPY-SERIALIZED-COLUMNS--Fr   NF   G   r	   __serialized_columns__datatypedescriptionr	   name)r	   index
ValueErrorKeyErrorr   lenget_header_from_yamlsetattrinfor
   _construct_mixins_from_columns)	tbli0i1continuation_linelineslinerR   colattrs	            r-   _decode_mixinsr\   J   s   Xj!''(NOOXj!''(LMM!   


 E$R!Vb[1 , , 	$b	D"I-E"IILLcrc###IIOR"q&[)8J !HZ $U++D *.f6N)OCH%& J @ @+ 	@ 	@Ds{{CK(-tSY???	@ 
23
7
7CJs   A A AAFTwarnc                 f	   t          | t                    rt                      }t          |           D ],\  }}	t          |	t          t
          t          f          r|	||<   -t          |          dk    rq|:t          j	        dt          |           dt                     t          |          }|                     |          }||v r
||         }
nJt          d|           t          |          dk    r|d}	 |                     |          }|t          |           k    rd| d}n ||vrd| }n# t          $ r	 d| d}Y nw xY w|-t          j	        | d	t          |           d
t                     |t          |                   }
nt          d          t          | t          t
          t          f          r| }
nX|rd}t!          | ||          }	 t#          |||||          |                                 S # |                                 w xY w|
j        }g }|j        D ]r}dx}}|j        j        r|j        j        d         j        n|j        j        }|j        ||j                 |j        k    }d}nc|r:t5          |t6          j                  r t7          j        ||j                           }n'|r%t5          |t6          j                  r|j        dk    }|st7          j         |          r$tC          ||j                 |j        |d          }n"tE          ||j                 |j        d          }|j#        !tI          j%        |j#        d|          |_#        |j&        tO          |j&                  |_(        |)                    |           ttU          |d          }|
j+        }|rddl,m-}  |||          }|j.        D ]\  }}}|dv r>|dk    rd}||j/        v r!|j/        |         )                    |           <|g|j/        |<   H||j/        v rYt          |j/        |         t`                    r!|j/        |         )                    |           |j/        |         |g|j/        |<   tc          |          s	|td          v r||j/        |<   tg          |          }|S )a  
    Read a Table object from an FITS file.

    If the ``astropy_native`` argument is ``True``, then input FITS columns
    which are representations of an astropy core object will be converted to
    that class and stored in the ``Table`` as "mixin columns".  Currently this
    is limited to FITS columns which adhere to the FITS Time standard, in which
    case they will be converted to a `~astropy.time.Time` column in the output
    table.

    Parameters
    ----------
    input : str or file-like or compatible `astropy.io.fits` HDU object
        If a string, the filename to read the table from. If a file object, or
        a compatible HDU object, the object to extract the table from. The
        following `astropy.io.fits` HDU objects can be used as input:
        - :class:`~astropy.io.fits.hdu.table.TableHDU`
        - :class:`~astropy.io.fits.hdu.table.BinTableHDU`
        - :class:`~astropy.io.fits.hdu.table.GroupsHDU`
        - :class:`~astropy.io.fits.hdu.hdulist.HDUList`
    hdu : int or str, optional
        The HDU to read the table from.
    astropy_native : bool, optional
        Read in FITS columns as native astropy objects where possible instead
        of standard Table Column objects. Default is False.
    memmap : bool, optional
        Whether to use memory mapping, which accesses data on disk as needed. If
        you are only accessing part of the data, this is often more efficient.
        If you want to access all the values in the table, and you are able to
        fit the table in memory, you may be better off leaving memory mapping
        off. However, if your table would not fit in memory, you should set this
        to `True`.
        When set to `True` then ``mask_invalid`` is set to `False` since the
        masking would cause loading the full data array.
    character_as_bytes : bool, optional
        If `True`, string columns are stored as Numpy byte arrays (dtype ``S``)
        and are converted on-the-fly to unicode strings when accessing
        individual elements. If you need to use Numpy unicode arrays (dtype
        ``U``) internally, you should set this to `False`, but note that this
        will use more memory. If set to `False`, string columns will not be
        memory-mapped even if ``memmap`` is `True`.
    unit_parse_strict : str, optional
        Behaviour when encountering invalid column units in the FITS header.
        Default is "warn", which will emit a ``UnitsWarning`` and create a
        :class:`~astropy.units.core.UnrecognizedUnit`.
        Values are the ones allowed by the ``parse_strict`` argument of
        :class:`~astropy.units.core.Unit`: ``raise``, ``warn`` and ``silent``.
    mask_invalid : bool, optional
        By default the code masks NaNs in float columns and empty strings in
        string columns. Set this parameter to `False` to avoid the performance
        penalty of doing this masking step. The masking is always deactivated
        when using ``memmap=True`` (see above).

    r   Nz^hdu= was not specified but multiple tables are present, reading in first available table (hdu=)zNo table found in hdu=zSpecified hdu=z
 not foundz No table found in specified hdu=z(, reading in first available table (hdu=z;) instead. This will result in an error in future versions!zNo table foundF)character_as_bytesmemmap)hduastropy_nativeunit_parse_strictmask_invalidr   Tr/   )datarK   maskcopy)rf   rK   rh   fits)formatparse_strict)rh   )fits_to_time)COMMENTHISTORYrm   rA   )4r7   r   dict	enumerater   r   r   rO   warningsr]   r   r   index_ofrM   rN   r   	fits_openread_table_fitscloserf   columnsdtypesubdtypetypenullrK   
issubclassnpinexactisnan	characterarrayanyr   r   unituUnitdispr   rj   r   r   headerfitstimerl   cardsr	   listr.   REMOVE_KEYWORDSr\   )inputrb   rc   ra   r`   rd   re   tablesihduhdu_itemtablemsghdihdulistrf   rv   rZ   maskedrg   coltypecolumnthdrrl   keyvaluecomments                              r-   rt   rt   y   se   ~ %!! F'.. 	( 	(ND((X{I$FGG ('tv;;??{4#(==4 4 4 '	   Fmm ..%%Cf}}s !?#!?!?@@@[[AG..--C c%jj((>s>>>F**FFF   ; ; ;:3:::CCC; ?M B B %fB B B 2	   5==)EE -...	EHk9=	>	>   	! !LE6HQWXXX		"-"3)   MMOOOOGMMOOOO :DG| $ $ 03	0BV#)$Q',,	8>SX-D FF 	$j"*== 	$8DN++DD 	$j",?? 	$9#D 	LRVD\\ 	L!#(^#(E  FF chchUKKKF 8&6G  FK
 85ch??FMv 	gE"""A ,C #******l3"""y    UG(((i af}}s""5))))$gsAF]]!&+t,, 3s""5)))) vc{E2ss## 	 so'='=  AF3KK
 	qAHs   =D6 6E	E	H Hc           	      @  	 t          d |                                 D                       }t          d          5  t          j        | t
          f          }ddd           n# 1 swxY w Y   || u r|s| S || u r+t          | j                  }t          | j	        |d          }d}|j        
                    |i            |j                                        }	 ||j        |         i|_        t          j        |          }||_        n# ||_        w xY w|j        |= d|j        vr
g |j        d<   |j        d                             d	           |D ]	t          	          d
k    rdg}nmt          t!          d
t          	          dz   d                    }	fdt#          |dd         |dd                   D             }|d         dd         |d<   |j        d                             |           |j        d                             d           |S )zEncode a Table ``tbl`` that may have mixin columns to a Table with only
    astropy Columns + appropriate meta-data to allow subsequent decoding.
    c              3   N   K   | ]t          fd dD                       V   dS )c              3   L   K   | ]}t          j        |d           d i fvV  d S r(   )getattrrR   ).0r[   rZ   s     r-   	<genexpr>z+_encode_mixins.<locals>.<genexpr>.<genexpr>_  sN       
 
 CHdD))$;
 
 
 
 
 
r/   rI   N)r   )r   rZ   s    @r-   r   z!_encode_mixins.<locals>.<genexpr>^  si        
 	 	 
 
 
 
/
 
 
 	
 	
     r/   ri   )exclude_classesNF)r	   rh   rG   rA   rB   r    rE   c                 0    g | ]\  }}||         d z   S )\ )r   rU   rV   rY   s      r-   
<listcomp>z"_encode_mixins.<locals>.<listcomp>  s*    OOOFBT"R%[4'OOOr/   rD   r   rC   )r   itercolsr   r
   represent_mixins_as_columnsr   r   r	   r   rv   
setdefaultrh   get_yaml_from_tabler   rO   r   rangezipextend)
rT   	info_lost
encode_tbl	meta_copyser_coltbl_meta_copymeta_yaml_linesrX   idxsrY   s
            @r-   _encode_mixinsr   V  s      
 <<>>    I 
f	%	% Y Y:3QUPWXXX
Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y S

 SSX&&	3;YUCCC
 'G Ow+++O((**M("JOG$<=
2:>>'
-
'''' ((&(

#OJ&&'MNNN 2 2t99>>DEE aTR4455DOOOOc$ss)T!""X6N6NOOOEb	#2#E"I
#**51111OJ&&'KLLLs   A%%A),A)(D 	Dc                    t          |           } t          | d          }t          |t                    rYt          j                            |          r:|rt	          j        |           n#|s!t          t          j
        |                    |rt          ||j        |j        d           dS |                    |           dS )a[  
    Write a Table object to a FITS file.

    Parameters
    ----------
    input : Table
        The table to write out.
    output : str
        The filename to write the table to.
    overwrite : bool
        Whether to overwrite any existing file without warning.
    append : bool
        Whether to append the table to an existing file
    T)r`   F)verifyN)r   r   r7   strospathexistsremoveOSErrorr   rj   fits_appendrf   r   writeto)r   output	overwriter   	table_hdus        r-   write_table_fitsr     s      5!!EUt<<<I &# >27>>&#9#9 > 	>If 	>-4V<<=== "FINI,<UKKKKKK&!!!!!r/   ri   )NFFTr]   T)FF):r   r)   rq   rh   r   numpyr|   astropyr   r   
astropy.ior   io_registryastropy.tabler   r   r   r	   r
   astropy.timer   astropy.utils.data_infor   astropy.utils.exceptionsr   r   astropy.utils.miscr   r   r   r   r   r   r   r   r   r   r   conveniencer   hdu.hdulistr   r   rs   utilr   r   joinr+   r.   r?   r\   rt   r   r   register_readerregister_writerregister_identifierr   r/   r-   <module>r      sx   
			 				                  . . . . . . F F F F F F F F F F F F F F       8 8 8 8 8 8 R R R R R R R R 2 2 2 2 2 2 7 7 7 7 7 7 7 7 7 7 7 7 # # # # # # < < < < < < < < % % % % % % ' ' ' ' ' ' . . . . . .      
 
 
 chh}555	A @ @ @L L L4, , ,b 	Z Z Z ZzH H HV" " " "D  FE? ; ; ;  FE+; < < <  w 7 7 7 7 7r/   