σ
ίΘ[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 g Z
 d d d  Z d e f d	     YZ d
 e f d     YZ d e f d     YZ d e f d     YZ d d d d d  Z d S(   uι   
Combine 3 images to produce a properly-scaled RGB image following Lupton et al. (2004).

The three images must be aligned and have the same pixel scale and size.

For details, see : http://adsabs.harvard.edu/abs/2004PASP..116..133L
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   ZScaleIntervalu   make_lupton_rgbc         C` sk   | d k s | d k rC | d k o- | d k s? t d   n  |  S|  | | d } t j | d |  j S(   ux  
    Return a naive total intensity from the red, blue, and green intensities.

    Parameters
    ----------
    image_r : `~numpy.ndarray`
        Intensity of image to be mapped to red; or total intensity if ``image_g``
        and ``image_b`` are None.
    image_g : `~numpy.ndarray`, optional
        Intensity of image to be mapped to green.
    image_b : `~numpy.ndarray`, optional
        Intensity of image to be mapped to blue.

    Returns
    -------
    intensity : `~numpy.ndarray`
        Total intensity from the red, blue and green intensities, or ``image_r``
        if green and blue images are not provided.
    uD   please specify either a single image or red, green, and blue images.g      @t   dtypeN(   t   Nonet
   ValueErrort   npt   asarrayR   (   t   image_rt   image_gt   image_bt	   intensity(    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyt   compute_intensity   s    t   Mappingc           B` sA   e  Z d  Z d d d  Z d   Z d   Z d   Z d   Z RS(   uJ  
    Baseclass to map red, blue, green intensities into uint8 values.

    Parameters
    ----------
    minimum : float or sequence(3)
        Intensity that should be mapped to black (a scalar or array for R, G, B).
    image : `~numpy.ndarray`, optional
        An image used to calculate some parameters of some mappings.
    c         C` s   t  t j t j  j  |  _ y t |  Wn t k
 rL d | g } n Xt |  d k rn t d   n  | |  _	 t j
 |  |  _ d  S(   Ni   u)   please provide 1 or 3 values for minimum.(   t   floatR   t   iinfot   uint8t   maxt	   _uint8Maxt   lent	   TypeErrorR   t   minimumR	   t   _image(   t   selfR   t   image(    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyt   __init__>   s    	c         C` s¦   t  j |  } t  j |  } t  j |  } | j | j k sQ | j | j k r~ d } t | j | j | j | j    n  t  j |  j | | |   j t  j  S(   uθ  
        Convert 3 arrays, image_r, image_g, and image_b into an 8-bit RGB image.

        Parameters
        ----------
        image_r : `~numpy.ndarray`
            Image to map to red.
        image_g : `~numpy.ndarray`
            Image to map to green.
        image_b : `~numpy.ndarray`
            Image to map to blue.

        Returns
        -------
        RGBimage : `~numpy.ndarray`
            RGB (integer, 8-bits per channel) color image as an NxNx3 numpy array.
        u/   The image shapes must match. r: {}, g: {} b: {}(	   R   R	   t   shapeR   t   formatt   dstackt   _convert_images_to_uint8t   astypeR   (   R   R
   R   R   t   msg(    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyt   make_rgb_imageK   s    $'c         C` s   t  | | |  S(   uώ  
        Return the total intensity from the red, blue, and green intensities.
        This is a naive computation, and may be overridden by subclasses.

        Parameters
        ----------
        image_r : `~numpy.ndarray`
            Intensity of image to be mapped to red; or total intensity if
            ``image_g`` and ``image_b`` are None.
        image_g : `~numpy.ndarray`, optional
            Intensity of image to be mapped to green.
        image_b : `~numpy.ndarray`, optional
            Intensity of image to be mapped to blue.

        Returns
        -------
        intensity : `~numpy.ndarray`
            Total intensity from the red, blue and green intensities, or
            ``image_r`` if green and blue images are not provided.
        (   R   (   R   R
   R   R   (    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyR   g   s    c         C` s9   t  j d d d d   t  j | d |  j  SWd QXd S(   uί  
        Return an array which, when multiplied by an image, returns that image
        mapped to the range of a uint8, [0, 255] (but not converted to uint8).

        The intensity is assumed to have had minimum subtracted (as that can be
        done per-band).

        Parameters
        ----------
        I : `~numpy.ndarray`
            Intensity to be mapped.

        Returns
        -------
        mapped_I : `~numpy.ndarray`
            ``I`` mapped to uint8
        t   invalidu   ignoret   dividei    N(   R   t   errstatet   clipR   (   R   t   I(    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyt   map_intensity_to_uint8~   s    c         C` sΒ  | |  j  d } | |  j  d } | |  j  d } |  j |  j | | |   } | | | g } x( | D]  } | | 9} d | | d k  <qg W|  j } | \ } }	 }
 t j d d d d  xω t |  D]λ \ } } t j | |	 k t j | |
 k t j | | k | | | |  t j |
 | k | | |
 |   t j |	 |
 k t j |	 | k | | |	 |  t j |
 | k | | |
 |    j t j	  } | | | | k <| | | <qΙ WWd QX| S(   u\   Use the mapping to convert images image_r, image_g, and image_b to a triplet of uint8 imagesi    i   i   R#   u   ignoreR$   N(
   R   R(   R   R   R   R%   t	   enumeratet   whereR    R   (   R   R
   R   R   t   fact	   image_rgbt   ct   pixmaxt   r0t   g0t   b0t   i(    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyR      s,    
	 # 5N(	   t   __name__t
   __module__t   __doc__R   R   R"   R   R(   R   (    (    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyR   2   s   
			t   LinearMappingc           B` s)   e  Z d  Z d d d d  Z d   Z RS(   u  
    A linear map map of red, blue, green intensities into uint8 values.

    A linear stretch from [minimum, maximum].
    If one or both are omitted use image min and/or max to set them.

    Parameters
    ----------
    minimum : float
        Intensity that should be mapped to black (a scalar or array for R, G, B).
    maximum : float
        Intensity that should be mapped to white (a scalar).
    c         C` sΨ   | d  k s | d  k rl | d  k r3 t d   n  | d  k rN | j   } n  | d  k rl | j   } ql n  t j |  d | d | | |  _ | d  k r¦ d  |  _ n. | | k rΑ t d   n  t | |  |  _ d  S(   NuC   you must provide an image if you don't set both minimum and maximumR   R   u,   minimum and maximum values must not be equal(	   R   R   t   minR   R   R   t   maximumt   _rangeR   (   R   R   R8   R   (    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyR   Β   s    	c         C` se   t  j d d d d  G t  j | d k d t  j | |  j k |  j | |  j |  j   SWd  QXd  S(   NR#   u   ignoreR$   i    (   R   R%   R*   R9   R   (   R   R'   (    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyR(   Φ   s    N(   R3   R4   R5   R   R   R(   (    (    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyR6   ³   s   t   AsinhMappingc           B` s#   e  Z d  Z d d  Z d   Z RS(   uδ  
    A mapping for an asinh stretch (preserving colours independent of brightness)

    x = asinh(Q (I - minimum)/stretch)/Q

    This reduces to a linear stretch if Q == 0

    See http://adsabs.harvard.edu/abs/2004PASP..116..133L

    Parameters
    ----------

    minimum : float
        Intensity that should be mapped to black (a scalar or array for R, G, B).
    stretch : float
        The linear stretch of the image.
    Q : float
        The asinh softening parameter.
    i   c         C` s   t  j |  |  d d } t |  | k  r5 d } n d } | | k rP | } n  d } | |  j t j | |  |  _ | t |  |  _ d  S(   Ng      π?i   i   gΉ?g    _ Bi   (	   R   R   t   absR   R   t   arcsinht   _slopeR   t   _soften(   R   R   t   stretcht   Qt   epsilont   Qmaxt   frac(    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyR   ρ   s    
		!c      
   C` sW   t  j d d d d  9 t  j | d k d t  j | |  j  |  j |  SWd  QXd  S(   NR#   u   ignoreR$   i    (   R   R%   R*   R<   R>   R=   (   R   R'   (    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyR(     s    (   R3   R4   R5   R   R(   (    (    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyR:   ά   s   t   AsinhZScaleMappingc           B` s#   e  Z d  Z d d d d d  Z RS(   uj  
    A mapping for an asinh stretch, estimating the linear stretch by zscale.

    x = asinh(Q (I - z1)/(z2 - z1))/Q

    Parameters
    ----------
    image1 : `~numpy.ndarray` or a list of arrays
        The image to analyse, or a list of 3 images to be converted to
        an intensity image.
    image2 : `~numpy.ndarray`, optional
        the second image to analyse (must be specified with image3).
    image3 : `~numpy.ndarray`, optional
        the third image to analyse (must be specified with image2).
    Q : float, optional
        The asinh softening parameter. Default is 8.
    pedestal : float or sequence(3), optional
        The value, or array of 3 values, to subtract from the images; or None.

    Notes
    -----
    pedestal, if not None, is removed from the images when calculating the
    zscale stretch, and added back into Mapping.minimum[]
    i   c         C` s·  | d k s | d k rK | d k o- | d k s? t d   n  | g } n | | | g } | d k	 ry t |  Wn t k
 r d | g } n Xt |  d k rΆ t d   n  t |  } xU t |  D]1 \ } } | | d k rΟ | | | | | <qΟ qΟ Wn t |  d g } t |   } t   j |  }	 t	 d | |	  }
 |
 j
 |
 j d } |
 j } x* t |  D] \ } } | | c | 7<qtWt j |  | | |  | |  _ d S(   u	   
        u5   please specify either a single image or three images.i   u    please provide 1 or 3 pedestals.g        R   i    N(   R   R   R   R   t   listR)   R   R   t
   get_limitsR6   R8   R   R:   R   R   (   R   t   image1t   image2t   image3R@   t   pedestalR   R2   t   imt   zscale_limitst   zscaleR?   R   t   level(    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyR      s4    	N(   R3   R4   R5   R   R   (    (    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyRD     s   i   i   c   
      C` sY   t  | | |  } | j |  | |  } | rU d d l }	 |	 j j | | d d n  | S(   u΅  
    Return a Red/Green/Blue color image from up to 3 images using an asinh stretch.
    The input images can be int or float, and in any range or bit-depth.

    For a more detailed look at the use of this method, see the document
    :ref:`astropy-visualization-rgb`.

    Parameters
    ----------

    image_r : `~numpy.ndarray`
        Image to map to red.
    image_g : `~numpy.ndarray`
        Image to map to green.
    image_b : `~numpy.ndarray`
        Image to map to blue.
    minimum : float
        Intensity that should be mapped to black (a scalar or array for R, G, B).
    stretch : float
        The linear stretch of the image.
    Q : float
        The asinh softening parameter.
    filename: str
        Write the resulting RGB image to a file (file type determined
        from extension).

    Returns
    -------
    rgb : `~numpy.ndarray`
        RGB (integer, 8-bits per channel) color image as an NxNx3 numpy array.
    i    Nt   originu   lower(   R:   R"   t   matplotlib.imageR   t   imsave(
   R
   R   R   R   R?   R@   t   filenamet   asinhMapt   rgbt
   matplotlib(    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyt   make_lupton_rgbJ  s    !(   R5   t
   __future__R    R   R   R   t   numpyR   t    R   t   __all__R   R   t   objectR   R6   R:   RD   RV   (    (    (    s?   lib/python2.7/site-packages/astropy/visualization/lupton_rgb.pyt   <module>   s   "	 )*D	