a
    ߙfbZ/                     @   s   d dl 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gZdgZejej Zejejejejd	 efZejejejd
ZG dd deZdS )    N)siequivalencies)Unit)SpecificTypeQuantityQuantity)quantity_inputSpectralQuantityzSpectralQuantity.*)ZradioZopticalZrelativisticc                       s   e Zd ZdZeZdZd fdd	Z fddZdd	 Z	 fd
dZ
edd Zejeeddd Zedd Zejdd Zeedg ddf fdd	ZdddZ  ZS )r	   a  
    One or more value(s) with spectral units.

    The spectral units should be those for frequencies, wavelengths, energies,
    wavenumbers, or velocities (interpreted as Doppler velocities relative to a
    rest spectral value). The advantage of using this class over the regular
    `~astropy.units.Quantity` class is that in `SpectralQuantity`, the
    ``u.spectral`` equivalency is enabled by default (allowing automatic
    conversion between spectral units), and a preferred Doppler rest value and
    convention can be stored for easy conversion to/from velocities.

    Parameters
    ----------
    value : ndarray or `~astropy.units.Quantity` or `SpectralQuantity`
        Spectral axis data values.
    unit : unit-like
        Unit for the given data.
    doppler_rest : `~astropy.units.Quantity` ['speed'], optional
        The rest value to use for conversions from/to velocities
    doppler_convention : str, optional
        The convention to use when converting the spectral data to/from
        velocities.
    TNc                    sR   t  j| |fd|i|}|d u r.t|dd }|d u rBt|dd }||_||_|S )Nunitdoppler_restdoppler_convention)super__new__getattr_doppler_rest_doppler_convention)clsvaluer   r   r   kwargsobj	__class__ Dlib/python3.9/site-packages/astropy/coordinates/spectral_quantity.pyr   5   s    zSpectralQuantity.__new__c                    s,   t  | t|dd | _t|dd | _d S )Nr   r   )r   __array_finalize__r   r   r   )selfr   r   r   r   r   G   s    z#SpectralQuantity.__array_finalize__c                 C   s   || j u rtdfS tdfS d S )NTF)r   r	   r   )r   r   r   r   r   __quantity_subclass__L   s    
z&SpectralQuantity.__quantity_subclass__c                    s   t  j||g|R i |}|tju s<|tju rH|d | u rH|j| jksh|tjtjtjtj	fv r|dv r|
| j}||  n>|| u rtd| jj |jjdkr|
tj}n
|
t}|S )Nr   )reduceZreduceatz-Cannot store the result of this operation in b)r   __array_ufunc__npZmultiplyZtrue_divider   ZminimumZmaximumZfmaxZfminviewr   r   	TypeError__name__Zdtypekindndarrayr   )r   ZfunctionmethodZinputsr   resultr   r   r   r    V   s(    



z SpectralQuantity.__array_ufunc__c                 C   s   | j S )z
        The rest value of the spectrum used for transformations to/from
        velocity space.

        Returns
        -------
        `~astropy.units.Quantity` ['speed']
            Rest value as an astropy `~astropy.units.Quantity` object.
        )r   r   r   r   r   r   i   s    zSpectralQuantity.doppler_rest)r   c                 C   s   | j durtd|| _ dS )z
        New rest value needed for velocity-space conversions.

        Parameters
        ----------
        value : `~astropy.units.Quantity` ['speed']
            Rest value.
        Nzdoppler_rest has already been set, and cannot be changed. Use the ``to`` method to convert the spectral values(s) to use a different rest value)r   AttributeErrorr   r   r   r   r   r   v   s    
