
[c        	   @   s  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	 m
 Z
 m Z m 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 d d
 l m Z d d l m Z d d l m Z m Z m  Z  m! Z! d d l" m# Z# m$ Z$ d d l% m& Z& d d l' m( Z( d d l) m* Z* d d l+ m, Z, m- Z- y d d l. m/ Z/ e0 Z1 Z2 Wn e3 k
 re4 Z1 Z2 n Xd  Z5 d Z6 d Z7 i d e5 6d e6 6d e7 6Z8 d Z9 d  Z: d Z; d Z< e5 Z= e9 Z> d Z? d Z@ d ZA d ZB i  ZC e1 ry> e/ jD d k ZE e/ jD d k ZF e/ jD d k r^d eC d <n  Wn eG k
 r~e0 ZE e0 ZF n Xn  eH d d d  d! d" d# d$ d% g  ZI d& e f d'     YZJ d( e f d)     YZK d S(*   iNi   (   t   DELAYEDt   ExtensionHDUt   BITPIX2DTYPEt   DTYPE2BITPIX(   t   ImageHDU(   t   BinTableHDUi   (   t   Card(   t   Columnt   ColDefst   TDEF_RE(   t   KEYWORD_NAMES(   t   FITS_rec(   t   Header(   t   _is_pseudo_unsignedt   _unsigned_zerot   _is_intt   _get_array_mmapi   (   t   string_typest	   iteritems(   t   range(   t   lazyproperty(   t   suppress(   t    AstropyPendingDeprecationWarningt   AstropyUserWarning(   t   compressiont	   NO_DITHERt   SUBTRACTIVE_DITHER_1t   SUBTRACTIVE_DITHER_2i    t   RICE_1g      0@i    g=
ףp=
@g
@t   RICE_ONEt   ZIMAGEt   ZCMPTYPEt   ZBITPIXt   ZNAXISt   ZMASKCMPt   ZSIMPLEt   ZTENSIONt   ZEXTENDt   CompImageHeaderc           B   s2  e  Z d  Z i
 d d 6d d 6d d 6d d 6d	 d
 6d d 6d d 6d d 6d d 6d d 6Z e j d  Z e e j    j	 d d d d d g  Z
 e d d d d g  Z d( d  Z d   Z d    Z d( e e e d!  Z e e d"  Z d#   Z d( d( e d$  Z e e d%   Z e d&    Z d'   Z RS()   sg  
    Header object for compressed image HDUs designed to keep the compression
    header and the underlying image header properly synchronized.

    This essentially wraps the image header, so that all values are read from
    and written to the image header.  However, updates to the image header will
    also update the table header where appropriate.
    R#   t   SIMPLER$   t   XTENSIONR    t   BITPIXR!   t   NAXISR%   t   EXTENDt   ZBLOCKEDt   BLOCKEDt   ZPCOUNTt   PCOUNTt   ZGCOUNTt   GCOUNTt   ZHECKSUMt   CHECKSUMt   ZDATASUMt   DATASUMs0   (?P<label>^[Zz][a-zA-Z]*)(?P<num>[1-9][0-9 ]*$)?R   R   R"   t   ZQUANTIZt   ZDITHER0t   ZTILEt   ZNAMEt   ZVALc         C   sU   | d  k r t   } n  | j |  _ | j |  _ | j |  _ | j |  _ | |  _ d  S(   N(   t   NoneR   t   _cardst   _keyword_indicest   _rvkc_indicest	   _modifiedt   _table_header(   t   selft   table_headert   image_header(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyt   __init__l   s    c         C   s   |  j  | | |   r d  St | t  r@ |  j |  \ } } n+ t | t  r^ | \ } } n | d  } } |  j |  r~ d  St t |   j	 | |  | d  k	 r |  j
 |  } | |  j | | f <n  d  S(   N(   t
   _set_slicet
   isinstancet   intt   _keyword_from_indext   tupleR;   t   _is_reserved_keywordt   superR&   t   __setitem__t   _remap_keywordR@   (   RA   t   keyt   valuet   keywordt   indext   remapped_keyword(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyRL   x   s    c         C   s  t  | t  s |  j |  r4 t t |   j |  St  | t  r[ |  j |  \ } } n+ t  | t  ry | \ } } n | d  } } | |  k r t
 d j |    n  t t |   j |  |  j |  } | |  j k r
