B
    dsx                 @   sz  d Z ddlmZ ddlZddl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dlmZ ddlmZ ddlmZmZ ddddddddddddddddddd gZG d!d  d eZd"d Zd#d Zd$d Zd%d Zd6d&dZd'd Zd(d Z d)d Z!d*d Z"d+d Z#d7d,dZ$d-d Z%d8d.dZ&d/d Z'd0d Z(d1d2 Z)d3d Z*d4d Z+d9d5dZ,dS ):z-A set of standard astronomical equivalencies.    )UserListN)si)AstropyDeprecationWarning)
isiterable   )cgs)	astrophys)misc)units)dimensionless_unscaled)
UnitsErrorUnitparallaxspectralspectral_densitydoppler_radiodoppler_opticaldoppler_relativisticmass_energybrightness_temperaturethermodynamic_temperaturebeam_angular_areadimensionless_angleslogarithmictemperaturetemperature_energymolar_mass_amupixel_scaleplate_scalewith_H0Equivalencyc                   s2   e Zd ZdZd
ddZ fddZdd	 Z  ZS )r    z
    A container for a units equivalency.

    Attributes
    ----------
    name: `str`
        The name of the equivalency.
    kwargs: `dict`
        Any positional or keyword arguments used to make the equivalency.
     Nc             C   s*   || _ |g| _|d k	r|gnt g| _d S )N)datanamedictkwargs)selfZ
equiv_listr#   r%    r'   h/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/astropy/units/equivalencies.py__init__+   s    zEquivalency.__init__c                sV   t |trFt |}| jd d  |j |_| jd d  |j |_|S | j|S d S )N)
isinstancer    super__add__r#   r%   r"   )r&   othernew)	__class__r'   r(   r,   0   s    
zEquivalency.__add__c             C   s$   t || jo"| j|jko"| j|jkS )N)r*   r/   r#   r%   )r&   r-   r'   r'   r(   __eq__9   s    zEquivalency.__eq__)r!   N)__name__
__module____qualname____doc__r)   r,   r0   __classcell__r'   r'   )r/   r(   r       s   

	c               C   s   t tjdfgdS )a  Allow angles to be equivalent to dimensionless (with 1 rad = 1 m/m = 1).

    It is special compared to other equivalency pairs in that it
    allows this independent of the power to which the angle is raised,
    and independent of whether it is part of a more complicated unit.
    Nr   )r    r   radianr'   r'   r'   r(   r   ?   s    c               C   s   t ttjtjdd fgdS )zBAllow logarithmic units to be converted to dimensionless fractionsc             S   s   d|  S )Ng      $@r'   )xr'   r'   r(   <lambda>M       zlogarithmic.<locals>.<lambda>r   )r    r   function_unitsZdexnplog10r'   r'   r'   r(   r   I   s    c              C   s   dd } t tjtj| fgdS )zq
    Returns a list of equivalence pairs that handle the conversion
    between parallax angle and distance.
    c             S   sH   t | } d|  }t|r,t j||dk < |S |dk r@t t jS |S d S )Nr   r   )r;   Z
asanyarrayr   nanarray)r7   dr'   r'   r(   parallax_converterW   s    
z$parallax.<locals>.parallax_converterr   )r    r   Z	arcsecondr   Zparsec)r@   r'   r'   r(   r   Q   s    c                 s  t jjt jj  dtj tjd } tjtj }t	tjtj
dd ftjtj fddftj
tjdd dd ftj| dd ftj
| d	d d
d ftj|  fdd fddf| |fddfddftj|fddftj
|fddfddftj| fdd fddfg
dS )a  
    Returns a list of equivalence pairs that handle spectral
    wavelength, wave number, frequency, and energy equivalencies.

    Allows conversions between wavelength units, wave number units,
    frequency units, and energy units as they relate to light.

    There are two types of wave number:

        * spectroscopic - :math:`1 / \lambda` (per meter)
        * angular - :math:`2 \pi / \lambda` (radian per meter)

    g       @c             S   s   t jj|  S )N)_sicvalue)r7   r'   r'   r(   r8   ~   r9   zspectral.<locals>.<lambda>c                s    |  S )Nr'   )r7   )hcr'   r(   r8      r9   c             S   s   t jj|  S )N)rB   hrD   )r7   r'   r'   r(   r8      r9   c             S   s   | t jj S )N)rB   rF   rD   )r7   r'   r'   r(   r8      r9   c             S   s   d|  S )Ng      ?r'   )r7   r'   r'   r(   r8      r9   c             S   s   | t jj S )N)rB   rC   rD   )r7   r'   r'   r(   r8      r9   c             S   s   t jj|  S )N)rB   rC   rD   )r7   r'   r'   r(   r8      r9   c                s   |   S )Nr'   )r7   )rE   r'   r(   r8      r9   c                s    |  S )Nr'   )r7   )rE   r'   r(   r8      r9   c                s   |   S )Nr'   )r7   )two_pir'   r(   r8      r9   c                s   |   S )Nr'   )r7   )rG   r'   r(   r8      r9   c                s    |  S )Nr'   )r7   )rG   r'   r(   r8      r9   c                s    |  t jj S )N)rB   rC   rD   )r7   )rG   r'   r(   r8      r9   c                s   t jj|    S )N)rB   rC   rD   )r7   )rG   r'   r(   r8      r9   c                s   |    S )Nr'   )r7   )rE   rG   r'   r(   r8      r9   c                s    |   S )Nr'   )r7   )rE   rG   r'   r(   r8      r9   r   )rB   rF   rD   rC   r;   pir   mr6   r    HzJ)Z
