U
    e»{jÒ7  ã                   @   sŒ   d Z ddlZ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
mZ dgZG dd	„ d	e
jƒZG d
d„ de
jƒZG dd„ de
jƒZdS )zÒAn extensible ASCII table reader and writer.

cds.py:
  Classes to read CDS / Vizier table format

:Copyright: Smithsonian Astrophysical Observatory (2011)
:Author: Tom Aldcroft (aldcroft@head.cfa.harvard.edu)
é    N)Úsuppress)ÚUnité   )ÚcoreÚ
fixedwidthÚ*c                   @   s:   e Zd ZdZejejejejdœZdZ	dd„ Z
dd„ ZdS )Ú	CdsHeaderÚCDS)ÚeÚfÚiÚaNc                 C   sB   t  d|j ¡ ¡}|s8td| j› d|j› d|j› d�ƒ‚| d¡S )Nz\d*(\S)zUnrecognized z	 format "z" for column"ú"r   )ÚreÚmatchÚraw_typeÚlowerÚ
ValueErrorÚ_subfmtÚnameÚgroup)ÚselfÚcolr   © r   úS/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/io/ascii/cds.pyÚget_type_map_key&   s    ÿzCdsHeader.get_type_map_keyc              	   C   s4  | j ræ| jjræd}t ¡ }| | j ¡}g }d}|D ]–}| ¡ }|rl| |¡ | d¡rÈ|d7 }|dkrÈ qæq2t	 
d|t	j¡}|r2dd„ t	 d	| d
¡¡D ƒ}|D ]&}	t | jj|	¡r d}| |¡  q2q q2t d| jj› d| j › �¡‚d}
t|ƒD ]2\}}t	 
d|t	j¡�rd}
qò|
rò|d8 } �q.qòtdƒ‚t	 dt	j¡}g }t ||d d¡D �]Ä}| d¡�rj �q| 
|¡}|�rètj| d
¡d�}tt	 dd| d¡�pª| d¡¡ƒd |_t| d¡ƒ|_| d¡}|dk�râd|_nt|ddd�|_| d¡�p d ¡ |_| d¡|_|  |¡|_ t	 
d|jt	j¡}|�rÜ| d ¡�pFd ¡ |_t!|j tj"ƒ�rdd!}nd"}| d#¡d$k�r¦d|_#d%D ] }| jj$ d$| ||j%f¡ �q‚n6| d#¡|_#|j#dk�rÄd|_#| jj$ |j#||j%f¡ | |¡ n.|�r|d&  j| ¡ 7  _ntd'|› d(�ƒ‚�qRd)d„ |D ƒ| _&|| _'dS )*zÊ
        Initialize the header Column objects from the table ``lines`` for a CDS/MRT
        header.

        Parameters
        ----------
        lines : list
            List of table lines

        Fr   ©z------z=======r   é   z/Byte-by-byte Description of file: (?P<name>.+)$c                 S   s   g | ]}|r|‘qS r   r   )Ú.0Úsr   r   r   Ú
<listcomp>T   s      z&CdsHeader.get_cols.<locals>.<listcomp>z[, ]+r   TzCan't find table z in zByte-by-byte Descriptionz-no line with "Byte-by-byte Description" foundzø\s*
                (?P<start> \d+ \s* -)? \s*
                (?P<end>   \d+)        \s+
                (?P<format> [\w.]+)     \s+
                (?P<units> \S+)        \s+
                (?P<name>  \S+)
                (\s+ (?P<descr> \S.*))?é   N©r   z[-\s]Ú ÚstartÚendZunitsz---ÚcdsÚwarn)ÚformatZparse_strictÚdescrr(   zu(?P<limits>[\[\]] \S* [\[\]])?\?((?P<equal>=)(?P<nullval> \S*))?(?P<order>[-+]?[=]?)(\s* (?P<descriptiontext> \S.*))?ZdescriptiontextÚnanÚ0Znullvalú-)r   é   r   r!   éÿÿÿÿzLine "z" not parsable as CDS headerc                 S   s   g | ]
}|j ‘qS r   r"   )r   Úxr   r   r   r    µ   s     )(ÚreadmeÚdataÚ
table_namer   ZBaseInputterÚ	get_linesÚstripÚappendÚ
startswithr   r   Ú
IGNORECASEÚsplitr   ÚfnmatchÚInconsistentTableErrorÚ	enumerater   ÚcompileÚVERBOSEÚ	itertoolsÚisliceÚColumnÚintÚsubr$   r%   Úunitr   Údescriptionr   Zget_col_typeÚtypeÚ
issubclassÚ	FloatTypeÚnullZfill_valuesr   ÚnamesÚcols)r   ÚlinesZ	in_headerZreadme_inputterr   Zcomment_linesÚliner   rI   ÚpatternZ
found_lineZ	i_col_defZ
re_col_defrJ   r   rC   Zfillvalr   r   r   r   Úget_cols/   sª    

