ó
ßČ[c           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l m Z m Z d d l	 m
 Z
 d d l m Z d d l m Z d d	 l m Z d
 d l m Z d
 d l m Z d
 d l m Z d d l m Z d d l m Z d e f d     YZ d e f d     YZ 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 j$ f d     YZ% d S(   sĖ   An extensible ASCII table reader and writer.

ipac.py:
  Classes to read IPAC table format

:Copyright: Smithsonian Astrophysical Observatory (2011)
:Author: Tom Aldcroft (aldcroft@head.cfa.harvard.edu)
i    (   t   absolute_importt   divisiont   print_functionN(   t   defaultdictt   OrderedDict(   t   wrap(   t   warni   (   t   six(   t   zipi   (   t   core(   t
   fixedwidth(   t   basic(   t   AstropyUserWarning(   t   get_auto_format_funct   IpacFormatErrorDBMSc           B` s   e  Z d    Z RS(   c         C` s   d j  t t |   j   d  S(   Ns   {0}
See {1}sH   http://irsa.ipac.caltech.edu/applications/DDGEN/Doc/DBMSrestriction.html(   t   formatt   supert	   Exceptiont   __str__(   t   self(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR      s    (   t   __name__t
   __module__R   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR      s   t   IpacFormatErrorc           B` s   e  Z d    Z RS(   c         C` s   d j  t t |   j   d  S(   Ns   {0}
See {1}sA   http://irsa.ipac.caltech.edu/applications/DDGEN/Doc/ipac_tbl.html(   R   R   R   R   (   R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR   $   s    (   R   R   R   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR   #   s   t   IpacHeaderSplitterc           B` sM   e  Z d  Z d Z d Z d Z d Z e Z	 d Z
 d Z d Z d Z d   Z RS(   s®   Splitter for Ipac Headers.

    This splitter is similar its parent when reading, but supports a
    fixed width format (as required for Ipac table headers) for writing.
    t   |t    s   \s*\\s   \\c   
      C` s   |  j  p d } |  j p d } | | | } | | } | | } g  t | |  D]$ \ } }	 d |	 t |  | ^ qP } | | j |  | S(   NR   t    (   t   delimiter_padt	   delimiterR   t   lent   join(
   R   t   valst   widthst   padR   t   padded_delimt   bookend_leftt   bookend_rightt   valt   width(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR   :   s    

:N(   R   R   t   __doc__t   Nonet   process_linet   process_valR   R   t   Falset   skipinitialspacet   commentt   write_commentt
   col_startst   col_endsR   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR   *   s   t
   IpacHeaderc           B` s°   e  Z d  Z e Z d e j f d e j f d e j f d e j f d e j f d e j f d e j f f Z	 d Z
 d Z d	   Z d
   Z d   Z d   Z d   Z d   Z RS(   s   IPAC table headert   integert   longt   doublet   floatt   realt   chart   datet   ignorec         c` s\   |  j  j } xI | D]A } | j   } | j |  r | j |  r | j |  Vq q Wd S(   s   Generator to yield IPAC header lines, i.e. those starting and ending with
        delimiter character (with trailing whitespace stripped)N(   t   splitterR   t   rstript
   startswitht   endswitht   strip(   R   t   linest   delimt   line(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyt   process_linesV   s
    c         C` sH  d   } | d } g  | d <t    | d <| d } t j d t j  } xų | D]š } | j d  si Pn  | j |  } | r| j d  }	 | | j d   }
 |	 | k rš t |
 t j	  rš | |	 d } t | t j	  rš | |
 }
 qš n  i |
 d 6| |	 <qP | j d	  rP | d
 j
   }
 |
 r@| d j |
  q@qP qP Wd S(   s   
        Extract table-level comments and keywords for IPAC table.  See:
        http://irsa.ipac.caltech.edu/applications/DDGEN/Doc/ipac_tbl.html#kw
        c         S` s   |  j    }  y t |   }  Wnu t k
 r y t |   }  Wq t k
 r xA d D]5 } |  j |  rS |  j |  rS |  d d !}  PqS qS Wq Xn X|  S(   sw   
            Take a string value and convert to float, int or str, and strip quotes
            as needed.
            t   "t   'i   i’’’’(   RC   RD   (   R>   t   intR   R5   R<   R=   (   R%   t   quote(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyt   process_keyword_valued   s    t   tablet   commentst   keywordss&   \\(?P<name> \w+)\s* = (?P<value> .+) $s   \t   namet   values   \ i   N(   R   t   ret   compilet   VERBOSER<   t   matcht   groupt
   isinstanceR   t   string_typesR>   t   append(   R   R?   t   metaRG   t
   table_metaRJ   t
   re_keywordRA   t   mRK   R%   t   prev_val(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyt   update_meta_   s.    	


	c         C` sX   xQ |  j  D]( \ } } | j | j j    r
 | Sq
 Wt d j | j | j    d  S(   Ns(   Unknown data type ""{}"" for column "{}"(   t   col_type_listR<   t   raw_typet   lowert
   ValueErrorR   RK   (   R   t   colt   col_type_keyt   col_type(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyt   get_col_type   s
    	c         C` s;  |  j  |  } g  |  j |  D] } | ^ q } t |  d k rR t d   n! t |  d k rs t d   n  g  } d } xt | d  D]{\ } } t j d | j d   }	 | |	 _ | t |  |	 _	 t |  d k r| d | j d  |	 _
 |  j |	  |	 _ n  t |  d k rF| d | j   p=d |	 _ n  t |  d	 k rÆ| d	 | j   }
 t |	 j t j  rd
 n d } |  j j j |
 | |	 j f  n  |	 j	 d } | j |	  |  j d k rź|	 j d 8_ q |  j d k r |	 j	 d 7_	 q q Wg  | D] } | j ^ q|  _ | |  _ d S(   s:  
        Initialize the header Column objects from the table ``lines``.

        Based on the previously set Header attributes find or create the column names.
        Sets ``self.cols`` with the list of Columns.

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

        i    sE   At least one header line beginning and ending with delimiter requiredi   s&   More than four header lines were foundi   RK   s    -i   i   R   t   0t   rightt   leftN(   RB   R:   R   R^   t	   enumerateR	   t   ColumnR>   t   startt   endR\   Rb   t   typeR(   t   unitt
   issubclasst   StrTypet   datat   fill_valuesRT   RK   t   ipac_definitiont   namest   cols(   R   R?   t   header_linesR   t   header_valsRr   Rh   t   iRK   R_   t   nullt   fillvalt   x(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyt   get_cols¤   s:    "	 !"c         C` s$  |  j  r t } n t } |  j } |  j  r° t t  } x' |  j D] } | | j   c d 7<q@ Wg  | D] } | | d k rg | ^ qg } | g  k r° | d j |    q° n  x| D]} t j	 d |  } | j
   t |  k r’ | d j |    n  |  j  rA| d j   p%| d d k rA| d j |    n  |  j  r| d k rn| d j |    n  t |  d k rÅ| d j |    qÅq· t |  d k r· | d j |    q· q· Wg  } g  }	 g  }
 x6|  j D]+} | j j } | j j } | j j } | j d k r.| j d  n, | j d k rM| j d  n | j d  | d  k rv|	 j d  n |	 j t | j j   | j t j } yD t |  } | j j j | |  } |
 j | | |  j    Wqåt k
 r|
 j t |  j    qåXqåW| | |	 |
 g S(   Ni   sP   IPAC DBMS tables are not case sensitive. This causes duplicate column names: {0}s   \w+sE   {0} - Only alphanumeric characters and _ are allowed in column names.i    t   _s)   Column name cannot start with numbers: {}Rx   t   yt   zt   Xt   Yt   ZsM   {0} - x, y, z, X, Y, Z are reserved names and cannot be used as column names.i   s5   {0} - Maximum length for column name is 16 charactersi(   s6   {0} - Maximum length for column name is 40 characters.Ru   t   uR3   t   fR4   R7   R   (   Rx   R{   R|   R}   R~   R   (   Ru   R   (   t   DBMSR   R   t   colnamesR   RE   R]   R   RM   RP   Ri   R   t   isalphaRr   t   infot   dtypeRk   t   kindRT   R(   t   strRo   R	   t   maskedR   t   _format_funcst   getR>   R   (   R   t   IpacFormatEt   namelistt   countnamelistRK   Rx   t   doublenamesRX   t	   dtypelistt   unitlistt   nullistR_   t	   col_dtypet   col_unitt
   col_formatRv   t   auto_format_funct   format_func(    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyt   str_valsÜ   sh    				)		*		 !c         C` s7   x0 |  j    D]" } | j |  j j | |   q W| S(   så   Write header.

        The width of each column is determined in Ipac.write. Writing the header
        must be delayed until that time.
        This function is called from there, once the width information is
        available.(   R   RT   R:   R   (   R   R?   R    R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyt   write"  s     N(   R   R   R'   R   t   splitter_classR	   t   IntTypet	   FloatTypeRm   R[   t
   definitionR(   t
   start_lineRB   RZ   Rb   Ry   R   R   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR1   E   s"   			=		8	Ft   IpacDataSplitterc           B` s   e  Z d  Z d Z e Z RS(   R   R   (   R   R   R   R   t   Truet   bookend(    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR   /  s   t   IpacDatac           B` s;   e  Z d  Z d Z d Z e Z e j d f g Z	 d   Z
 RS(   s   IPAC table data readers   [|\\]i    Rv   c         C` s1   x* | D]" } | j  |  j j | |   q W| S(   s.    IPAC writer, modified from FixedWidth writer (   RT   R:   R   (   R   R?   R    t	   vals_listR   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR   <  s     (   R   R   R'   R-   R   R   R   R	   R   Ro   R   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR¢   5  s   t   Ipacc           B` sM   e  Z d  Z d Z d g Z e Z d Z e Z	 e
 Z d e d  Z d   Z RS(   sÉ  Read or write an IPAC format table.  See
    http://irsa.ipac.caltech.edu/applications/DDGEN/Doc/ipac_tbl.html::

      \\name=value
      \\ Comment
      |  column1 |   column2 | column3 | column4  |    column5    |
      |  double  |   double  |   int   |   double |     char      |
      |  unit    |   unit    |   unit  |    unit  |     unit      |
      |  null    |   null    |   null  |    null  |     null      |
       2.0978     29.09056    73765     2.06000    B8IVpMnHg

    Or::

      |-----ra---|----dec---|---sao---|------v---|----sptype--------|
        2.09708   29.09056     73765   2.06000    B8IVpMnHg

    The comments and keywords defined in the header are available via the output
    table ``meta`` attribute::

      >>> import os
      >>> from astropy.io import ascii
      >>> filename = os.path.join(ascii.__path__[0], 'tests/t/ipac.dat')
      >>> data = ascii.read(filename)
      >>> print(data.meta['comments'])
      ['This is an example of a valid comment']
      >>> for name, keyword in data.meta['keywords'].items():
      ...     print(name, keyword['value'])
      ...
      intval 1
      floatval 2300.0
      date Wed Sp 20 09:48:36 1995
      key_continue IPAC keywords can continue across lines

    Note that there are different conventions for characters occuring below the
    position of the ``|`` symbol in IPAC tables. By default, any character
    below a ``|`` will be ignored (since this is the current standard),
    but if you need to read files that assume characters below the ``|``
    symbols belong to the column before or after the ``|``, you can specify
    ``definition='left'`` or ``definition='right'`` respectively when reading
    the table (the default is ``definition='ignore'``). The following examples
    demonstrate the different conventions:

    * ``definition='ignore'``::

        |   ra  |  dec  |
        | float | float |
          1.2345  6.7890

    * ``definition='left'``::

        |   ra  |  dec  |
        | float | float |
           1.2345  6.7890

    * ``definition='right'``::

        |   ra  |  dec  |
        | float | float |
        1.2345  6.7890

    IPAC tables can specify a null value in the header that is shown in place
    of missing or bad data. On writing, this value defaults to ``null``.
    To specify a different null value, use the ``fill_values`` option to
    replace masked values with a string or number of your choice as
    described in :ref:`io_ascii_write_parameters`::

        >>> from astropy.io.ascii import masked
        >>> fill = [(masked, 'N/A', 'ra'), (masked, -999, 'sptype')]
        >>> ascii.write(data, format='ipac', fill_values=fill)
        \ This is an example of a valid comment
        ...
        |          ra|         dec|      sai|          v2|            sptype|
        |      double|      double|     long|      double|              char|
        |        unit|        unit|     unit|        unit|              ergs|
        |         N/A|        null|     null|        null|              -999|
                  N/A     29.09056      null         2.06               -999
         2345678901.0 3456789012.0 456789012 4567890123.0 567890123456789012


    Parameters
    ----------
    definition : str, optional
        Specify the convention for characters in the data table that occur
        directly below the pipe (``|``) symbol in the header column definition:

          * 'ignore' - Any character beneath a pipe symbol is ignored (default)
          * 'right' - Character is associated with the column to the right
          * 'left' - Character is associated with the column to the left

    DBMS : bool, optional
        If true, this verifies that written tables adhere (semantically)
        to the `IPAC/DBMS <http://irsa.ipac.caltech.edu/applications/DDGEN/Doc/DBMSrestriction.html>`_
        definition of IPAC tables. If 'False' it only checks for the (less strict)
        `IPAC <http://irsa.ipac.caltech.edu/applications/DDGEN/Doc/ipac_tbl.html>`_
        definition.
    t   ipacs   IPAC format tableR9   c         C` sJ   t  t |   j   | d k r. | |  j _ n t d   | |  j _ d  S(   NR9   Re   Rd   s-   definition should be one of ignore/left/right(   R9   Re   Rd   (   R   R¤   t   __init__t   headerRp   R^   R   (   R   R   R   (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR¦   ¬  s
    c         C` s}  |  j  j j t j d f  t t j | j   |  j	 _
 |  j	 j |  j |  j |  j  t j | |  j |  j |  j  t t j | j   } | |  j	 _
 | |  j  _
 g  } d | j k r9xy | j d D]g } t t |   d k rł t d t  n  x6 t t |  d d d d d D] } | j |  qWqĖ Wn  d	 | j k rÄ| j d	 } xl | D]a } y1 | | d
 } | j d j | j   |   Wq\t k
 r¼t d j |  t  q\Xq\Wn  g  | j D] }	 |	 d k rĪ|	 ^ qĪ}
 t |
  rt d j |
  t  n  |  j  j |  j  j
  xX t | j j    D]A \ } } t g  |  j	 j    D] } t | |  ^ q\ | _! q=Wg  } |  j  j    } x! t" |   D] } | j |  q¤Wxa t | j j    D]J \ } } | rt g  | D] } t | |  ^ qķ | _# qŃd | _# qŃWg  | j j   D] } t | j# | j!  ^ q/} |  j	 j$ | |  |  j  j$ | | |  | S(   s  
        Write ``table`` as list of strings.

        Parameters
        ----------
        table : `~astropy.table.Table`
            Input table data

        Returns
        -------
        lines : list
            List of strings corresponding to ASCII table

        Rv   RI   iN   s9   Comment string > 78 characters was automatically wrapped.iP   t   initial_indents   \ t   subsequent_indentRJ   RL   s
   \{0}={1!r}s   Table metadata keyword {0} has been skipped.  IPAC metadata must be in the form {{'keywords':{{'keyword': {{'value': value}} }}s   Table metadata keyword(s) {0} were not written.  IPAC metadata must be in the form {{'keywords':{{'keyword': {{'value': value}} }}i    (   RJ   RI   (%   Rn   Ro   RT   R	   R   t   listR   t
   itervaluest   columnsR§   Rr   t   check_column_namesRq   t   strict_namest   guessingt   _apply_include_exclude_namest   include_namest   exclude_namesRU   R   R   R   R   R   R   R>   t	   TypeErrort   anyt   _set_fill_valuesRf   t   valuest   maxR   t	   headwidthR   R&   R   (   R   RH   t   new_colsR?   R-   RA   t   keydictt   keywordR%   t   keyt   ignored_keysRu   R_   R   t   data_str_valst   col_str_itersR    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR   µ  sZ    
(#	(	
"9"/1(   R   R   R'   t   _format_namet   _io_registry_format_aliasesR    t   _io_registry_can_writet   _descriptionR¢   t
   data_classR1   t   header_classR+   R¦   R   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyR¤   C  s   `		(&   R'   t
   __future__R    R   R   RM   t   collectionsR   R   t   textwrapR   t   warningsR   t   externR   t   extern.six.movesR   R   R	   R
   R   t   utils.exceptionsR   t   table.pprintR   R   R   R   t   BaseSplitterR   t   FixedWidthHeaderR1   t   FixedWidthSplitterR   t   FixedWidthDataR¢   t   BasicR¤   (    (    (    s4   lib/python2.7/site-packages/astropy/io/ascii/ipac.pyt   <module>	   s&   ź