B
    d|                 @   s"  d Z ddlmZ ddlZddlZ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 ddlmZmZ dd	lmZ d
dlmZ d
dlmZ dddddddgZG dd dejZeeje _ G dd de!Z"G dd de!Z#dddZ$dej%dfddZ&dd Z'dd Z(G dd dZ)dS ) z=
This module includes helper functions for array operations.
    )deepcopyN)units)SkyCoord)lazyproperty)AstropyDeprecationWarning)skycoord_to_pixelproj_plane_pixel_scales)Sip   )block_reduce)block_replicateextract_array	add_arraysubpixel_indicesoverlap_slicesNoOverlapErrorPartialOverlapErrorCutout2Dc               @   s   e Zd Zdd ZdS )_ModuleWithDeprecationc             C   s<   d}||kr0t | dt t| d| S t| |S )N)r   r   zU was moved to the astropy.nddata.blocks module.  Please update your import statement._)warningswarnr   object__getattribute__)selfname
deprecated r   a/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/nddata/utils.pyr      s    z'_ModuleWithDeprecation.__getattribute__N)__name__
__module____qualname__r   r   r   r   r   r      s   r   c               @   s   e Zd ZdZdS )r   z>Raised when determining the overlap of non-overlapping arrays.N)r   r    r!   __doc__r   r   r   r   r   ,   s   c               @   s   e Zd ZdZdS )r   z*Raised when arrays only partially overlap.N)r   r    r!   r"   r   r   r   r   r   1   s   partialc             C   s  |dkrt dt|r |f}t| r0| f} t|r@|f}tt| rXt dt|t| krpt dt|t|krt ddd t||D }dd t||D }x|D ]}|d	k rtd
qW x&t|| D ]\}}||krtd
qW |dkrNx |D ]}|d	k rtdqW x*t|| D ]\}}||kr.tdq.W t	dd t||| D }	|dkrt	dd |	D }
nt	dd t||| D }
|	|
fS )a	  
    Get slices for the overlapping part of a small and a large array.

    Given a certain position of the center of the small array, with
    respect to the large array, tuples of slices are returned which can be
    used to extract, add or subtract the small array at the given
    position. This function takes care of the correct behavior at the
    boundaries, where the small array is cut of appropriately.
    Integer positions are at the pixel centers.

    Parameters
    ----------
    large_array_shape : tuple of int or int
        The shape of the large array (for 1D arrays, this can be an
        `int`).
    small_array_shape : int or tuple thereof
        The shape of the small array (for 1D arrays, this can be an
        `int`).  See the ``mode`` keyword for additional details.
    position : number or tuple thereof
        The position of the small array's center with respect to the
        large array.  The pixel coordinates should be in the same order
        as the array shape.  Integer positions are at the pixel centers.
        For any axis where ``small_array_shape`` is even, the position
        is rounded up, e.g. extracting two elements with a center of
        ``1`` will define the extracted region as ``[0, 1]``.
    mode : {'partial', 'trim', 'strict'}, optional
        In ``'partial'`` mode, a partial overlap of the small and the
        large array is sufficient.  The ``'trim'`` mode is similar to
        the ``'partial'`` mode, but ``slices_small`` will be adjusted to
        return only the overlapping elements.  In the ``'strict'`` mode,
        the small array has to be fully contained in the large array,
        otherwise an `~astropy.nddata.utils.PartialOverlapError` is
        raised.  In all modes, non-overlapping arrays will raise a
        `~astropy.nddata.utils.NoOverlapError`.

    Returns
    -------
    slices_large : tuple of slice
        A tuple of slice objects for each axis of the large array, such
        that ``large_array[slices_large]`` extracts the region of the
        large array that overlaps with the small array.
    slices_small : tuple of slice
        A tuple of slice objects for each axis of the small array, such
        that ``small_array[slices_small]`` extracts the region that is
        inside the large array.
    )r#   trimstrictz0Mode can be only "partial", "trim", or "strict".z6Input position contains invalid values (NaNs or infs).zT"large_array_shape" and "small_array_shape" must have the same number of dimensions.zJ"position" must have the same number of dimensions as "small_array_shape".c             S   s&   g | ]\}}t t||d   qS )g       @)intnpceil).0possmall_shaper   r   r   
