
    HR-eA                        d Z ddlZddlmZ dddddddd	d	dd
ddddddddddddddZ ej        d          Zd Zd Z G d dej	                  Z
 G d dej                  Z G d  d!ej                  Z G d" d#ej                  Z G d$ d%ej                  Z G d& d'e          Z G d( d)e          Z G d* d+e          Z G d, d-e          ZdS ).zAn extensible ASCII table reader and writer.

latex.py:
  Classes to read and write LaTeX tables

:Copyright: Smithsonian Astrophysical Observatory (2011)
:Author: Tom Aldcroft (aldcroft@head.cfa.harvard.edu)
    N   )coretablez\hline \hlinez\hline)	tabletypeheader_start
header_enddata_endz\hline\hliner   caption
tablealign	col_alignpreambler   r   
data_startr	   	tablefootzunit of col1zunit of col2)col1col2)r   r
   r   r   r   r   r   r   r	   r   units)AAdoublelinestemplatez(?<!\\)%c                     || v rUt          | |         t                    r|                    | |                    dS |                    | |                    dS dS )z
    Add a value from a dictionary to a list.

    Parameters
    ----------
    adict : dictionary
    key : hashable
    alist : list
        List where value should be added
    N)
isinstancestrappendextend)adictkeyalists      6lib/python3.11/site-packages/astropy/io/ascii/latex.pyadd_dictval_to_listr   0   sb     e||eCj#&& 	%LLs$$$$$LLs$$$$$	 |    c                     t          j        |                    dd                    }t          |           D ]\  }}|                    |          r|c S dS )a  
    Find the first line which matches a pattern.

    Parameters
    ----------
    lines : list
        List of strings
    latex : str
        Search pattern

    Returns
    -------
    line_num : int, None
        Line number. Returns None, if no match was found

    \\\N)recompilereplace	enumeratematch)lineslatex	re_stringilines        r   find_latex_liner.   B   se    " 
5==v6677IU##  4??4   	HHH	 tr    c                       e Zd Zd ZdS )LatexInputterc                     d |D             S )Nc                 6    g | ]}|                                 S  strip).0lins     r   
<listcomp>z/LatexInputter.process_lines.<locals>.<listcomp>]   s     ---		---r    r3   selfr)   s     r   process_lineszLatexInputter.process_lines\   s    --u----r    N)__name__
__module____qualname__r;   r3   r    r   r0   r0   [   s#        . . . . .r    r0   c                   8     e Zd ZdZdZ fdZd Zd Zd Z xZ	S )LatexSplitterz1Split LaTeX table data. Default delimiter is `&`.&c                     t                               |d                   d                                         }|                    d          s|dz   |d<   t	                                          |          S )Nr   r#   )
RE_COMMENTsplitr5   endswithsuper__call__)r:   r)   	last_line	__class__s      r   rH   zLatexSplitter.__call__e   sg    $$U2Y//288::	!!%(( 	*!E)E"Iww&&&r    c                     t                               |          d         }|                                }|                    d          r|                    d          }nt          j        d          |S )zaRemove whitespace at the beginning or end of line. Also remove
        \ at end of line.
        r   r#   z(Lines in LaTeX table have to end with \\)rD   rE   r5   rF   rstripr   InconsistentTableErrorr:   r-   s     r   process_linezLatexSplitter.process_linel   so     %%a(zz||== 	;;u%%DD-;   r    c                 v    |                                 }|r"|d         dk    r|d         dk    r