inv_m_specZ	inv_m_angr'   )rE   rG   r(   r   j   s$    

"c       :   %      s  ddl m} t|r.|dkr&td| tjtjtj	  tj
jj  tjtj tjd  tj	 }tjtj tjd  tj	 }tjtjd  tj	 }|}tjtjd tj	 tj  }tjtjd tj	 tj  }tjtjd tj	  }	tjtj	 tj }
tjtj	 tj }tjtj	 }|}tjtj	tj  }tjtj	tj  }tjtj tjd  tj	 tj }tjtj tjd  tj	 tj }tjtjd  tj	 tj }|}tjtjd tj	 tj tj  }tjtjd tj	 tj tj  }tjtj	 tj tj }tjtj	 tj tj }tjtj	 tj }|}tjtj	tj tj  }tjtj	tj tj  } fdd} fdd	}fd
d}fdd}fdd} fdd}!fdd}"fdd}#fdd}$fdd}% fdd}& fdd}'|"}(|#}) fdd}* fd d!}+|},|}-| }.|!}/|"}0|#}1|$}2|%}3|&}4|'}5|(}6|)}7|*}8|+}9t||||f||||f||| |!f|||"|#f|||$|%f|||&|'f|||(|)f|||*|+f|	||"|#f||
||f|
||,|-f|||.|/f|||0|1f||
|2|3f|||4|5f||
|6|7f|||8|9f||||f||||f||| |!f|||"|#f|||$|%f|||&|'f|||(|)f|||*|+f||||f|||,|-f|||.|/f|||0|1f|||2|3f|||4|5f|||6|7f|||8|9fg!d"|d#S )$a	  
    Returns a list of equivalence pairs that handle spectral density
    with regard to wavelength and frequency.

    Parameters
    ----------
    wav : `~astropy.units.Quantity`
        `~astropy.units.Quantity` associated with values being converted
        (e.g., wavelength or frequency).

    Notes
    -----
    The ``factor`` argument is left for backward-compatibility with the syntax
    ``spectral_density(unit, factor)`` but users are encouraged to use
    ``spectral_density(factor * unit)`` instead.

    r   )UnitBaseNz7If `wav` is specified as a unit, `factor` should be set   c                s   |  tjt d    S )NrM   )to_valuer   AAr   )r7   )c_Apswavr'   r(   	converter   s    z#spectral_density.<locals>.converterc                s   |  tjt d    S )NrM   )rN   r   rO   r   )r7   )rP   rQ   r'   r(   
iconverter   s    z$spectral_density.<locals>.iconverterc                s   |   tjt  S )N)rN   r   rJ   r   )r7   )rQ   r'   r(   converter_f_nu_to_nu_f_nu   s    z3spectral_density.<locals>.converter_f_nu_to_nu_f_nuc                s   |   tjt  S )N)rN   r   rJ   r   )r7   )rQ   r'   r(   iconverter_f_nu_to_nu_f_nu   s    z4spectral_density.<locals>.iconverter_f_nu_to_nu_f_nuc                s   |   tjt  S )N)rN   r   rO   r   )r7   )rQ   r'   r(   converter_f_la_to_la_f_la   s    z3spectral_density.<locals>.converter_f_la_to_la_f_lac                s   |   tjt  S )N)rN   r   rO   r   )r7   )rQ   r'   r(   iconverter_f_la_to_la_f_la   s    z4spectral_density.<locals>.iconverter_f_la_to_la_f_lac                s    |   tjt  S )N)rN   r   rO   r   )r7   )rE   rQ   r'   r(   converter_phot_f_la_to_f_la   s    z5spectral_density.<locals>.converter_phot_f_la_to_f_lac                s   |  tjt    S )N)rN   r   rO   r   )r7   )rE   rQ   r'   r(   iconverter_phot_f_la_to_f_la   s    z6spectral_density.<locals>.iconverter_phot_f_la_to_f_lac                s    |   tjt  S )N)rN   r   rO   r   )r7   )h_cgsrQ   r'   r(   converter_phot_f_la_to_f_nu   s    z5spectral_density.<locals>.converter_phot_f_la_to_f_nuc                s   |  tjt    S )N)rN   r   rO   r   )r7   )rZ   rQ   r'   r(   iconverter_phot_f_la_to_f_nu   s    z6spectral_density.<locals>.iconverter_phot_f_la_to_f_nuc                s   |  tjt d    S )NrM   )rN   r   rO   r   )r7   )rP   rQ   r'   r(   converter_phot_f_la_phot_f_nu   s    z7spectral_density.<locals>.converter_phot_f_la_phot_f_nuc                s    |   tjt d  S )NrM   )rN   r   rO   r   )r7   )rP   rQ   r'   r(   iconverter_phot_f_la_phot_f_nu   s    z8spectral_density.<locals>.iconverter_phot_f_la_phot_f_nuc                s    |     tjt d  S )N   )rN   r   rO   r   )r7   )rP   rE   rQ   r'   r(   converter_phot_f_nu_to_f_la   s    z5spectral_density.<locals>.converter_phot_f_nu_to_f_lac                s    |  tjt d     S )Nr_   )rN   r   rO   r   )r7   )rP   rE   rQ   r'   r(   iconverter_phot_f_nu_to_f_la   s    z6spectral_density.<locals>.iconverter_phot_f_nu_to_f_lar   )rQ   factor)corerL   r*   
ValueErrorrB   rC   rN   r   rO   srF   r   rD   ZergZangstromcmrJ   r   Zphotonsrr    ):rQ   rb   rL   Zf_laZf_nuZnu_f_nuZla_f_laZ	phot_f_laZ	phot_f_nuZla_phot_f_laZL_nuZL_laZnu_L_nuZla_L_laZ	phot_L_laZ	phot_L_nuZS_laZS_nuZnu_S_nuZla_S_laZ	phot_S_laZ	phot_S_nuZSL_nuZSL_laZnu_SL_nuZla_SL_laZ
phot_SL_laZ
phot_SL_nurR   rS   rT   rU   rV   rW   rX   rY   r[   r\   r]   r^   Zconverter_phot_f_nu_to_f_nuZiconverter_phot_f_nu_to_f_nur`   ra   Zconverter_L_nu_to_nu_L_nuZiconverter_L_nu_to_nu_L_nuZconverter_L_la_to_la_L_laZiconverter_L_la_to_la_L_laZconverter_phot_L_la_to_L_laZiconverter_phot_L_la_to_L_laZconverter_phot_L_la_to_L_nuZiconverter_phot_L_la_to_L_nuZconverter_phot_L_la_phot_L_nuZiconverter_phot_L_la_phot_L_nuZconverter_phot_L_nu_to_L_nuZiconverter_phot_L_nu_to_L_nuZconverter_phot_L_nu_to_L_laZiconverter_phot_L_nu_to_L_lar'   )rP   rZ   rE   rQ   r(   r      s    

""""































c                s   t  tjd  fdd} fdd} fdd} fdd	} fd
d} fdd}ttjtjtj ||ftj	tjtj ||ftj
tjtj ||fgddiS )a  
    Return the equivalency pairs for the radio convention for velocity.

    The radio convention for the relation between velocity and frequency is:

    :math:`V = c \frac{f_0 - f}{f_0}  ;  f(V) = f_0 ( 1 - V/c )`

    Parameters
    ----------
    rest : `~astropy.units.Quantity`
        Any quantity supported by the standard spectral equivalencies
        (wavelength, energy, frequency, wave number).

    References
    ----------
    `NRAO site defining the conventions <https://www.gb.nrao.edu/~fghigo/gbtdoc/doppler.html>`_

    Examples
    --------
    >>> import astropy.units as u
    >>> CO_restfreq = 115.27120*u.GHz  # rest frequency of 12 CO 1-0 in GHz
    >>> radio_CO_equiv = u.doppler_radio(CO_restfreq)
    >>> measured_freq = 115.2832*u.GHz
    >>> radio_velocity = measured_freq.to(u.km/u.s, equivalencies=radio_CO_equiv)
    >>> radio_velocity  # doctest: +FLOAT_CMP
    <Quantity -31.209092088877583 km / s>
    zkm/sc                s"   j tjt d}||  |   S )N)equivalencies)rN   r   rJ   r   )r7   restfreq)ckmsrestr'   r(   to_vel_freqT  s    z"doppler_radio.<locals>.to_vel_freqc                s&   j tjt d}|   }|d|  S )N)rh   r   )rN   r   rJ   r   )r7   ri   voverc)rj   rk   r'   r(   from_vel_freqX  s    z$doppler_radio.<locals>.from_vel_freqc                s     tjt }| | |    S )N)rN   r   rO   r   )r7   restwav)rj   rk   r'   r(   
to_vel_wav]  s    z!doppler_radio.<locals>.to_vel_wavc                s     tjt }|   |   S )N)rN   r   rO   r   )r7   ro   )rj   rk   r'   r(   from_vel_wava  s    z#doppler_radio.<locals>.from_vel_wavc                s"   j tjt d}||  |   S )N)rh   )rN   r   eVr   )r7   resten)rj   rk   r'   r(   	to_vel_ene  s    z doppler_radio.<locals>.to_vel_enc                s&   j tjt d}|   }|d|  S )N)rh   r   )rN   r   rr   r   )r7   rs   rm   )rj   rk   r'   r(   from_vel_eni  s    z"doppler_radio.<locals>.from_vel_enr   rk   )assert_is_spectral_unitrB   rC   rN   r    r   rJ   kmre   rO   rr   )rk   rl   rn   rp   rq   rt   ru   r'   )rj   rk   r(   r   3  s    c                s   t  tjd  fdd} fdd} fdd} fdd	} fd
d} fdd}ttjtjtj ||ftj	tjtj ||ftj
tjtj ||fgddiS )a  
    Return the equivalency pairs for the optical convention for velocity.

    The optical convention for the relation between velocity and frequency is:

    :math:`V = c \frac{f_0 - f}{f  }  ;  f(V) = f_0 ( 1 + V/c )^{-1}`

    Parameters
    ----------
    rest : `~astropy.units.Quantity`
        Any quantity supported by the standard spectral equivalencies
        (wavelength, energy, frequency, wave number).

    References
    ----------
    `NRAO site defining the conventions <https://www.gb.nrao.edu/~fghigo/gbtdoc/doppler.html>`_

    Examples
    --------
    >>> import astropy.units as u
    >>> CO_restfreq = 115.27120*u.GHz  # rest frequency of 12 CO 1-0 in GHz
    >>> optical_CO_equiv = u.doppler_optical(CO_restfreq)
    >>> measured_freq = 115.2832*u.GHz
    >>> optical_velocity = measured_freq.to(u.km/u.s, equivalencies=optical_CO_equiv)
    >>> optical_velocity  # doctest: +FLOAT_CMP
    <Quantity -31.20584348799674 km / s>
    zkm/sc                s"   j tjt d} ||   |  S )N)rh   )rN   r   rJ   r   )r7   ri   )rj   rk   r'   r(   rl     s    z$doppler_optical.<locals>.to_vel_freqc                s&   j tjt d}|   }|d|  S )N)rh   r   )rN   r   rJ   r   )r7   ri   rm   )rj   rk   r'   r(   rn     s    z&doppler_optical.<locals>.from_vel_freqc                s     tjt } | | d  S )Nr   )rN   r   rO   r   )r7   ro   )rj   rk   r'   r(   rp     s    z#doppler_optical.<locals>.to_vel_wavc                s$    tjt }|   }|d|  S )Nr   )rN   r   rO   r   )r7   ro   rm   )rj   rk   r'   r(   rq     s    z%doppler_optical.<locals>.from_vel_wavc                s"   j tjt d} ||   |  S )N)rh   )rN   r   rr   r   )r7   rs   )rj   rk   r'   r(   rt     s    z"doppler_optical.<locals>.to_vel_enc                s&   j tjt d}|   }|d|  S )N)rh   r   )rN   r   rr   r   )r7   rs   rm   )rj   rk   r'   r(   ru     s    z$doppler_optical.<locals>.from_vel_enr   rk   )rv   rB   rC   rN   r    r   rJ   rw   re   rO   rr   )rk   rl   rn   rp   rq   rt   ru   r'   )rj   rk   r(   r   t  s    c                s   t  tjd  fdd} fdd} fdd} fdd	} fd