| d  k	 r |  j | | f =q
|  j | =n  d  S(   Ns   Keyword {!r} not found.(   RF   t   slicet   _haswildcardRK   R&   t   __delitem__RG   RH   RI   R;   t   KeyErrort   formatRM   R@   (   RA   RN   RP   RQ   RR   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyRU      s    c      	   C   s  t  | t  r t |  } n] t  | t  r< t |   } n? | d  k rT t   } n' t  | t  s{ t d j |    n  |  j | j  r d  St	 t
 |   j d | d | d | d |  |  j | j  } t | | j | j  } |  j j d | d t d | d |  d  S(   Nsg   The value appended to a Header must be either a keyword or (keyword, value, [comment]) tuple; got: {!r}t   cardt	   useblankst   bottomt   end(   RF   R   R   RI   R;   t
   ValueErrorRW   RJ   RP   RK   R&   t   appendRM   RO   t   commentR@   t   False(   RA   RX   RY   RZ   R[   RR   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR]      s$    c         C   si  t  | t  ro | r@ | d k r3 t |  j  } q@ | d 7} n  | t |  j  k ro |  j | d t d  Sn  t  | t  r t |  } nE t  | t  r t |   } n' t  | t  s t	 d j
 |    n  |  j | j  r d  S|  j |  } |  j | j  } t t |   j | | d | d | t | | j | j  } |  j j | | d t d | d  S(   Nii   R[   si   The value inserted into a Header must be either a keyword or (keyword, value, [comment]) tuple; got: {!r}RY   t   after(   RF   RG   t   lenR<   R]   t   TrueR   R   RI   R\   RW   RJ   RP   t   _remap_indexRM   RK   R&   t   insertRO   R^   R@   R_   (   RA   RN   RX   RY   R`   t   remapped_indexRR   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyRd      s2    	c         C   st   | d } |  j  |  r d  St t |   j |  | t j k rF d  S|  j |  } |  j j | f | d  d  S(   Ni    i   (   RJ   RK   R&   t   _updateR   t   _commentary_keywordsRM   R@   (   RA   RX   RP   RR   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyRf      s    
c   	      C   s  | d } |  j  |  r d  S| d  k rb t | t  rJ |  j |  } n |  j |  } d  } n6 t | t  r |  j |  } n |  j |  } d  } t t |   j | d | d | d | |  j |  } t	 | | d | d  } |  j
 j | d | d | d | d  S(   Ni    t   beforeR`   t   replacei   i   (   RJ   R;   RF   RG   Rc   RM   RK   R&   t   _relativeinsertR   R@   (	   RA   RX   Rh   R`   Ri   RP   t   remapped_aftert   remapped_beforeRR   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyRj     s(    
	c         C   s   d j  |  } | d k r5 | r1 t j |  n  t St j |  } | r | j d  j   t k r | r{ t j |  n  t S|  j	 j |  } | r| j d  j   } | j d  } | d  k	 r | |  j k r | r t j |  n  t S| |  j k r| rt j |  n  t Sn  t S(   Ns   Keyword {!r} is reserved for use by the FITS Tiled Image Convention and will not be stored in the header for the image being compressed.t   TFIELDSt   labelt   num(   RW   t   warningst   warnRb   R	   t   matcht   groupt   uppert   TABLE_KEYWORD_NAMESt   _zdef_reR;   t   _indexed_compression_keywordst   _compression_keywordsR_   (   t   clsRP   Rq   t   msgt   mRn   Ro   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyRJ   .  s0    	!c         C   sn   t  } | d  d k rH t t  ! t | d  } | d k } Wd  QXn  | r[ d j |  S|  j j | |  S(   Ni   R*   i    s   ZNAXIS{}(   R_   R   R\   RG   RW   t   _keyword_remapst   get(   Ry   RP   t	   is_naxisnRQ   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyRM   P  s    c         C   sw   t  | t  s! |  j |  } n  |  j |  \ } } |  j |  } t t t   |  j j | | f  } Wd  QX| S(   N(	   RF   RG   t
   _cardindexRH   RM   R   t
   IndexErrorRV   R@   (   RA   t   idxRP   t   repeatt   remapped_insert_keyword(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyRc   d  s    N(   t   __name__t
   __module__t   __doc__R|   t   ret   compileRv   t   sett   valuest   unionRx   Rw   R;   RD   RL   RU   Rb   R_   R]   Rd   Rf   Rj   t   classmethodRJ   RM   Rc   (    (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR&   P   s*   
		0	!t   CompImageHDUc           B   so  e  Z d  Z i d d 6d d 6d d 6d d 6d	 d
 6Z e Z e e e e e e e	 e
 e e e e e d  Z e d    Z e e e e e e e e d  Z e d    Z e j d    Z e d    Z e j d    Z e d    Z e d    Z d   Z d   Z e d d d d  Z e e d  Z d   Z d   Z e d  Z  d   Z! e d  Z" d    Z# RS(!   s%   
    Compressed Image HDU class.
    t   compression_typet   compressionTypet	   tile_sizet   tileSizet   hcomp_scalet
   hcompScalet   hcomp_smootht   hcompSmootht   quantize_levelt   quantizeLevelc         K   s  t  s t d   n  t j | |  } i  } xw |  j j   D]f \ } } | | k r t j d j | |  j	 j
 |  t  | | | | <| | =q= t   | | | <q= W|	 | d <|
 | d <| t k r t t |   j d | d |  n; t t |   j d d d |  | |  _ |  j | | |  | |  _ | |  _ | |  _ g  t |  j j d d   D]) } |  j j d t | d	  d  ^ q[|  _ | rd |  _ d	 |  _ n0 |  j j d
 d  |  _ |  j j d d	  |  _ |  j d |  _ |  j |  _ |  j |  _ |  j |  _  d S(   s0  
        Parameters
        ----------
        data : array, optional
            Uncompressed image data

        header : Header instance, optional
            Header to be associated with the image; when reading the HDU from a
            file (data=DELAYED), the header read from the file

        name : str, optional
            The ``EXTNAME`` value; if this value is `None`, then the name from
            the input image header will be used; if there is no name in the
            input image header then the default name ``COMPRESSED_IMAGE`` is
            used.

        compression_type : str, optional
            Compression algorithm: one of
            ``'RICE_1'``, ``'RICE_ONE'``, ``'PLIO_1'``, ``'GZIP_1'``,
            ``'GZIP_2'``, ``'HCOMPRESS_1'``

        tile_size : int, optional
            Compression tile sizes.  Default treats each row of image as a
            tile.

        hcomp_scale : float, optional
            HCOMPRESS scale parameter

        hcomp_smooth : float, optional
            HCOMPRESS smooth parameter

        quantize_level : float, optional
            Floating point quantization level; see note below

        quantize_method : int, optional
            Floating point quantization dithering method; can be either
            ``NO_DITHER`` (-1), ``SUBTRACTIVE_DITHER_1`` (1; default), or
            ``SUBTRACTIVE_DITHER_2`` (2); see note below

        dither_seed : int, optional
            Random seed to use for dithering; can be either an integer in the
            range 1 to 1000 (inclusive), ``DITHER_SEED_CLOCK`` (0; default), or
            ``DITHER_SEED_CHECKSUM`` (-1); see note below

        Notes
        -----
        The astropy.io.fits package supports 2 methods of image compression:

            1) The entire FITS file may be externally compressed with the gzip
               or pkzip utility programs, producing a ``*.gz`` or ``*.zip``
               file, respectively.  When reading compressed files of this type,
               Astropy first uncompresses the entire file into a temporary file
               before performing the requested read operations.  The
               astropy.io.fits package does not support writing to these types
               of compressed files.  This type of compression is supported in
               the ``_File`` class, not in the `CompImageHDU` class.  The file
               compression type is recognized by the ``.gz`` or ``.zip`` file
               name extension.

            2) The `CompImageHDU` class supports the FITS tiled image
               compression convention in which the image is subdivided into a
               grid of rectangular tiles, and each tile of pixels is
               individually compressed.  The details of this FITS compression
               convention are described at the `FITS Support Office web site
               <https://fits.gsfc.nasa.gov/registry/tilecompression.html>`_.
               Basically, the compressed image tiles are stored in rows of a
               variable length array column in a FITS binary table.  The
               astropy.io.fits recognizes that this binary table extension
               contains an image and treats it as if it were an image
               extension.  Under this tile-compression format, FITS header
               keywords remain uncompressed.  At this time, Astropy does not
               support the ability to extract and uncompress sections of the
               image without having to uncompress the entire image.

        The astropy.io.fits package supports 3 general-purpose compression
        algorithms plus one other special-purpose compression technique that is
        designed for data masks with positive integer pixel values.  The 3
        general purpose algorithms are GZIP, Rice, and HCOMPRESS, and the
        special-purpose technique is the IRAF pixel list compression technique
        (PLIO).  The ``compression_type`` parameter defines the compression
        algorithm to be used.

        The FITS image can be subdivided into any desired rectangular grid of
        compression tiles.  With the GZIP, Rice, and PLIO algorithms, the
        default is to take each row of the image as a tile.  The HCOMPRESS
        algorithm is inherently 2-dimensional in nature, so the default in this
        case is to take 16 rows of the image per tile.  In most cases, it makes
        little difference what tiling pattern is used, so the default tiles are
        usually adequate.  In the case of very small images, it could be more
        efficient to compress the whole image as a single tile.  Note that the
        image dimensions are not required to be an integer multiple of the tile
        dimensions; if not, then the tiles at the edges of the image will be
        smaller than the other tiles.  The ``tile_size`` parameter may be
        provided as a list of tile sizes, one for each dimension in the image.
        For example a ``tile_size`` value of ``[100,100]`` would divide a 300 X
        300 image into 9 100 X 100 tiles.

        The 4 supported image compression algorithms are all 'lossless' when
        applied to integer FITS images; the pixel values are preserved exactly
        with no loss of information during the compression and uncompression
        process.  In addition, the HCOMPRESS algorithm supports a 'lossy'
        compression mode that will produce larger amount of image compression.
        This is achieved by specifying a non-zero value for the ``hcomp_scale``
        parameter.  Since the amount of compression that is achieved depends
        directly on the RMS noise in the image, it is usually more convenient
        to specify the ``hcomp_scale`` factor relative to the RMS noise.
        Setting ``hcomp_scale = 2.5`` means use a scale factor that is 2.5
        times the calculated RMS noise in the image tile.  In some cases it may
        be desirable to specify the exact scaling to be used, instead of
        specifying it relative to the calculated noise value.  This may be done
        by specifying the negative of the desired scale value (typically in the
        range -2 to -100).

        Very high compression factors (of 100 or more) can be achieved by using
        large ``hcomp_scale`` values, however, this can produce undesirable
        'blocky' artifacts in the compressed image.  A variation of the
        HCOMPRESS algorithm (called HSCOMPRESS) can be used in this case to
        apply a small amount of smoothing of the image when it is uncompressed
        to help cover up these artifacts.  This smoothing is purely cosmetic
        and does not cause any significant change to the image pixel values.
        Setting the ``hcomp_smooth`` parameter to 1 will engage the smoothing
        algorithm.

        Floating point FITS images (which have ``BITPIX`` = -32 or -64) usually
        contain too much 'noise' in the least significant bits of the mantissa
        of the pixel values to be effectively compressed with any lossless
        algorithm.  Consequently, floating point images are first quantized
        into scaled integer pixel values (and thus throwing away much of the
        noise) before being compressed with the specified algorithm (either
        GZIP, RICE, or HCOMPRESS).  This technique produces much higher
        compression factors than simply using the GZIP utility to externally
        compress the whole FITS file, but it also means that the original
        floating point value pixel values are not exactly preserved.  When done
        properly, this integer scaling technique will only discard the
        insignificant noise while still preserving all the real information in
        the image.  The amount of precision that is retained in the pixel
        values is controlled by the ``quantize_level`` parameter.  Larger
        values will result in compressed images whose pixels more closely match
        the floating point pixel values, but at the same time the amount of
        compression that is achieved will be reduced.  Users should experiment
        with different values for this parameter to determine the optimal value
        that preserves all the useful information in the image, without
        needlessly preserving all the 'noise' which will hurt the compression
        efficiency.

        The default value for the ``quantize_level`` scale factor is 16, which
        means that scaled integer pixel values will be quantized such that the
        difference between adjacent integer values will be 1/16th of the noise
        level in the image background.  An optimized algorithm is used to
        accurately estimate the noise in the image.  As an example, if the RMS
        noise in the background pixels of an image = 32.0, then the spacing
        between adjacent scaled integer pixel values will equal 2.0 by default.
        Note that the RMS noise is independently calculated for each tile of
        the image, so the resulting integer scaling factor may fluctuate
        slightly for each tile.  In some cases, it may be desirable to specify
        the exact quantization level to be used, instead of specifying it
        relative to the calculated noise value.  This may be done by specifying
        the negative of desired quantization level for the value of
        ``quantize_level``.  In the previous example, one could specify
        ``quantize_level = -2.0`` so that the quantized integer levels differ
        by 2.0.  Larger negative values for ``quantize_level`` means that the
        levels are more coarsely-spaced, and will produce higher compression
        factors.

        The quantization algorithm can also apply one of two random dithering
        methods in order to reduce bias in the measured intensity of background
        regions.  The default method, specified with the constant
        ``SUBTRACTIVE_DITHER_1`` adds dithering to the zero-point of the
        quantization array itself rather than adding noise to the actual image.
        The random noise is added on a pixel-by-pixel basis, so in order
        restore each pixel from its integer value to its floating point value
        it is necessary to replay the same sequence of random numbers for each
        pixel (see below).  The other method, ``SUBTRACTIVE_DITHER_2``, is
        exactly like the first except that before dithering any pixel with a
        floating point value of ``0.0`` is replaced with the special integer
        value ``-2147483647``.  When the image is uncompressed, pixels with
        this value are restored back to ``0.0`` exactly.  Finally, a value of
        ``NO_DITHER`` disables dithering entirely.

        As mentioned above, when using the subtractive dithering algorithm it
        is necessary to be able to generate a (pseudo-)random sequence of noise
        for each pixel, and replay that same sequence upon decompressing.  To
        facilitate this, a random seed between 1 and 10000 (inclusive) is used
        to seed a random number generator, and that seed is stored in the
        ``ZDITHER0`` keyword in the header of the compressed HDU.  In order to
        use that seed to generate the same sequence of random numbers the same
        random number generator must be used at compression and decompression
        time; for that reason the tiled image convention provides an
        implementation of a very simple pseudo-random number generator.  The
        seed itself can be provided in one of three ways, controllable by the
        ``dither_seed`` argument:  It may be specified manually, or it may be
        generated arbitrarily based on the system's clock
        (``DITHER_SEED_CLOCK``) or based on a checksum of the pixels in the
        image's first tile (``DITHER_SEED_CHECKSUM``).  The clock-based method
        is the default, and is sufficient to ensure that the value is
        reasonably "arbitrary" and that the same seed is unlikely to be
        generated sequentially.  The checksum method, on the other hand,
        ensures that the same seed is used every time for a specific image.
        This is particularly useful for software testing as it ensures that the
        same image will always use the same seed.
        sh   The astropy.io.fits.compression module is not available.  Creation of compressed image HDUs is disabled.s@   Keyword argument {} to {} is pending deprecation; use {} insteadt   quantize_methodt   dither_seedt   datat   headerR!   i    i   t   BZEROt   BSCALER    N(!   t   COMPRESSION_SUPPORTEDt	   Exceptiont   CMTYPE_ALIASESR}   t   DEPRECATED_KWARGSt   itemsRp   Rq   RW   t	   __class__R   R   t   localsR    RK   R   RD   R;   R   t   _update_header_datat   _do_not_scale_image_datat   _uintt   _scale_backR   t   _headert   strt   _axest   _bzerot   _bscalet   _bitpixt   _orig_bzerot   _orig_bscalet   _orig_bitpix(   RA   R   R   t   nameR   R   R   R   R   R   R   t   do_not_scale_image_datat   uintt
   scale_backt   kwargst   compression_optst   oldargt   newargt   axis(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyRD     sB    


"				K	c         C   s   | j  d } | j d k r  t S| j } t | t  rG | j   } n  | d k rW t Sd | k sn | d rr t St r t r t	 St s t
 j d t  t St Sd  S(   Ni    R(   t   BINTABLEt   A3DTABLER   s   Failure matching header to a compressed image HDU: The compression module is not available.
The HDU will be treated as a Binary Table HDU.(   R   R   (   t   cardsRP   R_   RO   RF   R   t   rstripR   t   COMPRESSION_ENABLEDRb   Rp   Rq   R   (   Ry   R   RX   t   xtension(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyt   match_header  s"    		c
   5   	   C   s  t  d |  j d |  j  }
 t |  j |
 j  |  _ |
 j |  _ ~
 |  j r |  j j d k } | r t	 r t
 d j |  j |  j f |  j j    q n t } | r d |  j k r d } n  | r |  j j d | d d	 d
 | |  _ n |  j d |  _ | rP| d k r1t j d j t  t  t } n  |  j j d | d d	 d
 n' |  j j d t  } t j | |  } | r| j d d  } | j d d  } d } | d k r|  j j d | d	 | d } n  | d k r|  j j d | d	 | n  | j d } | j d } n d } d } |  j j d d d d	 d
 | d k rc| rZd n d } n | rod  n d! } |  j j d" | d# d	 d t d$ |  j d d% |  } |  j d } | d& k  r| d k rd' } t rd( } | rd  n d! } n d) } | d* k r | rd  n d! } nu | d+ k rA| r8d n d } nT | d k rb| rYd, n d- } n3 | d. k r| rzd/ n d0 } n | rd1 n d2 } |  j j d3 | d4 d	 d" |  j j d5 | d# d	 d3 t d$ | d% |  } |  j j d6 d7 d8 d	 d5 |  j j d9 d: d; d	 d6 t d$ |  j d6 d% |  j d9  } |  j j d< d= d> d	 d9 |  j j d? d: d; d	 d< d? } t d$ |  j d< d% |  j d?  } t | | | | g  } nf d@ } d" } d3 d5 d6 d9 d< d? g } x0 | D]( } y |  j | =Wqt k
 rqXqWt | g  } |  j j dA | j j dB  |  j j d
 | dC d	 dD |  j j dE t dF d	 | |  j j dG | | d	 dE |  j j dH |  j d | d	 dG d@ } xU t ry6 |  j dH t |  =|  j dI t |  =| d@ 7} Wqt k
 rPqXqW|  j d } | s,g  } n+ t |  | k rWt j dJ t  g  } n  | d k re	|  j dA d' k  s|  j dK d' k  rt  dL   n~ | r| d& d' k  s| d@ d' k  rt  dM   n  t g  | D] }  |  d@ k r|  ^ q }! |! d k rt  dN   qn  | r| d& d& k r| d@ d& k r|  j dA | d& <|  j dK | d@ <x	t! d |  D] }" d@ | |" <qnWn | sj| j" |  j dA  |  j dK dO k r| j" |  j dA  nw |  j dK }# xg d+ dP dQ dO dR dS dT dU dV g	 D]7 }$ |# |$ d& k s|# |$ dW k r| j" |$  PqqW| j" dX  x' t! d |  D] }" | j" d@  qPWn  |  j dA | d& }% |% d& k r|% d' k  r| d& c d@ 7<|  j dA | d& }% |% d& k r|% d' k  rt  dY   qn  |  j dK | d@ }% |% d& k re	|% d' k  re	| d@ c d@ 7<|  j dK | d@ }% |% d& k rb	|% d' k  rb	t  dZ   qb	qe	n  dH }& |  j d d& k r	d[ }' n dH }' d& }( xwt# |  j  D]f\ } }) d t | d@  } dH t | d@  }* dI t | d@  }+ | r