|dd         }|S )z:Remove whitespace and {} at the beginning or end of value.r   {rC   }r   r4   )r:   vals     r   process_valzLatexSplitter.process_valz   sA    iikk 	CFcMMB3ad)C
r    c                 `    d| j         z   dz   }|                    d |D                       dz   S )z@Join values together and add a few extra spaces for readability. c              3   >   K   | ]}|                                 V  d S Nr4   r6   xs     r   	<genexpr>z%LatexSplitter.join.<locals>.<genexpr>   s*      66Aaggii666666r    z \\)	delimiterjoin)r:   valsr\   s      r   r]   zLatexSplitter.join   s9    $.(3.	~~6666666??r    )
r<   r=   r>   __doc__r\   rH   rO   rT   r]   __classcell__rJ   s   @r   r@   r@   `   sw        ;;I' ' ' ' '    @ @ @ @ @ @ @r    r@   c                   ,    e Zd ZdZdZeZd Zd Zd Z	dS )LatexHeaderz)Class to read the header of Latex Tables.z\begin{tabular}c                 >    t          || j                  }||dz   S d S Nr   )r.   r   )r:   r)   r-   s      r   
start_linezLatexHeader.start_line   s(    ud&788!8O4r    c                     i }d | j         D             }t          | j        |          D ]7\  }}|r0	 |                    d          ||<   "# t          $ r |||<   Y 3w xY w8|S )Nc                 &    g | ]}|j         j        S r3   )infounit)r6   cols     r   r8   z*LatexHeader._get_units.<locals>.<listcomp>   s    888sSX]888r    latex_inline)format)colszipcolnames	to_stringAttributeError)r:   r   	col_unitsnamerj   s        r   
_get_unitszLatexHeader._get_units   s    88di888	dmY77 	' 	'JD$ ''"&..."G"GE$KK% ' ' '"&E$KKK''
 s   AAAc                    d| j         vrt          | j                  dz  | j         d<   d| j         v rd| j         d         z   dz   }nd}| j         d         )|                    d| j         d         z   d	z   |z              t	          | j         d
|           d| j         v r&|                    d| j         d         z   d	z              |                    | j        dz   | j         d         z   d	z              t	          | j         d|           |                    | j                            | j                             | 	                                d| j         v r 
                    | j         d                    r>|                    | j                            fd| j        D                                  t	          | j         d|           d S )Nr   cr   [] r   \begin{rR   r   r
   z	\caption{rQ   r   r   c                 <    g | ]}                     |d           S rV   getr6   rt   r   s     r   r8   z%LatexHeader.write.<locals>.<listcomp>   s'    #S#S#STEIIdC$8$8#S#S#Sr    r   )r*   lenrn   r   r   r   splitterr]   rp   ru   update)r:   r)   alignr   s      @r   writezLatexHeader.write   s   dj((&)$)nns&:DJ{#4:%%$*\22S8EEE:k".LLdj&==DuLMMMDJ
