1# -*- coding: utf-8 -*-
2# Licensed under a 3-clause BSD style license - see LICENSE.rst
3
4"""
5This package defines the astrophysics-specific units.  They are also
6available in the `astropy.units` namespace.
7"""
8
9
10from . import si
11from astropy.constants import si as _si
12from .core import (UnitBase, def_unit, si_prefixes, binary_prefixes,
13                   set_enabled_units)
14
15# To ensure si units of the constants can be interpreted.
16set_enabled_units([si])
17
18import numpy as _numpy
19
20_ns = globals()
21
22###########################################################################
23# LENGTH
24
25def_unit((['AU', 'au'], ['astronomical_unit']), _si.au, namespace=_ns, prefixes=True,
26         doc="astronomical unit: approximately the mean Earth--Sun "
27         "distance.")
28
29def_unit(['pc', 'parsec'], _si.pc, namespace=_ns, prefixes=True,
30         doc="parsec: approximately 3.26 light-years.")
31
32def_unit(['solRad', 'R_sun', 'Rsun'], _si.R_sun, namespace=_ns,
33         doc="Solar radius", prefixes=False,
34         format={'latex': r'R_{\odot}', 'unicode': 'R\N{SUN}'})
35def_unit(['jupiterRad', 'R_jup', 'Rjup', 'R_jupiter', 'Rjupiter'],
36         _si.R_jup, namespace=_ns, prefixes=False, doc="Jupiter radius",
37         # LaTeX jupiter symbol requires wasysym
38         format={'latex': r'R_{\rm J}', 'unicode': 'R\N{JUPITER}'})
39def_unit(['earthRad', 'R_earth', 'Rearth'], _si.R_earth, namespace=_ns,
40         prefixes=False, doc="Earth radius",
41         # LaTeX earth symbol requires wasysym
42         format={'latex': r'R_{\oplus}', 'unicode': 'R⊕'})
43
44def_unit(['lyr', 'lightyear'], (_si.c * si.yr).to(si.m),
45         namespace=_ns, prefixes=True, doc="Light year")
46def_unit(['lsec', 'lightsecond'], (_si.c * si.s).to(si.m),
47         namespace=_ns, prefixes=False, doc="Light second")
48
49
50###########################################################################
51# MASS
52
53def_unit(['solMass', 'M_sun', 'Msun'], _si.M_sun, namespace=_ns,
54         prefixes=False, doc="Solar mass",
55         format={'latex': r'M_{\odot}', 'unicode': 'M\N{SUN}'})
56def_unit(['jupiterMass', 'M_jup', 'Mjup', 'M_jupiter', 'Mjupiter'],
57         _si.M_jup, namespace=_ns, prefixes=False, doc="Jupiter mass",
58         # LaTeX jupiter symbol requires wasysym
59         format={'latex': r'M_{\rm J}', 'unicode': 'M\N{JUPITER}'})
60def_unit(['earthMass', 'M_earth', 'Mearth'], _si.M_earth, namespace=_ns,
61         prefixes=False, doc="Earth mass",
62         # LaTeX earth symbol requires wasysym
63         format={'latex': r'M_{\oplus}', 'unicode': 'M⊕'})
64
65##########################################################################
66# ENERGY
67
68# Here, explicitly convert the planck constant to 'eV s' since the constant
69# can override that to give a more precise value that takes into account
70# covariances between e and h.  Eventually, this may also be replaced with
71# just `_si.Ryd.to(eV)`.
72def_unit(['Ry', 'rydberg'],
73         (_si.Ryd * _si.c * _si.h.to(si.eV * si.s)).to(si.eV),
74         namespace=_ns, prefixes=True,
75         doc="Rydberg: Energy of a photon whose wavenumber is the Rydberg "
76         "constant",
77         format={'latex': r'R_{\infty}', 'unicode': 'R∞'})
78
79###########################################################################
80# ILLUMINATION
81
82def_unit(['solLum', 'L_sun', 'Lsun'], _si.L_sun, namespace=_ns,
83         prefixes=False, doc="Solar luminance",
84         format={'latex': r'L_{\odot}', 'unicode': 'L\N{SUN}'})
85
86
87###########################################################################
88# SPECTRAL DENSITY
89
90def_unit((['ph', 'photon'], ['photon']),
91         format={'ogip': 'photon', 'vounit': 'photon'},
92         namespace=_ns, prefixes=True)
93def_unit(['Jy', 'Jansky', 'jansky'], 1e-26 * si.W / si.m ** 2 / si.Hz,
94         namespace=_ns, prefixes=True,
95         doc="Jansky: spectral flux density")
96def_unit(['R', 'Rayleigh', 'rayleigh'],
97         (1e10 / (4 * _numpy.pi)) *
98         ph * si.m ** -2 * si.s ** -1 * si.sr ** -1,
99         namespace=_ns, prefixes=True,
100         doc="Rayleigh: photon flux")
101
102
103###########################################################################
104# EVENTS
105
106def_unit((['ct', 'count'], ['count']),
107         format={'fits': 'count', 'ogip': 'count', 'vounit': 'count'},
108         namespace=_ns, prefixes=True, exclude_prefixes=['p'])
109def_unit(['adu'], namespace=_ns, prefixes=True)
110def_unit(['DN', 'dn'], namespace=_ns, prefixes=False)
111
112###########################################################################
113# MISCELLANEOUS
114
115# Some of these are very FITS-specific and perhaps considered a mistake.
116# Maybe they should be moved into the FITS format class?
117# TODO: This is defined by the FITS standard as "relative to the sun".
118# Is that mass, volume, what?
119def_unit(['Sun'], namespace=_ns)
120def_unit(['chan'], namespace=_ns, prefixes=True)
121def_unit(['bin'], namespace=_ns, prefixes=True)
122def_unit(['beam'], namespace=_ns, prefixes=True)
123def_unit(['electron'], doc="Number of electrons", namespace=_ns,
124         format={'latex': r'e^{-}', 'unicode': 'e⁻'})
125
126###########################################################################
127# CLEANUP
128
129del UnitBase
130del def_unit
131del si
132
133
134###########################################################################
135# DOCSTRING
136
137# This generates a docstring for this module that describes all of the
138# standard units defined here.
139from .utils import generate_unit_summary as _generate_unit_summary
140if __doc__ is not None:
141    __doc__ += _generate_unit_summary(globals())
142
143
144# -------------------------------------------------------------------------
145
146def __getattr__(attr):
147    if attr == "littleh":
148        import warnings
149        from astropy.cosmology.units import littleh
150        from astropy.utils.exceptions import AstropyDeprecationWarning
151
152        warnings.warn(
153            ("`littleh` is deprecated from module `astropy.units.astrophys` "
154             "since astropy 5.0 and may be removed in a future version. "
155             "Use `astropy.cosmology.units.littleh` instead."),
156            AstropyDeprecationWarning)
157
158        return littleh
159
160    raise AttributeError(f"module {__name__!r} has no attribute {attr!r}.")
161