ó
šßÈ[c           @` sñ  d  Z  d d l m Z m Z m Z m Z d d l Z d d l 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 d l m Z d d l m Z d d d g Z e a d „  Z d e	 f d „  ƒ  YZ d d d g Z d „  Z d e d d e d „ Z  d d e d „ Z! e j" e ƒ D e j# d e e  ƒ e j$ d e e! ƒ e j% d e e j& j' ƒ Wd QXy e  j  e j( _  Wn# e) k
 r³e  j  e j( j* _  n Xy e! j  e j+ _  Wn# e) k
 rìe! j  e j+ j* _  n Xd S(   u.   This module implements the base CCDData class.i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   NDDataArray(   t   StdDevUncertaintyt   NDUncertaintyi   (   t   fitst   registry(   t   units(   t   log(   t   WCS(   t   sharedmethodu   CCDDatau   fits_ccddata_readeru   fits_ccddata_writerc         ` s   ‡  f d †  } | S(   u  Decorator factory which temporarly disables the need for a unit when
    creating a new CCDData instance. The final result must have a unit.

    Parameters
    ----------
    op : function
        The function to apply. Supported are:

        - ``np.add``
        - ``np.subtract``
        - ``np.multiply``
        - ``np.true_divide``

    Notes
    -----
    Should only be used on CCDData ``add``, ``subtract``, ``divide`` or
    ``multiply`` because only these methods from NDArithmeticMixin are
    overwritten.
    c         ` s1   d  ‡  f d † } d j |  j ƒ | _ t | ƒ S(   Nc         ` s1   t  a |  j ˆ  | | |  } t a | j | ƒ S(   N(   t   Falset   _config_ccd_requires_unitt   _prepare_then_do_arithmetict   Truet	   __class__(   t   selft   operandt   operand2t   kwargst   result(   t   op(    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   inner/   s
    u*   See `astropy.nddata.NDArithmeticMixin.{}`.(   t   Nonet   formatt   __name__t   __doc__R   (   t   funcR   (   R   (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt	   decorator.   s    (    (   R   R   (    (   R   s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   _arithmetic   s    t   CCDDatac           B` sC  e  Z d  Z d „  Z e d „  ƒ Z e j d „  ƒ Z e d „  ƒ Z e j d „  ƒ Z e d „  ƒ Z e j d „  ƒ Z e d „  ƒ Z	 e	 j d	 „  ƒ Z	 e d
 „  ƒ Z
 e
 j d „  ƒ Z
 d d d e d „ Z d „  Z e e j ƒ e j ƒ Z e e j ƒ e j ƒ Z e e j ƒ e j ƒ Z e e j ƒ e j ƒ Z d „  Z RS(   u´  A class describing basic CCD data.

    The CCDData class is based on the NDData object and includes a data array,
    uncertainty frame, mask frame, flag frame, meta data, units, and WCS
    information for a single CCD image.

    Parameters
    -----------
    data : `~astropy.nddata.CCDData`-like or `numpy.ndarray`-like
        The actual data contained in this `~astropy.nddata.CCDData` object.
        Note that the data will always be saved by *reference*, so you should
        make a copy of the ``data`` before passing it in if that's the desired
        behavior.

    uncertainty : `~astropy.nddata.StdDevUncertainty`, `numpy.ndarray` or             None, optional
        Uncertainties on the data.
        Default is ``None``.

    mask : `numpy.ndarray` or None, optional
        Mask for the data, given as a boolean Numpy array with a shape
        matching that of the data. The values must be `False` where
        the data is *valid* and `True` when it is not (like Numpy
        masked arrays). If ``data`` is a numpy masked array, providing
        ``mask`` here will causes the mask from the masked array to be
        ignored.
        Default is ``None``.

    flags : `numpy.ndarray` or `~astropy.nddata.FlagCollection` or None,             optional
        Flags giving information about each pixel. These can be specified
        either as a Numpy array of any type with a shape matching that of the
        data, or as a `~astropy.nddata.FlagCollection` instance which has a
        shape matching that of the data.
        Default is ``None``.

    wcs : `~astropy.wcs.WCS` or None, optional
        WCS-object containing the world coordinate system for the data.
        Default is ``None``.

    meta : dict-like object or None, optional
        Metadata for this object. "Metadata" here means all information that
        is included with this object but not part of any other attribute
        of this particular object, e.g. creation date, unique identifier,
        simulation parameters, exposure time, telescope name, etc.

    unit : `~astropy.units.Unit` or str, optional
        The units of the data.
        Default is ``None``.

        .. warning::

            If the unit is ``None`` or not otherwise specified it will raise a
            ``ValueError``

    Raises
    ------
    ValueError
        If the ``uncertainty`` or ``mask`` inputs cannot be broadcast (e.g.,
        match shape) onto ``data``.

    Methods
    -------
    read(\*args, \**kwargs)
        ``Classmethod`` to create an CCDData instance based on a ``FITS`` file.
        This method uses :func:`fits_ccddata_reader` with the provided
        parameters.
    write(\*args, \**kwargs)
        Writes the contents of the CCDData instance into a new ``FITS`` file.
        This method uses :func:`fits_ccddata_writer` with the provided
        parameters.

    Notes
    -----
    `~astropy.nddata.CCDData` objects can be easily converted to a regular
     Numpy array using `numpy.asarray`.

    For example::

        >>> from astropy.nddata import CCDData
        >>> import numpy as np
        >>> x = CCDData([1,2,3], unit='adu')
        >>> np.asarray(x)
        array([1, 2, 3])

    This is useful, for example, when plotting a 2D image using
    matplotlib.

        >>> from astropy.nddata import CCDData
        >>> from matplotlib import pyplot as plt   # doctest: +SKIP
        >>> x = CCDData([[1,2,3], [4,5,6]], unit='adu')
        >>> plt.imshow(x)   # doctest: +SKIP

    c         O` s   d | k r% | j  d d  ƒ | d <n  d | k r@ t d ƒ ‚ n  t t |  ƒ j | | Ž  t r} |  j d  k r} t d ƒ ‚ n  d  S(   Nu   metau   headeru    can't have both header and meta.u%   a unit for CCDData must be specified.(   t   popR   t
   ValueErrort   superR    t   __init__R   t   unit(   R   t   argst   kwd(    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyR$      s    c         C` s   |  j  S(   N(   t   _data(   R   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   dataª   s    c         C` s   | |  _  d  S(   N(   R(   (   R   t   value(    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyR)   ®   s    c         C` s   |  j  S(   N(   t   _wcs(   R   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   wcs²   s    c         C` s   | |  _  d  S(   N(   R+   (   R   R*   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyR,   ¶   s    c         C` s   |  j  S(   N(   t   _unit(   R   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyR%   º   s    c         C` s   t  j | ƒ |  _ d  S(   N(   t   ut   UnitR-   (   R   R*   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyR%   ¾   s    c         C` s   |  j  S(   N(   t   _meta(   R   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   headerÂ   s    c         C` s   | |  _  d  S(   N(   t   meta(   R   R*   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyR1   Æ   s    c         C` s   |  j  S(   N(   t   _uncertainty(   R   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   uncertaintyÊ   s    c         C` sÑ   | d  k	 rÄ t | t ƒ rW t | d d  ƒ d  k	 rK | j | d t ƒ} n  | |  _ n^ t | t j ƒ r© | j	 |  j	 k rŠ t
 d ƒ ‚ n  t | ƒ |  _ t j d ƒ n t d ƒ ‚ |  |  j _ n	 | |  _ d  S(   Nu   _parent_nddatat   copyu)   uncertainty must have same shape as data.uC   array provided for uncertainty; assuming it is a StdDevUncertainty.uK   uncertainty must be an instance of a NDUncertainty object or a numpy array.(   R   t
   isinstanceR   t   getattrR   R   R3   t   npt   ndarrayt   shapeR"   R   R
   t   infot	   TypeErrort   parent_nddata(   R   R*   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyR4   Î   s    u   MASKu   UNCERTc         C` sB  t  |  j t j ƒ r' |  j j ƒ  } nZ t d g d t j ƒ  d d ƒ} x- |  j j ƒ  D] \ } } | j | | ƒ qX W| j } |  j t	 j
 k	 r© |  j j ƒ  | d <n  |  j rã |  j j d | ƒ }	 | j |	 d t d t ƒn  t j |  j | ƒ g }
 | re|  j d k	 ret |  j d	 ƒ s1t d
 ƒ ‚ n  t j |  j j t j ƒ d | ƒ} |
 j | ƒ n  | r|  j d k	 r|  j j j d k ržt d ƒ ‚ n  t |  j d ƒ ræ|  j j d k	 ræ|  j j |  j k ræt d ƒ ‚ n  t j |  j j d | ƒ} |
 j | ƒ n  | r/|  j  r/t! d ƒ ‚ n  t j" |
 ƒ } | S(   u9  Creates an HDUList object from a CCDData object.

        Parameters
        ----------
        hdu_mask, hdu_uncertainty, hdu_flags : str or None, optional
            If it is a string append this attribute to the HDUList as
            `~astropy.io.fits.ImageHDU` with the string as extension name.
            Flags are not supported at this time. If ``None`` this attribute
            is not appended.
            Default is ``'MASK'`` for mask, ``'UNCERT'`` for uncertainty and
            ``None`` for flags.

        wcs_relax : bool
            Value of the ``relax`` parameter to use in converting the WCS to a
            FITS header using `~astropy.wcs.WCS.to_header`. The common
            ``CTYPE`` ``RA---TAN-SIP`` and ``DEC--TAN-SIP`` requires
            ``relax=True`` for the ``-SIP`` part of the ``CTYPE`` to be
            preserved.

        Raises
        -------
        ValueError
            - If ``self.mask`` is set but not a `numpy.ndarray`.
            - If ``self.uncertainty`` is set but not a
              `~astropy.nddata.StdDevUncertainty`.
            - If ``self.uncertainty`` is set but has another unit then
              ``self.data``.

        NotImplementedError
            Saving flags is not supported.

        Returns
        -------
        hdulist : `~astropy.io.fits.HDUList`
        i   R2   R%   u   aduu   bunitt   relaxt	   useblankst   updateu   shapeu'   only a numpy.ndarray mask can be saved.t   nameu   StdDevUncertaintyu$   only StdDevUncertainty can be saved.u   unituN   saving uncertainties with a unit differingfrom the data unit is not supported.u8   adding the flags to a HDU is not supported at this time.N(#   R6   R1   R   t   HeaderR5   R    t   itemst   _insert_in_metadata_fits_safeR%   R.   t   dimensionless_unscaledt	   to_stringR,   t	   to_headert   extendR   R   t
   PrimaryHDUR)   t   maskR   t   hasattrR"   t   ImageHDUt   astypeR8   t   uint8t   appendR4   R   R   t   arrayt   flagst   NotImplementedErrort   HDUList(   R   t   hdu_maskt   hdu_uncertaintyt	   hdu_flagst	   wcs_relaxR1   t	   dummy_ccdt   kt   vt
   wcs_headert   hdust   hduMaskt	   hduUncertt   hdulist(    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   to_hduã   s>    %!		$	c         C` s   |  j  |  d t ƒS(   u6   
        Return a copy of the CCDData object.
        R5   (   R   R   (   R   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyR5   P  s    c         C` sz   t  | ƒ d k ri t  | ƒ d k ri | d  } | d j | ƒ f |  j d j | j ƒ  ƒ <| |  j | <n | |  j | <d S(   u  
        Insert key/value pair into metadata in a way that FITS can serialize.

        Parameters
        ----------
        key : str
            Key to be inserted in dictionary.

        value : str or None
            Value to be inserted.

        Notes
        -----
        This addresses a shortcoming of the FITS standard. There are length
        restrictions on both the ``key`` (8 characters) and ``value`` (72
        characters) in the FITS standard. There is a convention for handling
        long keywords and a convention for handling long values, but the
        two conventions cannot be used at the same time.

        This addresses that case by checking the length of the ``key`` and
        ``value`` and, if necessary, shortening the key.
        i   iH   u   Shortened name for {}u   HIERARCH {0}N(   t   lenR   R2   t   upper(   R   t   keyR*   t
   short_name(    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyRD   [  s
    $
+N(   R   t
   __module__R   R$   t   propertyR)   t   setterR,   R%   R1   R4   R   R   R`   R5   R   R8   t   addR   t   subtractt   multiplyt   true_dividet   divideRD   (    (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyR    =   s(   ^	l	u   JD-OBSu   MJD-OBSu   DATE-OBSc         C` sÊ   y t  |  ƒ } WnB t k
 rT } t j d j t | ƒ j t | ƒ ƒ ƒ |  d f SX| j	 j
 d so |  d f S|  j ƒ  } | j d t ƒ } x0 | D]( } | t k r” | j | d t ƒq” q” W| | f S(   ué   
    Generate a WCS object from a header and remove the WCS-specific
    keywords from the header.

    Parameters
    ----------

    hdr : astropy.io.fits.header or other dict-like

    Returns
    -------

    new_header, wcs
    uO   An exception happened while extracting WCS informations from the Header.
{}: {}i    R>   t   ignore_missingN(   R   t	   ExceptionR
   R;   R   t   typeR   t   strR   R,   t   ctypeR5   RG   R   t   _KEEP_THESE_KEYWORDS_IN_HEADERt   remove(   t   hdrR,   t   exct   new_hdrR[   RY   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   _generate_wcs_and_update_header„  s    
u   UNCERTu   MASKc         K` sî  i d d 6d d 6} xS | j  ƒ  D]E \ } }	 | | k r! d j | ƒ }
 t d j |
 |	 g ƒ ƒ ‚ q! q! Wt j |  |  l} | | j } | d k	 rº | | k rº t | | j	 ƒ } n d } | d k	 rô | | k rô | | j	 j
 t j ƒ } n d } | d k	 r!| | k r!t d ƒ ‚ n  | d k râ| | j	 d k râxŸ t t | ƒ ƒ D]ˆ } | j | ƒ | d	 d
 k rS| j | ƒ d d k rS| } | | j j ƒ  } | j | d t ƒ| } t j d j | ƒ ƒ PqSqSWn  d | k r"| d } | j ƒ  j ƒ  d k r(| j ƒ  } q(n d } | r•| d k ryy t j | ƒ } Wq’t k
 rut d j | ƒ ƒ ‚ q’Xq•t j d j | | ƒ ƒ n  | pž| } t | ƒ \ } } t | | j	 d | d | d | d | d | ƒ} Wd QX| S(   uU  
    Generate a CCDData object from a FITS file.

    Parameters
    ----------
    filename : str
        Name of fits file.

    hdu : int, optional
        FITS extension from which CCDData should be initialized. If zero and
        and no data in the primary extension, it will search for the first
        extension with data. The header will be added to the primary header.
        Default is ``0``.

    unit : `~astropy.units.Unit`, optional
        Units of the image data. If this argument is provided and there is a
        unit for the image in the FITS header (the keyword ``BUNIT`` is used
        as the unit, if present), this argument is used for the unit.
        Default is ``None``.

    hdu_uncertainty : str or None, optional
        FITS extension from which the uncertainty should be initialized. If the
        extension does not exist the uncertainty of the CCDData is ``None``.
        Default is ``'UNCERT'``.

    hdu_mask : str or None, optional
        FITS extension from which the mask should be initialized. If the
        extension does not exist the mask of the CCDData is ``None``.
        Default is ``'MASK'``.

    hdu_flags : str or None, optional
        Currently not implemented.
        Default is ``None``.

    kwd :
        Any additional keyword parameters are passed through to the FITS reader
        in :mod:`astropy.io.fits`; see Notes for additional discussion.

    Notes
    -----
    FITS files that contained scaled data (e.g. unsigned integer images) will
    be scaled and the keywords used to manage scaled data in
    :mod:`astropy.io.fits` are disabled.
    u   Image data must be scaled.u   do_not_scale_image_datau#   Scale information is not preserved.u
   scale_backu   unsupported keyword: {0}.u    u)   loading flags is currently not supported.i    i   u   ImageHDUu   datSpant   uniqueu%   first HDU with data is extension {0}.u   bunitu   aduuá   The Header value for the key BUNIT ({}) cannot be interpreted as valid unit. To successfully read the file as CCDData you can pass in a valid `unit` argument explicitly or change the header of the FITS file before reading it.uV   using the unit {0} passed to the FITS reader instead of the unit {1} in the FITS file.R2   R%   RJ   R4   R,   N(   RC   R   R<   t   joinR   t   openR1   R   R   R)   RM   R8   t   bool_RR   t   rangeRa   R;   t   fileinfoR5   RH   R   R
   t   stript   lowerR.   R/   R"   Rw   R    (   t   filenamet   hduR%   RU   RT   RV   R'   t   unsupport_open_keywordsRc   t   msgt   prefixR\   Rt   R4   RJ   t   it   comb_hdrt   fits_unit_stringt   use_unitR,   t   ccd_data(    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   fits_ccddata_reader­  s`    .
"

c         K` s2   |  j  d | d | d | ƒ } | j | |  d S(   u»  
    Write CCDData object to FITS file.

    Parameters
    ----------
    filename : str
        Name of file.

    hdu_mask, hdu_uncertainty, hdu_flags : str or None, optional
        If it is a string append this attribute to the HDUList as
        `~astropy.io.fits.ImageHDU` with the string as extension name.
        Flags are not supported at this time. If ``None`` this attribute
        is not appended.
        Default is ``'MASK'`` for mask, ``'UNCERT'`` for uncertainty and
        ``None`` for flags.

    kwd :
        All additional keywords are passed to :py:mod:`astropy.io.fits`

    Raises
    -------
    ValueError
        - If ``self.mask`` is set but not a `numpy.ndarray`.
        - If ``self.uncertainty`` is set but not a
          `~astropy.nddata.StdDevUncertainty`.
        - If ``self.uncertainty`` is set but has another unit then
          ``self.data``.

    NotImplementedError
        Saving flags is not supported.
    RT   RU   RV   N(   R`   t   writeto(   R‰   R€   RT   RU   RV   R'   R   (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   fits_ccddata_writer)  s    !	u   fits(,   R   t
   __future__R    R   R   R   t   numpyR8   t   compatR   t   nduncertaintyR   R   t   ioR   R   t    R	   R.   R
   R,   R   t   utils.decoratorsR   t   __all__R   R   R   R    Rr   Rw   R   RŠ   RŒ   t   delay_doc_updatest   register_readert   register_writert   register_identifiert   connectt   is_fitst   readt   AttributeErrort   __func__t   write(    (    (    s5   lib/python2.7/site-packages/astropy/nddata/ccddata.pyt   <module>   sD   "	#ÿ B		)	{%