ó
šßÈ[c           @` sí  d  Z  d d l m Z m Z m Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z i i d	 d
 6d d 6d d 6d d 6d 6i d	 d
 6d d 6d d 6d d 6d 6i d
 d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6i d d 6d d 6d 6d 6Z e j d ƒ Z d  „  Z d! „  Z d" e j f d# „  ƒ  YZ d$ e j f d% „  ƒ  YZ d& e j f d' „  ƒ  YZ d( e j f d) „  ƒ  YZ d* e f d+ „  ƒ  YZ d, e f d- „  ƒ  YZ d. e f d/ „  ƒ  YZ d0 e f d1 „  ƒ  YZ d S(2   sÑ   An 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)
i    (   t   absolute_importt   divisiont   print_functionNi   (   t   six(   t   zipi   (   t   coret   tablet	   tabletypes   \hline \hlinet   header_starts   \hlinet
   header_endt   data_endt   AAs   \hline\hlinet   doublelinest   captiont
   tablealignt	   col_alignt   preamblet
   data_startt	   tablefoots   unit of col1t   col1s   unit of col2t   col2t   unitst   templates   (?<!\\)%c         C` sN   | |  k rJ t  |  | t j ƒ r6 | j |  | ƒ qJ | j |  | ƒ n  d S(   s´   
    Add a value from a dictionary to a list

    Parameters
    ----------
    adict : dictionary
    key : hashable
    alist : list
        List where value should be added
    N(   t
   isinstanceR   t   string_typest   appendt   extend(   t   adictt   keyt   alist(    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyt   add_dictval_to_list&   s    c         C` sS   t  j | j d d ƒ ƒ } x1 t |  ƒ D] \ } } | j | ƒ r( | Sq( Wd Sd S(   s  
    Find the first line which matches a patters

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

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

    s   \s   \\N(   t   ret   compilet   replacet	   enumeratet   matcht   None(   t   linest   latext	   re_stringt   it   line(    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyt   find_latex_line8   s
    t   LatexSplitterc           B` s8   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z RS(   s6   Split LaTeX table date. Default delimiter is `&`.
    t   &c         C` sS   t  j | d ƒ d j ƒ  } | j d ƒ s= | d | d <n  t t |  ƒ j | ƒ S(   Niÿÿÿÿi    s   \\(   t
   RE_COMMENTt   splitt   stript   endswitht   superR+   t   __call__(   t   selfR%   t	   last_line(    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR2   V   s    c         C` sS   t  j | ƒ d } | j ƒ  } | j d ƒ r@ | j d ƒ } n t j d ƒ ‚ | S(   sW   Remove whitespace at the beginning or end of line. Also remove
        \ at end of linei    s   \\s(   Lines in LaTeX table have to end with \\(   R-   R.   R/   R0   t   rstripR   t   InconsistentTableError(   R3   R)   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyt   process_line]   s    c         C` sF   | j  ƒ  } | rB | d d k rB | d d k rB | d d !} n  | S(   s:   Remove whitespace and {} at the beginning or end of value.i    t   {iÿÿÿÿt   }i   (   R/   (   R3   t   val(    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyt   process_valh   s    &c         C` s,   d |  j  d } | j d „  | Dƒ ƒ d S(   s?   Join values together and add a few extra spaces for readabilityt    c         s` s   |  ] } | j  ƒ  Vq d  S(   N(   R/   (   t   .0t   x(    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pys	   <genexpr>r   s    s    \\(   t	   delimitert   join(   R3   t   valsR?   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR@   o   s    (   t   __name__t
   __module__t   __doc__R?   R2   R7   R;   R@   (    (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR+   Q   s   			t   LatexHeaderc           B` s5   e  Z d  Z d Z e Z d „  Z d „  Z d „  Z RS(   s(   Class to read the header of Latex Tabless   \begin{tabular}c         C` s.   t  | |  j ƒ } | d  k	 r& | d Sd  Sd  S(   Ni   (   R*   R   R$   (   R3   R%   R)   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyt
   start_linez   s    c         C` s   i  } g  |  j  D] } | j j ^ q } xa t |  j | ƒ D]M \ } } | r; y | j d d ƒ | | <Wqˆ t k
 r„ | | | <qˆ Xq; q; W| S(   Nt   formatt   latex_inline(   t   colst   infot   unitR   t   colnamest	   to_stringt   AttributeError(   R3   R   t   colt	   col_unitst   nameRK   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyt
   _get_units   s    "c         C` s¯  d |  j  k r, t |  j ƒ d |  j  d <n  d |  j  k rS d |  j  d d } n d } |  j  d d  k	 r | j d |  j  d d	 | ƒ n  t |  j  d
 | ƒ d |  j  k rÐ | j d |  j  d d	 ƒ n  | j |  j d |  j  d d	 ƒ t |  j  d | ƒ | j |  j j |  j	 ƒ ƒ |  j
 ƒ  } d |  j  k rT| j |  j  d ƒ n  | r˜| j |  j j g  |  j	 D] } | j | d ƒ ^ qsƒ ƒ n  t |  j  d | ƒ d  S(   NR   t   cR   t   [t   ]t    R   s   \begin{R9   R   R   s	   \caption{R8   R   R   R<   R	   (   R&   t   lenRI   R$   R   R   R   t   splitterR@   RL   RR   t   updatet   get(   R3   R%   t   alignR   RQ   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyt   writeŒ   s&    ##>(	   RB   RC   RD   R   R+   t   splitter_classRF   RR   R\   (    (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyRE   u   s   		t	   LatexDatac           B` s;   e  Z d  Z d Z d Z e Z d „  Z d „  Z	 d „  Z
 RS(   s&   Class to read the data in LaTeX tabless   \end{tabular}c         C` s1   |  j  r t | |  j  ƒ S|  j j | ƒ d Sd  S(   Ni   (   R   R*   t   headerRF   (   R3   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyRF   ©   s    	c         C` s!   |  j  r t | |  j  ƒ Sd  Sd  S(   N(   R
   R*   R$   (   R3   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyt   end_line¯   s    	c         C` s’   t  |  j d | ƒ t j j |  | ƒ t  |  j d | ƒ | j |  j ƒ t  |  j d | ƒ |  j d d  k	 rŽ | j d |  j d d ƒ n  d  S(   NR   R
   R   R   s   \end{R9   (   R   R&   R   t   BaseDataR\   R   R
   R$   (   R3   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR\   µ   s    N(   RB   RC   RD   R$   R   R
   R+   R]   RF   R`   R\   (    (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR^   £   s   		t   Latexc           B` s_   e  Z d  Z d Z d g Z d Z d Z e Z e	 Z
 d d d g i  d d
 d „ Z d
 d	 „ Z RS(   s  Write and read LaTeX tables.

    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&   s   .texs   LaTeX tablet   hlinet   vspacet	   tablelineRV   c         C` sÕ   t  t |  ƒ j ƒ  i  |  _ |  j |  j _ |  j |  j _ d |  j d <|  j j | ƒ | rm | |  j d <n  | rƒ | |  j d <n  | |  _ d d j g  |  j D] } d | ^ qŸ ƒ |  j _	 |  j j	 |  j _	 d  S(   NR   R   R   R   s   %|t   |s   \\(
   R1   Rb   t   __init__R&   R_   t   dataRY   t   ignore_latex_commandsR@   t   comment(   R3   Ri   t	   latexdictR   R   t   command(    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyRg   2  s    			*c         C` s.   d  |  j _ d  |  j _ t j j |  d | ƒS(   NR   (   R$   R_   RF   Rh   R   t
   BaseReaderR\   (   R3   R   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR\   H  s    N(   RB   RC   RD   t   _format_namet   _io_registry_format_aliasest   _io_registry_suffixt   _descriptionRE   t   header_classR^   t
   data_classR$   Rg   R\   (    (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyRb   ¿   s   j	t   AASTexHeaderSplitterc           B` s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sµ   Extract column names from a `deluxetable`_.

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

        \tablehead{\colhead{col1} & ... & \colhead{coln}}
    c         C` s   t  t |  ƒ j | ƒ S(   N(   R1   R+   R2   (   R3   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR2   V  s    c         C` s€   | j  d ƒ d } | j d d ƒ } | j ƒ  } | d d k ra | d d k ra | d d !} n t j d	 ƒ ‚ | j d
 d ƒ S(   s,   extract column names from tablehead
        t   %i    s
   \tableheadRV   R8   iÿÿÿÿR9   i   s   \tablehead is missing {}s   \colhead(   R.   R!   R/   R   R6   (   R3   R)   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR7   Y  s     c         C` s.   d j  g  | D] } d t | ƒ d ^ q ƒ S(   Ns    & s	   \colhead{R9   (   R@   t   str(   R3   RA   R>   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR@   e  s    (   RB   RC   RD   R2   R7   R@   (    (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyRt   N  s   		t   AASTexHeaderc           B` s,   e  Z d  Z d Z e Z d „  Z d „  Z RS(   sÀ   In 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.
    s
   \tableheadc         C` s   t  | d ƒ S(   Ns
   \tablehead(   R*   (   R3   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyRF   s  s    c         C` s†  d |  j  k r, t |  j ƒ d |  j  d <n  d |  j  k rS d |  j  d d } n d } | j d |  j  d d	 |  j  d d
 | ƒ t |  j  d | ƒ d |  j  k rÉ | j d |  j  d d
 ƒ n  d j g  |  j D] } d | d
 ^ qÙ ƒ } |  j ƒ  } d |  j  k r(| j |  j  d ƒ n  | rm| d |  j	 j g  |  j D] } | j
 | d ƒ ^ qGƒ 7} n  | j d | d
 ƒ d  S(   NR   RS   R   RT   RU   RV   s   \begin{R   s   }{R9   R   R   s   \tablecaption{s    & s	   \colhead{R   s   \\ R<   s   \tablehead{(   R&   RW   RI   R   R   R@   RL   RR   RY   RX   RZ   (   R3   R%   R[   RQ   t	   tableheadR   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR\   v  s$    )--(   RB   RC   RD   R   Rt   R]   RF   R\   (    (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyRw   i  s
   	t
   AASTexDatac           B` s,   e  Z d  Z d Z d Z d „  Z d „  Z RS(   sL   In a `deluxetable`_ the data is enclosed in `\startdata` and `\enddata`
    s
   \startdatas   \enddatac         C` s   t  | |  j ƒ d S(   Ni   (   R*   R   (   R3   R%   (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyRF   ’  s    c         C` s¼   | j  |  j ƒ t | ƒ } t j j |  | ƒ t | ƒ | k ry t j d d t j ƒ} t j	 | d | d ƒ | d <n  | j  |  j
 ƒ t |  j d | ƒ | j  d |  j d d ƒ d  S(	   Ns   \s* \\ \\ \s* $t   flagsRV   iÿÿÿÿR   s   \end{R   R9   (   R   R   RW   R   Ra   R\   R   R    t   VERBOSEt   subR
   R   R&   (   R3   R%   t   lines_length_initialt   re_final_line(    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR\   •  s     (   RB   RC   RD   R   R
   RF   R\   (    (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyRy   Œ  s
   	t   AASTexc           B` s>   e  Z d  Z d Z d g Z d Z d Z e Z e	 Z
 d „  Z RS(   sŽ  Write and read AASTeX tables.

    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.
    t   aastexRV   s(   AASTeX deluxetable used for AAS journalsc         K` sF   t  t |  ƒ j |   d | k o/ d | d k sB d |  j d <n  d  S(   NRk   R   t   deluxetable(   R1   R   Rg   R&   (   R3   t   kwargs(    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyRg   ¸  s    (   RB   RC   RD   Rn   Ro   Rp   Rq   Rw   Rr   Ry   Rs   Rg   (    (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyR   ¤  s   
	(   RD   t
   __future__R    R   R   R   t   externR   t   extern.six.movesR   RV   R   t
   latexdictsR    R-   R   R*   t   BaseSplitterR+   t
   BaseHeaderRE   Ra   R^   Rm   Rb   Rt   Rw   Ry   R   (    (    (    s5   lib/python2.7/site-packages/astropy/io/ascii/latex.pyt   <module>	   s:   
		$.#