E:::
""LL
9(==CDDDT&-
;0GG$NOOODJ>>>T]''66777!!dj  LLG,--- 	LL""#S#S#S#ST]#S#S#STT   	DJe<<<<<r    N)
r<   r=   r>   r_   r   r@   splitter_classrf   ru   r   r3   r    r   rc   rc      sQ        33%L"N  	 	 	= = = = =r    rc   c                   0    e Zd ZdZdZdZeZd Zd Z	d Z
dS )	LatexDataz'Class to read the data in LaTeX tables.Nz\end{tabular}c                     | j         rt          || j                   S | j                            |          }|t	          j        d          |dz   S )NzCould not find table startr   )r   r.   headerrf   r   rM   )r:   r)   starts      r   rf   zLatexData.start_line   sT    ? 	"5$/:::K**511E}12OPPP19r    c                 >    | j         rt          || j                   S d S rX   )r	   r.   r9   s     r   end_linezLatexData.end_line   s#    = 	"5$-8884r    c                 h   t          | j        d|           t          j                            | |           t          | j        d|           |                    | j                   t          | j        d|           | j        d         (|                    d| j        d         z   dz              d S d S )Nr   r	   r   r   \end{rR   )r   r*   r   BaseDatar   r   r	   r9   s     r   r   zLatexData.write   s    DJe<<<D%(((DJ
E:::T]###DJU;;;:k".LLDJ{$;;cABBBBB /.r    )r<   r=   r>   r_   r   r	   r@   r   rf   r   r   r3   r    r   r   r      s[        11JH"N    C C C C Cr    r   c                   Z     e Zd ZdZdZdgZdZdZeZ	e
ZeZdZg di ddf fd	Zd
d	Z xZS )Latexa  LaTeX format table.

    This class implements some LaTeX specific commands.  Its main
    purpose is to write out a table in a form that LaTeX can compile. It
    is beyond the scope of this class to implement every possible LaTeX
    command, instead the focus is to generate a syntactically valid
    LaTeX tables.

    This class can also read simple LaTeX tables (one line per table
    row, no ``\multicolumn`` or similar constructs), specifically, it
    can read the tables that it writes.

    Reading a LaTeX table, the following keywords are accepted:

    **ignore_latex_commands** :
        Lines starting with these LaTeX commands will be treated as comments (i.e. ignored).

    When writing a LaTeX table, the some keywords can customize the
    format.  Care has to be taken here, because python interprets ``\\``
    in a string as an escape character.  In order to pass this to the
    output either format your strings as raw strings with the ``r``
    specifier or use a double ``\\\\``.

    Examples::

        caption = r'My table \label{mytable}'
        caption = 'My table \\\\label{mytable}'

    **latexdict** : Dictionary of extra parameters for the LaTeX output

        * tabletype : used for first and last line of table.
            The default is ``\\begin{table}``.  The following would generate a table,
            which spans the whole page in a two-column document::

                ascii.write(data, sys.stdout, Writer = ascii.Latex,
                            latexdict = {'tabletype': 'table*'})

            If ``None``, the table environment will be dropped, keeping only
            the ``tabular`` environment.

        * tablealign : positioning of table in text.
            The default is not to specify a position preference in the text.
            If, e.g. the alignment is ``ht``, then the LaTeX will be ``\\begin{table}[ht]``.

        * col_align : Alignment of columns
            If not present all columns will be centered.

        * caption : Table caption (string or list of strings)
            This will appear above the table as it is the standard in
            many scientific publications.  If you prefer a caption below
            the table, just write the full LaTeX command as
            ``latexdict['tablefoot'] = r'\caption{My table}'``

        * preamble, header_start, header_end, data_start, data_end, tablefoot: Pure LaTeX
            Each one can be a string or a list of strings. These strings
            will be inserted into the table without any further
            processing. See the examples below.

        * units : dictionary of strings
            Keys in this dictionary should be names of columns. If
            present, a line in the LaTeX table directly below the column
            names is added, which contains the values of the
            dictionary. Example::

              from astropy.io import ascii
              data = {'name': ['bike', 'car'], 'mass': [75,1200], 'speed': [10, 130]}
              ascii.write(data, Writer=ascii.Latex,
                               latexdict = {'units': {'mass': 'kg', 'speed': 'km/h'}})

            If the column has no entry in the ``units`` dictionary, it defaults
            to the **unit** attribute of the column. If this attribute is not
            specified (i.e. it is None), the unit will be written as ``' '``.

        Run the following code to see where each element of the
        dictionary is inserted in the LaTeX table::

            from astropy.io import ascii
            data = {'cola': [1,2], 'colb': [3,4]}
            ascii.write(data, Writer=ascii.Latex, latexdict=ascii.latex.latexdicts['template'])

        Some table styles are predefined in the dictionary
        ``ascii.latex.latexdicts``. The following generates in table in
        style preferred by A&A and some other journals::

            ascii.write(data, Writer=ascii.Latex, latexdict=ascii.latex.latexdicts['AA'])

        As an example, this generates a table, which spans all columns
        and is centered on the page::

            ascii.write(data, Writer=ascii.Latex, col_align='|lr|',
                        latexdict={'preamble': r'\begin{center}',
                                   'tablefoot': r'\end{center}',
                                   'tabletype': 'table*'})

    **caption** : Set table caption
        Shorthand for::

            latexdict['caption'] = caption

    **col_align** : Set the column alignment.
        If not present this will be auto-generated for centered
        columns. Shorthand for::

            latexdict['col_align'] = col_align

    r*   z.texzLaTeX tableN)hlinevspace	tablelinetoprulemidrule