d} fdd}ttjtjtj ||ftj	tjtj ||ftj
tjtj ||fgddiS )a  
    Return the equivalency pairs for the relativistic convention for velocity.

    The full relativistic convention for the relation between velocity and frequency is:

    :math:`V = c \frac{f_0^2 - f^2}{f_0^2 + f^2} ;  f(V) = f_0 \frac{\left(1 - (V/c)^2\right)^{1/2}}{(1+V/c)}`

    Parameters
    ----------
    rest : `~astropy.units.Quantity`
        Any quantity supported by the standard spectral equivalencies
        (wavelength, energy, frequency, wave number).

    References
    ----------
    `NRAO site defining the conventions <https://www.gb.nrao.edu/~fghigo/gbtdoc/doppler.html>`_

    Examples
    --------
    >>> import astropy.units as u
    >>> CO_restfreq = 115.27120*u.GHz  # rest frequency of 12 CO 1-0 in GHz
    >>> relativistic_CO_equiv = u.doppler_relativistic(CO_restfreq)
    >>> measured_freq = 115.2832*u.GHz
    >>> relativistic_velocity = measured_freq.to(u.km/u.s, equivalencies=relativistic_CO_equiv)
    >>> relativistic_velocity  # doctest: +FLOAT_CMP
    <Quantity -31.207467619351537 km / s>
    >>> measured_velocity = 1250 * u.km/u.s
    >>> relativistic_frequency = measured_velocity.to(u.GHz, equivalencies=relativistic_CO_equiv)
    >>> relativistic_frequency  # doctest: +FLOAT_CMP
    <Quantity 114.79156866993588 GHz>
    >>> relativistic_wavelength = measured_velocity.to(u.mm, equivalencies=relativistic_CO_equiv)
    >>> relativistic_wavelength  # doctest: +FLOAT_CMP
    <Quantity 2.6116243681798923 mm>
    zkm/sc                s6   j tjt d}|d | d  |d | d     S )N)rh   rM   )rN   r   rJ   r   )r7   ri   )rj   rk   r'   r(   rl     s    z)doppler_relativistic.<locals>.to_vel_freqc                s2   j tjt d}|   }|d| d|  d  S )N)rh   r   g      ?)rN   r   rJ   r   )r7   ri   rm   )rj   rk   r'   r(   rn     s    z+doppler_relativistic.<locals>.from_vel_freqc                s4    tjt }| d |d  |d | d     S )NrM   )rN   r   rO   r   )r7   ro   )rj   rk   r'   r(   rp     s    z(doppler_relativistic.<locals>.to_vel_wavc                s0    tjt }|   }|d| d|  d  S )Nr   g      ?)rN   r   rO   r   )r7   ro   rm   )rj   rk   r'   r(   rq     s    z*doppler_relativistic.<locals>.from_vel_wavc                s4    tjt }|d | d  |d | d     S )NrM   )rN   r   rr   r   )r7   rs   )rj   rk   r'   r(   rt     s    z'doppler_relativistic.<locals>.to_vel_enc                s0    tjt }|   }|d| d|  d  S )Nr   g      ?)rN   r   rr   r   )r7   rs   rm   )rj   rk   r'   r(   ru     s    z)doppler_relativistic.<locals>.from_vel_enr   rk   )rv   rB   rC   rN   r    r   rJ   rw   re   rO   rr   )rk   rl   rn   rp   rq   rt   ru   r'   )rj   rk   r(   r     s    $c               C   s   t tjtj tjfgdS )z=
    Returns the equivalence between amu and molar mass.
    r   )r    r   gZmolr	   ur'   r'   r'   r(   r     s    c            	   C   s   t tjtjdd dd ftjtjd  tjtjd  dd dd ftjtjd  tjtjd  dd d	d ftjtj tjtj d