t |  | d@ k r
| | }  nH |+ |  j k rB
| s9
|  j dA }  qO
d@ }  n |  j |+ }  | j" |   |( sw
|) d@ |  d@ }( n |( |) d@ |  d@ 9}( | r
| | k r
|  j j |* |) | j | d	 |& n |  j j |* |) d\ d	 |& |  j j |+ |  d] d	 |' |* }& |+ }' q	W|  j j dK |( d^  | |  _$ d@ } x t rd_ t |  }, |, |  j k r^Pn  d` t |  }- |  j |, da k r| d k r|  j |- } qn  |  j |, db k r| d k r|  j |- } qn  |  j |, dc k r| d k r|  j |- } qn  | d@ 7} q5W| d k r't& } n  | d k r<t' } n  | d k rQt' } n  d@ } x[ t rd_ t |  }, |, |  j k rPn  d` t |  }- |  j |, =|  j |- =| d@ 7} qZWd } d@ } | d k r|  j j dd de df d	 | |  j j dg t( dh d	 dd |  j j di dj dk d	 dg |  j dG d* k r=d@ }. n" |  j dG d+ k rYd }. n t) }. |  j j dl |. dk d	 di dl } dW } n | d k r|  j j dd dm dn d	 | |  j j dg | dn d	 dd |  j j di do dp d	 dg |  j j dl | dp d	 di dl } dW } n  |  j d d& k  r |  j j d_ t |  da dq d	 | |  j j d` t |  | dq d	 d_ t |  | r
| t* t+ t, g k rt- t. } t j dr j |   t. } n  | t* k rds }/ n dt }/ |  j j du t- | |/ d	 d` t |  nf |  j j du t*  } t/ | t0  rpx? t1 t-  D]( \ } }0 |0 j2   | k r;| } Pq;q;Wt* } n  | t* k rdv |  j k r|  j dv =qq |	 r|  j3 |	  }	 n. dv |  j k r|  j dv }	 n |  j3 t4  }	 |  j j dv |	 dw d	 du n  | rdx | k r<|  j j dy | dx | j dx dz dG n  d{ | k rl|  j j d| | d{ | j d{  n  d} | k r|  j j d~ | d} | j d}  n  d | k r|  j j d d | j d dz dG n  d | k r|  j j d | d | j d d	 |& n  dD | k r:|  j j d | dD | j dD d	 d n  d | k rj|  j j d | d | j d  n  d | k rS|  j j d | d | j d  qSn d |  j k r|  j j d d |  j j d dz dG n  d |  j k r|  j j d |  j d |  j j d d	 |& n  dD |  j k rS|  j j d |  j dD |  j j dD d	 d n  d |  j k r| j5   }1 |  j j5   }2 |  j j5   }3 x/ t! |1 |2  D] }4 |  j j"   |3 d@ 7}3 qWx( t! |1 |3  D] }4 |  j j"   qWn  d S(   s	  
        Update the table header (`_header`) to the compressed
        image format and to match the input data (if any).  Create
        the image header (`_image_header`) from the input image
        header (if any) and ensure it matches the input
        data. Create the initially-empty table data array to hold
        the compressed data.

        This method is mainly called internally, but a user may wish to
        call this method after assigning new data to the `CompImageHDU`
        object that is of a different type.

        Parameters
        ----------
        image_header : Header instance
            header to be associated with the image

        name : str, optional
            the ``EXTNAME`` value; if this value is `None`, then the name from
            the input image header will be used; if there is no name in the
            input image header then the default name 'COMPRESSED_IMAGE' is used

        compression_type : str, optional
            compression algorithm 'RICE_1', 'PLIO_1', 'GZIP_1', 'GZIP_2',
            'HCOMPRESS_1'; if this value is `None`, use value already in the
            header; if no value already in the header, use 'RICE_1'

        tile_size : sequence of int, optional
            compression tile sizes as a list; if this value is `None`, use
            value already in the header; if no value already in the header,
            treat each row of image as a tile

        hcomp_scale : float, optional
            HCOMPRESS scale parameter; if this value is `None`, use the value
            already in the header; if no value already in the header, use 1

        hcomp_smooth : float, optional
            HCOMPRESS smooth parameter; if this value is `None`, use the value
            already in the header; if no value already in the header, use 0

        quantize_level : float, optional
            floating point quantization level; if this value is `None`, use the
            value already in the header; if no value already in header, use 16

        quantize_method : int, optional
            floating point quantization dithering method; can be either
            NO_DITHER (-1), SUBTRACTIVE_DITHER_1 (1; default), or
            SUBTRACTIVE_DITHER_2 (2)

        dither_seed : int, optional
            random seed to use for dithering; can be either an integer in the
            range 1 to 1000 (inclusive), DITHER_SEED_CLOCK (0; default), or
            DITHER_SEED_CHECKSUM (-1)
        R   R   i   i    sa   Astropy cannot compress images greater than 4 GB in size ({} is {} bytes) without CFITSIO >= 3.35t   EXTNAMEt   COMPRESSED_IMAGEs#   name of this binary table extensionR`   Rm   R   t   GZIP_1t   GZIP_2t   PLIO_1t   HCOMPRESS_1sB   Unknown compression type provided.  Default ({}) compression used.R   s   compression algorithmR   g        R   g      ?R)   R*   s   data type of original images   dimension of original imaget   TTYPE1t   COMPRESSED_DATAs   label for field 1t   1QIt   1PIt   1QBt   1PBt   TFORM1s+   data format of field: variable length arrayR   RW   i    i   t   GZIP_COMPRESSED_DATAt   UNCOMPRESSED_DATAi   i   t   1QJt   1PJit   1QEt   1PEt   1QDt   1PDt   TTYPE2s   label for field 2t   TFORM2t   TTYPE3t   ZSCALEs   label for field 3t   TFORM3t   1Ds#   data format of field: 8-byte DOUBLEt   TTYPE4t   ZZEROs   label for field 4t   TFORM4i   t   NAXIS1s   width of table in bytess   number of fields in each rowR1   R   s#   extension contains compressed imageR    R!   R8   sQ   Provided tile size not appropriate for the data.  Default tile size will be used.t   NAXIS2s-   Hcompress minimum image dimension is 4 pixelss,   Hcompress minimum tile dimension is 4 pixelssm   HCOMPRESS can only support 2-dimensional tile sizes.All but two of the tile_size dimensions must be set to 1.i   i   i   i   i   i   i   i   i   i   s4   Last tile along 1st dimension has less than 4 pixelss4   Last tile along 2nd dimension has less than 4 pixelst   ZNAXIS1s   length of original image axiss   size of tiles to be compresseds   number of rows in tableR9   R:   t   NOISEBITs   SCALE   s   SMOOTH  t   ZNAME1t	   BLOCKSIZEs   compression block sizet   ZVAL1s   pixels per blockt   ZNAME2t   BYTEPIXs   bytes per pixel (1, 2, 4, or 8)t   ZVAL2t   SCALEs   HCOMPRESS scale factort   SMOOTHs   HCOMPRESS smooth options!   floating point quantization levels@   Unknown quantization method provided.  Default method ({}) used.s    No dithering during quantizations   Pixel Quantization AlgorithmR6   R7   s'   dithering offset when quantizing floatsR'   R#   Rh   R+   R%   R-   R,   R(   R$   t   IMAGER/   R.   R0   R3   R2   R5   R4   NI       (   R   R   R   R   R   (6   R   R   R   R&   R   t   _image_headerR   t	   _has_datat   nbytest   CFITSIO_SUPPORTS_Q_FORMATt   IOErrorRW   R   t   verR_   R   Rp   Rq   t   DEFAULT_COMPRESSION_TYPER   R}   R   t   commentsR   t   CFITSIO_SUPPORTS_GZIPDATAR   RV   t   dtypet   itemsizeRb   R   Ra   R\   R   R]   t	   enumeratet   columnsR;   t   DEFAULT_QUANTIZE_LEVELt   DEFAULT_HCOMP_SCALEt   DEFAULT_BLOCK_SIZEt   DEFAULT_BYTE_PIXR   R   R   t   QUANTIZE_METHOD_NAMESt   DEFAULT_QUANTIZE_METHODRF   R   R   Rt   t   _generate_dither_seedt   DEFAULT_DITHER_SEEDt   _countblanks(5   RA   RC   R   R   R   R   R   R   R   R   t	   image_hdut   huge_hdut   bzerot   bscalet   after_keywordt   bitpix_commentt   naxis_commentt   tform1t   col1t   zbitpixt   ncolst   ttype2t   tform2t   col2t   col3R`   t   col4t   colst	   to_removet   kR   t   naxist   tst
   major_dimst   it   naxis2t   dimt   remaint   last_znaxist   after1t   nrowsR   t   znaxist   ztilet   znamet   zvalt   bytepixt   zquantiz_commentt   vt   required_blankst   image_blankst   table_blankst   _(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR     s   @	'													 +&( 		

						

					

				







c         C   s  t  j |   } | d  k r | S|  j d k s= |  j d k r|  j   } t j | d | } d  } d |  j j	 j
 k r |  j d } n\ d |  j k r t j |  j d d d } n. d |  j k r t j |  j d d d } n  | d  k	 r | | k } n  |  j d k r(t j | |  j |  n  |  j d k rYt j | |  j d | d d	 n  | d  k	 rt j | t j |  } qn  |  j | j  | S(
   Ni    i   R   t   ZBLANKt   int32t   BLANKt   outt   castingt   unsafe(   R   t   decompress_hduR;   R   R   t   _dtype_for_bitpixt   npt   arrayt   compressed_dataR   t   namesR   R   t   multiplyR   t   addt   wheret   nant   _update_header_scale_infoR   (   RA   R   t	   new_dtypet   zblankt   blanks(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR   _  s.    "c         C   s\   | d  k	 rX t | t j  s1 | j j d  k	 rX t d j t |  | j j    n  d  S(   Ns:   CompImageHDU data has incorrect type:{}; dtype.fields = {}(	   R;   RF   R3  t   ndarrayR   t   fieldst	   TypeErrorRW   t   type(   RA   R   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR     s    	c         C   sF   t  t |   j } t | t j j  r5 |  j d =| S|  j   |  j	 S(   NR   (
   RK   R   R   RF   R3  t   rect   recarrayt   __dict__t   _update_compressed_dataR5  (   RA   R5  (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR5    s    

c         C   s7   d |  j  k r3 |  j  d ` |  j  d =t j   n  d  S(   NR5  (   RE  t   _coldefst   gct   collect(   RA   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR5    s    
c         C   s   t  t |  j   S(   sX   
        Shape of the image array--should be equivalent to ``self.data.shape``.
        (   RI   t   reversedR   (   RA   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyt   shape  s    c         C   s5  t  |  d  r |  j S|  j j   } x3 t |  D]% } t j | d t r2 | | =q2 q2 Wd |  j k r | j d |  j d |  j j d d d nq d |  j k r |  j d d k r t	 j
 d	 t  n  | j d
 d |  j j d d d n | j d
 d d d | j d |  j d |  j j d d d | j d |  j d |  j j d d d d } xf t | d  D]T } d t | d  } | d } | j | |  j | |  j j | d | | } qsW| d } xY t | d  D]G } y t | d  } Wn t k
 rqn X| | k r| | =qqWd |  j k rm| j d |  j d |  j j d d | n | j d d d | d |  j k r| j d |  j d |  j j d d d n | j d d d d d |  j k r| j d |  j d |  j j d  n  d |  j k rA| j d |  j d |  j j d  n  d |  j k rw| j d |  j d |  j j d  n  d |  j k r| j d |  j d |  j j d  n  d  |  j k r|  j d  d! k r| d  =n  |  j j   } | j   }	 x" t | |	  D] }
 | j   qWt |  j |  |  _ |  j S("   NR   Rq   R#   R'   Rh   i    R$   R   s3   ZTENSION keyword in compressed extension != 'IMAGE'R(   R)   R    i   R*   R!   i   R`   s   NAXIS?*i   R.   R/   R0   R1   R%   R+   R,   R-   R2   R3   R4   R5   R   R   (   t   hasattrR   R   t   copyR   R&   RJ   R_   R   Rp   Rq   R   R   R   t   listRG   R   R  R]   (   RA   RC   RP   t
   last_naxisR   R   R  t   nR)  R(  R*  (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR     s    		






c         C   s   |  j  j } |  j r |  j d k r3 d \ } } q t |  j j  } |  j j j } | j	   t
 |  } | | j d  d } nW d	 } x= t |  j d  D]( } | |  j d t | d  f 7} q Wt |  j d } |  j |  j | t |  j  | | f S(
   sC   
        Summarize the HDU: name, dimensions, and formats.
        t    t   .i   R*   R)   N(    (   (    RQ  (    (   R   R   t   _data_loadedR   R;   RN  RK  R   R   t   reverseRI   t   rfindR   R   R   R   R   Ra   (   RA   t
   class_namet   _shapet   _formatR   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyt   _summary:  s    	
&c   
      C   s  t  |  j j j } | |  j k s7 |  j j |  j k rJ |  j |  j  n  |  j } t |  j j  r t	 j
 |  j t |  j j  d d j |  j j j  |  _ t } n |  j j j } | r |  j j j r |  j j t  q |  j j t  |  _ n  zr |  j d } |  j d | } d |  j d <d |  j k rE|  j d =n  | |  _ |  ` t j |   \ } |  _ Wd | r|  j j t  n  | |  _ X|  j j j d	  } |  j } | |  j  j d | d
 t	 j j  }	 |	 j t  |  _ |  j |  j _ |  j |  j _  | |  j _! d S(   sN   
        Compress the image data so that it may be written to a file.
        R   s   =i{}R   R   i    R/   t   THEAPNt   >RB  ("   R   R   R   R   R   RK  R   R   R   R3  R4  R   RW   R   R_   t   isnativet   flagst	   writeablet   byteswapRb   R   t   _theapR5  R   t   compress_hduR   t   newbyteordert   viewRC  RD  R   RG  t   _heapoffsett	   _heapsize(
   RA   t   image_bitpixt   old_datat   should_swapR  t   tbsizet   heapsizeR   t   bufR5  (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyRF  Z  sD    $			
	t   oldi   i    c         C   s  |  j  d k r d S| d k r/ t |  j } n  t t |  } | d k sV | d k re | } | } n | d k r |  j } |  j } n | d k r7t | t j	  r d } d } q7t j
 j |  j  j  } t j j |  j  j  }	 | t j k r	| } |	 | d } q7|	 | d } |	 | d d | j d } n  | d k rut j |  j  | d	 |  j  d
 d | |  j d <n7 x4 |  j |  j f D]  }
 t t   |
 d =Wd QXqW| d k r|  j  | :_  | |  j d <n7 x4 |  j |  j f D]  }
 t t   |
 d =Wd QXqW|  j  j j | k rJt j t j |  j   d | |  _  n  t |  j  j j |  _ |  j j d d  |  _ |  j j d d  |  _ |  j |  j d <|  j |  j  |  j |  _ |  j |  _ |  j |  _ d S(   sc  
        Scale image data by using ``BSCALE`` and ``BZERO``.

        Calling this method will scale ``self.data`` and update the keywords of
        ``BSCALE`` and ``BZERO`` in ``self._header`` and ``self._image_header``.
        This method should only be used right before writing to the output
        file, as the data will be scaled and is therefore not very usable after
        the call.

        Parameters
        ----------

        type : str, optional
            destination data type, use a string representing a numpy dtype
            name, (e.g. ``'uint8'``, ``'int16'``, ``'float32'`` etc.).  If is
            `None`, use the current data type.

        option : str, optional
            how to scale the data: if ``"old"``, use the original ``BSCALE``
            and ``BZERO`` values when the data was read/created. If
            ``"minmax"``, use the minimum and maximum of the data to scale.
            The option will be overwritten by any user-specified bscale/bzero
            values.

        bscale, bzero : int, optional
            user specified ``BSCALE`` and ``BZERO`` values.
        Ni   i    Rl  t   minmaxg       @i   i   R.  R/  R0  R   R   R   R)   g      p@g     o@(    R   R;   R   R   t   getattrR3  R   R   RF   t   floatingt   minimumt   reducet   flatt   maximumt   uint8t   bytest   subtractR   R   R   RV   R   RB  R4  t   aroundR   R   R}   R   R   R   R   (   RA   RB  t   optionR  R  t   _typet   _scalet   _zerot   _mint   _maxR   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyt   scale  sX    			 "'c         C   s  |  j  r  |  j t |  j  n  |  j r |  j   |  j   t d |  j d |  j	  } | j
 |  d | j	 k r |  j j d | j	 d | j	 j d  n  d | j	 k r |  j j d | j	 d | j	 j d  n  |  j |  _ |  j |  j d <n  t t |   j d | d |  S(   NR   R   R3   R5   t   checksumt   inplace(   R   R~  R   R   R   RF  t   _update_uint_scale_keywordsR   R   R   t   _update_checksumR   R   R   t
   _imagedataR5  RE  RK   R   t   _prewriteto(   RA   R  R  R  (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR    s$    		


c         C   s   t  j |  |  S(   s   
        Bypasses `BinTableHDU._writeheader()` which updates the header with
        metadata about the data that is meaningless here; another reason
        why this class maybe shouldn't inherit directly from BinTableHDU...
        (   R   t   _writeheader(   RA   t   fileobj(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR  /  s    c         C   sP   z t  t |   j |  SWd t |  d  rE |  j |  j d <|  ` n |  ` Xd S(   s   
        Wrap the basic ``_writedata`` method to restore the ``.data``
        attribute to the uncompressed image data in the case of an exception.
        NR  R   (   RK   R   t
   _writedataRL  R  RE  R   (   RA   R  (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR  8  s    	c         C   sJ   t  t |   j d |  | rF |  j rF t |  j  d  k	 rF |  ` n  d  S(   Nt   closed(   RK   R   t   _closeRS  R   R5  R;   (   RA   R  (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR  H  s    c         C   s   |  j  } |  j r |  j d k r xt d t j d  f d t j d  f d t j d  f f D]3 \ } } | | k r^ |  j d | d >k r^ | Sq^ Wn  | d k r t j d  S| d	 k r t j d
  Sd S(   s   
        Determine the dtype that the data should be converted to depending on
        the BITPIX value in the header, and possibly on the BSCALE value as
        well.  Returns None if there should not be any change.
        i   i   t   uint16i    t   uint32i@   t   uint64t   float64i    t   float32N(   R   R   R   R3  R   R   (   RA   t   bitpixt   bitsR   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR2  S  s    	"#c         C   s   |  j  r |  j d k o% |  j d k r xU d d g D]G } x> |  j |  j f D]* } t t   | | =| j   Wd  QXqO Wq6 W| d  k r |  j	   } n  | d  k	 r t
 | j |  j d <n  d |  _ d |  _ |  j d |  _ n  d  S(   Ni    i   R   R   R)   (   R   R   R   R   R   R   RV   R]   R;   R2  R   R   R   R   R   (   RA   R   RP   R   (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR;  h  s    
		c         C   s;  t  |  s t d   n  d | k o2 d k n sO t d j |    n  | t k r |  j d } g  t |  D]  } |  j d j | d  ^ qu } | j   |  j t	 d   | D  } | j
 d	 d
  j   } t j |  j d d S| t k r3t d   t j t j    D  t |   d d S| Sd  S(   Ns   Seed must be an integerii'  s   Seed for random dithering must be either between 1 and 10000 inclusive, 0 for autogeneration from the system clock, or -1 for autogeneration from a checksum of the first image tile (got {})R!   s   ZTILE{}i   c         s   s   |  ] } t  |  Vq d  S(   N(   RS   (   t   .0t   d(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pys	   <genexpr>  s    R   Rt  c         s   s   |  ] } t  |  Vq d  S(   N(   RG   (   R  t   x(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pys	   <genexpr>  s    (   R   RA  R\   RW   t   DITHER_SEED_CHECKSUMR   R   RT  R   RI   Rc  t   sumt   ctypest   c_ulongRO   t   DITHER_SEED_CLOCKt   matht   modft   timet   id(   RA   t   seedR  R   t	   tile_dimst
   first_tilet   csum(    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR     s$    0
,	($   R   R   R   R   Rb   t   _manages_own_heapR;   R   R   t   DEFAULT_HCOMP_SMOOTHR   R   R  R_   RD   R   R   R   R   R   t   setterR5  t   deletert   propertyRK  R   RY  RF  R~  R  R  R  R  R2  R;  R   (    (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyR   z  sX   
		   +	x	 	El$				(L   R  RH  R  R   R  Rp   t   numpyR3  t   baseR    R   R   R   t   imageR   t   tableR   RX   R   t   columnR   R   R	   R
   Ru   t   fitsrecR   R   R   t   utilR   R   R   R   t
   extern.sixR   R   t   extern.six.movesR   t   utilsR   t   utils.compatR   t   utils.exceptionsR   R   RQ  R   Rb   R   R   t   ImportErrorR_   R   R   R   R   R  R  R   R   R   R  R   R  R   R   R   t   CFITSIO_VERSIONR   R   t   AttributeErrorR   t   COMPRESSION_KEYWORDSR&   R   (    (    (    s=   lib/python2.7/site-packages/astropy/io/fits/hdu/compressed.pyt   <module>   sr   ""
 +