a
    ߙfb2                     @   s   d Z ddlmZ ddlmZ g dZddgiZe	 Z
ejdgde
d	d
didZejdge
ddd
didZdd Zd ddZd!ddZd"ddZd#dddddddZd$ddZee  e dure ee
7 Z dS )%z&Cosmological units and equivalencies.
    N)generate_unit_summary)littlehredshiftdimensionless_redshiftwith_redshiftredshift_distanceredshift_hubbleredshift_temperaturewith_H0)r   r   Zscipyr   FzCosmological redshift.Zlatex )prefixes	namespacedocformatr   z'Reduced/"dimensionless" Hubble constantzh_{100})r   r   r   r   c                   C   s   t tdfgdS )aN  Allow redshift to be 1-to-1 equivalent to dimensionless.

    It is special compared to other equivalency pairs in that it
    allows this independent of the power to which the redshift is raised,
    and independent of whether it is part of a more complicated unit.
    It is similar to u.dimensionless_angles() in this respect.
    Nr   )uEquivalencyr    r   r   6lib/python3.9/site-packages/astropy/cosmology/units.pyr   ,   s    r   comovingc                    s   ddl m}m | dur| n| } ||  | } W d   n1 sL0    Y  d}||vrptd| t| |d fdd} fd	d
}tt	tj
||fgd| |dS )a  Convert quantities between redshift and distance.

    Care should be taken to not misinterpret a relativistic, gravitational, etc
    redshift as a cosmological one.

    Parameters
    ----------
    cosmology : `~astropy.cosmology.Cosmology`, str, or None, optional
        A cosmology realization or built-in cosmology's name (e.g. 'Planck18').
        If None, will use the default cosmology
        (controlled by :class:`~astropy.cosmology.default_cosmology`).
    kind : {'comoving', 'lookback', 'luminosity'} or None, optional
        The distance type for the Equivalency.
        Note this does NOT include the angular diameter distance as this
        distance measure is not monotonic.
    **atzkw
        keyword arguments for :func:`~astropy.cosmology.z_at_value`

    Returns
    -------
    `~astropy.units.equivalencies.Equivalency`
        Equivalency between redshift and temperature.

    Examples
    --------
    >>> import astropy.units as u
    >>> import astropy.cosmology.units as cu
    >>> from astropy.cosmology import WMAP9

    >>> z = 1100 * cu.redshift
    >>> z.to(u.Mpc, cu.redshift_distance(WMAP9, kind="comoving"))  # doctest: +FLOAT_CMP
    <Quantity 14004.03157418 Mpc>
    r   default_cosmology
z_at_valueN)r   ZlookbackZ
luminosityz`kind` is not one of Z	_distancec                    s    | S )zRedshift to distance.r   z)methodr   r   z_to_distancef   s    z(redshift_distance.<locals>.z_to_distancec                    s   | t j> fi  S )zDistance to redshift.)r   Mpc)datzkwr   r   r   r   distance_to_zj   s    z(redshift_distance.<locals>.distance_to_zr   )	cosmologydistance)astropy.cosmologyr   r   getset
ValueErrorgetattrr   r   r   r   )r!   kindr   r   Zallowed_kindsr   r    r   r   r   r   7   s    "&r   c                    s   ddl m}m durn| | | W d   n1 sL0    Y  fdd fddfdd	}fd
d}tttjtj	 tj
 ftt||fgddiS )a  Convert quantities between redshift and Hubble parameter and little-h.

    Care should be taken to not misinterpret a relativistic, gravitational, etc
    redshift as a cosmological one.

    Parameters
    ----------
    cosmology : `~astropy.cosmology.Cosmology`, str, or None, optional
        A cosmology realization or built-in cosmology's name (e.g. 'Planck18').
        If None, will use the default cosmology
        (controlled by :class:`~astropy.cosmology.default_cosmology`).
    **atzkw
        keyword arguments for :func:`~astropy.cosmology.z_at_value`

    Returns
    -------
    `~astropy.units.equivalencies.Equivalency`
        Equivalency between redshift and Hubble parameter and little-h unit.

    Examples
    --------
    >>> import astropy.units as u
    >>> import astropy.cosmology.units as cu
    >>> from astropy.cosmology import WMAP9

    >>> z = 1100 * cu.redshift
    >>> equivalency = cu.redshift_hubble(WMAP9)  # construct equivalency

    >>> z.to(u.km / u.s / u.Mpc, equivalency)  # doctest: +FLOAT_CMP
    <Quantity 1565637.40154275 km / (Mpc s)>

    >>> z.to(cu.littleh, equivalency)  # doctest: +FLOAT_CMP
    <Quantity 15656.37401543 littleh>
    r   r   Nc                    s
     | S )zRedshift to Hubble parameter.Hr   r!   r   r   z_to_hubble   s    z$redshift_hubble.<locals>.z_to_hubblec                    s&   j | tjtj tj > fi  S )zHubble parameter to redshift.)r*   r   kmsr   r)   r   r!   r   r   r   hubble_to_z   s    z$redshift_hubble.<locals>.hubble_to_zc                    s$    |  tjtj tj d t S )z$Redshift to :math:`h`-unit Quantity.d   )to_valuer   r-   r.   r   r   r   )r,   r   r   z_to_littleh   s    z%redshift_hubble.<locals>.z_to_littlehc                    s    | d S )z$:math:`h`-unit Quantity to redshift.r1   r   )h)r0   r   r   littleh_to_z   s    z%redshift_hubble.<locals>.littleh_to_zr   r!   )r#   r   r   r$   r%   r   r   r   r-   r.   r   r   )r!   r   r   r3   r5   r   )r   r!   r0   r   r,   r   r   s   s    #&
