
[c           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l 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 m Z m Z d d
 l m Z m Z m 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 S(   s   
An extensible ASCII table reader and writer.

Classes to read DAOphot table format

:Copyright: Smithsonian Astrophysical Observatory (2011)
:Author: Tom Aldcroft (aldcroft@head.cfa.harvard.edu)
i    (   t   absolute_importt   divisiont   print_functionN(   t   defaultdictt   OrderedDicti   (   t   core(   t
   fixedwidthi   (   t   zipt   mapt   range(   t   first_true_indext   first_false_indext	   groupmoret   DaophotHeaderc           B` sk   e  Z d  Z d Z e j d  Z e j d e j  Z d	 Z	 d   Z
 d   Z d   Z d   Z d   Z RS(
   sK   
    Read the header from a file produced by the IRAF DAOphot routine.
    s   \s*#Ks   %-?(\d+)\.?\d?[sdfg]s,   [#]K\s+ (?P<name> \w+)\s* = (?P<stuff> .+) $c         C` s   t  j j |   d  S(   N(   R   t
   BaseHeadert   __init__(   t   self(    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyR   '   s    c         ` s4  d } t  t  } d   } x` t t | j |    D]F } x= t | t | |   D]# \ } } | | j | j    qS Wq4 W  j j rnt t t | j |     d \ } }	 }
 t	   j j
  } x t j d | d  j d  D] } t t d j t | t j |     } | d	 k r4| | d d <q | d j |  | d j |	  | d j |
  q Wn    f d
   } g  | d D]% } g  | D] } | |  ^ q^ q} t j t t |  t  } t j | } x- t | |  D] \ } } | d c | 7<qW|   _ t d   | j   D  } | S(   s  
        Parse a series of column definition lines like below.  There may be several
        such blocks in a single file (where continuation characters have already been
        stripped).
        #N ID    XCENTER   YCENTER   MAG         MERR          MSKY           NITER
        #U ##    pixels    pixels    magnitudes  magnitudes    counts         ##
        #F %-9d  %-10.3f   %-10.3f   %-12.3f     %-14.3f       %-15.7g        %-6d
        s   #Ns   #Us   #Fc         S` s   |  d j  d  S(   Ni   s    \(   t   strip(   t   s(    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyt   <lambda>7   s    ii   t   U2t    t   1c         ` s    t    j j |   j   d  S(   Ni    (   t   intt	   re_formatt   searcht   groups(   R   (   R   (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyR   N   s    c         s` s*   |  ]  \ } } | t  | g   f Vq d  S(   N(   t   sum(   t   .0t   kt   v(    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pys	   <genexpr>[   s    (   s   #Ns   #Us   #F(   R   t   listR   R   t   gett   appendt   splitt   datat   is_multilinet   lent   first_blockt   npt   aranget   astypet   joint   ittt   repeatt   fromiterR   R   t   Daophott   table_widtht
   col_widthst   dictt   items(   R   t   grouped_lines_dictt   line_idst   coldef_dictt   strippert   defblockt   keyt   linet
   last_namest
   last_unitst   last_formatst   N_multilinet   it   extended_namest   get_col_widtht   formatst   fR0   t
   row_widthst	   row_shortt   wt   r(    (   R   s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyt   parse_col_defs*   s4    		%+&*3	c         C` s+  | d } t  |  j  } | d k r'd   } t t | |  j t |     \ } } t |   \ } }	 t t | |   }
 d |
 k r t t |  j |
 d   } | | d <n  |  j	 |
  } d } xE t t | j
 |    D]+ \ } } } i | d	 6| d
 6| d | <q W| |  _ | d |  _ n  d S(   sy   
        Extract table-level keywords for DAOphot table.  These are indicated by
        a leading '#K ' prefix.
        t   tablei    c         S` s   |  j  d  d  d S(   Ni   i    (   R"   t   None(   R   (    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyR   l   s    s   #Kt   keywordss   #Ns   #Us   #Ft   unitt   formatt   colsN(   s   #Ns   #Us   #F(   R%   t   linesR   R   R	   R1   R   R   t   extract_keyword_lineRG   R    t   metat   names(   R   RN   RP   t
   table_metat   Nlinest   get_line_idt   gidR   t   grouped_linest   gixR3   RJ   R5   R4   t   nameRK   t   fmt(    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyt   update_meta_   s"    
	'(
	c         C` s   |  j  j |  } | r | j d  j   j d d  } i | d d 6| d d 6t |  d k rn | d n d d	 6} | j d
  | f Sd S(   s>   
        Extract info from a header keyword line (#K)
        t   stuffi   it   unitsiRL   i    R   t   valueRX   N(   t   re_header_keywordt   matcht   groupR   t   rsplitRI   R%   (   R   R9   t   mt   valst   keyword_dict(    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyRO      s    !&c   	      ` s  |  j  s t j d   n  |  j   |  j d } x` |  j D]U   t |   j j d  \ } } | d k ry |   _	 n  | d k r< |   _
 q< q< Wt |  j g   } t j |  } | | } x t |  j  D] \ }   | | | |   _   _   j   j   _ t   d  r t   f d   d D  rDt j   _ qd   j
 k rbt j   _ qd	   j
 k rt j   _ qq q W|  j j j d  d S(   s  
        Initialize the header Column objects from the table ``lines`` for a DAOphot
        header.  The DAOphot header is specialized so that we just copy the entire BaseHeader
        get_cols routine and modify as needed.



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

        Returns
        ----------
        col : list
            List of table Columns
        s'   No column names found in DAOphot headerRM   RK   RL   s   ##c         3` s   |  ] } |   j  k Vq d  S(   N(   RL   (   R   t   x(   t   col(    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pys	   <genexpr>   s    t   fgt   dR   t   INDEFt   0N(   RK   RL   (   Ri   Rj   (   RQ   R   t   InconsistentTableErrort   _set_cols_from_namesRP   RM   R   RX   R    RK   RL   R   R0   R'   t   cumsumt	   enumeratet   startt   endt   spant   hasattrt   anyt	   FloatTypet   typet   IntTypet   StrTypeR#   t   fill_valuesR!   (	   R   RN   t   coldefsRK   RY   t	   col_widtht   endst   startsR>   (    (   Rf   s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyt   get_cols   s0    	

(    (   t   __name__t
   __module__t   __doc__t   commentt   ret   compileR   t   VERBOSER^   t   aperture_valuesR   RG   RZ   RO   R}   (    (    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyR      s   			5	(	t   DaophotDatac           B` s/   e  Z e j Z d  Z d Z d   Z d   Z RS(   i    s   \s*#c         C` s   t  j j |   t |  _ d  S(   N(   R   t   BaseDataR   t   FalseR$   (   R   (    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyR      s    c         C` s_   |  j  rH t t t t j |  j     } t t t |   |  j	 _
 n  t j j |  |  d  S(   N(   R$   t   nextR   R   t   strR"   R&   t   tuplet   floatt   headerR   R   R   t   get_data_lines(   R   RN   t   aplist(    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyR      s    	!(	   R~   R   R   t   FixedWidthSplittert   splitter_classt
   start_lineR   R   R   (    (    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyR      s
   		t   DaophotInputterc           B` s>   e  Z d  Z d Z d Z e j d  Z d d  Z d   Z	 RS(   s   \t   *t    s   (#?)[^\\*#]*(\*?)(\\*) ?$i   c         ` s   t    f d   | |  D   \ } } } t |  } | d k rR d d | |  f S| |  } t | | | ! } | d k r d d | f St | | | | ! }	 t j | | |	 g  }
 | |
 d |
 d !} |
 | | f S(   s   
        Search lines for special continuation character to determine number of
        continued rows in a datablock.  For efficiency, depth gives the upper
        limit of lines to search.
        c         3` s'   |  ] }   j  j |  j   Vq d  S(   N(   t   re_multilineR   R   (   R   t   l(   R   (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pys	   <genexpr>   s   i   iN(   R   R   RI   R
   R'   Rm   (   R   RN   t   depthR   t   specialt   contt
   data_startt   header_linest   first_specialt   last_specialt   markerst   multiline_block(    (   R   s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyt   search_multiline   s    
c         C` s  |  j  |  \ } } } | d  k	 |  j _ | |  j _ | |  j _ | |  j j _ | d  k	 rn | | d } n  |  j } |  j	 } |  j
 } g  } g  }	 x t |  D] \ }
 } |  j j |  } | ri| j   \ } } } | s | r | j | |  } n  | r| j | |  } n  | r4| r4| j |  n  | s| j |  |	 j d j |   g  } qq t d j |
 |    q W|	 S(   Ni    R   s(   multiline re could not match line {}: {}(   R   RI   R#   R$   R   R&   R   RN   t   continuation_chart   multiline_chart   replace_charRn   R   R   R   t   replaceR!   R*   t
   ValueErrorRL   (   R   RN   R   t   blockR   R   R   R   t   partst   outlinesR>   R9   t   moR   R   R   (    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyt   process_lines  s:    				(
   R~   R   R   R   R   R   R   R   R   R   (    (    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyR      s   0R.   c           B` sV   e  Z d  Z d Z d g Z e Z d Z e Z	 e
 Z e Z d Z d   Z d d  Z RS(   s^	  
    Read a DAOphot file.
    Example::

      #K MERGERAD   = INDEF                   scaleunit  %-23.7g
      #K IRAF = NOAO/IRAFV2.10EXPORT version %-23s
      #K USER = davis name %-23s
      #K HOST = tucana computer %-23s
      #
      #N ID    XCENTER   YCENTER   MAG         MERR          MSKY           NITER    \
      #U ##    pixels    pixels    magnitudes  magnitudes    counts         ##       \
      #F %-9d  %-10.3f   %-10.3f   %-12.3f     %-14.3f       %-15.7g        %-6d
      #
      #N         SHARPNESS   CHI         PIER  PERROR                                \
      #U         ##          ##          ##    perrors                               \
      #F         %-23.3f     %-12.3f     %-6d  %-13s
      #
      14       138.538     INDEF   15.461      0.003         34.85955       4        \
                  -0.032      0.802       0     No_error

    The keywords defined in the #K records are available via the output table
    ``meta`` attribute::

      >>> import os
      >>> from astropy.io import ascii
      >>> filename = os.path.join(ascii.__path__[0], 'tests/t/daophot.dat')
      >>> data = ascii.read(filename)
      >>> for name, keyword in data.meta['keywords'].items():
      ...     print(name, keyword['value'], keyword['units'], keyword['format'])
      ...
      MERGERAD INDEF scaleunit %-23.7g
      IRAF NOAO/IRAFV2.10EXPORT version %-23s
      USER  name %-23s
      ...

    The unit and formats are available in the output table columns::

      >>> for colname in data.colnames:
      ...     col = data[colname]
      ...     print(colname, col.unit, col.format)
      ...
      ID None %-9d
      XCENTER pixels %-10.3f
      YCENTER pixels %-10.3f
      ...

    Any column values of INDEF are interpreted as a missing value and will be
    masked out in the resultant table.

    In case of multi-aperture daophot files containing repeated entries for the last
    row of fields, extra unique column names will be created by suffixing
    corresponding field names with numbers starting from 2 to N (where N is the
    total number of apertures).
    For example,
    first aperture radius will be RAPERT and corresponding magnitude will be MAG,
    second aperture radius will be RAPERT2 and corresponding magnitude will be MAG2,
    third aperture radius will be RAPERT3 and corresponding magnitude will be MAG3,
    and so on.

    t   daophots   IRAF DAOphot format tableiP   c         C` s#   t  j j |   |  j |  j _ d  S(   N(   R   t
   BaseReaderR   R#   t   inputter(   R   (    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyR     s    c         C` s
   t   d  S(   N(   t   NotImplementedError(   R   RH   (    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyt   write  s    N(   R~   R   R   t   _format_namet   _io_registry_format_aliasesR   t   _io_registry_can_writet   _descriptionR   t   header_classR   t
   data_classR   t   inputter_classR/   R   RI   R   (    (    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyR.   =  s   <		(    R   t
   __future__R    R   R   R   t   numpyR'   t	   itertoolsR+   t   collectionsR   R   R   R   R   t   extern.six.movesR   R   R	   t   miscR
   R   R   R   R   R   R   t   ContinuationLinesInputterR   R   R.   (    (    (    s7   lib/python2.7/site-packages/astropy/io/ascii/daophot.pyt   <module>	   s   ^