d dd fgdS )ze
    Returns a list of equivalence pairs that handle the conversion
    between mass and energy.
    c             S   s   | t jjd  S )NrM   )rB   rC   rD   )r7   r'   r'   r(   r8     r9   zmass_energy.<locals>.<lambda>c             S   s   | t jjd  S )NrM   )rB   rC   rD   )r7   r'   r'   r(   r8     r9   rM   c             S   s   | t jjd  S )NrM   )rB   rC   rD   )r7   r'   r'   r(   r8     r9   c             S   s   | t jjd  S )NrM   )rB   rC   rD   )r7   r'   r'   r(   r8     r9   r_   c             S   s   | t jjd  S )NrM   )rB   rC   rD   )r7   r'   r'   r(   r8     r9   c             S   s   | t jjd  S )NrM   )rB   rC   rD   )r7   r'   r'   r(   r8     r9   c             S   s   | t jjd  S )NrM   )rB   rC   rD   )r7   r'   r'   r(   r8     r9   c             S   s   | t jjd  S )NrM   )rB   rC   rD   )r7   r'   r'   r(   r8     r9   r   )r    r   ZkgrK   rI   re   r'   r'   r'   r(   r     s    
c                s   | j tjr:|j tjs$tdtdt ||  } }| 	tj
t |dk	r|tj  fdd} fdd}ttjtj||ftjtj tj||fgd| |d	S fd
d}fdd}ttjtj tj||fgd| |d	S dS )a  
    Defines the conversion between Jy/sr and "brightness temperature",
    :math:`T_B`, in Kelvins.  The brightness temperature is a unit very
    commonly used in radio astronomy.  See, e.g., "Tools of Radio Astronomy"
    (Wilson 2009) eqn 8.16 and eqn 8.19 (these pages are available on `google
    books
    <https://books.google.com/books?id=9KHw6R8rQEMC&pg=PA179&source=gbs_toc_r&cad=4#v=onepage&q&f=false>`__).

    :math:`T_B \equiv S_\nu / \left(2 k \nu^2 / c^2 \right)`

    If the input is in Jy/beam or Jy (assuming it came from a single beam), the
    beam area is essential for this computation: the brightness temperature is
    inversely proportional to the beam area.

    Parameters
    ----------
    frequency : `~astropy.units.Quantity`
        The observed ``spectral`` equivalent `~astropy.units.Unit` (e.g.,
        frequency or wavelength).  The variable is named 'frequency' because it
        is more commonly used in radio astronomy.
        BACKWARD COMPATIBILITY NOTE: previous versions of the brightness
        temperature equivalency used the keyword ``disp``, which is no longer
        supported.
    beam_area : `~astropy.units.Quantity` ['solid angle']
        Beam area in angular units, i.e. steradian equivalent

    Examples
    --------
    Arecibo C-band beam::

        >>> import numpy as np
        >>> from astropy import units as u
        >>> beam_sigma = 50*u.arcsec
        >>> beam_area = 2*np.pi*(beam_sigma)**2
        >>> freq = 5*u.GHz
        >>> equiv = u.brightness_temperature(freq)
        >>> (1*u.Jy/beam_area).to(u.K, equivalencies=equiv)  # doctest: +FLOAT_CMP
        <Quantity 3.526295144567176 K>

    VLA synthetic beam::

        >>> bmaj = 15*u.arcsec
        >>> bmin = 15*u.arcsec
        >>> fwhm_to_sigma = 1./(8*np.log(2))**0.5
        >>> beam_area = 2.*np.pi*(bmaj*bmin*fwhm_to_sigma**2)
        >>> freq = 5*u.GHz
        >>> equiv = u.brightness_temperature(freq)
        >>> (u.Jy/beam_area).to(u.K, equivalencies=equiv)  # doctest: +FLOAT_CMP
        <Quantity 217.2658703625732 K>

    Any generic surface brightness:

        >>> surf_brightness = 1e6*u.MJy/u.sr
        >>> surf_brightness.to(u.K, equivalencies=u.brightness_temperature(500*u.GHz)) # doctest: +FLOAT_CMP
        <Quantity 130.1931904778803 K>
    zFThe inputs to `brightness_temperature` are frequency and angular area.zThe inputs to `brightness_temperature` have changed. Frequency is now the first input, and angular area is the second, optional input.Nc                s8   dt j tj d  t jd  tjj}|   | S )NrM   )	rB   k_Br   KrC   tor   JyrD   )x_jybmrb   )beamnur'   r(   convert_Jy_to_Kc  s    ,z/brightness_temperature.<locals>.convert_Jy_to_Kc                s8   t jdtj d  tjd   tjj}|   | S )NrM   )	r   r}   rB   rz   rC   r|   r   r{   rD   )x_Krb   )r   r   r'   r(   convert_K_to_Jyg  s    ,z/brightness_temperature.<locals>.convert_K_to_Jyr   )	frequency	beam_areac                s4   dt j tj  d  t jd  tjj}| | S )NrM   )	rB   rz   r   r{   rC   r|   r   r}   rD   )Zx_jysrrb   )r   r'   r(   convert_JySr_to_Ko  s    ,z1brightness_temperature.<locals>.convert_JySr_to_Kc                s4   t jdtj  d  tjd   tjj}| | S )NrM   )	r   r}   rB   rz   rC   r|   r   r{   rD   )r   rb   )r   r'   r(   convert_K_to_JySrs  s    ,z1brightness_temperature.<locals>.convert_K_to_JySr)unitis_equivalentr   rg   rJ   rd   warningswarnr   r|   GHzr   rN   r    r   r}   r{   r   )r   r   r   r   r   r   r'   )r   r   r(   r     s$    9
