U
    e»{j¨A  ã                	   @   s‚  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- dddddddddg	Z.dd /e$¡ d Z0dd„ Z1d d!„ Z2d"d#„ Z3d.d'd(„Z4d)d*„ Z5d/d+d,„Z6e 7d-ee4¡ e 8d-ee6¡ e 9d-ee2¡ dS )0é    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)ÚfirstZXTENSIONZBITPIXZNAXISZNAXIS1ZNAXIS2ZPCOUNTZGCOUNTZTFIELDSZTHEAPú(ú|z)[0-9]+c                 C   s   t  t| ¡d k	S ©N)ÚreÚmatchÚCOLUMN_KEYWORD_REGEXP)Úkeyword© r"   úV/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/io/fits/connect.pyÚis_column_keyword,   s    r$   c                 O   sj   |dk	r,|  ¡ }| d¡}| |¡ |tkS |dk	rH| ¡  d¡rfdS nt|d ttt	t
fƒrbdS dS d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.gzTr   F)ÚtellÚreadÚseekr   ÚlowerÚendswithÚ
isinstancer   r   r   r   )ÚoriginÚfilepathÚfileobjÚargsÚkwargsÚposÚsigr"   r"   r#   Úis_fits0   s    

ÿr3   c           	   	   C   s,  z$| j d  d¡}| j d  d¡}W n ttfk
r@   |  Y S X d}g }| j d |d |… D ]@}|r‚|d |dd…  |d< n| |dd… ¡ t|ƒd	k}q`| j d ||d …= | j d sÈ| j d= t  |¡}|d
 d | j d< |d D ]0}dD ]&}||krôt| |d  j||| ƒ qôqìt	 
| ¡} | S )z’Decode 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   éÿÿÿÿNéF   éG   r   Ú__serialized_columns__Údatatype©Údescriptionr   Úname)r   ÚindexÚ
ValueErrorÚKeyErrorr   ÚlenZget_header_from_yamlÚsetattrÚinfor	   Z_construct_mixins_from_columns)	ÚtblÚi0Úi1Zcontinuation_lineÚlinesÚlinerD   ÚcolÚattrr"   r"   r#   Ú_decode_mixinsN   s.    



rL   FTÚwarnc                 C   sÌ  t | tƒ�rVtƒ }t| ƒD ] \}}	t |	tttfƒr|	||< qt|ƒdkrœ|dkrpt 	dt
|ƒ› d�t¡ t
|ƒ}|  |¡}||krŒ|| }
ntd|› �ƒ‚n¸t|ƒdk�rL|dk	�r>d}z|  |¡}W n  tk
ræ   d|› d�}Y n2X |t| ƒk�rd|› d�}n||k�rd|› �}|dk	�r>t 	|› d	t
|ƒ› d
�t¡ |t
|ƒ }
ntdƒ‚nRt | tttfƒ�rn| }
n:|�rxd}t| ||d�}zt|||||d�W ¢S | ¡  X |
j}g }|jD �]}d }}|jj�rÞ|jjd jn|jj}|jdk	�r||j |jk}d}nD|�r.t|tjƒ�r.t ||j ¡}n|�rLt|tjƒ�rL|jdk}|�s^t  |¡�rxt!||j |j|dd�}nt"||j |jdd�}|j#dk	�r®t$j%|j#d|d�|_#|j&dk	�rÆt'|j&ƒ|_(| )|¡ �q¸t*|dd�}|
j+}|�rddl,m-} |||ƒ}|j.D ]¶\}}}|dk�rV|dk�r*d}||j/k�rH|j/|  )|¡ n|g|j/|< nf||j/k�rœt |j/| t0ƒ�r†|j/|  )|¡ n|j/| |g|j/|< n t1|ƒ�s¼|t2k�r²n
||j/|< �qt3|ƒ}|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   Tó    )Údatar>   ÚmaskÚcopy)rV   r>   rX   Úfits)ÚformatZparse_strict)rX   )Úfits_to_time)ÚCOMMENTZHISTORYr\   r4   )4r+   r   ÚdictÚ	enumerater   r   r   rB   ÚwarningsrM   r   r   Zindex_ofr@   rA   r   Ú	fits_openÚcloseÚread_table_fitsrV   ÚcolumnsZdtypeZsubdtypeÚtypeÚnullr>   Ú
issubclassÚnpZinexactÚisnanÚ	characterÚarrayÚanyr   r   ÚunitÚuZUnitZdispr   rZ   r   r   ÚheaderZfitstimer[   Zcardsr   Úlistr$   ÚREMOVE_KEYWORDSrL   )ÚinputrQ   rR   rP   rO   rS   rT   ZtablesZihduZhdu_itemÚtableÚmsgZhdiZhdulistrV   rc   rJ   ZmaskedrW   ZcoltypeÚcolumnÚtÚhdrr[   ÚkeyÚvalueÚcommentr"   r"   r#   rb   }   sÄ    @
ü





ü
û

 
   ÿ  ÿ


rb   c           	   	      sr  t dd„ |  ¡ D ƒƒ}tdƒ� tj| tfd�}W 5 Q R X || krJ|sJ| S || krlt| jƒ}t| j	|dd�}d}|j 
|i ¡ |j ¡ }z||j| i|_t |¡}W 5 ||_X |j|= d|jkrÌg |jd< |jd  d	¡ |D ]|‰ tˆ ƒd
krødg}nTttd
tˆ ƒd dƒƒ}‡ fdd„t|dd… |dd… ƒD ƒ}|d dd… |d< |jd  |¡ qà|jd  d¡ |S )z–Encode a Table ``tbl`` that may have mixin columns to a Table with only
    astropy Columns + appropriate meta-data to allow subsequent decoding.
    c                 3   s$   | ]‰ t ‡ fd d„dD ƒƒV  qdS )c                 3   s$   | ]}t ˆ j|d ƒd i fkV  qd S r   )ÚgetattrrD   )Ú.0rK   ©rJ   r"   r#   Ú	<genexpr>d  s   ÿz+_encode_mixins.<locals>.<genexpr>.<genexpr>r<   N)rk   )r{   r"   r|   r#   r}   c  s   üþz!_encode_mixins.<locals>.<genexpr>rY   )Zexclude_classesF)r   rX   r:   r4   r5   r   Ú r8   c                    s    g | ]\}}ˆ ||… d  ‘qS )ú\r"   )r{   rF   rG   ©rI   r"   r#   Ú
<listcomp>�  s     z"_encode_mixins.<locals>.<listcomp>Nr7   r   r6   )rk   Zitercolsr   r	   Zrepresent_mixins_as_columnsr
   r   r   r   rc   Ú
setdefaultrX   Zget_yaml_from_tabler   rB   ro   ÚrangeÚzipÚextend)	rE   Z	info_lostZ
encode_tblZ	meta_copyZser_colZtbl_meta_copyZmeta_yaml_linesrH   Zidxsr"   r€   r#   Ú_encode_mixins[  s<    û




(r†   c                 C   st   t | ƒ} t| dd�}t|tƒrLtj |¡rL|r:t |¡ n|sLtt	 
|¡ƒ‚|rft||j|jdd� n
| |¡ dS )aZ  
    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)rO   F)ÚverifyN)r†   r   r+   ÚstrÚosÚpathÚexistsÚremoveÚOSErrorr   rZ   Úfits_appendrV   rn   Zwriteto)rq   ÚoutputÚ	overwriter   Z	table_hdur"   r"   r#   Úwrite_table_fits¦  s    r‘   rY   )NFFTrM   T)FF):r‰   r   r_   rX   r   Únumpyrg   Zastropyr   rm   Z
astropy.ior   Zio_registryZastropy.tabler   r   r   r   r	   Zastropy.timer
   Zastropy.utils.data_infor   Zastropy.utils.exceptionsr   r   Zastropy.utils.miscr   r~   r   r   r   r   r   rŽ   rt   r   r   Zconveniencer   Zhdu.hdulistr   r   r`   Úutilr   rp   Újoinr    r$   r3   rL   rb   r†   r‘   Zregister_readerZregister_writerZregister_identifierr"   r"   r"   r#   Ú<module>   sZ   ÷1      ù
 _K
#