a
    ߙfb                     @   s<   d dl mZ d dlmZmZmZmZ dgZG dd dZdS )    )log)BaseLowLevelWCSBaseHighLevelWCSSlicedLowLevelWCSHighLevelWCSWrapperNDSlicingMixinc                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )r   a  Mixin to provide slicing on objects using the `NDData`
    interface.

    The ``data``, ``mask``, ``uncertainty`` and ``wcs`` will be sliced, if
    set and sliceable. The ``unit`` and ``meta`` will be untouched. The return
    will be a reference and not a copy, if possible.

    Examples
    --------
    Using this Mixin with `~astropy.nddata.NDData`:

        >>> from astropy.nddata import NDData, NDSlicingMixin
        >>> class NDDataSliceable(NDSlicingMixin, NDData):
        ...     pass

    Slicing an instance containing data::

        >>> nd = NDDataSliceable([1,2,3,4,5])
        >>> nd[1:3]
        NDDataSliceable([2, 3])

    Also the other attributes are sliced for example the ``mask``::

        >>> import numpy as np
        >>> mask = np.array([True, False, True, True, False])
        >>> nd2 = NDDataSliceable(nd, mask=mask)
        >>> nd2slc = nd2[1:3]
        >>> nd2slc[nd2slc.mask]
        NDDataSliceable([3])

    Be aware that changing values of the sliced instance will change the values
    of the original::

        >>> nd3 = nd2[1:3]
        >>> nd3.data[0] = 100
        >>> nd2
        NDDataSliceable([  1, 100,   3,   4,   5])

    See also
    --------
    NDDataRef
    NDDataArray
    c                 C   s.   | j jdkrtd| |}| jf i |S )N zscalars cannot be sliced.)datashape	TypeError_slice	__class__selfitemkwargsr   r   >lib/python3.9/site-packages/astropy/nddata/mixins/ndslicing.py__getitem__8   s    
zNDSlicingMixin.__getitem__c                 C   sT   i }| j | |d< | ||d< | ||d< | ||d< | j|d< | j|d< |S )a  Collects the sliced attributes and passes them back as `dict`.

        It passes uncertainty, mask and wcs to their appropriate ``_slice_*``
        method, while ``meta`` and ``unit`` are simply taken from the original.
        The data is assumed to be sliceable and is sliced directly.

        When possible the return should *not* be a copy of the data but a
        reference.

        Parameters
        ----------
        item : slice
            The slice passed to ``__getitem__``.

        Returns
        -------
        dict :
            Containing all the attributes after slicing - ready to
            use them to create ``self.__class__.__init__(**kwargs)`` in
            ``__getitem__``.
        r	   uncertaintymaskwcsunitmeta)r	   _slice_uncertainty_slice_mask
_slice_wcsr   r   r   r   r   r   r   A   s    

zNDSlicingMixin._slicec                 C   s>   | j d u rd S z| j | W S  ty6   td Y n0 | j S )Nzuncertainty cannot be sliced.)r   r   r   infor   r   r   r   r   r   b   s    
z!NDSlicingMixin._slice_uncertaintyc                 C   s>   | j d u rd S z| j | W S  ty6   td Y n0 | j S )Nzmask cannot be sliced.)r   r   r   r   r   r   r   r   r   m   s    
zNDSlicingMixin._slice_maskc              
   C   s\   | j d u rd S zt| j j|}t|W S  tyV } z| || W Y d }~n
d }~0 0 d S )N)r   r   Zlow_level_wcsr   	Exception_handle_wcs_slicing_error)r   r   Zllwcserrr   r   r   r   v   s    

zNDSlicingMixin._slice_wcsc                 C   s   t d| d|d S )Nz'Slicing the WCS object with the slice 'z' failed, if you want to slice the NDData object without the WCS, you can remove by setting `NDData.wcs = None` and then retry.)
ValueError)r   r    r   r   r   r   r      s    z(NDSlicingMixin._handle_wcs_slicing_errorN)
__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r      s   +	!	N)	Zastropyr   Zastropy.wcs.wcsapir   r   r   r   __all__r   r   r   r   r   <module>   s   