B
    q\:              	   @   sz  d dl Z d dlZd dlZd dlm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 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" d
dl#m$Z$ d
dl%m&Z' d
dl(m)Z) d
dl*m+Z+m,Z, d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 )/    N)OrderedDict)registry)units)Table	serializemetaColumnMaskedColumn)has_info_class)Time)AstropyUserWarning)	MixinInfoserialize_context_as   )HDUListTableHDUBinTableHDU	GroupsHDU)KEYWORD_NAMESASCII_DEFAULT_WIDTHS_fortran_to_python_format)table_to_hdu)fitsopen)first)VerifyErrorVerifyWarnings   SIMPLE  =                    TZ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"   6lib/python3.7/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 object
        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seekFITS_SIGNATURElowerendswith
isinstancer   r   r   r   )originfilepathZfileobjargskwargsposZsigr"   r"   r#   is_fits)   s    

r2   c       	   	   C   s4  y$| j d d}| j d d}W n ttfk
r<   | S X d}g }xZ| j d |d | D ]@}|r|d |dd  |d< n||dd  t|d	k}q^W | j d ||d = | j d s| j d= t |}|d
 d | j d< x@|d D ]4}x.dD ]&}||krt| |d  j|||  qW qW t	
| } | S )zDecode a Table ``tbl`` that has astropy Columns + appropriate meta-data into
    the corresponding table with mixin columns (as appropriate).
    commentsz$--BEGIN-ASTROPY-SERIALIZED-COLUMNS--z"--END-ASTROPY-SERIALIZED-COLUMNS--Fr   NF   G   r   __serialized_columns__Zdatatype)descriptionr   name)r   index
ValueErrorKeyErrorappendlenZget_header_from_yamlsetattrinfor   Z_construct_mixins_from_columns)	tbli0i1Zcontinuation_linelinesliner@   colattrr"   r"   r#   _decode_mixinsF   s.    


"
rH   FTc             C   s  t | trt }x,t| D ] \}}t |tttfr|||< qW t|dkr|dkrpt	d
t|t t|}| |}||kr|| }qtd
|n"t|dkr|t| }ntdn>t | tttfr| }n(t| ||d}	zt|	||dS |	  X tdd	 |jD }
|j}g }x|jD ]}|
rpt||j |jd
d}|jdk	r||j d|j|j|jk< nt||j |jd
d}|jdk	rtj|jddd|_|jdk	rt|j|_
| | q$W t!||
d
d}|j"}|rddl#m$} |||}x|j%D ]\}}}|dkrT|dkr(d}||j&krF|j&|  | n|g|j&|< nf||j&krt |j&| t'r|j&|  | n|j&| |g|j&|< n t(|s|t)krn
||j&|< qW t*|}|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 object 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`.
    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`.
    r   Nzbhdu= was not specified but multiple tables are present, reading in first available table (hdu={0})zNo table found in hdu={0}zNo table found)character_as_bytesmemmap)hduastropy_nativec             s   s   | ]}|j d k	V  qd S )N)null).0rF   r"   r"   r#   	<genexpr>   s    z"read_table_fits.<locals>.<genexpr>F)datar9   copyTfitsZsilent)formatZparse_strict)maskedrQ   )fits_to_time)COMMENTZHISTORYrV   r3   )+r,   r   r   	enumerater   r   r   r>   warningswarnrS   r   r   Zindex_ofr;   	fits_openread_table_fitscloseanycolumnsrP   r	   r9   rM   Zset_fill_valuemaskr   ZunituZUnitZdispr   r=   r   headerZfitstimerU   Zcardsr   listr$   REMOVE_KEYWORDSrH   )inputrK   rL   rJ   rI   ZtablesZihduZhdu_itemtableZhdulistrT   rP   r^   rF   columntZhdrrU   keyvaluecommentr"   r"   r#   r[   t   s|    +







r[   c       
   	      s  t dd |  D }yddl}W nl tk
r   xV|  D ]6}t|tr<|jtjt	fkr<t
d|jj|jjq<W |rtdt | S Y nX td tj| t	fd}W dQ R X || kr|s| S d	}|j|i  |j }z||j| i|_t|}W d||_X |j|= d
|jkr&g |jd
< |jd
 d x|D ] t dkrVdg}nTttdt d d}	 fddt|	dd |	dd D }|d dd |d< |jd
 | q<W |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   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 )N)getattrr@   )rN   rG   )rF   r"   r#   rO   "  s   z+_encode_mixins.<locals>.<genexpr>.<genexpr>)r8   r   N)r]   )rN   r"   )rF   r#   rO   "  s   z!_encode_mixins.<locals>.<genexpr>r   NzBcannot write type {} column '{}' to FITS without PyYAML installed.ztable contains column(s) with defined 'format', 'description', or 'meta' info attributes. These will be dropped unless you install PyYAML.rR   )Zexclude_classesr7   r3   z$--BEGIN-ASTROPY-SERIALIZED-COLUMNS-- r5   c                s    g | ]\}} || d  qS )\r"   )rN   rB   rC   )rE   r"   r#   
<listcomp>j  s    z"_encode_mixins.<locals>.<listcomp>r4   r   z"--END-ASTROPY-SERIALIZED-COLUMNS--)r]   ZitercolsyamlImportErrorr
   r   	__class__r`   ZQuantityr   	TypeErrorrS   __name__r@   r9   rX   rY   r   r   r   Z_represent_mixins_as_columnsr   
setdefaultrQ   Zget_yaml_from_tabler=   r>   rb   rangezipextend)
rA   Z	info_lostro   rF   Z
encode_tblZser_colZtbl_meta_copyZmeta_yaml_linesrD   Zidxsr"   )rE   r#   _encode_mixins  sN    	





(rx   c             C   sV   t | } t| dd}t|trHtj|rH|r:t| ntd	||
| 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.
    T)rI   zFile exists: {0}N)rx   r   r,   strospathexistsremoveOSErrorrS   Zwriteto)rd   outputZ	overwriteZ	table_hdur"   r"   r#   write_table_fitss  s    r   rR   )NFFT)F):rz   r   rX   collectionsr   Z
astropy.ior   Zio_registryZastropyr   r`   Zastropy.tabler   r   r   r   r	   Zastropy.table.tabler
   Zastropy.timer   Zastropy.utils.exceptionsr   Zastropy.utils.data_infor   r   rl   r   r   r   r   rf   r   r   r   Zconveniencer   Zhdu.hdulistr   rZ   utilr   Zverifyr   r   r)   rc   joinr    r$   r2   rH   r[   rx   r   Zregister_readerZregister_writerZregister_identifierr"   r"   r"   r#   <module>   s>   
. 
 &Y
