B
    q\a                 @   sP   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ dd
dZ	dddZ
dS )    N)LooseVersion)simple_norm)log)getdatalinear      ?皙?Greys_rc             C   s  ddl }ddlm} ddlm} yt|}W n tk
r@   Y nX yt| |}W n, tk
r| } zt	
| dS d}~X Y nX |jdkrt	
d| |dkrtj| d }|drtj|d }|d7 }tj|d dd }| dkrt|jtd	kr|ddd
 }||jkr:t	
d| dS t||||||||	|
d	}|j||||d|d t	d| dS )a	  
    Create a bitmap file from a FITS image, applying a stretching
    transform between minimum and maximum cut levels and a matplotlib
    colormap.

    Parameters
    ----------
    filename : str
        The filename of the FITS file.
    ext : int
        FITS extension name or number of the image to convert.  The
        default is 0.
    out_fn : str
        The filename of the output bitmap image.  The type of bitmap
        is determined by the filename extension (e.g. '.jpg', '.png').
        The default is a PNG file with the same name as the FITS file.
    stretch : {{'linear', 'sqrt', 'power', log', 'asinh'}}
        The stretching function to apply to the image.  The default is
        'linear'.
    power : float, optional
        The power index for ``stretch='power'``.  The default is 1.0.
    asinh_a : float, optional
        For ``stretch='asinh'``, the value where the asinh curve
        transitions from linear to logarithmic behavior, expressed as a
        fraction of the normalized image.  Must be in the range between
        0 and 1.  The default is 0.1.
    min_cut : float, optional
        The pixel value of the minimum cut level.  Data values less than
        ``min_cut`` will set to ``min_cut`` before stretching the image.
        The default is the image minimum.  ``min_cut`` overrides
        ``min_percent``.
    max_cut : float, optional
        The pixel value of the maximum cut level.  Data values greater
        than ``min_cut`` will set to ``min_cut`` before stretching the
        image.  The default is the image maximum.  ``max_cut`` overrides
        ``max_percent``.
    min_percent : float, optional
        The percentile value used to determine the pixel value of
        minimum cut level.  The default is 0.0.  ``min_percent``
        overrides ``percent``.
    max_percent : float, optional
        The percentile value used to determine the pixel value of
        maximum cut level.  The default is 100.0.  ``max_percent``
        overrides ``percent``.
    percent : float, optional
        The percentage of the image values used to determine the pixel
        values of the minimum and maximum cut levels.  The lower cut
        level will set at the ``(100 - percent) / 2`` percentile, while
        the upper cut level will be set at the ``(100 + percent) / 2``
        percentile.  The default is 100.0.  ``percent`` is ignored if
        either ``min_percent`` or ``max_percent`` is input.
    cmap : str
        The matplotlib color map name.  The default is 'Greys_r'.
    r   N      z,data in FITS extension {0} is not a 2D arrayz.fitsz.pngZpngz2.0.0z,{0} is not a valid matplotlib colormap name.)stretchpowerasinh_amin_cutmax_cutmin_percentmax_percentpercentlower)cmaporiginformatzSaved file to {0}.)
matplotlibZmatplotlib.cmcmZmatplotlib.imageimageint
ValueErrorr   	Exceptionr   Zcriticalndimr   ospathsplitextendswithr   r   __version__Zdatadr   Zimsaveinfo)filenameextout_fnr   r   r   r   r   r   r   r   r   r   r   Zmimgr   eZ
out_formatZnorm r*   Hlib/python3.7/site-packages/astropy/visualization/scripts/fits2bitmap.pyfits2bitmap   sF    ;



r,   c             C   sF  dd l }|jdd}|jdddddd |jd	d
td dd |jdtddd |jdtddd |jdtddd |jdtd dd |jdtd dd |jdtd dd |jdtd dd |jdtd d d |jd!d"td#d$d |jd
d%d&d' || } xF| jD ]<}t|| j| j	| j
| j| j| j| j| j| j| j| jd( qW d S ))Nr   z'Create a bitmap file from a FITS image.)Zdescriptionz-ez--extZhduz8Specify the HDU extension number or name (Default is 0).)metavardefaulthelpz-or&   zZFilename for the output image (Default is a PNG file with the same name as the FITS file).)r-   typer.   r/   z	--stretchr   z^Type of image stretching ("linear", "sqrt", "power", "log", or "asinh") (Default is "linear").)r0   r.   r/   z--powerg      ?z4Power index for "power" stretching (Default is 1.0).z	--asinh_ag?zThe value in normalized image where the asinh curve transitions from linear to logarithmic behavior (used only for "asinh" stretch) (Default is 0.1).z	--min_cutzHThe pixel value of the minimum cut level (Default is the image minimum).z	--max_cutzHThe pixel value of the maximum cut level (Default is the image maximum).z--min_percentzLThe percentile value used to determine the minimum cut level (Default is 0).z--max_percentzNThe percentile value used to determine the maximum cut level (Default is 100).z	--percentz}The percentage of the image values used to determine the pixel values of the minimum and maximum cut levels (Default is 100).z--cmapZcolormap_namer	   z1matplotlib color map name (Default is "Greys_r").+z)Path to one or more FITS files to convert)nargsr/   )r'   r(   r   r   r   r   r   r   r   r   r   )argparseArgumentParseradd_argumentstrfloat
parse_argsr&   r,   r'   or   r   r   r   r   r   r   r   r   )argsr3   parserr&   r*   r*   r+   mainy   sD    










r<   )r   Nr   r   r   NNNNNr	   )N)r    Zdistutils.versionr   Z#astropy.visualization.mpl_normalizer   Zastropyr   Zastropy.io.fitsr   r,   r<   r*   r*   r*   r+   <module>   s      
k