bottomrulerz   c                    t                                                       i | _        | j        | j        _        | j        | j        _        d| j        d<   | j                            |           |r
|| j        d<   |r
|| j        d<   || _        dd                    d | j        D                       z   | j        _        | j        j        | j        _        d S )Nr   r   r
   r   z%||c                     g | ]}d |z   S )r#   r3   )r6   commands     r   r8   z"Latex.__init__.<locals>.<listcomp>s  s    GGGUW_GGGr    )	rG   __init__r*   r   datar   ignore_latex_commandsr]   comment)r:   r   	latexdictr
   r   rJ   s        r   r   zLatex.__init__U  s     	
 !J*	")
;
)$$$ 	,$+DJy! 	0&/DJ{#%:""SXXGGD,FGGG&
 &
 
 !K/	r    c                 t    d | j         _        d | j        _        t          j                            | |          S )N)r   )r   rf   r   r   
BaseReaderr   )r:   r   s     r   r   zLatex.writew  s1    !%#	$$T$777r    rX   )r<   r=   r>   r_   _format_name_io_registry_format_aliases_io_registry_suffix_descriptionrc   header_classr   
data_classr0   inputter_classmax_ndimr   r   r`   ra   s   @r   r   r      s        i iV L#*)  LLJ"N H
 
 
  0  0  0  0  0  0D8 8 8 8 8 8 8 8r    r   c                   .     e Zd ZdZ fdZd Zd Z xZS )AASTexHeaderSplitterzExtract column names from a `deluxetable`_.

    This splitter expects the following LaTeX code **in a single line**:

        \tablehead{\colhead{col1} & ... & \colhead{coln}}
    c                 T    t          t          |                               |          S rX   )rG   r@   rH   )r:   r)   rJ   s     r   rH   zAASTexHeaderSplitter.__call__  s!    ]D))225999r    c                 &   |                     d          d         }|                    dd          }|                                }|d         dk    r|d         dk    r|dd         }nt          j        d	          |                    d
d          S )z$extract column names from tablehead.%r   
\tableheadrz   rQ   rC   rR   r   z\tablehead is missing {}z\colhead)rE   r&   r5   r   rM   rN   s     r   rO   z!AASTexHeaderSplitter.process_line  s    zz#q!||M2..zz||GsNNbS":DD-.IJJJ||K,,,r    c                 @    d                     d |D                       S )N & c                 8    g | ]}d t          |          z   dz   S z	\colhead{rR   )r   rY   s     r   r8   z-AASTexHeaderSplitter.join.<locals>.<listcomp>  s(    EEE1<#a&&036EEEr    )r]   )r:   r^   s     r   r]   zAASTexHeaderSplitter.join  s#    zzEEEEEFFFr    )r<   r=   r>   r_   rH   rO   r]   r`   ra   s   @r   r   r   }  sg         : : : : :	- 	- 	-G G G G G G Gr    r   c                   &    e Zd ZdZdZeZd Zd ZdS )AASTexHeaderzIn a `deluxetable
    <http://fits.gsfc.nasa.gov/standard30/deluxetable.sty>`_ some header
    keywords differ from standard LaTeX.

    This header is modified to take that into account.
    r   c                 "    t          |d          S )Nr   )r.   r9   s     r   rf   zAASTexHeader.start_line  s    um444r    c                    d| j         vrt          | j                  dz  | j         d<   d| j         v rd| j         d         z   dz   }nd}|                    d| j         d         z   d	z   | j         d         z   d
