
[c           @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d  d l m Z d  d l 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 d d l m Z m Z d d l m Z m Z d d	 l m  Z  m! Z! d d
 l" m# Z# d d l$ m% Z% d e& f d     YZ' d e j( f d     YZ) d   Z* e+ d  Z, d e- f d     YZ. e+ d  Z/ d   Z0 d S(   iN(   t   reduce(   t   chari   (   t
   ASCIITNULLt
   FITS2NUMPYt   ASCII2NUMPYt	   ASCII2STRt   ColDefst   _AsciiColDefst   _FormatXt   _FormatPt   _VLFt
   _get_indext   _wrapxt   _unwrapxt   _makept   Delayed(   t   decode_asciit   encode_asciii   (   t   string_typest   PY2(   t   ranget   zip(   t   lazyproperty(   t   suppresst   FITS_recordc           B   st   e  Z d  Z d d d d d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 e d	    Z d
   Z RS(   s  
    FITS record class.

    `FITS_record` is used to access records of the `FITS_rec` object.
    This will allow us to deal with scaled columns.  It also handles
    conversion/scaling of columns in ASCII tables.  The `FITS_record`
    class expects a `FITS_rec` object as input.
    i    c   
      K   ss   | |  _  | |  _ | r' t |  } n |  j  j } t | | |  j |  }	 |	 \ |  _ |  _ |  _ | |  _	 d S(   s  
        Parameters
        ----------
        input : array
           The array to wrap.

        row : int, optional
           The starting logical row of the array.

        start : int, optional
           The starting column in the row associated with this object.
           Used for subsetting the columns of the `FITS_rec` object.

        end : int, optional
           The ending column in the row associated with this object.
           Used for subsetting the columns of the `FITS_rec` object.
        N(
   t   arrayt   rowt   lent   _nfieldst   slicet   indicest   startt   endt   stept   base(
   t   selft   inputR   R   R    R!   R"   t   kwargst   widtht   s(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   __init__"   s    		c         C   s   t  | t  ra t |  j j |  } | |  j k  sF | |  j d k r t d j |    q nq t  | t	  r t
 |   |  j |  j | j | j | j |   S|  j |  } | |  j j d k r t d   n  |  j j |  |  j S(   Ni   s   Key '{}' does not exist.s   Index out of bounds(   t
   isinstanceR   R   R   t   namesR   R    t   KeyErrort   formatR   t   typeR   t   stopR!   R   t
   IndexErrort   field(   R#   t   keyt   indx(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   __getitem__A   s    "!c         C   s  t  | t  ra t |  j j |  } | |  j k  sF | |  j d k r t d j |    q n t  | t	  r x t
 t	 j t	 j t	 j  D]. } |  j |  } | |  j j |  |  j <q Wn4 |  j |  } | |  j j d k r t d   n  | |  j j |  |  j <d  S(   Ni   s   Key '{}' does not exist.s   Index out of bounds(   R)   R   R   R   R*   R   R    R+   R,   R   R   R.   R!   t	   _get_indxR0   R   R   R/   (   R#   R1   t   valueR2   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   __setitem__R   s    "" c         C   s   t  t |  j |  j |  j   S(   N(   R   R   R   R    R!   (   R#   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   __len__c   s    c         C   sP   g  } x1 t  t |    D] } | j t |  |   q Wd j d j |   S(   s'   
        Display a single row.
        s   ({})s   , (   R   R   t   appendt   reprR,   t   join(   R#   t   outlistt   idx(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   __repr__f   s    c         C   s   |  j  |  S(   s3   
        Get the field data of the record.
        (   R3   (   R#   R0   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR0   p   s    c         C   s   |  j  | |  d S(   s3   
        Set the field data of the record.
        N(   R6   (   R#   R0   R5   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   setfieldw   s    c         C   sB   t  j |   g } |  j } x  | r= | j |  | j } q W| S(   N(   t   weakreft   proxyR"   R8   (   R#   t   basesR"   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   _bases~   s    		c         C   s   t  j |  j j  } xf t |  j  D]U } | j d k  rS t | j d  | j  } n t | j | j
 | j  } | | } q# W| | S(   Ni   (   t   npt   ogridR   R   t   reversedRB   R!   R   R   t   NoneR    (   R#   R2   R   R"   R'   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR      s    N(   t   __name__t
   __module__t   __doc__RF   R(   R3   R6   R7   R=   R0   R>   R   RB   R   (    (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR      s   				
			t   FITS_recc           B   s  e  Z d  Z e Z d   Z d   Z d   Z d   Z d   Z	 e
 d e d   Z d   Z d	   Z d
   Z e r d   Z d   Z n  d d  Z e d    Z e d    Z e j d    Z e j d    Z d   Z e d    Z e d    Z e d    Z d   Z d   Z d   Z d   Z  d   Z! d   Z" d   Z# d   Z$ d   Z% d    Z& d!   Z' e( d"  Z) d#   Z* d$   Z+ RS(%   s   
    FITS record array class.

    `FITS_rec` is the data part of a table HDU's data part.  This is a layer
    over the `~numpy.recarray`, so we can deal with scaled columns.

    It inherits all of the standard methods from `numpy.ndarray`.
    c         C   s   | j  j d k r< t j j |  | j | j  d | j } n0 t j j |  | j | j  d | j d | j } | j	   | j  j
 r t | j  j
  | _ n  | S(   s@   
        Construct a FITS record array from a recarray.
        t   buft   stridesN(   t   dtypet   subdtypeRF   RC   t   recarrayt   __new__t   shapet   dataRL   t   _initt   fieldsR   R   (   t   subtypeR$   R#   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyRP      s    
c         C   sw   | d } | d } | d  } t  t |   j |  t j   |  _ x- t | |  D] \ } } t |  | |  qS Wd  S(   Nii(   t   superRJ   t   __setstate__R?   t   WeakSett   _col_weakrefsR   t   setattr(   R#   t   statet   metat   column_statet   attrR5   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyRW      s    


c      	   C   s   t  t |   j   \ } } } g  } g  } x d d d d d d d d g D]d } t t  P | d k r | j |  j j d	   n | j t	 |  |   | j |  Wd	 QXqI W| | | f } | | | f S(
   s   
        Return a 3-tuple for pickling a FITS_rec. Use the super-class
        functionality but then add in a tuple of FITS_rec-specific
        values that get used in __setstate__.
        t
   _convertedt   _heapoffsett	   _heapsizeR   t   _gapt   _uintt   parnamest   _coldefsN(
   RV   RJ   t
   __reduce__R   t   AttributeErrorR8   Re   t   __deepcopy__RF   t   getattr(   R#   t   reconst_funct   reconst_func_argsR[   R]   R\   t   attrs(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyRf      s    c         C   ss  | d  k r d  St | t  r | j |  j k r | j |  _ | j |  _ | j |  _ | j |  _ | j |  _ | j	 |  _	 | j
 |  _
 | j |  _ n |  j j d  k	 ret |  j j  |  _	 i  |  _ t | d d  |  _ t | d d  |  _ t | d d  |  _
 t | d t  |  _ t j   |  _ t |   |  _ x6 |  j D] } | ` t j |   | _ q@Wn
 |  j   d  S(   NR`   i    Ra   Rb   Rc   (   RF   R)   RJ   RM   R_   R`   Ra   RY   Re   R   Rb   Rc   RT   R   Ri   t   FalseR?   RX   R   R   t   reft   _parent_fits_recRS   (   R#   t   objt   col(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   __array_finalize__   s0    !	c         C   sR   d |  _  i  |  _ d |  _ d |  _ t j   |  _ d |  _ d |  _	 t
 |  _ d S(   s6   Initializes internal attributes specific to FITS-isms.i    N(   R   R_   R`   Ra   R?   RX   RY   RF   Re   Rb   Rm   Rc   (   R#   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyRS      s    						i    c      	   C   s+  t  | t  s t |  } n  xb | D]Z } | j } t  | t  r% | j j d k ra d | _ q t | j j | j  | _ q% q% W| `	 | d k r xK | j	 D]= } | d k	 r | j
 d } n d } | | k r | } q q Wn  t j | j j | d t j } | j t | j   t j | d | j d | j |   } | j |  | | _ | rb| Sxt |  D]\ }	 } | j } | d k rd }
 n t |  }
 t |
 |  } | sqon  t | |	  } | j } | j } | j } | |  } | |  } t  | t  r?| j
 d | j k rt | | | j  qoqnt  | t   rv| j! | t" | | | d |  qon| d t# d k r| j t$ k rt d  | (t j% | j
 d t$ } | | | *| j! | |  t j& | t j' k t d  t d	   } n| |	 j( r| |	 j) r| j* } t j% | j
 d | j } | | | *| j! | |  | | k  rw| | | )n  | | } n t  | t+  r| j, r| j t j t j-  |  } nH | j j. d k r| j! | t j% | d | j  | j/ | |  } n  | | (qon  | j
 | j
 k r| j j. | j j. k r| j j. d k r| j | j k r| d j0 } | j1   j | j  } n  | j2 d k r| d j0 } | j3 | | f  } | | d d  d |  f <q| j4   | | *qo| | (qoWx0 t5 t |   D] }	 | j |	  | j	 |	 <qW| S(   s  
        Given a `ColDefs` object of unknown origin, initialize a new `FITS_rec`
        object.

        .. note::

            This was originally part of the ``new_table`` function in the table
            module but was moved into a class method since most of its
            functionality always had more to do with initializing a `FITS_rec`
            object than anything else, and much of it also overlapped with
            ``FITS_rec._scale_back``.

        Parameters
        ----------
        columns : sequence of `Column` or a `ColDefs`
            The columns from which to create the table data.  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.

        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.
        i    RM   RK   it   nrowsit   Lt   Ft   Tt   St   Ui   N(   Rw   Rx   (   Rx   Rw   (6   R)   R   R   R   t   hduRR   RF   t   _get_recarray_fieldR0   t   _arraysRQ   RC   t   emptyRM   t   itemsizet   uint8t   fillt   ordt   _padding_byteRO   t   viewt   _add_listenerRe   t	   enumerateR   t   mint   nameR,   t	   recformatR   t   repeatR   R	   t   _cache_fieldR   R   t   boolt   zerost   wheret   False_t   _physical_valuest   _pseudo_unsigned_intst   bzeroR   t   _pseudo_logicalt   ndarrayt   kindR_   t   sizet   flattent   ndimt   reshapet   ravelR   (   t   clst   columnsRs   R   t   columnt   arrt   dimt   raw_dataRR   R<   t
   array_sizet   nR0   R   t
   fitsformatR   t   outarrt   inarrt	   convertedR   t   inarr_rowsize(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   from_columns  s    "	"'							

	
#
-	
	"c         C   s   t  j j |   S(   N(   RC   R   R=   (   R#   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR=     s    c         C   sA  |  j  d  k r% t t |   j |  St | t  rA |  j |  S|  j t	 j
  | } t |  t	 j
 k	 r| |  j |  |  S| j t |    } t |  j   | _  g  } i  | _ x| t |  j  j  D]h \ } } | j |  j  j | |  | |  j k r |  j | } t	 j j | |  } | | j | <q q W| | j  _ | S(   N(   Re   RF   RV   RJ   R3   R)   R   R0   R   RC   RO   R-   t   _record_typeR   R_   R   R*   R8   R{   R   (   R#   R1   t   outt   arraysR<   R   t   dummyR0   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR3     s&    	c         C   s  |  j  d  k r( t t |   j | |  St | t  rF | |  | (d  St | t  r t t	 |   | j
 ps t	 |    } t d |  } t d | j p d  } t | | t	 |   } x/ t | |  D] } |  j | | | |  q Wd  St | t  rEx t |  j  D]0 } | j |  j |  |  j |  j |  | <qWn t | t t t j f  r|  j t	 |  k rxI t |  j  D] } | | |  j |  | <qWqt d j |  j    n t d   d  S(   Ni    s1   Input tuple or list required to have {} elements.s;   Assignment requires a FITS_record, tuple, or list as input.(   Re   RF   RV   RJ   R6   R)   R   R   R   R   R.   t   maxR   R   R   R   R0   R*   t   tuplet   listRC   t   voidt
   ValueErrorR,   t	   TypeError(   R#   R1   R5   R    R   R<   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR6     s.    $1	c         C   s   |  j  t | |   S(   N(   R3   R   (   R#   R   R    (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   __getslice__(  s    c         C   s   |  j  t | |  |  d  S(   N(   R6   R   (   R#   R   R    R5   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   __setslice__+  s    t   Cc         C   s4   t  t |   j d |  } t j |  j  | _ | S(   s  
        The Numpy documentation lies; `numpy.ndarray.copy` is not equivalent to
        `numpy.copy`.  Differences include that it re-views the copied array as
        self's ndarray subclass, as though it were taking a slice; this means
        ``__array_finalize__`` is called and the copy shares all the array
        attributes (including ``._converted``!).  So we need to make a deep
        copy of all those attributes so that the two arrays truly do not share
        any data.
        t   order(   RV   RJ   t   copyt   deepcopyt   __dict__(   R#   R   t   new(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR   .  s    c         C   s   |  j  S(   sx   
        A user-visible accessor for the coldefs.

        See https://aeon.stsci.edu/ssb/trac/pyfits/ticket/44
        (   Re   (   R#   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR   >  s    c         C   s   |  j  j d  S(   NRe   (   R   t   get(   R#   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyRe   H  s    c         C   sG   | |  j  d <t | t  rC x$ | j D] } |  j j |  q& Wn  d  S(   NRe   (   R   R)   R   R   RY   t   add(   R#   t   colsRq   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyRe   \  s    c         C   s;   y |  j  d =Wn& t k
 r6 } t | j d   n Xd  S(   NRe   i    (   R   R+   Rg   t   args(   R#   t   exc(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyRe   c  s    c         C   su   yW |  `  |  j j d  k	 rV x8 |  j D]* } | j d  k	 r% | j j   | _ q% q% Wn  Wn t t f k
 rp n Xd  S(   N(	   Re   RM   RT   RF   RY   R   R   Rg   R   (   R#   Rq   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   __del__j  s     c         C   sF   |  j  j r t |  j  j  St |  d d  d k	 r> |  j j Sd Sd S(   s   List of column names.Re   N(   RM   RT   R   R*   Ri   RF   Re   (   R#   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR*   w  s
    
c         C   s&   t  |  d d  d k	 r" |  j j Sd S(   s   List of column FITS formats.Re   N(   Ri   RF   Re   t   formats(   R#   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR     s    
c         C   su   t  |   rj d } xQ |  j j j   D]= } | d j } | d j d k rX | d } n  | | 7} q% W| S|  j Sd S(   s   
        Returns the size of row items that would be written to the raw FITS
        file, taking into account the possibility of unicode columns being
        compactified.

        Currently for internal use only.
        i    Rx   i   N(   t   _has_unicode_fieldsRM   RT   t   valuesR}   R   (   R#   t   total_itemsizeR0   R}   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   _raw_itemsize  s    
c   	      C   s  |  j  | } | j } | j } | j j d k r] t j d j |   t j g  d | j S|  } x1 t	 | t
  r t	 | j t j  r | j } qf Wt | |  } | |  j k r| j } t	 | t  r |  j | | |  } n |  j | | |  } |  j | |  | S|  j | S(   s:   
        A view of a `Column`'s data as an array.
        i    sQ   Field {!r} has a repeat count of 0 in its format code, indicating an empty field.RM   (   R   R   R,   RM   R}   t   warningst   warnRC   R   R)   RJ   R"   RO   Rz   R_   R   R	   t
   _convert_pt   _convert_otherR   (	   R#   R1   R   R   R,   R"   R0   R   R   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR0     s*    		
	c         C   s   | } xw t  r |  } x? t  rV | | k r. d St | d d  d k	 rR | j } q Pq Wt | d d  d k	 r{ | j } q	 Pq	 W| |  j | <d S(   s  
        Do not store fields in _converted if one of its bases is self,
        or if it has a common base with self.

        This results in a reference cycle that cannot be broken since
        ndarrays do not participate in cyclic garbage collection.
        NR"   (   t   TrueRi   RF   R"   R_   (   R#   R   R0   R"   t	   self_base(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR     s    			c         C   sA   d j  |  } t |  |  r= t |  |  | | | |  n  d S(   s  
        Update how the data is formatted depending on changes to column
        attributes initiated by the user through the `Column` interface.

        Dispatches column attribute change notifications to individual methods
        for each attribute ``_update_column_<attr>``
        s   _update_column_{0}N(   R,   t   hasattrRi   (   R#   R   R<   R^   t	   old_valuet	   new_valuet   method_name(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt    _update_column_attribute_changed  s    
c         C   s3   |  j  } | j |  | f | j | d | _ d S(   s;   Update the dtype field names when a column name is changed.i   N(   RM   R*   (   R#   R   R<   t   old_nameR   RM   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   _update_column_name  s    	c         C   s<   t  j |  j | j f d t  j } t | | | j  | S(   s]   Convert a raw table column to a bit array as specified by the
        FITS X format.
        RM   (   RC   R   RQ   R   t   bool_R   (   R#   R0   R   R   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt
   _convert_x  s    %c         C   s  t  d g t |   d | j } |  j   } | d k rU t d j | j    n  xAt t |    D]-} | | d f |  j	 } | | d f } | j d k rt
 j | j t d   }	 | |	 j }
 | | | |
 !j |	  } t
 j j | j d |	  d | } t |  | | <qh t
 j | j  }	 | |	 j }
 | | | |
 !j |	  | | <| | j j d  | | _ |  j | | | |  | | <qh W| S(	   s   Convert a raw table column of FITS P or Q format descriptors
        to a VLA column with the array data returned from the heap.
        RM   sC   Could not find heap data for the {!r} variable-length array column.i   i    t   aR}   t   >N(   R
   RF   R   RM   t   _get_raw_datat   IOErrorR,   R   R   R`   RC   t   strR}   R   R   R   R   t   newbyteorderR   (   R#   R   R0   R   R   R   R<   t   offsett   countt   dtt   arr_lent   da(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR     s,    "$
c   
      C   su  | j  } t | d } t | j  j   j d  } t |  | j k rZ | | j  } n  t j	 j
 | | j  } t j	 j | t d  t d   } t t t  j | j   } t j t j	 j |  | k | |  } | d k rt j t j	 j |  d k | |  } n  y t j | d | } WnD t k
 rp} |  j j | j  }	 t d j  | |	 d    n X| S(	   s   
        Special handling for ASCII table columns to convert columns containing
        numeric types to actual numeric arrays from the string representation.
        i    t   asciit   Dt   Et    RM   s^   {}; the header may be missing the necessary TNULL{} keyword or the table contains invalid datai   (   R,   R   R   t   nullt   stript   encodeR   R&   RC   R   t   ljustt   replaceR   R   t   rjustR   R   R   R*   t   indexR   (
   R#   R   R0   R,   R   t   nullvalR   t	   null_fillR   R2   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   _convert_ascii9  s&    	$'*c         C   sM  t  | t  r |  j | |  S|  j |  \ } } } } } }	 }
 } |  j j | j  } | r t  |  j t  r |  j	 | |  } n  | r| j
 d k r | j d } | r | | j f } q n | j d } | | k r d } qt t j |  } | r| j } n+ t | j  d k r+d } n | j d } | | k rt j d j | d |  j | j | | d   d } qn  | j r| j j r| j j } n | j j } | rS| s| rS| j rS|  j r|
 d k r
| d k r
t j | d t j } q|
 d k r=| d	 k r=t j | d t j } q|
 d k r| d k rt j | d t j } t j d  } qt j | d t j } n t j | d t j } | rt j  | |	 |  n  | rG|  j r:| d k r:| j!   } y | | 7} Wn+ t" k
 r0t j d j | d   qDX| } qG| |
 7} n  t# | _ nX | r| j$ t% k rt j& | t' d   } n( | rt( t)   t* |  } Wd QXn  | rIt t j |  } | j
 d k r| d d  d |  f } n  | r/| j$ j+ } d j | | d  | d  f } | | _$ qI| j d f | | _ n  | S(   s   Perform conversions on any other fixed-width column data types.

        This may not perform any conversion at all if it's not necessary, in
        which case the original column array is returned.
        i   i    sb   TDIM{} value {:d} does not fit with the size of the array items ({:d}).  TDIM{:d} will be ignored.i   i   t   IRM   i   t   Ji?   t   KsE   Overflow detected while applying TZERO{0:d}. Returning unscaled data.Rv   Ns   |{}{}ii   I       l            l            (,   R)   R   R   t   _get_scale_factorsR*   R   R   Re   R   R   R   RQ   R}   RF   R    t   operatort   mulR   R   R   R,   t   dimsR   t   p_formatR   Rc   RC   R   t   uint16t   uint32t   uint64t   float64t   multiplyR   t   OverflowErrorR   RM   R   t   equalR   R   t   UnicodeDecodeErrorR   R   (   R#   R   R0   R   t   _strt   _boolt   _numbert   _scalet   _zerot   bscaleR   R   R2   t   actual_shapet   nitemst   actual_nitemst   format_codet   bzero64t   test_overflowt   fmtRM   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR   b  s    '					 c         C   sY   |  j  r? |  j   j t j  } |  j |  j  } | |  j | !St j g  d t j Sd S(   s   
        Returns a pointer into the table's raw data to its heap (if present).

        This is returned as a numpy byte array.
        RM   N(   Ra   R   R   RC   t   ubyteR`   R   (   R#   R   t   heap_end(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   _get_heap_data  s
    	c         C   sj   |  j  |  j } |  } xM t | d  re | j d k	 re | j } t | d  r | j  | k r | Sq Wd S(   s  
        Returns the base array of self that "raw data array" that is the
        array in the format that it was first read from a file before it was
        sliced or viewed as a different type in any way.

        This is determined by walking through the bases until finding one that
        has at least the same number of bytes as self, plus the heapsize.  This
        may be the immediate .base but is not always.  This is used primarily
        for variable-length array support which needs to be able to find the
        heap (the raw data *may* be larger than nbytes + heapsize if it
        contains a gap or padding).

        May return ``None`` if no array resembling the "raw data" according to
        the stated criteria can be found.
        R"   t   nbytesN(   R  Ra   R   R"   RF   (   R#   t   raw_data_bytesR"   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR     s    !	c   
      C   s   | j  j  d k } | j  j  d k } | p- | } | j } | j } | d k } | d k } | sj d } n  | sy d } n  | j }	 | | | | | | | |	 f S(	   s5   Get all the scaling flags and factors for one column.t   ARt   R   i   i    N(   R   Ni   (   R   Ni    (   R,   R  R   RF   t   _dims(
   R#   R   R   R   R   R  R   R   R   R   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR   
  s    					c      	   C   s  d } xt  |  j j  D]t\ } } |  j | } | j j } t |  |  } t | t  rVt	 j
 g  d | j j } | r,| |  j k r,d | (g  |  j | D] }	 t |	  ^ q }
 |
 | d t |
   d f <t	 j j | d d  d f  | j | d d  d f <| d d  d f | 7(n  | | d d  d f j   | j 7} n  t | t  r| |  j k rt |  j | | | j  q n  |  j |  \ } } } } } } } } |  j j | |  } | s| r| r5| s| r5| j r5| j   } | r| | 8} n  | r)| | :} n  t | _ n$ | sMt |  j t  r | } n q t |  j t  r|  j | | |  n t | t j  r|  j | | |  ni t |  rt | d t	 j  rt	 j |  } n  | j | j k r| | (n | j    j! | j  | (~ q | r | |  j k r t	 j
 t" d  g d t	 j# d t	 j
 t" d  g d t	 j# d f } t	 j$ | |  | (q q W| |  _% d S(   s	  
        Update the parent array, using the (latest) scaled array.

        If ``update_heap_pointers`` is `False`, this will leave all the heap
        pointers in P/Q columns as they are verbatim--it only makes sense to do
        this if there is already data on the heap and it can be guaranteed that
        that data has not been modified, and there is not new data to add to
        the heap.  Currently this is only used as an optimization for
        CompImageHDU that does its own handling of the heap.
        i    RM   Nii   Ru   Rv   (&   R   RM   R*   Re   R,   R   Rz   R)   R	   RC   R   R_   R   R   t
   accumulateR}   t   sumR   R   R   R   R   R   R   Rm   R   t   _scale_back_asciit	   chararrayt   _scale_back_stringst   integert   aroundRQ   R   R   R   t   int8t   chooseRa   (   R#   t   update_heap_pointerst   heapsizeR2   R   R   R   t	   raw_fieldRM   R   t   nptsR   R   R   R   R   R  R   t   _R0   R   t   choices(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   _scale_back%  s^    &*'	
"(c         C   s   | j  j d k r | j  j d k r y t | d | Wq t k
 r } t d j |  j | j t | j	  d k r | j	 d n | j	 | j
    q Xn" | j   j | j   } | | j (t |  d  S(   NRx   Rw   R   s   Could not save column '{0}': Contains characters that cannot be encoded as ASCII as required by FITS, starting at the index {1!r} of the column, and the index {2} of the string at that location.i   i    (   RM   R   t   _ascii_encodet   _UnicodeArrayEncodeErrorR   R,   Re   R   R   R   R   R   R   t   flatt   _rstrip_inplace(   R#   t   col_idxt   input_fieldt   output_fieldR   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR    s    $(
c         C   s9  |  j  j } |  j  j } |  j  | j } t t |   j d  j } | j | | d  | d k r | | | | d | | d } n d } | d k  r t	 j
 d j | d   n  | | d | | | | }	 |	 d k  r t	 j
 d j | d   n  | GHd | k rd }
 n d }
 d	 j |
 | d t | d d
 d |	 g  } | j d k oi| j d k } x t |  D] \ } } | j |  } t |  | | d | | k rt d j | | |    n  | r| d d k r| d d } n  | | | <qyWd | k r5| j t d  t d   n  d S(   s  
        Convert internal array values back to ASCII table representation.

        The ``input_field`` is the internal representation of the values, and
        the ``output_field`` is the character array representing the ASCII
        output that will be written.
        ii    i   s8   Column {!r} starting point overlaps the previous column.s2   Column {!r} ending point overlaps the next column.R  s   {:s   {:>R   t   }t    Ru   R   R   s9   Value {!r} does not fit into the output's itemsize of {}.t   .N(   Ru   R   R   (   Re   t   startst   spansR,   RV   RJ   R0   R}   R8   R   R   R:   R   t	   precisionR   R   R   R   R   (   R#   R$  R%  R&  R*  R+  R,   R    t   leadt   trailt   _pcR  t   trailing_decimalt   jdxR5   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR    sD    	%	"(,   RG   RH   RI   R   R   RP   RW   Rf   Rr   RS   t   classmethodRm   R   R=   R3   R6   R   R   R   R   t   propertyR   Re   t   settert   deleterR   R*   R   R   R0   R   R   R   R   R   R   R   R  R   R   R   R  R  R  (    (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyRJ      sL   				%			&	 	
			2						*	)				f	/c         C   sS   t  j j |  |  } | j j d k rO t | t j  rO | j t j  } n  | S(   s   
    Compatibility function for using the recarray base class's field method.
    This incorporates the legacy functionality of returning string arrays as
    Numeric-style chararray objects.
    Rw   Rx   (   Rw   Rx   (   RC   RO   R0   RM   R   R)   R  R   (   R   R1   R0   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyRz     s
    
c         C   sI   xB t  j |  d d g d d g D] } | j   j |  | d <q" Wd S(   s  
    Performs an in-place rstrip operation on string arrays.
    This is necessary since the built-in `np.char.rstrip` in Numpy does not
    perform an in-place calculation.  This can be removed if ever
    https://github.com/numpy/numpy/issues/6303 is implemented (however, for
    the purposes of this module the only in-place vectorized string functions
    we need are rstrip and encode).
    t   flagst   zerosize_okt   op_flagst	   readwrite.N(   RC   t   nditert   itemt   rstrip(   R   t   charsR;  (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR#    s    
R!  c           B   s   e  Z d    Z RS(   c         C   s/   t  t |   j | | | | |  | |  _ d  S(   N(   RV   R!  R(   R   (   R#   t   encodingt   object_R   R    t   reasonR   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR(   #  s    (   RG   RH   R(   (    (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR!  "  s   c   
      C   s  t  j d j |  j j d  |  j j f  } | d k	 rL | j |  } n  |  j | g } d g d d g g } t  j |  | g d | d | d d	 g } y4 x- | D]% \ } } | j   j	 d
  | d <q WWnA t
 k
 r} t  j | j |  j  }	 t | j |	 f    n X| j d S(   s[  
    Takes a unicode array and fills the output string array with the ASCII
    encodings (if possible) of the elements of the input array.  The two arrays
    must be the same size (though not necessarily the same shape).

    This is like an inplace version of `np.char.encode` though simpler since
    it's only limited to ASCII, and hence the size of each character is
    guaranteed to be 1 byte.

    If any strings are non-ASCII an UnicodeArrayEncodeError is raised--this is
    just a `UnicodeEncodeError` with an additional attribute for the index of
    the item that couldn't be encoded.
    s   S{0}i   t   readonlyt	   writeonlyt   allocatet	   op_dtypesR8  R6  R7  R   .i   N(   RC   RM   R,   R}   RQ   RF   R   R:  R;  R   t   UnicodeEncodeErrort   unravel_indext	   iterindexR!  R   t   operands(
   t   inarrayR   t	   out_dtypeRD  R8  t   itt   initemt   outitemR   R   (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR   )  s    !c         C   s0   d   |  j  j j   D } t d   | D  S(   sN   
    Returns True if any fields in a structured array have Unicode dtype.
    c         s   s   |  ] } | d  Vq d S(   i    N(    (   t   .0t   d(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pys	   <genexpr>Q  s    c         s   s   |  ] } | j  d  k Vq d S(   Rx   N(   R   (   RN  RO  (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pys	   <genexpr>R  s    (   RM   RT   R   t   any(   R   t   dtypes(    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyR   L  s    (1   R   R   R   R?   t	   functoolsR    t   numpyRC   R   R  R   R   R   R   R   R   R   R   R	   R
   R   R   R   R   R   t   utilR   R   t
   extern.sixR   R   t   extern.six.movesR   R   t   utilsR   t   utils.compatR   t   objectR   RO   RJ   Rz   RF   R#  RE  R!  R   R   (    (    (    s6   lib/python2.7/site-packages/astropy/io/fits/fitsrec.pyt   <module>   s.   ^z    t	#