Constants (astropy.constants)

Introduction

astropy.constants contains a number of physical constants useful in Astronomy. Constants are Quantity objects with additional metadata describing their provenance and uncertainties.

Getting Started

To use the constants in International System of Units (SI units), you can import the constants directly from the astropy.constants sub-package:

>>> from astropy.constants import G

Or, if you want to avoid having to explicitly import all of the constants you need, you can do:

>>> from astropy import constants as const

and then subsequently use, for example, const.G. Constants are fully-fledged Quantity objects, so you can conveniently convert them to different units. For example:

>>> print(const.c)
  Name   = Speed of light in vacuum
  Value  = 299792458.0
  Uncertainty  = 0.0
  Unit  = m / s
  Reference = CODATA 2014

>>> print(const.c.to('km/s'))
299792.458 km / s

>>> print(const.c.to('pc/yr'))  
0.306601393788 pc / yr

You can then use them in conjunction with unit and other nonconstant Quantity objects:

>>> from astropy import units as u
>>> F = (const.G * 3. * const.M_sun * 100 * u.kg) / (2.2 * u.au) ** 2
>>> print(F.to(u.N))  
0.3675671602160826 N

It is possible to convert most constants to Centimeter-Gram-Second (CGS) units using, for example:

>>> const.c.cgs  
<Quantity   2.99792458e+10 cm / s>

However, some constants are defined with different physical dimensions in CGS and cannot be directly converted. Because of this ambiguity, such constants cannot be used in expressions without specifying a system:

>>> 100 * const.e
Traceback (most recent call last):
    ...
TypeError: Constant u'e' does not have physically compatible units
across all systems of units and cannot be combined with other
values without specifying a system (eg. e.emu)
>>> 100 * const.e.esu  
<Quantity 4.8032045057134676e-08 Fr>

Collections of Constants (and Prior Versions)

Constants are organized into version modules. The constants for astropy 1.3 can be accessed in the astropyconst13 module. For example:

>>> from astropy.constants import astropyconst13 as const
>>> print(const.e)
  Name   = Electron charge
  Value  = 1.602176565e-19
  Uncertainty  = 3.5e-27
  Unit  = C
  Reference = CODATA 2010

Physical CODATA constants are in modules with names like codata2010 or codata2014:

>>> from astropy.constants import codata2010 as const
>>> print(const.h)
  Name   = Planck constant
  Value  = 6.62606957e-34
  Uncertainty  = 2.9e-41
  Unit  = J s
  Reference = CODATA 2010

Astronomical constants defined (primarily) by the International Astronomical Union (IAU) are collected in modules with names like iau2012 or iau2015:

>>> from astropy.constants import iau2012 as const
>>> print(const.L_sun)
  Name   = Solar luminosity
  Value  = 3.846e+26
  Uncertainty  = 5e+22
  Unit  = W
  Reference = Allen's Astrophysical Quantities 4th Ed.
>>> from astropy.constants import iau2015 as const
>>> print(const.L_sun)
  Name   = Nominal solar luminosity
  Value  = 3.828e+26
  Uncertainty  = 0.0
  Unit  = W
  Reference = IAU 2015 Resolution B 3

The astronomical and physical constants are combined into modules with names like astropyconst13 and astropyconst20 for different versions. However, importing these prior version modules directly will lead to inconsistencies with other subpackages that have already imported astropy.constants. Notably, astropy.units will have already used the default version of constants. When using prior versions of the constants in this manner, quantities should be constructed with constants instead of units.

To ensure consistent use of a prior version of constants in other Astropy packages (such as astropy.units) that import constants, the physical and astronomical constants versions should be set via ScienceState classes. These must be set before the first import of either astropy.constants or astropy.units. For example, you can use the CODATA2010 physical constants and the IAU 2012 astronomical constants:

>>> from astropy import physical_constants, astronomical_constants
>>> physical_constants.set('codata2010')  # doctest: +SKIP
<ScienceState physical_constants: 'codata2010'>
>>> physical_constants.get()  # doctest: +SKIP
'codata2010'
>>> astronomical_constants.set('iau2012')  # doctest: +SKIP
<ScienceState astronomical_constants: 'iau2012'>
>>> astronomical_constants.get()  # doctest: +SKIP
'iau2012'

Then all other packages that import astropy.constants will self-consistently initialize with that prior version of constants.

The versions may also be set using values referring to the version modules:

>>> from astropy import physical_constants, astronomical_constants
>>> physical_constants.set('astropyconst13')  # doctest: +SKIP
<ScienceState physical_constants: 'codata2010'>
>>> physical_constants.get()  # doctest: +SKIP
'codata2010'
>>> astronomical_constants.set('astropyconst13')  # doctest: +SKIP
<ScienceState astronomical_constants: 'iau2012'>
>>> astronomical_constants.get()  # doctest: +SKIP
'iau2012'

If either astropy.constants or astropy.units have already been imported, a RuntimeError will be raised.

>>> import astropy.units
>>> from astropy import physical_constants, astronomical_constants
>>> astronomical_constants.set('astropyconst13')
Traceback (most recent call last):
    ...
RuntimeError: astropy.units is already imported

To temporarily set constants to an older version (e.g., for regression testing), a context manager is available, as follows:

>>> from astropy import constants as const
>>> with const.set_enabled_constants('astropyconst13'):
...     print(const.h)
  Name   = Planck constant
  Value  = 6.62606957e-34
  Uncertainty  = 2.9e-41
  Unit  = J s
  Reference = CODATA 2010
>>> print(const.h)
  Name   = Planck constant
  Value  = 6.62607004e-34
  Uncertainty  = 8.1e-42
  Unit  = J s
  Reference = CODATA 2014

The context manager may be used at any time in a Python session, but it uses the prior version only for astropy.constants, and not for any other subpackage such as astropy.units.

Reference/API

astropy.constants Package

Contains astronomical and physical constants for use in Astropy or other places.

A typical use case might be:

>>> from astropy.constants import c, m_e
>>> # ... define the mass of something you want the rest energy of as m ...
>>> m = m_e
>>> E = m * c**2
>>> E.to('MeV')  
<Quantity 0.510998927603161 MeV>

The following constants are available:

Name

Value

Unit

Description

G

6.67408e-11

m3 / (kg s2)

Gravitational constant

N_A

6.02214086e+23

1 / (mol)

Avogadro’s number

R

8.3144598

J / (K mol)

Gas constant

Ryd

10973731.6

1 / (m)

Rydberg constant

a0

5.29177211e-11

m

Bohr radius

alpha

0.00729735257

Fine-structure constant

atm

101325

Pa

Standard atmosphere

b_wien

0.0028977729

m K

Wien wavelength displacement law constant

c

299792458

m / (s)

Speed of light in vacuum

e

1.60217662e-19

C

Electron charge

e

1.60217662e-20

abC

Electron charge

e

4.80320467e-10

statC

Electron charge

e

4.80320467e-10

Fr

Electron charge

eps0

8.85418782e-12

F/m

Electric constant

g0

9.80665

m / s2

Standard acceleration of gravity

h

6.62607004e-34

J s

Planck constant

hbar

1.0545718e-34

J s

Reduced Planck constant

k_B

1.38064852e-23

J / (K)

Boltzmann constant

m_e

9.10938356e-31

kg

Electron mass

m_n

1.67492747e-27

kg

Neutron mass

m_p

1.6726219e-27

kg

Proton mass

mu0

1.25663706e-06

N/A2

Magnetic constant

muB

9.27400999e-24

J/T

Bohr magneton

sigma_T

6.65245872e-29

m2

Thomson scattering cross-section

sigma_sb

5.670367e-08

W / (K4 m2)

Stefan-Boltzmann constant

u

1.66053904e-27

kg

Atomic mass

G

6.67408e-11

m3 / (kg s2)

Gravitational constant

GM_earth

3.986004e+14

m3 / (s2)

Nominal Earth mass parameter

GM_jup

1.2668653e+17

m3 / (s2)

Nominal Jupiter mass parameter

GM_sun

1.3271244e+20

m3 / (s2)

Nominal solar mass parameter

L_bol0

3.0128e+28

W

Luminosity for absolute bolometric magnitude 0

L_sun

3.828e+26

W

Nominal solar luminosity

M_earth

5.97236473e+24

kg

Earth mass

M_jup

1.89818717e+27

kg

Jupiter mass

M_sun

1.98847542e+30

kg

Solar mass

R_earth

6378100

m

Nominal Earth equatorial radius

R_jup

71492000

m

Nominal Jupiter equatorial radius

R_sun

695700000

m

Nominal solar radius

au

1.49597871e+11

m

Astronomical Unit

kpc

3.08567758e+19

m

Kiloparsec

pc

3.08567758e+16

m

Parsec

Functions

set_enabled_constants(modname)

Context manager to temporarily set values in the constants namespace to an older version.

Classes

Constant

A physical or astronomical constant.

EMConstant

An electromagnetic constant.

Class Inheritance Diagram

Inheritance diagram of astropy.constants.constant.Constant, astropy.constants.constant.EMConstant