r   c                    s   ddl m}m durn| | | W d   n1 sL0    Y  fdd} fdd}tttj||fgdd	iS )
a  Convert quantities between redshift and CMB temperature.

    Care should be taken to not misinterpret a relativistic, gravitational, etc
    redshift as a cosmological one.

    Parameters
    ----------
    cosmology : `~astropy.cosmology.Cosmology`, str, or None, optional
        A cosmology realization or built-in cosmology's name (e.g. 'Planck18').
        If None, will use the default cosmology
        (controlled by :class:`~astropy.cosmology.default_cosmology`).
    **atzkw
        keyword arguments for :func:`~astropy.cosmology.z_at_value`

    Returns
    -------
    `~astropy.units.equivalencies.Equivalency`
        Equivalency between redshift and temperature.

    Examples
    --------
    >>> import astropy.units as u
    >>> import astropy.cosmology.units as cu
    >>> from astropy.cosmology import WMAP9

    >>> z = 1100 * cu.redshift
    >>> z.to(u.K, cu.redshift_temperature(WMAP9))
    <Quantity 3000.225 K>
    r   r   Nc                    s
     | S N)Tcmbr   r+   r   r   	z_to_Tcmb   s    z'redshift_temperature.<locals>.z_to_Tcmbc                    s   j | tj> fi  S r6   )r7   r   K)Tr/   r   r   	Tcmb_to_z   s    z'redshift_temperature.<locals>.Tcmb_to_zr	   r!   )	r#   r   r   r$   r%   r   r   r   r9   )r!   r   r   r8   r;   r   r/   r   r	      s    &r	   T)r"   hubbler7   r   c             	   C   s   ddl m}m} | dur| n| } ||  | } W d   n1 sL0    Y  |durb|ni }g }|r|t| fi | |r|t| fi | |dur|t| fd|i| t	
|d| |||dS )a  Convert quantities between measures of cosmological distance.

    Note: by default all equivalencies are on and must be explicitly turned off.
    Care should be taken to not misinterpret a relativistic, gravitational, etc
    redshift as a cosmological one.

    Parameters
    ----------
    cosmology : `~astropy.cosmology.Cosmology`, str, or None, optional
        A cosmology realization or built-in cosmology's name (e.g. 'Planck18').
        If `None`, will use the default cosmology
        (controlled by :class:`~astropy.cosmology.default_cosmology`).

    distance : {'comoving', 'lookback', 'luminosity'} or None (optional, keyword-only)
        The type of distance equivalency to create or `None`.
        Default is 'comoving'.
    hubble : bool (optional, keyword-only)
        Whether to create a Hubble parameter <-> redshift equivalency, using
        ``Cosmology.H``. Default is `True`.
    Tcmb : bool (optional, keyword-only)
        Whether to create a CMB temperature <-> redshift equivalency, using
        ``Cosmology.Tcmb``. Default is `True`.

    atzkw : dict or None (optional, keyword-only)
        keyword arguments for :func:`~astropy.cosmology.z_at_value`

    Returns
    -------
    `~astropy.units.equivalencies.Equivalency`
        With equivalencies between redshift and distance / Hubble / temperature.

    Examples
    --------
    >>> import astropy.units as u
    >>> import astropy.cosmology.units as cu
    >>> from astropy.cosmology import WMAP9

    >>> equivalency = cu.with_redshift(WMAP9)
    >>> z = 1100 * cu.redshift

    Redshift to (comoving) distance:

    >>> z.to(u.Mpc, equivalency)  # doctest: +FLOAT_CMP
    <Quantity 14004.03157418 Mpc>

    Redshift to the Hubble parameter:

    >>> z.to(u.km / u.s / u.Mpc, equivalency)  # doctest: +FLOAT_CMP
    <Quantity 1565637.40154275 km / (Mpc s)>

    >>> z.to(cu.littleh, equivalency)  # doctest: +FLOAT_CMP
    <Quantity 15656.37401543 littleh>

    Redshift to CMB temperature:

    >>> z.to(u.K, equivalency)
    <Quantity 3000.225 K>
    r   r   Nr(   r   )r!   r"   r<   r7   )r#   r   r   r$   r%   extendr   r	   r   r   r   )r!   r"   r<   r7   r   r   r   Zequivsr   r   r   r      s"    =&r   c                 C   s^   | du rddl m} | j} td| tjtj tj	  t
 }tj|dfgdd| idS )aD  
    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
        :mod:`~astropy.cosmology.default_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
    N   )r   r1   r
   H0)kwargs)Zrealizationsr   r$   r?   r   ZUnitr2   r-   r.   r   r   r   )r?   r   Zh100_val_unitr   r   r   r
   >  s
    
&r
   )Nr   )N)N)N)N)__doc__Zastropy.unitsZunitsr   Zastropy.units.utilsr   Z_generate_unit_summary__all__Z__doctest_requires__globalsZ_nsZdef_unitr   r   r   r   r   r	   r   r
   Zadd_enabled_equivalenciesr   r   r   r   <module>   s0   
	
<
@
0[