<listcomp>}   s   z"overlap_slices.<locals>.<listcomp>c             S   s&   g | ]\}}t t||d   qS )g       @)r&   r'   r(   )r)   r*   r+   r   r   r   r,      s   r   zArrays do not overlap.r%   zArrays overlap only partially.c             s   s*   | ]"\}}}t td |t||V  qdS )r   N)slicemaxmin)r)   indices_minindices_maxlarge_shaper   r   r   	<genexpr>   s   z!overlap_slices.<locals>.<genexpr>r$   c             s   s    | ]}t d |j|j V  qdS )r   N)r-   stopstart)r)   Zslcr   r   r   r3      s   c             s   s4   | ],\}}}t td | t|| || V  qdS )r   N)r-   r.   r/   )r)   r0   r1   r2   r   r   r   r3      s   )

ValueErrorr'   isscalaranyisfinitelenzipr   r   tuple)Zlarge_array_shapeZsmall_array_shapepositionmoder0   r1   Ze_maxZe_minr2   Zslices_largeZslices_smallr   r   r   r   6   sN    1








Fc          
   C   s  t |r|f}t |r |f}|dkr0tdt| j|||d\}}| | }|rfdd t||D }	|j|kr|dkrt j|| jd}y||dd< W n4 tk
r }
 z|
 jd	7  _|
W dd}
~
X Y nX | | ||< |rd
d t|	|D }	|r|t	|	fS |S dS )a  
    Extract a smaller array of the given shape and position from a
    larger array.

    Parameters
    ----------
    array_large : ndarray
        The array from which to extract the small array.
    shape : int or tuple thereof
        The shape of the extracted array (for 1D arrays, this can be an
        `int`).  See the ``mode`` keyword for additional details.
    position : number or tuple thereof
        The position of the small array's center with respect to the
        large array.  The pixel coordinates should be in the same order
        as the array shape.  Integer positions are at the pixel centers
        (for 1D arrays, this can be a number).
    mode : {'partial', 'trim', 'strict'}, optional
        The mode used for extracting the small array.  For the
        ``'partial'`` and ``'trim'`` modes, a partial overlap of the
        small array and the large array is sufficient.  For the
        ``'strict'`` mode, the small array has to be fully contained
        within the large array, otherwise an
        `~astropy.nddata.utils.PartialOverlapError` is raised.   In all
        modes, non-overlapping arrays will raise a
        `~astropy.nddata.utils.NoOverlapError`.  In ``'partial'`` mode,
        positions in the small array that do not overlap with the large
        array will be filled with ``fill_value``.  In ``'trim'`` mode
        only the overlapping elements are returned, thus the resulting
        small array may be smaller than the requested ``shape``.
    fill_value : number, optional
        If ``mode='partial'``, the value to fill pixels in the extracted
        small array that do not overlap with the input ``array_large``.
        ``fill_value`` will be changed to have the same ``dtype`` as the
        ``array_large`` array, with one exception. If ``array_large``
        has integer type and ``fill_value`` is ``np.nan``, then a
        `ValueError` will be raised.
    return_position : bool, optional
        If `True`, return the coordinates of ``position`` in the
        coordinate system of the returned array.

    Returns
    -------
    array_small : ndarray
        The extracted array.
    new_position : tuple
        If ``return_position`` is true, this tuple will contain the
        coordinates of the input ``position`` in the coordinate system
        of ``array_small``. Note that for partially overlapping arrays,
        ``new_position`` might actually be outside of the
        ``array_small``; ``array_small[new_position]`` might give wrong
        results if any element in ``new_position`` is negative.

    Examples
    --------
    We consider a large array with the shape 11x10, from which we extract
    a small array of shape 3x5:

    >>> import numpy as np
    >>> from astropy.nddata.utils import extract_array
    >>> large_array = np.arange(110).reshape((11, 10))
    >>> extract_array(large_array, (3, 5), (7, 7))
    array([[65, 66, 67, 68, 69],
           [75, 76, 77, 78, 79],
           [85, 86, 87, 88, 89]])
    )r#   r$   r%   z0Valid modes are 'partial', 'trim', and 'strict'.)r>   c             S   s   g | ]\}}||j  qS r   )r5   )r)   isr   r   r   r,      s    z!extract_array.<locals>.<listcomp>r#   )dtypeN)zfill_value is inconsistent with the data type of the input array (e.g., fill_value cannot be set to np.nan if the input array has integer type). Please change either the input array dtype or the fill_value.c             S   s   g | ]\}}||j  qS r   )r5   )r)   r?   r@   r   r   r   r,     s    )