c             C   sH   t tjt| ftjd t| d ftjtj tjt|  fgdd| iS )a  
    Convert between the ``beam`` unit, which is commonly used to express the area
    of a radio telescope resolution element, and an area on the sky.
    This equivalency also supports direct conversion between ``Jy/beam`` and
    ``Jy/steradian`` units, since that is a common operation.

    Parameters
    ----------
    beam_area : unit-like
        The area of the beam in angular area units (e.g., steradians)
        Must have angular area equivalent units.
    rA   r   r   )r    r   r   r   r}   )r   r'   r'   r(   r   {  s    c                s|   |  tjt |dkr.ddlm} | j}|fdd  fdd} fdd	}tt	j
tj tj||fgd
| |dS )a}  Defines the conversion between Jy/sr and "thermodynamic temperature",
    :math:`T_{CMB}`, in Kelvins.  The thermodynamic temperature is a unit very
    commonly used in cosmology. See eqn 8 in [1]

    :math:`K_{CMB} \equiv I_\nu / \left(2 k \nu^2 / c^2  f(\nu) \right)`

    with :math:`f(\nu) = \frac{ x^2 e^x}{(e^x - 1 )^2}`
    where :math:`x = h \nu / k T`

    Parameters
    ----------
    frequency : `~astropy.units.Quantity`
        The observed `spectral` equivalent `~astropy.units.Unit` (e.g.,
        frequency or wavelength). Must have spectral units.
    T_cmb :  `~astropy.units.Quantity` ['temperature'] or None
        The CMB temperature at z=0.  If `None`, the default cosmology will be
        used to get this temperature. Must have units of temperature.

    Notes
    -----
    For broad band receivers, this conversion do not hold
    as it highly depends on the frequency

    References
    ----------
    .. [1] Planck 2013 results. IX. HFI spectral response
       https://arxiv.org/abs/1303.5070

    Examples
    --------
    Planck HFI 143 GHz::

        >>> from astropy import units as u
        >>> from astropy.cosmology import Planck15
        >>> freq = 143 * u.GHz
        >>> equiv = u.thermodynamic_temperature(freq, Planck15.Tcmb0)
        >>> (1. * u.mK).to(u.MJy / u.sr, equivalencies=equiv)  # doctest: +FLOAT_CMP
        <Quantity 0.37993172 MJy / sr>

    Nr   )default_cosmologyc             S   s4   t j|  t j | }|d t| t|d  S )NrM   )rB   rF   rz   r;   expexpm1)r   T_cmbr7   r'   r'   r(   f  s    z$thermodynamic_temperature.<locals>.fc                s:    d t j tj d  t jd  tj}| | S )NrM   )rB   rz   r   r{   rC   rN   r   r}   )r~   rb   )r   r   r'   r(   r     s    2z2thermodynamic_temperature.<locals>.convert_Jy_to_Kc                s:   t j d tj d  tjd   tj}| | S )NrM   )r   r}   rB   rz   rC   rN   r   r{   )r   rb   )r   r   r'   r(   r     s    2z2thermodynamic_temperature.<locals>.convert_K_to_Jyr   )r   r   )r|   r   r   r   Zastropy.cosmologyr   getZTcmb0r    r   r}   rg   r{   )r   r   r   r   r   r'   )r   r   r(   r     s    )
