B
    q\                 @   s$   d dl mZ dgZG dd dZdS )    )logNDSlicingMixinc               @   s8   e Zd Z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 |S )N zscalars cannot be sliced.)datashape	TypeError_slice	__class__)selfitemkwargsr   r   >lib/python3.7/site-packages/astropy/nddata/mixins/ndslicing.py__getitem__6   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   r   r   ?   s    

zNDSlicingMixin._slicec             C   s>   | j d krd S y
| j | S  tk
r6   td Y nX | j S )Nzuncertainty cannot be sliced.)r   r   r   info)r
   r   r   r   r   r   `   s    

z!NDSlicingMixin._slice_uncertaintyc             C   s>   | j d krd S y
| j | S  tk
r6   td Y nX | j S )Nzmask cannot be sliced.)r   r   r   r   )r
   r   r   r   r   r   k   s    

zNDSlicingMixin._slice_maskc             C   s>   | j d krd S y
| j | S  tk
r6   td Y nX | j S )Nzwcs cannot be sliced.)r   r   r   r   )r
   r   r   r   r   r   t   s    

zNDSlicingMixin._slice_wcsN)	__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   
   s   +	!	N)Zastropyr   __all__r   r   r   r   r   <module>   s   