r'   r7   r6   r   shaper;   zerosrA   argsr<   )array_largerB   r=   r>   
fill_valuereturn_positionlarge_slicessmall_slicesZextracted_arrayZnew_positionexcr   r   r   r      s2    D



c             C   sT   t dd t| j|jD rHt| j|j|\}}| |  || 7  < | S tddS )a  
    Add a smaller array at a given position in a larger array.

    Parameters
    ----------
    array_large : ndarray
        Large array.
    array_small : ndarray
        Small array to add. Can be equal to ``array_large`` in size in a given
        dimension, but not larger.
    position : tuple
        Position of the small array's center, with respect to the large array.
        Coordinates should be in the same order as the array shape.

    Returns
    -------
    new_array : ndarray
        The new array formed from the sum of ``array_large`` and
        ``array_small``.

    Notes
    -----
    The addition is done in-place.

    Examples
    --------
    We consider a large array of zeros with the shape 5x5 and a small
    array of ones with a shape of 3x3:

    >>> import numpy as np
    >>> from astropy.nddata.utils import add_array
    >>> large_array = np.zeros((5, 5))
    >>> small_array = np.ones((3, 3))
    >>> add_array(large_array, small_array, (1, 2))  # doctest: +FLOAT_CMP
    array([[0., 1., 1., 1., 0.],
           [0., 1., 1., 1., 0.],
           [0., 1., 1., 1., 0.],
           [0., 0., 0., 0., 0.],
           [0., 0., 0., 0., 0.]])
    c             s   s   | ]\}}||kV  qd S )Nr   )r)   r2   r+   r   r   r   r3   6  s    zadd_array.<locals>.<genexpr>z'Can't add array. Small array too large.N)allr;   rB   r   r6   )rE   Zarray_smallr=   rH   rI   r   r   r   r     s    *
c             C   s&   t t | d d }t || S )a  
    Convert decimal points to indices, given a subsampling factor.

    This discards the integer part of the position and uses only the decimal
    place, and converts this to a subpixel position depending on the
    subsampling specified. The center of a pixel corresponds to an integer
    position.

    Parameters
    ----------
    position : ndarray or array-like
        Positions in pixels.
    subsampling : int
        Subsampling factor per pixel.

    Returns
    -------
    indices : ndarray
        The integer subpixel indices corresponding to the input positions.

    Examples
    --------

    If no subsampling is used, then the subpixel indices returned are always 0:

    >>> from astropy.nddata.utils import subpixel_indices
    >>> subpixel_indices([1.2, 3.4, 5.6], 1)  # doctest: +FLOAT_CMP
    array([0., 0., 0.])

    If instead we use a subsampling of 2, we see that for the two first values
    (1.1 and 3.4) the subpixel position is 1, while for 5.6 it is 0. This is
    because the values of 1, 3, and 6 lie in the center of pixels, and 1.1 and
    3.4 lie in the left part of the pixels and 5.6 lies in the right part.

    >>> subpixel_indices([1.2, 3.4, 5.5], 2)  # doctest: +FLOAT_CMP
    array([1., 1., 0.])
    g      ?r   )r'   modf
asanyarrayfloor)r=   Zsubsampling	fractionsr   r   r   r   A  s    'c               @   s   e Zd ZdZddejdfddZdd Zd	d
 Zd#ddZ	e