ý
ÿø
ÿÿ


ò zCdsHeader.get_cols)Ú__name__Ú
__module__Ú__qualname__r   r   rG   ZIntTypeZStrTypeZcol_type_mapr0   r   rN   r   r   r   r   r      s   ü	r   c                   @   s"   e Zd ZdZdZejZdd„ ZdS )ÚCdsDatazCDS table data readerr	   c                 C   sP   | j jr| jr|S dd„ t|ƒD ƒ}|s<t d| j› d�¡‚||d d d… S )z>Skip over CDS/MRT header by finding the last section delimiterc                 S   s   g | ]\}}|  d ¡r|‘qS )r   )r6   )r   r   r/   r   r   r   r    È   s    
 z)CdsData.process_lines.<locals>.<listcomp>zNo z section delimiter foundr.   r   N)Úheaderr0   r2   r;   r   r:   r   )r   rK   Z
i_sectionsr   r   r   Úprocess_linesÀ   s    ÿÿzCdsData.process_linesN)	rO   rP   rQ   Ú__doc__r   r   ZFixedWidthSplitterZsplitter_classrT   r   r   r   r   rR   º   s   rR   c                       sR   e Zd ZdZdZdgZdZdZeZ	e
Zd‡ fdd„	Zddd	„Z‡ fd
d„Z‡  ZS )ÚCdsaV  CDS format table.

    See: http://vizier.u-strasbg.fr/doc/catstd.htx

    Example::

      Table: Table name here
      = ==============================================================================
      Catalog reference paper
          Bibliography info here
      ================================================================================
      ADC_Keywords: Keyword ; Another keyword ; etc

      Description:
          Catalog description here.
      ================================================================================
      Byte-by-byte Description of file: datafile3.txt
      --------------------------------------------------------------------------------
         Bytes Format Units  Label  Explanations
      --------------------------------------------------------------------------------
         1-  3 I3     ---    Index  Running identification number
         5-  6 I2     h      RAh    Hour of Right Ascension (J2000)
         8-  9 I2     min    RAm    Minute of Right Ascension (J2000)
        11- 15 F5.2   s      RAs    Second of Right Ascension (J2000)
      --------------------------------------------------------------------------------
      Note (1): A CDS file can contain sections with various metadata.
                Notes can be multiple lines.
      Note (2): Another note.
      --------------------------------------------------------------------------------
        1 03 28 39.09
        2 04 18 24.11

    **About parsing the CDS format**

    The CDS format consists of a table description and the table data.  These
    can be in separate files as a ``ReadMe`` file plus data file(s), or
    combined in a single file.  Different subsections within the description
    are separated by lines of dashes or equal signs ("------" or "======").
    The table which specifies the column information must be preceded by a line
    starting with "Byte-by-byte Description of file:".

    In the case where the table description is combined with the data values,
    the data must be in the last section and must be preceded by a section
    delimiter line (dashes or equal signs only).

    **Basic usage**

    Use the ``ascii.read()`` function as normal, with an optional ``readme``
    parameter indicating the CDS ReadMe file.  If not supplied it is assumed that
    the header information is at the top of the given table.  Examples::

      >>> from astropy.io import ascii
      >>> table = ascii.read("data/cds.dat")
      >>> table = ascii.read("data/vizier/table1.dat", readme="data/vizier/ReadMe")
      >>> table = ascii.read("data/cds/multi/lhs2065.dat", readme="data/cds/multi/ReadMe")
      >>> table = ascii.read("data/cds/glob/lmxbrefs.dat", readme="data/cds/glob/ReadMe")

    The table name and the CDS ReadMe file can be entered as URLs.  This can be used
    to directly load tables from the Internet.  For example, Vizier tables from the
    CDS::

      >>> table = ascii.read("ftp://cdsarc.u-strasbg.fr/pub/cats/VII/253/snrs.dat",
      ...             readme="ftp://cdsarc.u-strasbg.fr/pub/cats/VII/253/ReadMe")

    If the header (ReadMe) and data are stored in a single file and there
    is content between the header and the data (for instance Notes), then the
    parsing process may fail.  In this case you can instruct the reader to
    guess the actual start of the data by supplying ``data_start='guess'`` in the
    call to the ``ascii.read()`` function.  You should verify that the output
    data table matches expectation based on the input CDS file.

    **Using a reader object**

    When ``Cds`` reader object is created with a ``readme`` parameter
    passed to it at initialization, then when the ``read`` method is
    executed with a table filename, the header information for the
    specified table is taken from the ``readme`` file.  An
    ``InconsistentTableError`` is raised if the ``readme`` file does not
    have header information for the given table.

      >>> readme = "data/vizier/ReadMe"
      >>> r = ascii.get_reader(ascii.Cds, readme=readme)
      >>> table = r.read("data/vizier/table1.dat")
      >>> # table5.dat has the same ReadMe file
      >>> table = r.read("data/vizier/table5.dat")

    If no ``readme`` parameter is specified, then the header
    information is assumed to be at the top of the given table.

      >>> r = ascii.get_reader(ascii.Cds)
      >>> table = r.read("data/cds.dat")
      >>> #The following gives InconsistentTableError, since no
      >>> #readme file was given and table1.dat does not have a header.
      >>> table = r.read("data/vizier/table1.dat")
      Traceback (most recent call last):
        ...
      InconsistentTableError: No CDS section delimiter found

    Caveats:

    * The Units and Explanations are available in the column ``unit`` and
      ``description`` attributes, respectively.
    * The other metadata defined by this format is not available in the output table.
    r&   FzCDS format tableNc                    s   t ƒ  ¡  || j_d S )N)ÚsuperÚ__init__rS   r0   )r   r0   ©Ú	__class__r   r   rX   D  s    