c              C   s   ddl m} m} ttjtjdd dd ftj| dd dd ftj| dd d	d f|| d
d dd f|tjdd dd f|tjdd dd fgdS )zConvert between Kelvin, Celsius, Rankine and Fahrenheit here because
    Unit and CompositeUnit cannot do addition or subtraction properly.
    r   )deg_Fdeg_Rc             S   s   | d S )Ngfffffq@r'   )r7   r'   r'   r(   r8     r9   ztemperature.<locals>.<lambda>c             S   s   | d S )Ngfffffq@r'   )r7   r'   r'   r(   r8     r9   c             S   s   | d d S )Ng?g      @@r'   )r7   r'   r'   r(   r8     r9   c             S   s   | d d S )Ng      @@g?r'   )r7   r'   r'   r(   r8     r9   c             S   s   | d d d S )Ngfffffq@g?g      @@r'   )r7   r'   r'   r(   r8     r9   c             S   s   | d d d S )Ng      @@g?gfffffq@r'   )r7   r'   r'   r(   r8     r9   c             S   s   | d S )NgQ|@r'   )r7   r'   r'   r(   r8     r9   c             S   s   | d S )NgQ|@r'   )r7   r'   r'   r(   r8     r9   c             S   s   | d d S )NgQ~@grq?r'   )r7   r'   r'   r(   r8     r9   c             S   s   | d d S )Ng?gQ~@r'   )r7   r'   r'   r(   r8     r9   c             S   s   | d S )Ngrq?r'   )r7   r'   r'   r(   r8     r9   c             S   s   | d S )Ng?r'   )r7   r'   r'   r(   r8     r9   r   )Zimperialr   r   r    r   r{   Zdeg_C)r   r   r'   r'   r(   r     s    c               C   s    t tjtjdd dd fgdS )z;Convert between Kelvin and keV(eV) to an equivalent amount.c             S   s   | t jjt jj  S )N)rB   erD   rz   )r7   r'   r'   r(   r8     r9   z$temperature_energy.<locals>.<lambda>c             S   s   | t jjt jj  S )N)rB   r   rD   rz   )r7   r'   r'   r(   r8     r9   r   )r    r   r{   rr   r'   r'   r'   r(   r     s    c          
   C   sH   y|  tjt  W n. ttfk
rB } ztdW d d }~X Y nX d S )NzRThe 'rest' value must be a spectral equivalent (frequency, wavelength, or energy).)r|   r   rJ   r   AttributeErrorr   )rD   exr'   r'   r(   rv     s    rv   c             C   sz   | j  }tt|j|j}|tjd}|dkrBt	| tj }n |dkrZt	tj|  }nt
dttj|fgdd| iS )a  
    Convert between pixel distances (in units of ``pix``) and other units,
    given a particular ``pixscale``.

    Parameters
    ----------
    pixscale : `~astropy.units.Quantity`
        The pixel scale either in units of <unit>/pixel or pixel/<unit>.
    r   rA   r   z?The pixel scale unit must have pixel dimensionality of 1 or -1.r   pixscale)r   Z	decomposer$   zipbasesZpowersr   r	   Zpixr   r   r    )r   Z