dd Ze
dd Zedd Zedd Ze
dd Zedd Zedd Zedd Zedd Zedd  Zed!d" ZdS )$r   a  
    Create a cutout object from a 2D array.

    The returned object will contain a 2D cutout array.  If
    ``copy=False`` (default), the cutout array is a view into the
    original ``data`` array, otherwise the cutout array will contain a
    copy of the original data.

    If a `~astropy.wcs.WCS` object is input, then the returned object
    will also contain a copy of the original WCS, but updated for the
    cutout array.

    For example usage, see :ref:`cutout_images`.

    .. warning::

        The cutout WCS object does not currently handle cases where the
        input WCS object contains distortion lookup tables described in
        the `FITS WCS distortion paper
        <https://www.atnf.csiro.au/people/mcalabre/WCS/dcs_20040422.pdf>`__.

    Parameters
    ----------
    data : ndarray
        The 2D data array from which to extract the cutout array.

    position : tuple or `~astropy.coordinates.SkyCoord`
        The position of the cutout array's center with respect to
        the ``data`` array.  The position can be specified either as
        a ``(x, y)`` tuple of pixel coordinates or a
        `~astropy.coordinates.SkyCoord`, in which case ``wcs`` is a
        required input.

    size : int, array-like, or `~astropy.units.Quantity`
        The size of the cutout array along each axis.  If ``size``
        is a scalar number or a scalar `~astropy.units.Quantity`,
        then a square cutout of ``size`` will be created.  If
        ``size`` has two elements, they should be in ``(ny, nx)``
        order.  Scalar numbers in ``size`` are assumed to be in
        units of pixels.  ``size`` can also be a
        `~astropy.units.Quantity` object or contain
        `~astropy.units.Quantity` objects.  Such
        `~astropy.units.Quantity` objects must be in pixel or
        angular units.  For all cases, ``size`` will be converted to
        an integer number of pixels, rounding the the nearest
        integer.  See the ``mode`` keyword for additional details on
        the final cutout size.

        .. note::
            If ``size`` is in angular units, the cutout size is
            converted to pixels using the pixel scales along each
            axis of the image at the ``CRPIX`` location.  Projection
            and other non-linear distortions are not taken into
            account.

    wcs : `~astropy.wcs.WCS`, optional
        A WCS object associated with the input ``data`` array.  If
        ``wcs`` is not `None`, then the returned cutout object will
        contain a copy of the updated WCS for the cutout data array.

    mode : {'trim', 'partial', 'strict'}, optional
        The mode used for creating the cutout data array.  For the
        ``'partial'`` and ``'trim'`` modes, a partial overlap of the
        cutout array and the input ``data`` array is sufficient.
        For the ``'strict'`` mode, the cutout array has to be fully
        contained within the ``data`` array, otherwise an
        `~astropy.nddata.utils.PartialOverlapError` is raised.   In
        all modes, non-overlapping arrays will raise a
        `~astropy.nddata.utils.NoOverlapError`.  In ``'partial'``
        mode, positions in the cutout array that do not overlap with
        the ``data`` array will be filled with ``fill_value``.  In
        ``'trim'`` mode only the overlapping elements are returned,
        thus the resulting cutout array may be smaller than the
        requested ``shape``.

    fill_value : float or int, optional
        If ``mode='partial'``, the value to fill pixels in the
        cutout array that do not overlap with the input ``data``.
        ``fill_value`` must have the same ``dtype`` as the input
        ``data`` array.

    copy : bool, optional
        If `False` (default), then the cutout data will be a view
        into the original ``data`` array.  If `True`, then the
        cutout data will hold a copy of the original ``data`` array.

    Attributes
    ----------
    data : 2D `~numpy.ndarray`
        The 2D cutout array.

    shape : (2,) tuple
        The ``(ny, nx)`` shape of the cutout array.

    shape_input : (2,) tuple
        The ``(ny, nx)`` shape of the input (original) array.

    input_position_cutout : (2,) tuple
        The (unrounded) ``(x, y)`` position with respect to the cutout
        array.

    input_position_original : (2,) tuple
        The original (unrounded) ``(x, y)`` input position (with respect
        to the original array).

    slices_original : (2,) tuple of slice object
        A tuple of slice objects for the minimal bounding box of the
        cutout with respect to the original array.  For
        ``mode='partial'``, the slices are for the valid (non-filled)
        cutout values.

    slices_cutout : (2,) tuple of slice object
        A tuple of slice objects for the minimal bounding box of the
        cutout with respect to the cutout array.  For
        ``mode='partial'``, the slices are for the valid (non-filled)
        cutout values.

    xmin_original, ymin_original, xmax_original, ymax_original : float
        The minimum and maximum ``x`` and ``y`` indices of the minimal
        rectangular region of the cutout array with respect to the
        original array.  For ``mode='partial'``, the bounding box
        indices are for the valid (non-filled) cutout values.  These
        values are the same as those in `bbox_original`.

    xmin_cutout, ymin_cutout, xmax_cutout, ymax_cutout : float
        The minimum and maximum ``x`` and ``y`` indices of the minimal
        rectangular region of the cutout array with respect to the
        cutout array.  For ``mode='partial'``, the bounding box indices
        are for the valid (non-filled) cutout values.  These values are
        the same as those in `bbox_cutout`.

    wcs : `~astropy.wcs.WCS` or None
        A WCS object associated with the cutout array if a ``wcs``
        was input.

    Examples
    --------
    >>> import numpy as np
    >>> from astropy.nddata.utils import Cutout2D
    >>> from astropy import units as u
    >>> data = np.arange(20.).reshape(5, 4)
    >>> cutout1 = Cutout2D(data, (2, 2), (3, 3))
    >>> print(cutout1.data)  # doctest: +FLOAT_CMP
    [[ 5.  6.  7.]
     [ 9. 10. 11.]
     [13. 14. 15.]]

    >>> print(cutout1.center_original)
    (2.0, 2.0)
    >>> print(cutout1.center_cutout)
    (1.0, 1.0)
    >>> print(cutout1.origin_original)
    (1, 1)

    >>> cutout2 = Cutout2D(data, (2, 2), 3)
    >>> print(cutout2.data)  # doctest: +FLOAT_CMP
    [[ 5.  6.  7.]
     [ 9. 10. 11.]
     [13. 14. 15.]]

    >>> size = u.Quantity([3, 3], u.pixel)
    >>> cutout3 = Cutout2D(data, (0, 0), size)
    >>> print(cutout3.data)  # doctest: +FLOAT_CMP
    [[0. 1.]
     [4. 5.]]

    >>> cutout4 = Cutout2D(data, (0, 0), (3 * u.pixel, 3))
    >>> print(cutout4.data)  # doctest: +FLOAT_CMP
    [[0. 1.]
     [4. 5.]]

    >>> cutout5 = Cutout2D(data, (0, 0), (3, 3), mode='partial')
    >>> print(cutout5.data)  # doctest: +FLOAT_CMP
    [[nan nan nan]
     [nan  0.  1.]
     [nan  4.  5.]]
    Nr$   Fc             C   s  |d krt |dd }t|tr<|d kr.tdt||dd}t|rRt|d}t|tj	rt
