B
    d&                 @   s   d dl Zd dlmZmZm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 dd	lmZ dd
lmZ dgZG dd deeee	ZdS )    N)
UnitsErrorUnitConversionErrorUnit)log   )NDData)NDUncertainty)NDSlicingMixin)NDArithmeticMixin)	NDIOMixin)FlagCollectionNDDataArrayc                   s   e Zd ZdZdd fdd
Zedd Zejdd Zed	d
 Zejdd
 Zedd Z	e	jdd Z	edd Z
edd Zedd Zedd Zedd Zejdd Zdd Zd ddZg fddZ  ZS )!r   a	  
    An ``NDData`` object with arithmetic. This class is functionally equivalent
    to ``NDData`` in astropy  versions prior to 1.0.

    The key distinction from raw numpy arrays is the presence of
    additional metadata such as uncertainties, a mask, units, flags,
    and/or a coordinate system.

    See also: https://docs.astropy.org/en/stable/nddata/

    Parameters
    -----------
    data : ndarray or `NDData`
        The actual data contained in this `NDData` object. Not that this
        will always be copies by *reference* , so you should make copy
        the ``data`` before passing it in if that's the  desired behavior.

    uncertainty : `~astropy.nddata.NDUncertainty`, optional
        Uncertainties on the data.

    mask : array-like, optional
        Mask for the data, given as a boolean Numpy array or any object that
        can be converted to a boolean Numpy array with a shape
        matching that of the data. The values must be ``False`` where
        the data is *valid* and ``True`` when it is not (like Numpy
        masked arrays). If ``data`` is a numpy masked array, providing
        ``mask`` here will causes the mask from the masked array to be
        ignored.

    flags : array-like or `~astropy.nddata.FlagCollection`, optional
        Flags giving information about each pixel. These can be specified
        either as a Numpy array of any type (or an object which can be converted
        to a Numpy array) with a shape matching that of the
        data, or as a `~astropy.nddata.FlagCollection` instance which has a
        shape matching that of the data.

    wcs : None, optional
        WCS-object containing the world coordinate system for the data.

        .. warning::
            This is not yet defined because the discussion of how best to
            represent this class's WCS system generically is still under
            consideration. For now just leave it as None

    meta : `dict`-like object, optional
        Metadata for this object.  "Metadata" here means all information that
        is included with this object but not part of any other attribute
        of this particular object.  e.g., creation date, unique identifier,
        simulation parameters, exposure time, telescope name, etc.

    unit : `~astropy.units.UnitBase` instance or str, optional
        The units of the data.


    Raises
    ------
    ValueError :
        If the `uncertainty` or `mask` inputs cannot be broadcast (e.g., match
        shape) onto ``data``.
    N)flagsc               sR   t  j|f|| | j| _| j| _t|trH|d kr>|j}n
t	
d || _d S )Nz=Overwriting NDDataArrays's current flags with specified flags)super__init___uncertaintyuncertainty_maskmask
isinstancer   r   r   info)selfdatar   argskwargs)	__class__ b/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/nddata/compat.pyr   T   s    

zNDDataArray.__init__c             C   s   | j S )N)r   )r   r   r   r   r   m   s    zNDDataArray.uncertaintyc             C   sX   |d k	rNt |trD| jj}| js4|jr4td||| _| | j_	qTt
dn|| _d S )Nz;Cannot assign an uncertainty with unit to {} without a unitz9Uncertainty must be an instance of a NDUncertainty object)r   r   r   __name__unit_unit
ValueErrorformatr   Zparent_nddata	TypeError)r   value
class_namer   r   r   r   q   s    


c             C   s   | j S )N)r    )r   r   r   r   r      s    zNDDataArray.unitc             C   s^   ddl m} y| jd k	r(|jr(td W n tk
r>   Y nX |d krPd | _n
t|| _d S )Nr   )confzSetting the unit directly changes the unit without updating the data or uncertainty. Use the .convert_unit_to() method to change the unit and scale values appropriately.) r&   r    Zwarn_setting_unit_directlyr   r   AttributeErrorr   )r   r$   r&   r   r   r   r      s    c             C   s   | j tjjkrd S | j S d S )N)r   npmanomask)r   r   r   r   r      s    zNDDataArray.maskc             C   sT   |d k	rF|t jjk	rFt j|t jdd}|j| jjkr>tdqP|| _n
t jj| _d S )NF)dtypecopyz$dimensions of mask do not match data)	r)   r*   r+   arrayZbool_shaper   r!   r   )r   r$   r   r   r   r   r      s    
c             C   s   | j jS )z4
        shape tuple of this object's data.
        )r   r/   )r   r   r   r   r/      s    zNDDataArray.shapec             C   s   | j jS )z5
        integer size of this object's data.
        )r   size)r   r   r   r   r0      s    zNDDataArray.sizec             C   s   | j jS )z6
        `numpy.dtype` of this object's data.
        )r   r,   )r   r   r   r   r,      s    zNDDataArray.dtypec             C   s   | j jS )z:
        integer dimensions of this object's data
        )r   ndim)r   r   r   r   r1      s    zNDDataArray.ndimc             C   s   | j S )N)_flags)r   r   r   r   r      s    zNDDataArray.flagsc             C   sf   |d k	r\t |tr0|j| jkr(tdqZ|| _qbtj|dd}|j| jkrTtdqb|| _n|| _d S )Nz0dimensions of FlagCollection does not match dataF)r-   z%dimensions of flags do not match data)r   r   r/   r!   r2   r)   r.   )r   r$   r   r   r   r   r      s    


c             C   s,   | j dk	rtj| j| j S t| jS dS )zp
        This allows code that requests a Numpy array to use an NDData
        object as a Numpy array.
        N)r   r)   r*   masked_arrayr   r.   )r   r   r   r   	__array__   s    
zNDDataArray.__array__c             C   s"   | j dk	rtj|| j S |S dS )zQ
        This ensures that a masked array is returned if self is masked.
        N)r   r)   r*   r3   )r   r.   contextr   r   r   __array_prepare__   s    
zNDDataArray.__array_prepare__c             C   s   | j dkrtd| j j|| j|d}| jdk	rT| j j|| jj|d}| j|}nd}| jdk	rn| j }nd}| j|||| j	| j
|d}|S )am  
        Returns a new `NDData` object whose values have been converted
        to a new unit.

        Parameters
        ----------
        unit : `astropy.units.UnitBase` instance or str
            The unit to convert to.

        equivalencies : list of tuple
           A list of equivalence pairs to try if the units are not
           directly convertible.  See :ref:`unit_equivalencies`.

        Returns
        -------
        result : `~astropy.nddata.NDData`
            The resulting dataset

        Raises
        ------
        `~astropy.units.UnitsError`
            If units are inconsistent.

        Nz No unit specified on source data)equivalencies)r   r   wcsmetar   )r   r!   tor   r   r.   r   r   r-   r8   r9   )r   r   r7   r   Zuncertainty_valuesr   Znew_maskresultr   r   r   convert_unit_to   s     


zNDDataArray.convert_unit_to)N)r   
__module____qualname____doc__r   propertyr   setterr   r   r/   r0   r,   r1   r   r4   r6   r<   __classcell__r   r   )r   r   r      s"   <

	)numpyr)   Zastropy.unitsr   r   r   Zastropyr   Znddatar   Znduncertaintyr   Zmixins.ndslicingr	   Zmixins.ndarithmeticr
   Zmixins.ndior   Zflag_collectionr   __all__r   r   r   r   r   <module>   s   