U
    e»{j  ã                   @   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)ÚBaseHighLevelWCSÚBaseLowLevelWCSÚHighLevelWCSWrapperÚSlicedLowLevelWCSÚ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 |ŽS )N© zscalars cannot be sliced.)ÚdataÚshapeÚ	TypeErrorÚ_sliceÚ	__class__©ÚselfÚitemÚkwargsr   r   ú^/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/nddata/mixins/ndslicing.pyÚ__getitem__=   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   F   s    

zNDSlicingMixin._slicec                 C   s@   | j d krd S z| j | W S  tk
r8   t d¡ Y nX | j S )Nzuncertainty cannot be sliced.)r   r   r   Úinfo©r   r   r   r   r   r   g   s    
z!NDSlicingMixin._slice_uncertaintyc                 C   s@   | j d krd S z| j | W S  tk
r8   t d¡ Y nX | j S )Nzmask cannot be sliced.)r   r   r   r   r   r   r   r   r   r   s    
zNDSlicingMixin._slice_maskc              
   C   sZ   | j d krd S zt| j j|ƒ}t|ƒW S  tk
rT } z|  ||¡ W 5 d }~X Y nX d S )N)r   r   Zlow_level_wcsr   Ú	ExceptionÚ_handle_wcs_slicing_error)r   r   ZllwcsÚerrr   r   r   r   {   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   