c                 C   s   | j S )z
        The defined convention for conversions to/from velocity space.

        Returns
        -------
        str
            One of 'optical', 'radio', or 'relativistic' representing the
            equivalency used in the unit conversions.
        )r   r)   r   r   r   r      s    z#SpectralQuantity.doppler_conventionc                 C   sD   | j durtd|dur:|tvr:tddtt || _ dS )a  
        New velocity convention used for velocity space conversions.

        Parameters
        ----------
        value

        Notes
        -----
        More information on the equations dictating the transformations can be
        found in the astropy documentation [1]_.

        References
        ----------
        .. [1] Astropy documentation: https://docs.astropy.org/en/stable/units/equivalencies.html#spectral-doppler-equivalencies

        Nzdoppler_convention has already been set, and cannot be changed. Use the ``to`` method to convert the spectral values(s) to use a different convention$doppler_convention should be one of /)r   r*   DOPPLER_CONVENTIONS
ValueErrorjoinsortedr+   r   r   r   r      s
    
)r   c           
         s  t |}|du r:t j|dd}|| j}||  |S |du rH| j}|du rX| j}n |tvrxt	dd
tt | jtrd|trd|dur| jdu rt	d|dur| jdu rt	d|du r|du rt j||d}|| j}||  |S |du |du urt	dt| j | j}t jtj|| d}t| |}|j||| d}njt }	| jts|tr|du rt	d|du rt	d	|	t| | }	t j|||	 d}|| j}||  ||_||_|S )
ay  
        Return a new `~astropy.coordinates.SpectralQuantity` object with the specified unit.

        By default, the ``spectral`` equivalency will be enabled, as well as
        one of the Doppler equivalencies if converting to/from velocities.

        Parameters
        ----------
        unit : unit-like
            An object that represents the unit to convert to. Must be
            an `~astropy.units.UnitBase` object or a string parseable
            by the `~astropy.units` package, and should be a spectral unit.
        equivalencies : list of `~astropy.units.equivalencies.Equivalency`, optional
            A list of equivalence pairs to try if the units are not
            directly convertible (along with spectral).
            See :ref:`astropy:unit_equivalencies`.
            If not provided or ``[]``, spectral equivalencies will be used.
            If `None`, no equivalencies will be applied at all, not even any
            set globally or within a context.
        doppler_rest : `~astropy.units.Quantity` ['speed'], optional
            The rest value used when converting to/from velocities. This will
            also be set at an attribute on the output
            `~astropy.coordinates.SpectralQuantity`.
        doppler_convention : {'relativistic', 'optical', 'radio'}, optional
            The Doppler convention used when converting to/from velocities.
            This will also be set at an attribute on the output
            `~astropy.coordinates.SpectralQuantity`.

        Returns
        -------
        `SpectralQuantity`
            New spectral coordinate object with data converted to the new unit.
        Nr   r,   r-   z#Original doppler_convention not setzOriginal doppler_rest not setzeEither both or neither doppler_rest and doppler_convention should be defined for velocity conversionsz=doppler_convention not set, cannot convert to/from velocitiesz7doppler_rest not set, cannot convert to/from velocities)r   r   tor"   r   r   r   r   r.   r/   r0   r1   r   Zis_equivalentKMSr   HzeqZspectral)
r   r   r   r   r   r(   Z
vel_equiv1ZfreqZ
vel_equiv2Zadditional_equivalenciesr   r   r   r2      sR    (




zSpectralQuantity.toc                 O   s.   |d u r|  tjS | j|g|R i |jS )N)r"   r!   r&   r2   r   )r   r   argsr   r   r   r   to_value,  s    zSpectralQuantity.to_value)NNN)N)r$   
__module____qualname____doc__SPECTRAL_UNITSZ_equivalent_unitZ _include_easy_conversion_membersr   r   r   r    propertyr   setterr   r   r2   r7   __classcell__r   r   r   r   r	      s0     



w)Znumpyr!   Zastropy.unitsr   r   r5   r   Zastropy.units.quantityr   r   Zastropy.units.decoratorsr   __all__Z__doctest_skip__Zkmsr3   r4   mJr;   Zdoppler_radioZdoppler_opticalZdoppler_relativisticr.   r	   r   r   r   r   <module>   s   