zCds.__init__c                 C   s   t ‚dS )z<Not available for the CDS class (raises NotImplementedError)N)ÚNotImplementedError)r   Útabler   r   r   ÚwriteH  s    z	Cds.writec                    sº   | j jdkrªttƒ�$ tj|d kr4tj |¡| j _W 5 Q R X | j	| j _	| j | j	_ | j
 |¡}tt|ƒƒD ]<}|| j _ttƒ�" tƒ  |¡}|W  5 Q R £   S Q R X qjntƒ  |¡S d S )NÚguessr#   )r1   Ú
start_liner   Ú	TypeErrorÚosÚlinesepÚpathÚbasenamer2   rS   Zinputterr3   ÚrangeÚlenÚ	ExceptionrW   Úread)r   r\   rK   Z
data_startrY   r   r   rh   L  s    



zCds.read)N)N)rO   rP   rQ   rU   Z_format_nameZ_io_registry_format_aliasesZ_io_registry_can_writeZ_descriptionrR   Z
data_classr   Zheader_classrX   r]   rh   Ú__classcell__r   r   rY   r   rV   Ò   s   i
rV   )rU   r9   r>   ra   r   Ú
contextlibr   Zastropy.unitsr   r#   r   r   Z__doctest_skip__Z
BaseHeaderr   ZBaseDatarR   Z
BaseReaderrV   r   r   r   r   Ú<module>   s   
 "