ó
šßÈ[c           @   s  d  d l  Z  d  d l Z d d l 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 d d	 l m Z m Z m Z d
 d l m Z d
 d l m Z m Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e e f d „  ƒ  YZ d „  Z d „  Z d S(   iÿÿÿÿNi   (   t   DTYPE2BITPIX(   t
   PrimaryHDU(   t   _TableLikeHDUi   (   t   Columnt   ColDefst
   FITS2NUMPY(   t   FITS_rect   FITS_record(   t   _is_intt   _is_pseudo_unsignedt   _unsigned_zeroi   (   t   lazyproperty(   t   ranget   zipt   Groupc           B   se   e  Z d  Z d d d d d d „ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z	 d „  Z
 d „  Z RS(	   s-   
    One group of the random group data.
    i    c         C   s)   t  t |  ƒ j | | | | | | ƒ d  S(   N(   t   superR   t   __init__(   t   selft   inputt   rowt   startt   endt   stept   base(    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR      s    c         C   s
   |  j  j S(   N(   t   arrayt   parnames(   R   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR      s    c         C   s   |  j  |  j j j d ƒ S(   Niÿÿÿÿ(   t   fieldR   t   _coldefst   names(   R   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   data   s    c         C   s   t  |  j ƒ S(   N(   t   _par_indicesR   (   R   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   _unique#   s    c         C   sµ   t  | ƒ r# |  j |  j | } nŽ |  j | j ƒ  } t | ƒ d k rc |  j |  j | d } nN |  j |  j | d j d ƒ } x* | d D] } | |  j |  j | 7} q W| S(   s0   
        Get the group parameter value.
        i   i    t   f8(   R   R   R   R   t   uppert   lent   astype(   R   t   parnamet   resultt   indxt   i(    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   par'   s    !c         C   së   t  | ƒ r# | |  j |  j | <nÄ |  j | j ƒ  } t | ƒ d k rc | |  j |  j | d <n„ t | t t f ƒ rÌ t | ƒ t | ƒ k rÌ xT t	 t | ƒ ƒ D]" } | | |  j |  j | | <q£ Wn t
 d j t | ƒ ƒ ƒ ‚ d S(   s0   
        Set the group parameter value.
        i   i    s:   Parameter value must be a sequence with {} arrays/numbers.N(   R   R   R   R   R!   R"   t
   isinstancet   listt   tupleR   t
   ValueErrort   format(   R   R$   t   valueR&   R'   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   setpar;   s    
#	N(   t   __name__t
   __module__t   __doc__t   NoneR   t   propertyR   R   R   R   R(   R/   (    (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR      s   	t	   GroupDatac        	   B   sn   e  Z d  Z e Z d d d g  d d d d d „ Z d „  Z d „  Z e	 d „  ƒ Z
 e d „  ƒ Z d „  Z RS(   sz   
    Random groups data object.

    Allows structured access to FITS Group data in a manner analogous
    to tables.
    c	         C   s3  t  | t ƒ s| d k r$ d }	 n t | ƒ }	 | d k rL d g |	 } n  | d k rh d g |	 } n  | d k r£ g  t |	 ƒ D] }
 d j |
 d ƒ ^ q } n  t | ƒ |	 k rÄ t d ƒ ‚ n  t | d g ƒ } | d k rö t | j	 j
 } n  t j | } t | } d j t | j d ƒ | ƒ } d j | g |	 | g ƒ } | j d } g  t |	 ƒ D]3 }
 t d | |
 d	 | d
 | |
 d | |
 ƒ ^ qc} | j t d | d d	 | d
 | d | ƒ ƒ t | ƒ } t j |  t j j d d | d | j d | ƒƒ } | d | _ | | _ | | _ x„ t | d  ƒ D]r \ }
 } | |
 } | j | ƒ d d !\ } } | su| rŒ| j | | |
 ƒ q7| |
 t j j j  | |
 ƒ (q7W| | j } | j | ƒ d d !\ } } | sâ| rø| j | j | ƒ q/| t j j j  | |	 ƒ (n t j |  | ƒ } d | _ | S(   s›  
        Parameters
        ----------
        input : array or FITS_rec instance
            input data, either the group data itself (a
            `numpy.ndarray`) or a record array (`FITS_rec`) which will
            contain both group parameter info and the data.  The rest
            of the arguments are used only for the first case.

        bitpix : int
            data type as expressed in FITS ``BITPIX`` value (8, 16, 32,
            64, -32, or -64)

        pardata : sequence of arrays
            parameter data, as a list of (numeric) arrays.

        parnames : sequence of str
            list of parameter names.

        bscale : int
            ``BSCALE`` of the data

        bzero : int
            ``BZERO`` of the data

        parbscales : sequence of int
            list of bscales for the parameters

        parbzeros : sequence of int
            list of bzeros for the parameters
        i    s   PAR{}i   sL   The number of parameter data arrays does not match the number of parameters.t   DATAs   {}{}t   ,t   nameR-   t   bscalet   bzeroiÿÿÿÿt   formatsR   t   shapei   i   N(!   R)   R   R3   R"   R   R-   R,   t   _unique_parnamesR    t   dtypeR8   t	   GroupsHDUt   _bitpix2tformR   t   strR<   t   joinR   t   appendR   t   __new__t   npt   recR   R   t   _data_fieldR   R   t	   enumeratet   _get_scale_factorst   _cache_fieldt   recarrayR   (   t   clsR   t   bitpixt   pardataR   R9   R:   t
   parbscalest	   parbzerost   nparst   idxt   unique_parnamest   fits_fmtR-   t   data_fmtR;   t   gcountt   colst   coldefsR   R8   t   columnt   scalet   zero(    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyRD   b   s^    "	/
C				
!	c         C   sY   t  t |  ƒ j | ƒ t | t ƒ r4 | j |  _ n! t | t ƒ rU | j j |  _ n  d  S(   N(   R   R5   t   __array_finalize__R)   R   R   R   R   (   R   t   obj(    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR\   Í   s
    c         C   s:   t  t |  ƒ j | ƒ } t | t ƒ r6 |  j | _ n  | S(   N(   R   R5   t   __getitem__R)   R   (   R   t   keyt   out(    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR^   Ô   s    c         C   s   |  j  |  j j d ƒ S(   sf   
        The raw group data represented as a multi-dimensional `numpy.ndarray`
        array.
        iÿÿÿÿ(   R   R   R   (   R   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR   Ú   s    c         C   s   t  |  j ƒ S(   N(   R   R   (   R   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR   ä   s    c         C   s¡   t  | ƒ r |  j | ƒ } n |  j | j ƒ  } t | ƒ d k rY |  j | d ƒ } nD |  j | d ƒ j d ƒ } x% | d D] } | |  j | ƒ 7} q€ W| S(   s1   
        Get the group parameter values.
        i   i    R    (   R   R   R   R!   R"   R#   (   R   R$   R%   R&   R'   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR(   è   s    N(   R0   R1   R2   R   t   _record_typeR3   RD   R\   R^   R4   R   R   R   R(   (    (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR5   X   s   j		
R?   c           B   s  e  Z d  Z i d d 6d d 6d d 6d d 6d	 d
 6d d 6Z e Z d Z d d d „ Z e	 d „  ƒ Z
 e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z d d „ Z d „  Z d „  Z RS(   s¥   
    FITS Random Groups HDU class.

    See the :ref:`random-groups` section in the Astropy documentation for more
    details on working with this type of HDU.
    t   Bi   t   Ii   t   Ji    t   Ki@   t   Eiàÿÿÿt   DiÀÿÿÿR6   c         C   sm   t  t |  ƒ j d | d | ƒ t |  j ƒ d k ri d g |  _ d |  j d <|  j j d d d d ƒn  d  S(   NR   t   headeri    i   t   NAXISt   NAXIS1t   after(   R   R?   R   R"   t   _axest   _headert   set(   R   R   Rh   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR     s
    c         C   s6   | j  d j } | d k o5 d | k o5 | d t k S(   Ni    t   SIMPLEt   GROUPS(   t   cardst   keywordt   True(   RL   Rh   Rr   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   match_header  s    c         C   s.   |  j  ƒ  } |  j | _ |  j | _ |  ` | S(   sb   
        The data of a random group FITS file will be like a binary table's
        data.
        (   t   _get_tbdatat   columnsR   R   (   R   R   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR     s
    c         C   s?   |  j  d } g  t | ƒ D]! } |  j  d t | d ƒ ^ q S(   s=   The names of the group parameters as described by the header.t   PCOUNTt   PTYPEi   (   Rm   R   RA   (   R   t   pcountRR   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR   *  s    c         C   s  |  j  r% t |  j d ƒ r% |  j j S|  j |  j d } |  j d } g  } g  } g  } xŠ t | ƒ D]| } | j |  j j d t	 | d ƒ d  ƒ ƒ | j |  j j d t	 | d ƒ d  ƒ ƒ | j |  j d t	 | d ƒ ƒ qe W| g t | ƒ } d  g t | ƒ } | j d ƒ | j |  j j d	 ƒ ƒ | j |  j j d
 ƒ ƒ |  j d  }	 | j t	 t t j |	 ƒ ƒ ƒ | ƒ | j |	 ƒ t | ƒ } | d |  _ g  t | | | | | ƒ D]< \ }
 } } } } t d |
 d | d | d | d | ƒ ^ q¼} t | ƒ } | S(   NR   t   BITPIXRw   t   PSCALi   t   PZERORx   R6   t   BSCALEt   BZEROSiÿÿÿÿR8   R-   R9   R:   t   dim(   t	   _has_datat   hasattrR   R   R@   Rm   R   RC   t   getRA   R3   R"   R<   t   intRE   t   prodR=   RG   R   R   R   (   R   R-   Ry   R   t   bscalest   bzerosRR   R;   R   t
   data_shapeR8   t   fmtR9   R:   RW   RX   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyRv   3  s2    
**&&Xc         C   s-   |  j  s |  j j d d ƒ St |  j ƒ Sd  S(   Nt   GCOUNTi    (   t   _data_loadedRm   R‚   R"   R   (   R   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   _nrowsZ  s    	c         C   s   d S(   Ni    (    (   R   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   _theapc  s    c         C   s   t  S(   N(   t   False(   R   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   is_imageh  s    c         C   sÀ   d } |  j  j d d ƒ } | d k r¼ d } x6 t d | ƒ D]% } | |  j  d t | d ƒ } q= W|  j  d } |  j  j d d ƒ } |  j  j d d ƒ } t | ƒ | | | d } n  | S(   sE   
        Returns the size (in bytes) of the HDU's data part.
        i    Ri   i   Rz   R‰   Rw   i   (   Rm   R‚   R   RA   t   abs(   R   t   sizet   naxisRR   RM   RV   Ry   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR   l  s    #c         C   sä  |  j  j d d ƒ } |  j rà t |  j t ƒ rœ t |  j j j ƒ d |  _ |  j j	 ƒ  d g |  j |  _ |  j j
 j d } |  j j
 j | d j } n0 |  j d  k rÀ d g |  _ d } n t d ƒ ‚ t | |  j  d <n  t |  j ƒ |  j  d <xi t |  j ƒ D]X \ } } | d k r'd } n d t | ƒ } |  j  j d t | d ƒ | d | ƒqWxT t t |  j ƒ d | d ƒ D]2 } y |  j  d t | ƒ =Wqƒt k
 r´qƒXqƒW|  j r‡t |  j t ƒ r‡|  j  j d t d d t t |  j ƒ ƒ ƒ|  j  j d	 t |  j j ƒ d d ƒ|  j  j d
 t |  j ƒ d d	 ƒ|  j j |  j } |  j j | ƒ d d !\ } }	 | r˜|  j  j d | j ƒ n  |	 r·|  j  j d | j ƒ n  xÍ t |  j j ƒ D]¶ \ } }
 |  j  j d t | d ƒ |
 ƒ |  j j | } |  j j | ƒ d d !\ } }	 | rS|  j  j d t | d ƒ | j ƒ n  |	 rÊ|  j  j d t | d ƒ | j ƒ qÊqÊWn  d |  j  k ràt |  j ƒ rÁd t t |  j ƒ ƒ } n d } |  j  j d d | ƒn  d  S(   NRi   i    i   t   uint8s   incorrect array typeRz   Rk   Rp   Rw   R‰   i   i   R}   t   BZERORx   R{   R|   t   EXTEND(   Rm   R‚   RŠ   R)   R   R5   R*   R<   Rl   t   reverseR>   R   t   fieldsR8   R3   R,   R    R"   RH   RA   Rn   R   t   KeyErrorR€   Rs   R   R   RG   RI   R9   R:   (   R   t	   old_naxist   field0t   field0_codeRR   t   axisRk   RY   RZ   R[   R8   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   update_header€  s`    			+'%"!'.c         C   sl  d } |  j  d k	 rh|  j  j ƒ  t j d k r: d } n d	 } t |  j  j ƒ r• t j |  j  t	 |  j  j ƒ d d j
 |  j  j j ƒ ƒ} t } nF |  j  } |  j  j j d } |  j  j j | d j d } | | k } | j sQ| rA| j j r(| j t ƒ z | j | ƒ Wd | j t ƒ XqN| j | j t ƒ ƒ qQ| j | ƒ n  | | j | j 7} n  | S(
   s,  
        Basically copy/pasted from `_ImageBaseHDU._writedata_internal()`, but
        we have to get the data's byte order a different way...

        TODO: Might be nice to store some indication of the data's byte order
        as an attribute or function so that we don't have to do this.
        i    t   littlet   <t   =R>   s   >i{}N(   Rž   RŸ   (   Rž   (   R   R3   t   _scale_backt   syst	   byteorderR	   R>   RE   R   R
   R-   t   itemsizeR   R   R–   RA   t   simulateonlyt   flagst	   writeablet   byteswapRs   t
   writearrayR   (   R   t   fileobjR   t
   swap_typest   outputt   should_swapt   fnameR¢   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   _writedata_internalÂ  s4    					t   warnc            sÔ   t  t |  ƒ j d | ƒ } |  j d d d „  d | | ƒ |  j d d d „  d	 | | ƒ |  j d d ‰  ‡  f d
 †  } |  j d | t d | | ƒ |  j d | t d	 | | ƒ |  j d | d „  t | | ƒ | S(   Nt   optionRi   i   c         S   s"   t  |  ƒ o! |  d k o! |  d k S(   Ni   iç  (   R   (   t   v(    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   <lambda>û  s    i   Rj   i   c         S   s   t  |  ƒ o |  d k S(   Ni    (   R   (   R±   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR²   ý  s    i    c            s
   |  ˆ  k S(   N(    (   t   x(   Rk   (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR²     s    R‰   Rw   Rp   c         S   s
   |  t  k S(   N(   Rs   (   R±   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR²     s    (   R   R?   t   _verifyt	   req_cardsRm   R   Rs   (   R   R°   t   errst   pos(    (   Rk   s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR´   ö  s    	

c         C   s=  |  j  r |  j j j |  j j j d d j d } | d k r¯ |  j j j r t } |  j j	 t ƒ } | j j
 d ƒ | _ q¾ |  j j	 t ƒ } | j j
 d ƒ | _ t } n t } |  j } | j d t j d t j ƒ } |  j | d | ƒ} | r| j	 t ƒ | j j
 d ƒ | _ n  | St t |  ƒ j d | ƒ Sd S(   sJ   
        Calculate the value for the ``DATASUM`` card in the HDU.
        i    t   >t   typeR>   t   blockingRž   N(   R€   R   R>   R–   R   RA   R¥   R¦   Rs   R§   t   newbyteorderR   t   viewRE   t   ndarrayt   ubytet   _compute_checksumR   R?   t   _calculate_datasum(   R   Rº   R¢   t   byteswappedt   dt	   byte_datat   cs(    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyRÀ   	  s&    		+		c   	      C   sœ   t  t |  ƒ j ƒ  } | \ } } } } } } } | rk | d } | rk t | ƒ rk |  j d j j } qk n  d j |  j |  j	 ƒ } | | | | | | | f S(   Ni   i    s   {} Groups  {} Parameters(
   R   R?   t   _summaryt   allRv   R>   R8   R-   t   _gcountt   _pcount(	   R   t   summaryR8   t   vert	   classnamet   lengthR<   R-   RV   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyRÅ   :  s    
N(   R0   R1   R2   R@   R5   t
   _data_typeRG   R3   R   t   classmethodRt   R   R   R   Rv   R4   R‹   RŒ   RŽ   R   Rœ   R®   R´   RÀ   RÅ   (    (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR?   ý   s$   0		'		B	4	1c         C   s`   i  } xS t  |  ƒ D]E \ } } | j ƒ  } | | k rK | | j | ƒ q | g | | <q W| S(   s”   
    Given a list of objects, returns a mapping of objects in that list to the
    index or indices at which that object was found in the list.
    (   RH   R!   RC   (   R   t   uniqueRR   R8   (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR   K  s    c         C   sq   t  ƒ  } g  } x[ |  D]S } | j ƒ  } x$ | | k rN d | } d | } q+ W| j | ƒ | j | ƒ q W| S(   sÓ   
    Given a list of parnames, including possible duplicates, returns a new list
    of parnames with duplicates prepended by one or more underscores to make
    them unique.  This is also case insensitive.
    t   _(   Rn   R!   RC   t   add(   R   t   upper_namest   unique_namesR8   t
   name_upper(    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyR=   \  s    	
(   R¡   t   numpyRE   R   R    t   imageR   t   tableR   RY   R   R   R   t   fitsrecR   R   t   utilR   R	   R
   t   utilsR   t   extern.six.movesR   R   R   R5   R?   R   R=   (    (    (    s9   lib/python2.7/site-packages/astropy/io/fits/hdu/groups.pyt   <module>   s   G¥ÿ O	