z   |z              t	          | j         d|           d| j         v r&|                    d| j         d         z   d
z              d                    d | j        D                       }|                                 d| j         v r                     | j         d                    r1|d| j	                            fd| j        D                       z   z  }|                    d|z   d
z              d S )Nr   rw   r   rx   ry   rz   r{   r   z}{rR   r   r
   z\tablecaption{r   c                     g | ]
}d |z   dz   S r   r3   )r6   rt   s     r   r8   z&AASTexHeader.write.<locals>.<listcomp>  s"    TTTdt 3c 9TTTr    r   z\\ c                 <    g | ]}                     |d           S r}   r~   r   s     r   r8   z&AASTexHeader.write.<locals>.<listcomp>  s'    @@@$4%%@@@r    z\tablehead{)
r*   r   rn   r   r   r]   rp   ru   r   r   )r:   r)   r   	tableheadr   s       @r   r   zAASTexHeader.write  s   dj((&)$)nns&:DJ{#4:%%$*\22S8EEEj%& j%& 	
 	
 	
 	
 	DJ
E:::
""LL*TZ	-BBSHIIIJJTTdmTTTUU	!!dj  LLG,--- 	$-"4"4@@@@$-@@@# #  I 	^i/#566666r    N)	r<   r=   r>   r_   r   r   r   rf   r   r3   r    r   r   r     sH          !L)N5 5 57 7 7 7 7r    r   c                   &    e Zd ZdZdZdZd Zd ZdS )
AASTexDatazHIn a `deluxetable`_ the data is enclosed in `\startdata` and `\enddata`.z
\startdataz\enddatac                 2    t          || j                  dz   S re   )r.   r   r9   s     r   rf   zAASTexData.start_line  s    udo66::r    c                    |                     | j                   t          |          }t          j                            | |           t          |          |k    r+t          j        dd|d         t          j                  |d<   |                     | j	                   t          | j        d|           |                     d| j        d         z   dz              d S )	Nz\s* \\ \\ \s* $rz   rC   )flagsr   r   r   rR   )r   r   r   r   r   r   r$   subVERBOSEr	   r   r*   )r:   r)   lines_length_initials      r   r   zAASTexData.write  s    T_%%%"5zzD%((( u::,,,12uRy
SSSE"IT]###DJU;;;X
; 77$>?????r    N)r<   r=   r>   r_   r   r	   rf   r   r3   r    r   r   r     sG        SSJH; ; ;
@ 
@ 
@ 
@ 
@r    r   c                   <     e Zd ZdZdZdgZdZdZeZ	e
Z fdZ xZS )AASTexa  AASTeX format table.

    This class implements some AASTeX specific commands.
    AASTeX is used for the AAS (American Astronomical Society)
    publications like ApJ, ApJL and AJ.

    It derives from the ``Latex`` reader and accepts the same
    keywords.  However, the keywords ``header_start``, ``header_end``,
    ``data_start`` and ``data_end`` in ``latexdict`` have no effect.
    aastexrz   z(AASTeX deluxetable used for AAS journalsc                 n     t                      j        di | d|v r
d|d         v sd| j        d<   d S d S )Nr   r   deluxetabler3   )rG   r   r*   )r:   kwargsrJ   s     r   r   zAASTex.__init__  sU    ""6"""&&[F;<O-O-O&3DJ{### .P-Or    )r<   r=   r>   r_   r   r   r   r   r   r   r   r   r   r`   ra   s   @r   r   r     sf        	 	 L#+*=LLJ4 4 4 4 4 4 4 4 4r    r   )r_   r$   rz   r   
latexdictsr%   rD   r   r.   BaseInputterr0   BaseSplitterr@   
BaseHeaderrc   r   r   r   r   r   r   r   r   r3   r    r   <module>r      s    
			       (	  (%#	  !" &"" (.AA  
: RZ$$
% % %$  2. . . . .D% . . .
$@ $@ $@ $@ $@D% $@ $@ $@N.= .= .= .= .=$/ .= .= .=bC C C C C C C C@b8 b8 b8 b8 b8DO b8 b8 b8JG G G G G= G G G4(7 (7 (7 (7 (7; (7 (7 (7V@ @ @ @ @ @ @ @,4 4 4 4 4U 4 4 4 4 4r    