|}t|dkrt|d}t|dkrtdtdt}d }	xt|D ]\}
}t|tj	stt||
 ||
< q|jtjkrtt|j||
< q|jjdkrd|d kr"td	|	d krDt	t||jj|
 }	tt||	|
   ||
< qtd
qW t|}|d d d }t|t||||dd\}}|rt|}|| _|d d d | _t|j |||d\}}|| _!|| _"| jj | _ || _#|| _$| j%\\| _&| _'\| _(| _)| j*\\| _+| _,\| _-| _.| j/d | j"d j0 | j/d | j"d j0 f| _1|d k	rt2|| _| jj j3| j18  _3| jj | j_4|j5d k	rt6|j5j7|j5j8|j5j9|j5j:|j5j3| j1 | j_5nd | _d S )Nwcsz+wcs must be input if position is a SkyCoordrK   )r>      r
   z#size must have at most two elementsZanglez:wcs must be input if any element of size has angular unitsz=shape can contain Quantities with only pixel or angular unitsT)r>   rF   rG   r   );getattr
isinstancer   r6   r   r'   r7   repeatuZQuantityZ
atleast_1dr:   rC   Zastyper&   	enumerateroundunitZpixelvalueZphysical_typer   rP   ZcunitZ	decomposerM   r   r<   copydatainput_position_cutoutr   rB   slices_originalslices_cutoutinput_position_originalZshape_inputbbox_originalZymin_originalZymax_originalZxmin_originalZxmax_originalbbox_cutoutZymin_cutoutZymax_cutoutZxmin_cutoutZxmax_cutoutorigin_originalr5   Z_origin_original_truer   ZcrpixZarray_shapesipr	   abZapbp)r   r\   r=   sizerP   r>   rF   r[   rB   Zpixel_scalesZaxisZsideZpos_yxZcutout_datar]   r^   r_   r   r   r   __init__  sv    









