
    HR-e7                         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	e
j                  Z G d
 de
j                  Z G d de
j                  ZdS )zAn 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                   Z    e Zd ZdZej        ej        ej        ej        dZ	 dZ	d Z
d ZdS )	CdsHeaderCDS)efiaNc           	          t          j        d|j                                                  }|s(t	          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   s      4lib/python3.11/site-packages/astropy/io/ascii/cds.pyget_type_map_keyzCdsHeader.get_type_map_key&   s    S\%7%7%9%9:: 	     s|    H        {{1~~    c           
      	   | j         rM| j        j        r@d}t          j                    }|                    | j                   }g }d}|D ]}|                                }|r8|                    |           |                    d          r|dz  }|dk    r nPt          j
        d|t          j                  }|rod t          j        d|                    d	                    D             }|D ]:}	t          j        | j        j        |	          rd
}|                    |            n;t          j        d| j        j         d| j                    d}
t!          |          D ]1\  }}t          j
        d|t          j                  rd
}
(|
r|dz  } n2t#          d          t          j        dt          j                  }g }t)          j        ||dz   d          D ]}|                    d          r n|
                    |          }|rrt          j        |                    d	                    }t/          t          j        dd|                    d          p|                    d                              dz
  |_        t/          |                    d                    |_        |                    d          }|dk    rd|_        nt9          |dd          |_        |                    d          pd                                |_        |                    d          |_        |                     |          |_         t          j
        d|j        t          j                  }|r|                    d          pd                                |_        tC          |j         t          j"                  rd }nd!}|                    d"          d#k    r7d|_#        d$D ],}| j        j$                            d#|z  ||j%        f           -nT|                    d"          |_#        |j#        d|_#        | j        j$                            |j#        ||j%        f           |                    |           |r*|d%         xj        |                                z  c_        t#          d&| d'          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                     g | ]}||S  r#   ).0ss     r   
<listcomp>z&CdsHeader.get_cols.<locals>.<listcomp>S   s     X X XqVW X X X Xr   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endunitsz---cdswarn)formatparse_strictdescrr/   zu(?P<limits>[\[\]] \S* [\[\]])?\?((?P<equal>=)(?P<nullval> \S*))?(?P<order>[-+]?[=]?)(\s* (?P<descriptiontext> \S.*))?descriptiontextnan0nullval-)r      r!   r'   zLine "z" not parsable as CDS headerc                     g | ]	}|j         
S r#   r(   )r$   xs     r   r&   z&CdsHeader.get_cols.<locals>.<listcomp>   s    +++af+++r   )(readmedata
table_namer   BaseInputter	get_linesstripappend
startswithr   r   
IGNORECASEsplitr   fnmatchInconsistentTableError	enumerater   compileVERBOSE	itertoolsisliceColumnintsubr*   r+   unitr   descriptionr   get_col_typetype
issubclass	FloatTypenullfill_valuesr   namescols)r   lines	in_headerreadme_inputterr   comment_linesliner   rW   pattern
found_line	i_col_def
re_col_defrX   r   rO   fillvalr   s                     r   get_colszCdsHeader.get_cols/   s    ; #	49/ #	I"/11O))$+66AEM  zz|| &LL&&&'<== "%*(A--!EHJ E
  	& X XBHWekk&>Q>Q,R,R X X X (- & &G&ty/CWMM &,0	 %T 2 2 2 %& 1O	(<OO$+OO   
(// 	N 	NOItx3T2=II !

 Q	 LMMMZ+ J	
 	

 $UIM4@@ :	R :	RD455 $$T**E 6Rku{{6':':;;;F8RW)=)=)SUASASTTV VXYZ	ekk%0011{{7++5==#CHH#DVLLLCH#(;;w#7#7#=2"D"D"F"F${{844,,S11	9 OJ    T',{{3D'E'E'K&R&R&T&TCO!#(DN;; &"'"%{{9--44#( ". W WA I188#'7CH9UVVVVW $);;y#9#98+')CH	-44ch5RSSSC     RH((DJJLL8((($%Pd%P%P%PQQQ++d+++
			r   )__name__
__module____qualname__r   r   rT   IntTypeStrTypecol_type_mapr;   r   rc   r#   r   r   r
   r
      sh        G ^^\\	 L 0F  G G G G Gr   r
   c                   *    e Zd ZdZdZej        Zd ZdS )CdsDatazCDS table data reader.r   c                     | j         j        r	| j        r|S d t          |          D             }|st	          j        d| j         d          ||d         dz   d         S )z?Skip over CDS/MRT header by finding the last section delimiter.c                 B    g | ]\  }}|                     d           |S )r    )rB   )r$   r   r:   s      r   r&   z)CdsData.process_lines.<locals>.<listcomp>   s>     
 
 
!Qall;P.Q.Q

 
 
r   zNo z section delimiter foundr8   r   N)headerr;   r=   rG   r   rF   r   )r   rY   
i_sectionss      r   process_lineszCdsData.process_lines   s     ; 	$/ 	L
 
#E**
 
 

  	-<dl<<<   Z^a'))**r   N)	rd   re   rf   __doc__r   r   FixedWidthSplittersplitter_classrp   r#   r   r   rk   rk      s6          G2N+ + + + +r   rk   c                   P     e Zd ZdZdZdgZdZdZeZ	e
Zd	 fd	Zd	dZ fdZ xZS )
CdsaU  CDS format table.

    See: https://vizier.unistra.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                 `    t                                                       || j        _        d S N)super__init__rn   r;   )r   r;   	__class__s     r   ry   zCds.__init__C  s)    #r   c                     t           )z=Not available for the CDS class (raises NotImplementedError).)NotImplementedError)r   tables     r   writez	Cds.writeG  s    !!r   c                    | j         j        dk    r t          t                    5  t          j        |dz   vr)t          j                            |          | j         _        d d d            n# 1 swxY w Y   | j	        | j         _	        | j         | j	        _         | j
                            |          }t          t          |                    D ]_}|| j         _        t          t                    5  t                                          |          }|cd d d            c S # 1 swxY w Y   `d S t                                          |          S )Nguessr)   )r<   
start_liner   	TypeErroroslineseppathbasenamer=   rn   inputterr?   rangelen	Exceptionrx   read)r   r}   rY   
data_startrz   s       r   r   zCds.readK  s    97** )$$ C C:URZ//+-7+;+;E+B+BDI(C C C C C C C C C C C C C C C
  ${DI#yDK M++E22E
 $CJJ// ! !
'1	$i(( ! !!GGLL//E ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !! ! 77<<&&&s#   ;A..A25A23#D%%D)	,D)	rw   )rd   re   rf   rq   _format_name_io_registry_format_aliases_io_registry_can_write_descriptionrk   
data_classr
   header_classry   r~   r   __classcell__)rz   s   @r   ru   ru      s        g gR L#('"%LJL$ $ $ $ $ $" " " "' ' ' ' ' ' ' ' 'r   ru   )rq   rE   rJ   r   r   
contextlibr   astropy.unitsr   r)   r   r   __doctest_skip__
BaseHeaderr
   BaseDatark   
BaseReaderru   r#   r   r   <module>r      s         				 				                    5 ] ] ] ] ] ] ] ]@+ + + + +dm + + +0T' T' T' T' T'$/ T' T' T' T' T'r   