decomposed
dimensionsZ	pix_powerZphysical_unitr'   r'   r(   r     s    
c                s   | j tjtj r(| tjtj  n4| j tjtj rTd|  tjtj  ntdttjtj fdd fddfgdd| iS )a!  
    Convert between lengths (to be interpreted as lengths in the focal plane)
    and angular units with a specified ``platescale``.

    Parameters
    ----------
    platescale : `~astropy.units.Quantity`
        The pixel scale either in units of distance/pixel or distance/angle.
    r   z;The pixel scale must be in angle/distance or distance/anglec                s   |   S )Nr'   )r?   )platescale_valr'   r(   r8     r9   zplate_scale.<locals>.<lambda>c                s   |   S )Nr'   )Zrad)r   r'   r(   r8     r9   r   
platescale)	r   r   r   ZarcsecrI   rN   r6   r   r    )r   r'   )r   r(   r     s    
"c             C   s^   | dkr ddl m} |j j} td| tjtj	 t
j  t
j }t|dfgdd| idS )a;  
    Convert between quantities with little-h and the equivalent physical units.

    Parameters
    ----------
    H0 : None or `~astropy.units.Quantity` ['frequency']
        The value of the Hubble constant to assume. If a `~astropy.units.Quantity`,
        will assume the quantity *is* ``H0``.  If `None` (default), use the
        ``H0`` attribute from the default `astropy.cosmology` cosmology.

    References
    ----------
    For an illuminating discussion on why you may or may not want to use
    little-h at all, see https://arxiv.org/pdf/1308.4150.pdf
    Nr   )	cosmologyd   r   H0)r%   )Zastropyr   r   r   r   r   rN   r   rw   re   r   ZMpcZlittlehr    )r   r   Zh100_val_unitr'   r'   r(   r     s
    &)N)N)N)N)-r4   collectionsr   numpyr;   r   Zastropy.constantsr   rB   Zastropy.utils.exceptionsr   Zastropy.utils.miscr   r!   r   r   r	   functionr
   r:   r   rc   r   r   __all__r    r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rv   r   r   r   r'   r'   r'   r(   <module>   sP    
#
 'ABI	
`
?