zCutout2D.__init__c                s   t  fdddD S )a  
        Convert an ``(x, y)`` position in the cutout array to the original
        ``(x, y)`` position in the original large array.

        Parameters
        ----------
        cutout_position : tuple
            The ``(x, y)`` pixel position in the cutout array.

        Returns
        -------
        original_position : tuple
            The corresponding ``(x, y)`` pixel position in the original
            large array.
        c             3   s    | ]} | j |  V  qd S )N)rc   )r)   r?   )cutout_positionr   r   r   r3     s   z0Cutout2D.to_original_position.<locals>.<genexpr>)r   r
   )r<   )r   rj   r   )rj   r   r   to_original_position{  s    zCutout2D.to_original_positionc                s   t  fdddD S )a  
        Convert an ``(x, y)`` position in the original large array to
        the ``(x, y)`` position in the cutout array.

        Parameters
        ----------
        original_position : tuple
            The ``(x, y)`` pixel position in the original large array.

        Returns
        -------
        cutout_position : tuple
            The corresponding ``(x, y)`` pixel position in the cutout
            array.
        c             3   s    | ]} | j |  V  qd S )N)rc   )r)   r?   )original_positionr   r   r   r3     s   z.Cutout2D.to_cutout_position.<locals>.<genexpr>)r   r
   )r<   )r   rl   r   )rl   r   r   to_cutout_position  s    zCutout2D.to_cutout_positionc             K   s   ddl m} ddlm} ||d< |dkr0| }| j\}}|d |d  }}	| jt||	g }
|j	|
||df|}|
| |S )a  
        Plot the cutout region on a matplotlib Axes instance.

        Parameters
        ----------
        ax : `matplotlib.axes.Axes` instance, optional
            If `None`, then the current `matplotlib.axes.Axes` instance
            is used.

        fill : bool, optional
            Set whether to fill the cutout patch.  The default is
            `False`.

        kwargs : optional
            Any keyword arguments accepted by `matplotlib.patches.Patch`.

        Returns
        -------
        ax : `matplotlib.axes.Axes` instance
            The matplotlib Axes instance constructed in the method if
            ``ax=None``.  Otherwise the output ``ax`` is the same as the
            input ``ax``.
        r   Nfillg       @g        )Zmatplotlib.pyplotZpyplotZmatplotlib.patchesZpatchesZgcarB   position_originalr'   arrayZ	RectangleZ	add_patch)r   Zaxrn   kwargsZpltZmpatchesheightwidthZhwhhZpos_xypatchr   r   r   plot_on_original  s    

