
[c           @   sj  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l 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 m Z m Z d d l m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% d d	 l& m' Z' m( Z( m) Z) d d
 l* m+ Z+ m, Z, d d l- m. Z. m/ Z/ d d l0 m1 Z1 d d l2 m3 Z3 d d l4 m5 Z5 m6 Z6 d d l7 m8 Z8 d d l9 m: Z: d d l; m< Z< d d l= m> Z> d e j? f d     YZ@ d e f d     YZA d e eA f d     YZB d eB f d     YZC d eB f d     YZD e jE d    ZF d S(   i(   t   divisionN(   t   chari   (   t   DELAYEDt	   _ValidHDUt   ExtensionHDUi   (   t   _numpy_hacks(   t
   FITS2NUMPYt   KEYWORD_NAMESt   KEYWORD_TO_ATTRIBUTEt   ATTRIBUTE_TO_KEYWORDt   TDEF_REt   Columnt   ColDefst   _AsciiColDefst   _FormatPt   _FormatQt   _makept   _parse_tformatt   _scalar_to_formatt   _convert_formatt   _cmp_recformatst
   _get_index(   t   FITS_rect   _get_recarray_fieldt   _has_unicode_fields(   t   Headert   _pad_length(   t   _is_intt   _str_to_numi   (   t   six(   t   string_types(   t   ranget   zip(   t   lazyproperty(   t   suppress(   t   AstropyUserWarning(   t   deprecated_renamed_argumentt   FITSTableDumpDialectc           B   s/   e  Z d  Z d Z d Z d Z e j Z e	 Z
 RS(   sM   
    A CSV dialect for the Astropy format of ASCII dumps of FITS tables.
    t    s   
t   "(   t   __name__t
   __module__t   __doc__t	   delimitert   lineterminatort	   quotechart   csvt	   QUOTE_ALLt   quotingt   Truet   skipinitialspace(    (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR%   (   s   	t   _TableLikeHDUc           B   s   e  Z d  Z e Z e Z e Z e	 d    Z
 e	 d
 d e d   Z e d    Z e d    Z d   Z d   Z d   Z d	   Z RS(   s   
    A class for HDUs that have table-like data.  This is used for both
    Binary/ASCII tables as well as Random Access Group HDUs (which are
    otherwise too dissimilar for tables to use _TableBaseHDU directly).
    c         C   s
   t   d S(   s   
        This is an abstract HDU type for HDUs that contain table-like data.
        This is even more abstract than _TableBaseHDU which is specifically for
        the standard ASCII and Binary Table types.
        N(   t   NotImplementedError(   t   clst   header(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   match_headerB   s    i    c   	      K   sS   |  j  |  } t j | d | d | } |  d | d | |  } | j |  | S(   s  
        Given either a `ColDefs` object, a sequence of `Column` objects,
        or another table HDU or table data (a `FITS_rec` or multi-field
        `numpy.ndarray` or `numpy.recarray` object, return a new table HDU of
        the class this method was called on using the column definition from
        the input.

        See also `FITS_rec.from_columns`.

        Parameters
        ----------
        columns : sequence of `Column`, `ColDefs`, or other
            The columns from which to create the table data, or an object with
            a column-like structure from which a `ColDefs` can be instantiated.
            This includes an existing `BinTableHDU` or `TableHDU`, or a
            `numpy.recarray` to give some examples.

            If these columns have data arrays attached that data may be used in
            initializing the new table.  Otherwise the input columns will be
            used as a template for a new table with the requested number of
            rows.

        header : `Header`
            An optional `Header` object to instantiate the new HDU yet.  Header
            keywords specifically related to defining the table structure (such
            as the "TXXXn" keywords like TTYPEn) will be overridden by the
            supplied column definitions, but all other informational and data
            model-specific keywords are kept.

        nrows : int
            Number of rows in the new table.  If the input columns have data
            associated with them, the size of the largest input column is used.
            Otherwise the default is 0.

        fill : bool
            If `True`, will fill all cells with zeros or blanks.  If `False`,
            copy the data from input, undefined cells will still be filled with
            zeros/blanks.

        Notes
        -----

        Any additional keyword arguments accepted by the HDU class's
        ``__init__`` may also be passed in as keyword arguments.
        t   nrowst   fillt   dataR6   (   t   _columns_typeR   t   from_columnst   _add_listener(	   R5   t   columnsR6   R8   R9   t   kwargst   coldefsR:   t   hdu(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR<   L   s
    1c         C   s
   t  g   S(   sT   
        The :class:`ColDefs` objects describing the columns in this table.
        (   R   (   t   self(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR>      s    c         C   s
   t   d S(   s   
        Table-like HDUs must provide an attribute that specifies the number of
        rows in the HDU's table.

        For now this is an internal-only attribute.
        N(   R4   (   RB   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   _nrows   s    	c         C   s  |  j  } t d   | j D  r |  j d k	 r |  j |  j k r |  j |  j t j |  j	  } | |  j  j
 d | j d t j j  } nZ |  j |  j | j |  j	  } | d k r t j g  d | j } n  | j
 t j j  } |  j |  | j
 |  j  } | j |  | S(   s,   Get the table data from an input HDU object.c         s   s'   |  ] } t  |  t t f k Vq d  S(   N(   t   typeR   R   (   t   .0t   r(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pys	   <genexpr>   s   t   dtypeRD   N(   R>   t   anyt   _recformatst
   _data_sizet   Nonet   _theapt   _get_raw_datat   npt   uint8t   _data_offsett   viewRG   t   rect   recarrayRC   t   arrayt   _init_tbdatat
   _data_typeR=   (   RB   R>   t   raw_dataR:   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   _get_tbdata   s$    		c         C   s   |  j  } | j j d  | _ |  j | _ |  j | _ |  j d | _ |  j d |  j d } |  j | | _ x, t	 |  D] \ } } | j
 |  | _ q{ W| ` d  S(   Nt   >t   PCOUNTt   NAXIS1t   NAXIS2(   R>   RG   t   newbyteordert   _uintRL   t   _heapoffsett   _headert	   _heapsizet   _gapt	   enumeratet   fieldRT   t   _arrays(   RB   R:   R>   t   tbsizet   idxt   col(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyRU      s    	c         C   s   |  j  r |  ` n  d S(   sh   
        Update the data upon addition of a new column through the `ColDefs`
        interface.
        N(   t   _data_loadedR:   (   RB   R>   t   column(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   _update_column_added   s    
	c         C   s   |  j  r |  ` n  d S(   sc   
        Update the data upon removal of a column through the `ColDefs`
        interface.
        N(   Ri   R:   (   RB   R>   t   col_idx(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   _update_column_removed   s    
	N(   R(   R)   R*   R   RV   R   R;   t   FalseR^   t   classmethodR7   RK   R<   R!   R>   t   propertyRC   RX   RU   Rk   Rm   (    (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR3   4   s   
6
	!		t   _TableBaseHDUc           B   s   e  Z d  Z e Z d d d e d  Z e d    Z e	 d    Z
 e	 d    Z e j d    Z e d    Z e	 d    Z d   Z d	   Z e e d
  Z d d  Z d   Z d   Z d   Z d d  Z d   Z RS(   s.   
    FITS table extension base HDU class.
    c   	   
   C   ss  t  t |   j d | d | d |  | d  k	 rP t | t  rP t d   n  | |  _ | t k r | d  k r t d   q| |  _	 nd d d d d  d! d" d# g } | d  k	 r | j
 d t  } | j | j  n  t |  |  _	 t | t j  r| j j d  k	 rt | |  j  r,| |  _ n |  j j |  |  _ |  j j |  j	 d <|  j j d |  j	 d <t |  j j  |  j	 d <|  j j |  _ |  j   t t t  C x2 t |  j  D]! \ } } |  j j |  | _  qW|  j `! Wd  QXn | d  k rn t d   t |  j	 d t"  o>|  j	 d j#   |  j$ k s]|  j$ |  j% f |  j	 d <n  | ro| |  _& n  d  S($   NR:   R6   t   names   header must be a Header object.s   No header to setup HDU.t   XTENSIONt    t   BITPIXi   s   array data typet   NAXISi   s   number of array dimensionsR[   i    s   length of dimension 1R\   s   length of dimension 2RZ   s   number of group parameterst   GCOUNTi   s   number of groupst   TFIELDSs   number of table fieldst   strips   Table data has incorrect type.(   Rs   Rt   Rt   (   Ru   i   s   array data type(   Rv   i   s   number of array dimensions(   R[   i    s   length of dimension 1(   R\   i    s   length of dimension 2(   RZ   i    s   number of group parameters(   Rw   i   s   number of groups(   Rx   i    s   number of table fields('   t   superRq   t   __init__RK   t
   isinstanceR   t
   ValueErrorR^   R   R`   t   copyR1   t   extendt   cardsRN   t   ndarrayRG   t   fieldsRV   R:   R<   t   _raw_itemsizet   shapet   lent   _coldefsR>   t   updateR"   t	   TypeErrort   AttributeErrorRc   Rd   RT   Re   R   t   rstript
   _extensiont   _ext_commentRr   (	   RB   R:   R6   Rr   t   uintR   t   hcopyRg   Rh   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR{      sT    		$
c         C   s
   t   d S(   s   
        This is an abstract type that implements the shared functionality of
        the ASCII and Binary Table HDU types, which should be used instead of
        this.
        N(   R4   (   R5   R6   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR7   H  s    c         C   s2   |  j  r% t |  j d  r% |  j j S|  j |   S(   sT   
        The :class:`ColDefs` objects describing the columns in this table.
        R   (   t	   _has_datat   hasattrR:   R   R;   (   RB   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR>   R  s    
c         C   s"   |  j    } |  j | _ |  ` | S(   N(   RX   R>   R   (   RB   R:   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR:   \  s    c         C   s  d |  j  k r2 |  j  d | k r& d  St |  _ n	 t |  _ t |  _ | d  k r |  j r d j |  j j  } t j	 j
 d  d | d |  j j d d } n  t | t j  r| j j d  k	 r| j |  j  } t | j |  j  s
|  j | j  } t j |  } n  | |  j  d <|  j j |  _ |  j   t t t  C x2 t |  j  D]! \ } } |  j j |  | _
 qPW|  j ` Wd  QXn | d  k rn t d   | S(   NR:   t   ,t   formatst   namesR   i    s   Table data has incorrect type.(   t   __dict__R1   t   _data_replacedt	   _modifiedRK   R>   t   joinRI   RN   RR   RT   R   R|   R   RG   R   RQ   RV   R;   R   R<   R:   R   R"   R   R   Rc   Rd   Re   (   RB   R:   R   t   new_columnsRg   Rh   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR:   d  s6    		$
c         C   s-   |  j  s |  j j d d  St |  j  Sd  S(   NR\   i    (   Ri   R`   t   getR   R:   (   RB   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyRC     s    	c         C   s+   |  j  d |  j  d } |  j  j d |  S(   NR[   R\   t   THEAP(   R`   R   (   RB   t   size(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyRL     s    c         C   s|   |  j  j d |  j j d d |  j  j d |  j j d d d |  j  j d t |  j  d d |  j   |  j   d S(	   sN   
        Update header keywords to reflect recent changes of columns.
        R[   t   afterRv   R\   i    Rx   Rw   N(	   R`   t   setR:   R   R   R   R>   t   _clear_table_keywordst   _populate_table_keywords(   RB   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR     s
    #"
c         C   s(   |  j  d |  j j   d |  j j    S(   sP   
        Make a copy of the table HDU, both header and data are copied.
        R:   R6   (   t	   __class__R:   R~   R`   (   RB   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR~     s    c   
      C   s  |  j  rh|  j j d |  j  t |  j j  |  j d <|  j j d |  j d <|  j d |  j d } |  j j d |  } | | |  j _	 |  j j
 |  j j	 } | d k r | |  j d <n  |  j   x t |  j j  D] } |  j j j | } t | t  r |  j j |  j } | j }	 |	 | j d | j d	 | } | j |  j d
 t | d  <q q Wn  t t |   j | |  S(   Nt   update_heap_pointersRx   i    R\   R[   R   RZ   t   repeatt   maxt   TFORMi   (   R   R:   t   _scale_backt   _manages_own_heapR   R   R`   R   R   Rb   Ra   R   R   t   _nfieldsRI   R|   R   Rd   R   R   RG   R   t   tformt   strRz   Rq   t   _prewriteto(
   RB   t   checksumt   inplaceRf   t	   heapstartt   pcountRg   t   formatt   _maxt
   format_cls(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR     s*    	
		(t   warnc         C   s   t  t |   j d |  } |  j d d d   d | |  |  j d d d   d | |  |  j d d	 d
   d | |  |  j d } x> t |  D]0 } |  j d t | d  d d d | |  q W| S(   s.   
        _TableBaseHDU verify method.
        t   optionRv   c         S   s
   |  d k S(   Ni   (    (   t   v(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   <lambda>  s    i   Ru   c         S   s
   |  d k S(   Ni   (    (   R   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR     s    i   Rx   i   c         S   s"   t  |   o! |  d k o! |  d k S(   Ni    i  (   R   (   R   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR     s    i    R   i   N(   Rz   Rq   t   _verifyt	   req_cardsRK   R`   R   R   (   RB   R   t   errst   tfieldsRg   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR     s    	
#c   	      C   s  |  j  j } |  j rf |  j d
 k r9 d \ } } d } n t |  j  } t |  j  } |  j j } nl d } |  j d } |  j d } d j	 g  t
 |  D]! } |  j d t | d  ^ q  } d j |  } d	 j | |  } t |  j  } |  j |  j | | | | f S(   sC   
        Summarize the HDU: name, dimensions, and formats.
        Rt   i    R\   Rx   s   , R   i   s   [{}]s	   {}R x {}CN(    (   (    Rt   (    (   R   R(   Ri   R:   RK   R   R>   R   R`   R   R   R   R   Rr   t   ver(	   RB   t
   class_nameR   R   R8   t   ncolst   jt   dimst   ncards(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   _summary  s"    			4c         C   s-   t  t |   j | |  |  j d |  d  S(   Nt   index(   Rz   Rq   Rm   R   (   RB   R>   Rg   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyRm     s    c         C   s0  t  | } | t | d  } | |  j k rV | d k rF |  j | =q,| |  j | <n t j |  } x t t |   D]L }	 |	 t | d  7}	 |	 |  j k rv |  j j |	 | | f d t Pqv qv Wxc t | d D]F }
 |
 t | d  7}
 |
 |  j k r |  j j |
 | | f  Pq q W| |  j | <d S(   sN   
        Update the header when one of the column objects is updated.
        i   R   N(	   R	   R   R`   RK   R   R   t   reversedt   insertR1   (   RB   Rj   Rl   t   attrt	   old_valuet	   new_valuet   base_keywordt   keywordt   keyword_idxt   before_keywordt   after_keyword(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt    _update_column_attribute_changed  s(    
c         C   s  g  } x t  |  j j    D] \ } } t j |  } y | j d  } Wn t k
 r` q n X| t k r t | j d   d } | j	 | | j d  | | f  q q Wt
 | d t j d  d t } x? | D]7 \ } } }	 } | d	 k s| | k r |  j | =q q W| d	 k	 rt
 | d t j d  }
 xx |
 D]p \ }	 } } } | | k rdq@n  |  j j | } | t |  | j | j f } |  j j | |  |  j | =q@Wd |  j k r|  j d c d 8<qn  d	 S(
   s  
        Wipe out any existing table definition keywords from the header.

        If specified, only clear keywords for the given table index (shifting
        up keywords for any other columns).  The index is zero-based.
        Otherwise keywords for all columns.
        t   labelt   numi   i    t   keyt   reversei   Rx   N(   Rc   R`   t   keysR
   t   matcht   groupt	   ExceptionR   t   intt   appendt   sortedt   operatort
   itemgetterR1   RK   R   R   t   valuet   commentR   (   RB   R   t   table_keywordsRg   R   R   R   R   t   rev_sorted_idx_0t   _t   sorted_idx_3t   old_cardt   new_card(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR   >  s8    "	c         C   s   x| t  |  j  D]k \ } } x\ t j t  D]K \ } } t | |  } | d k	 r, | t | d  } | |  j | <q, q, Wq Wd S(   s;   Populate the new table definition keywords from the header.i   N(	   Rc   R>   R   t	   iteritemsR   t   getattrRK   R   R`   (   RB   Rg   Rj   R   R   t   val(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR   q  s    N(   R(   R)   R*   Rn   R   RK   R{   Ro   R7   R!   R>   R:   t   setterRp   RC   RL   R   R~   R   R   R   Rm   R   R   R   (    (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyRq      s$   
N

8		
			(3t   TableHDUc           B   st   e  Z d  Z d Z d Z d Z e Z e j	 d  Z
 d d d d  Z e d    Z d   Z d   Z d	 d
  Z RS(   s   
    FITS ASCII table extension HDU class.

    Parameters
    ----------
    data : array or `FITS_rec`
        Data to be used.
    header : `Header`
        Header to be used.
    name : str
        Name to be populated in ``EXTNAME`` keyword.

    t   TABLEs   ASCII table extensionR&   s4   (?P<code>[ADEFIJ])(?P<width>\d+)(?:\.(?P<prec>\d+))?c         C   s#   t  t |   j | | d | d  S(   NRr   (   Rz   R   R{   (   RB   R:   R6   Rr   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR{     s    c         C   sP   | j  d } | j } t | t  r4 | j   } n  | j d k oO | |  j k S(   Ni    Rs   (   R   R   R|   R   R   R   R   (   R5   R6   t   cardt   xtension(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR7     s
    	c         C   s{  |  j  } g  t | j  D] \ } } | ^ q } t j j |  } | ra t d j |    n  | j d | j	 d d } i  } x t
 t |   D] } d t | j |  } | t |  d k r|  j d | k rd t | j | |  j d |  } qn  | | j	 | d f | | j | <q W|  j |  j | |  j  }	 |	 j t j j  }
 |  j |
  |
 j |  j  S(   Ns   Duplicate field names: {}ii   t   SR[   (   R>   Rc   R   RN   RR   t   find_duplicateR}   R   t   spanst   startsR   R   R   R`   RM   RC   RP   RQ   RS   RU   RV   (   RB   R>   Rg   t   nR   t   dupt   itemsizeRG   t	   data_typeRW   R:   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyRX     s$    	(&&c         C   s   |  j  rz |  j j d t j d t j  } t j t |  j  d d t j } t j	 | |  } |  j
 | d | } | St t |   j |  Sd S(   sJ   
        Calculate the value for the ``DATASUM`` card in the HDU.
        RD   RG   R&   t   blockingN(   R   R:   RQ   RN   R   t   ubytet
   frombufferR   R   R   t   _compute_checksumRz   R   t   _calculate_datasum(   RB   R   t   bytes_arrayt   paddingt   dt   cs(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR     s    	!R   c         C   s   t  t |   j d |  } |  j d d d   d | |  |  j d } x> t |  D]0 } |  j d t | d  d t d | |  qT W| S(	   s+   
        `TableHDU` verify method.
        R   RZ   c         S   s
   |  d k S(   Ni    (    (   R   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR     s    i    Rx   t   TBCOLi   N(	   Rz   R   R   R   RK   R`   R   R   R   (   RB   R   R   R   Rg   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR     s    #N(   R(   R)   R*   R   R   t   _padding_byteR   R;   t   ret   compilet   _TableHDU__format_RERK   R{   Ro   R7   RX   R   R   (    (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR   |  s   		 	t   BinTableHDUc           B   sL  e  Z d  Z d Z d Z d d d e d  Z e d    Z	 d   Z
 d   Z d   Z d   Z e j d	  Z e d
 d d  d d d e d   Z e e j e  r e j e j d d  7_ n  d d e d d  Z e e j e  re j e j d d  7_ n  e e  Z d   Z d   Z e d d   Z e d    Z RS(   sg  
    Binary table HDU class.

    Parameters
    ----------
    data : array, `FITS_rec`, or `~astropy.table.Table`
        Data to be used.
    header : `Header`
        Header to be used.
    name : str
        Name to be populated in ``EXTNAME`` keyword.
    uint : bool, optional
        Set to `True` if the table contains unsigned integer columns.

    t   BINTABLEs   binary table extensionc         C   s   d d l  m } t | |  ro d d l m } | |  } | d  k	 rZ | j j |  n  | j } | j } n  t	 t
 |   j | | d | d | d  S(   Ni   (   t   Tablei   (   t   table_to_hduRr   R   (   t   tableR  R|   t   convenienceR  RK   R6   R   R:   Rz   R   R{   (   RB   R:   R6   Rr   R   R  R  RA   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR{     s    	c         C   sV   | j  d } | j } t | t  r4 | j   } n  | j d k oU | |  j d f k S(   Ni    Rs   t   A3DTABLE(   R   R   R|   R   R   R   R   (   R5   R6   R   R   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR7     s    	c         C   s   t  |  j   } | j d t j d t j  } |  j | d | } xw t | j  D]f } t	 | j
 j | t  rU xD | j |  D]0 } t |  s q n  |  j | | d | } q WqU qU W| SWd QXd S(   sS   
        Calculate the value for the ``DATASUM`` card given the input data
        RD   RG   R   N(   t   _binary_table_byte_swapR:   RQ   RN   R   R   R   R   R   R|   R>   RI   R   Rd   R   (   RB   R   R:   t   doutt   csumRg   t   coldata(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   _calculate_datasum_with_heap  s    c         C   s0   |  j  r |  j |  St t |   j |  Sd S(   sJ   
        Calculate the value for the ``DATASUM`` card in the HDU.
        N(   R   R  Rz   R   R   (   RB   R   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR   .  s    	c   	      C   s  d } |  j  d  k r | St |  j   ]} t |  rG |  j |  n* | j |  | j | j d j d   | j } |  j	 sx t
 | j  D] } t | j j | t  s q n  |  j  j |  } xI | D]A } t |  d k r | | j 7} | j s| j |  qq q Wq WnJ | j   } t |  d k rg| t |  7} | j sg| j |  qgn  | | j | _ | | 7} Wd  QX| |  j  j |  j  j 7} | S(   Ni    s    t   ascii(   R:   RK   R  R   t   _writedata_by_rowt
   writearrayt   writeRb   t   encodeR   R   R   R|   R>   RI   R   Rd   R   t   nbytest   simulateonlyt   _get_heap_dataRa   R   R   (	   RB   t   fileobjR   R:   R  Rg   Rd   t   rowt	   heap_data(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   _writedata_internal?  s:    					c         C   sP  g  t  t |  j j   D] } |  j j |  ^ q } xt  t |  j   D] } x | D] } | | } d  } | j j d k r | j j j	 | j j  } t
 | j j | d  } t j j | d  } n  | j |  | d  k	 rZ | j j j	 | j j  } t
 | j j | d  }	 d | |	 }
 | j |
 j d   qZ qZ WqM Wd  S(   Nt   Ui   R  s    (   R   R   R:   R>   Rd   RK   RG   t   kindR   R   R   RN   R   R  R  R  (   RB   R  Rg   R   Rd   t   itemt   field_widtht   iR   t   item_lengthR   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR  q  s     4
sq  

        - **datafile:** Each line of the data file represents one row of table
          data.  The data is output one column at a time in column order.  If
          a column contains an array, each element of the column array in the
          current row is output before moving on to the next column.  Each row
          ends with a new line.

          Integer data is output right-justified in a 21-character field
          followed by a blank.  Floating point data is output right justified
          using 'g' format in a 21-character field with 15 digits of
          precision, followed by a blank.  String data that does not contain
          whitespace is output left-justified in a field whose width matches
          the width specified in the ``TFORM`` header parameter for the
          column, followed by a blank.  When the string data contains
          whitespace characters, the string is enclosed in quotation marks
          (``""``).  For the last data element in a row, the trailing blank in
          the field is replaced by a new line character.

          For column data containing variable length arrays ('P' format), the
          array data is preceded by the string ``'VLA_Length= '`` and the
          integer length of the array for that row, left-justified in a
          21-character field, followed by a blank.

          .. note::

              This format does *not* support variable length arrays using the
              ('Q' format) due to difficult to overcome ambiguities. What this
              means is that this file format cannot support VLA columns in
              tables stored in files that are over 2 GB in size.

          For column data representing a bit field ('X' format), each bit
          value in the field is output right-justified in a 21-character field
          as 1 (for true) or 0 (for false).

        - **cdfile:** Each line of the column definitions file provides the
          definitions for one column in the table.  The line is broken up into
          8, sixteen-character fields.  The first field provides the column
          name (``TTYPEn``).  The second field provides the column format
          (``TFORMn``).  The third field provides the display format
          (``TDISPn``).  The fourth field provides the physical units
          (``TUNITn``).  The fifth field provides the dimensions for a
          multidimensional array (``TDIMn``).  The sixth field provides the
          value that signifies an undefined value (``TNULLn``).  The seventh
          field provides the scale factor (``TSCALn``).  The eighth field
          provides the offset value (``TZEROn``).  A field value of ``""`` is
          used to represent the case where no value is provided.

        - **hfile:** Each line of the header parameters file provides the
          definition of a single HDU header card as represented by the card
          image.
      t   clobbert	   overwrites   2.0c         C   s-  g  } | | | g } x | D] } t  | t  r t j j |  r t j j |  d k r | r t j d j |  t	  t j
 |  q | j |  q q q W| r t d j g  | D] } d j |  ^ q    n  |  j |  | r |  j |  n  | r)|  j j | d d d t d t n  d	 S(
   s  
        Dump the table HDU to a file in ASCII format.  The table may be dumped
        in three separate files, one containing column definitions, one
        containing header parameters, and one for table data.

        Parameters
        ----------
        datafile : file path, file object or file-like object, optional
            Output data file.  The default is the root name of the
            fits file associated with this HDU appended with the
            extension ``.txt``.

        cdfile : file path, file object or file-like object, optional
            Output column definitions file.  The default is `None`, no
            column definitions output is produced.

        hfile : file path, file object or file-like object, optional
            Output header parameters file.  The default is `None`,
            no header parameters output is produced.

        overwrite : bool, optional
            If ``True``, overwrite the output file if it exists. Raises an
            ``OSError`` (``IOError`` for Python 2) if ``False`` and the
            output file exists. Default is ``False``.

            .. versionchanged:: 1.3
               ``overwrite`` replaces the deprecated ``clobber`` argument.

        Notes
        -----
        The primary use for the `dump` method is to allow viewing and editing
        the table data and parameters in a standard text editor.
        The `load` method can be used to create a new table from the three
        plain text (ASCII) files.
        i    s   Overwriting existing file '{}'.s     s   File '{}' already exists.t   seps   
t   endcardR   N(   R|   R   t   ost   patht   existst   getsizet   warningsR   R   R#   t   removeR   t   IOErrorR   t
   _dump_datat   _dump_coldefsR`   t   tofileRn   (   RB   t   datafilet   cdfilet   hfileR  t   existt   filest   f(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   dump  s&    '*(s   
s	   
        c   	      C   s   | d k r t   } n  | r[ | r6 t j |  } q[ | j t j |  d t d t n  d } | ry |  j |  } n  |  j | |  } | d k r t |  } n  |  d | d |  } | | _ | S(   sU	  
        Create a table from the input ASCII files.  The input is from up to
        three separate files, one containing column definitions, one containing
        header parameters, and one containing column data.

        The column definition and header parameters files are not required.
        When absent the column definitions and/or header parameters are taken
        from the header object given in the header argument; otherwise sensible
        defaults are inferred (though this mode is not recommended).

        Parameters
        ----------
        datafile : file path, file object or file-like object
            Input data file containing the table data in ASCII format.

        cdfile : file path, file object, file-like object, optional
            Input column definition file containing the names,
            formats, display formats, physical units, multidimensional
            array dimensions, undefined values, scale factors, and
            offsets associated with the columns in the table.  If
            `None`, the column definitions are taken from the current
            values in this object.

        hfile : file path, file object, file-like object, optional
            Input parameter definition file containing the header
            parameter definitions to be associated with the table.  If
            `None`, the header parameter definitions are taken from
            the current values in this objects header.

        replace : bool
            When `True`, indicates that the entire header should be
            replaced with the contents of the ASCII file instead of
            just updating the current header.

        header : Header object
            When the cdfile and hfile are missing, use this Header object in
            the creation of the new table and HDU.  Otherwise this Header
            supercedes the keywords from hfile, which is only used to update
            values not present in this Header, unless ``replace=True`` in which
            this Header's values are completely replaced with the values from
            hfile.

        Notes
        -----
        The primary use for the `load` method is to allow the input of ASCII
        data that was edited in a standard text editor of the table data and
        parameters.  The `dump` method can be used to create the initial ASCII
        files.
        R   t   update_firstR:   R6   N(	   RK   R   t   fromtextfileR   R1   t   _load_coldefst
   _load_dataR   R>   (	   R5   R,  R-  R.  t   replaceR6   R@   R:   RA   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   load  s     5
	c         C   sQ  | r9 |  j  r9 t j j |  j  j  d } | d } n  t } t | t  rf t | d  } t	 } n  t
 j | d t } d   } x|  j D]} g  } x|  j D]} d
 }	 t | j  }
 t |
 t  r+| j d  | j d j t | | j    t | j  \ } } } t | d d }	 n  |	 rex | | j j D] } | j | | |	   qBWq |  j j j | j d } | j } | d k r| j j } n  | d	 k r| t | j  7} n  | j d k rxN | | j j D] } | j | | |   qWq | j | | | j |   q W| j |  q W| rM| j   n  d
 S(   si   
        Write the table data in the ASCII format read by BinTableHDU.load()
        to fileobj.
        i    s   .txtt   wt   dialectc         S   s   | d d k r3 t  | d  } d j |  d | S| t j d k rS d j |   S| t j d k r| d	 j |  j |  j  S| t j d
 k r t j r d |  Sd j |   Sn  d  S(   Ni    R   i   s	   {:{size}}R   t
   AllIntegers   {:21d}t   Complexs   {:21.15g}+{:.15g}jt   Floats   %#21.15gs
   {:#21.15g}(   R   R   RN   t	   typecodest   realt   imagR   t   PY2(   R   R   R   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   format_valueo  s    	s   VLA_Length=s   {:21d}t   VR   N(    t   _fileR"  R#  t   splitextRr   Rn   R|   R   t   openR1   R.   t   writerR%   R:   R>   RK   R   R   R   R   R   R   R   t   flatRG   R   R   t   baseR   R   t   writerowt   close(   RB   R  t   roott
   close_filet
   linewriterRB  R  t   lineRj   t
   vla_formatR   R   RG   R   R   t   array_formatR   (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR)  \  sH    		#	c            s   t  } t | t  r- t | d  } t } n  x |  j D]     j   j g } d d d d d d g } | g    f d   | D D]! } d	 j | r | n d
  ^ q 7} | j d j	 |   | j d  q7 W| r | j
   n  d S(   s{   
        Write the column definition parameters in the ASCII format read by
        BinTableHDU.load() to fileobj.
        R9  t   dispt   unitt   dimt   nullt   bscalet   bzeroc         3   s   |  ] } t    |  Vq d  S(   N(   R   (   RE   R   (   Rj   (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pys	   <genexpr>  s    s   {:16s}s   ""R&   s   
N(   Rn   R|   R   RF  R1   R>   Rr   R   R  R   RK  (   RB   R  RM  RO  t   attrsR   (    (   Rj   s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR*    s    	<c            s  t  } t | t  r- t | d  } t } n  | j   } t j | d t } g  } g    g  } d } | d k	 r | j
   | j } n    f d   }	 x/| D]'}
 | d 7} | d k	 r q n  d } d } x | t |
  k  r|
 | d k ry| t |  k  r| | } n! t |
 | d  } | j |  | d 7} x/ | rk|	 |
 | |  | d 8} | d 7} q=W| d 7} q | t |  k r| j d  n  |	 |
 | |  | d 7} | d 7} q Wq WxA t |  D]3 \ } } | d k	 rt |    |   | <qqWt j j   | d  j } t j t j d d d	 |  d
 | d t } | j } x t |  D] \ } } | d k	 rj| j j | }   | t t |   } t | d |   | <  | | j j
 | <| j j | } | j | t | |   |   qjqjW  f d   } | j |  t j | d t } x_t |  D]Q\ }
 } d } d } x6| t |  k  r| | d k r| | } | d 7} t | | |  } | | | | !| |
 | (| | 7} nR | | j  r"t t j! j" | | j    } t | | |  } | | 7} n d } | d k r\| | | |  | |
 | <| d 7} n2 g  | | D] } | | |  ^ qg| |
 | j# (| d 7} qfWqKW| r| j$   n  | S(   sW   
        Read the table data from the ASCII file output by BinTableHDU.dump().
        RF   R:  i    c            sd   t  |   } t |  } | t    k r:   j |  n& t   | |  d k  r` |   | <n  d  S(   Ni    (   R   R   R   R   R   (   R   Rg   t
   fitsformatt	   recformat(   t
   recformats(    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   update_recformats  s    i   s   VLA_Length=i   R   RG   R8   R9   R   c            sJ     |  t  d k r$ t t |   S  |  t  d k rB t |  S| Sd  S(   Nt   Lt   M(   R   t   boolR   t   complex(   Rh   R   (   R[  (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyRB     s
    
N(%   Rn   R|   R   RF  R1   t   tellR.   t   readerR%   RK   RI   R   R   R   R   Rc   R   RN   RR   t   format_parserRG   R   R<   RS   R:   R>   Re   R   t   _cache_fieldR   t   seekt   sliceR   t   multiplyt   reduceRH  RK  (   R5   R  R@   RM  t
   initialpost
   linereadert   vla_lengthsR   R8   R\  R  Rh   Rg   t
   vla_lengtht   lengthRG   RA   R:   t   arrt   dtRr   RB  RO  t   vla_lent   slice_t
   array_sizeR   (    (   R[  s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR6    s    		


	

	'

/c   	      C   s  t  } t | t  r- t | d  } t } n  g  } x | D] } | d  j   } i  } x< d d d d d g D]% } | j d  j d	 d
  | | <ql WxQ d d d g D]@ } | j d  j d	 d
  } | r t |  } n  | | | <q W| j	 t
 |    q: W| r| j   n  t |  S(   sm   
        Read the table column definitions from the ASCII file output by
        BinTableHDU.dump().
        RF   iRr   R   RR  RS  RT  i    s   ""Rt   RU  RV  RW  (   Rn   R|   R   RF  R1   t   splitt   popR7  R   R   R   RK  R   (	   R5   R  RM  R>   RO  t   wordsR?   R   t   word(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR5  Q  s&    	#N(   R(   R)   R*   R   R   RK   Rn   R{   Ro   R7   R  R   R  R  t   textwrapt   dedentt   _tdump_file_formatR$   R2  R|   R   R7  R8  R)  R*  R6  R5  (    (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR     s2   		 		2	3	C	M	N	c         c   s  |  j  } g  } g  } g  } g  } t j d k r9 d } n d	 } xMt | j  D]<\ } } t |  |  }	 | j | \ }
 } | j |  | j |
  | j |  t |	 t	 j	  r qO n  |	 j
 d k r|
 j j d | k r| j |	  |
 j   | d <n  |  j j | } t | t  rO |  j |  } xY | D]N } t | t	 j	  r6| j
 d k r6| j  j d | k r6| j |  q6q6WqO qO Wx! t |  D] } | j t  qWt j t j  t t | |    |  } | |  _  |  Vx | D] } | j t  qW| |  _  d S(
   s  
    Ensures that all the data of a binary FITS table (represented as a FITS_rec
    object) is in a big-endian byte order.  Columns are swapped in-place one
    at a time, and then returned to their previous byte order when this context
    manager exits.

    Because a new dtype is needed to represent the byte-swapped columns, the
    new dtype is temporarily applied as well.
    t   littlet   <t   =i   i    iN(   R{  R|  (   R{  (   RG   t   syst	   byteorderRc   R   R   R   R   R|   t	   chararrayR   RI  R   R]   R>   RI   R   Rd   R   t   byteswapR1   t   nht   realign_dtypeRN   t   listR    (   R:   t
   orig_dtypeR   R   t   offsetst   to_swapt
   swap_typesRg   Rr   Rd   t   field_dtypet   field_offsetRZ  R
  t   cRn  t	   new_dtype(    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyR  s  sF    		%%!		(G   t
   __future__R    t
   contextlibR.   R   R"  R   R}  Rw  R&  t   numpyRN   R   R  RI  R   R   R   Rt   R   R  Rj   R   R   R   R	   R
   R   R   R   R   R   R   R   R   R   R   R   t   fitsrecR   R   R   R6   R   R   t   utilR   R   t   externR   t
   extern.sixR   t   extern.six.movesR   R    t   utilsR!   t   utils.compatR"   t   utils.exceptionsR#   t   utils.decoratorsR$   t   excelR%   R3   Rq   R   R   t   contextmanagerR  (    (    (    s8   lib/python2.7/site-packages/astropy/io/fits/hdu/table.pyt   <module>   s@   j i  