zCutout2D.plot_on_originalc                s   t  fdddD S )z
        Calculate the center position.  The center position will be
        fractional for even-sized arrays.  For ``mode='partial'``, the
        central position is calculated for the valid (non-filled) cutout
        values.
        c             3   s*   | ]"}d  | j  | j d  V  qdS )g      ?r
   N)r5   r4   )r)   r?   )slicesr   r   r3     s   z(Cutout2D._calc_center.<locals>.<genexpr>)r
   r   )r<   )rw   r   )rw   r   _calc_center  s    zCutout2D._calc_centerc             C   s0   | d j | d jd f| d j | d jd ffS )a  
        Calculate a minimal bounding box in the form ``((ymin, ymax),
        (xmin, xmax))``.  Note these are pixel locations, not slice
        indices.  For ``mode='partial'``, the bounding box indices are
        for the valid (non-filled) cutout values.
        r   r
   )r5   r4   )rw   r   r   r   
_calc_bbox  s    	zCutout2D._calc_bboxc             C   s   | j d j| j d jfS )z
        The ``(x, y)`` index of the origin pixel of the cutout with
        respect to the original array.  For ``mode='partial'``, the
        origin pixel is calculated for the valid (non-filled) cutout
        values.
        r
   r   )r^   r5   )r   r   r   r   rc     s    zCutout2D.origin_originalc             C   s   | j d j| j d jfS )z
        The ``(x, y)`` index of the origin pixel of the cutout with
        respect to the cutout array.  For ``mode='partial'``, the origin
        pixel is calculated for the valid (non-filled) cutout values.
        r
   r   )r_   r5   )r   r   r   r   origin_cutout  s    zCutout2D.origin_cutoutc             C   s   t t| d S )z
        Round the input to the nearest integer.

        If two integers are equally close, the value is rounded up.
        Note that this is different from `np.round`, which rounds to the
        nearest even number.
        g      ?)r&   r'   rN   )re   r   r   r   _round  s    	zCutout2D._roundc             C   s    |  | jd |  | jd fS )zm
        The ``(x, y)`` position index (rounded to the nearest pixel) in
        the original array.
        r   r
   )r{   r`   )r   r   r   r   ro     s    zCutout2D.position_originalc             C   s    |  | jd |  | jd fS )zk
        The ``(x, y)`` position index (rounded to the nearest pixel) in
        the cutout array.
        r   r
   )r{   r]   )r   r   r   r   position_cutout  s    zCutout2D.position_cutoutc             C   s   |  | jS )z
        The central ``(x, y)`` position of the cutout array with respect
        to the original array.  For ``mode='partial'``, the central
        position is calculated for the valid (non-filled) cutout values.
        )rx   r^   )r   r   r   r   center_original  s    zCutout2D.center_originalc             C   s   |  | jS )z
        The central ``(x, y)`` position of the cutout array with respect
        to the cutout array.  For ``mode='partial'``, the central
        position is calculated for the valid (non-filled) cutout values.
        )rx   r_   )r   r   r   r   center_cutout  s    zCutout2D.center_cutoutc             C   s   |  | jS )a  
        The bounding box ``((ymin, ymax), (xmin, xmax))`` of the minimal
        rectangular region of the cutout array with respect to the
        original array.  For ``mode='partial'``, the bounding box
        indices are for the valid (non-filled) cutout values.
        )ry   r^   )r   r   r   r   ra   "  s    zCutout2D.bbox_originalc             C   s   |  | jS )a  
        The bounding box ``((ymin, ymax), (xmin, xmax))`` of the minimal
        rectangular region of the cutout array with respect to the
        cutout array.  For ``mode='partial'``, the bounding box indices
        are for the valid (non-filled) cutout values.
        )ry   r_   )r   r   r   r   rb   ,  s    zCutout2D.bbox_cutout)NF)r   r    r!   r"   r'   nanri   rk   rm   rv   staticmethodrx   ry   r   rc   rz   r{   ro   r|   r}   r~   ra   rb   r   r   r   r   r   l  s$    2[
(
					
)r#   )*r"   r[   r   systypesr   numpyr'   Zastropyr   rV   Zastropy.coordinatesr   Zastropy.utilsr   Zastropy.utils.decoratorsr   Zastropy.wcs.utilsr   r   Zastropy.wcsr	   blocksr   Z_block_reducer   Z_block_replicate__all__
ModuleTyper   modulesr   	__class__r6   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s4   
lh5+