Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions astropy/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Contains astronomical and physical constants for use in Astropy or other
places.

The package contains a `~astropy.constants.cgs` and `~astropy.constants.si`
module that define constants in CGS and SI units, respectively. A typical use
case might be::

from astropy.constants.cgs import c

... define the mass of something you want the rest energy of as m ...
E = m*c**2

"""

from . import cgs
from . import si
from .constant import Constant


# Update the docstring to include a list of units from the si module
__doc__ += """
The following constants are defined in `~astropy.constants.cgs` and
`~astropy.constants.si` .

"""

for nm, val in sorted(si.__dict__.iteritems()):
if isinstance(val, Constant):
__doc__ += ' * ' + nm + '\n ' + val.name + '\n'
del nm, val
__doc__ += '\n'
101 changes: 101 additions & 0 deletions astropy/constants/cgs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Numerical constants, in cgs units

from .constant import Constant
from . import si

# PHYSICAL CONSTANTS

# Planck constant
h = Constant(si.h * 1.e7, si.h.error * 1.e7,
si.h.name, si.h.origin, 'erg.s')

# Reduced Planck constant
hbar = Constant(si.hbar * 1.e7, si.hbar.error * 1.e7,
si.hbar.name, si.hbar.origin, 'erg.s')

# Boltzmann constant [CODATA]
k_B = Constant(si.k_B * 1.e7, si.k_B.error * 1.e7,
si.k_B.name, si.k_B.origin, 'erg.s')

# Speed of light [CODATA]
c = Constant(si.c * 1.e2, si.c.error * 1.e2,
si.c.name, si.c.origin, 'cm/s')

# Gravitional constant [CODATA]
G = Constant(si.G * 1.e3, si.G.error * 1.e3,
si.G.name, si.G.origin, 'cm^3/g/s^2')

# Proton mass [CODATA]
m_p = Constant(si.m_p * 1.e3, si.m_p.error * 1.e3,
si.m_p.name, si.m_p.origin, 'g')

# Neutron mass [CODATA]
m_n = Constant(si.m_n * 1.e3, si.m_n.error * 1.e3,
si.m_n.name, si.m_n.origin, 'g')

# Electron mass [CODATA]
m_e = Constant(si.m_e * 1.e3, si.m_e.error * 1.e3,
si.m_e.name, si.m_e.origin, 'g')

# Stefan-Boltzmann constant
sigma_sb = Constant(si.sigma_sb * 1.e3, si.sigma_sb.error * 1.e3,
si.sigma_sb.name, si.sigma_sb.origin,
'erg/cm^2/K^4/s')

# Electron charge
e = Constant(si.e * si.c * 10., si.e.error * si.c * 10.,
si.e.name, si.e.origin, 'statC')

# Avogadro's number
N_A = Constant(si.N_A, si.N_A.error,
si.N_A.name, si.N_A.origin, '/mol')

# Gas constant
R = Constant(si.R * 1.e7, si.R.error * 1.e7,
si.R.name, si.R.origin, 'erg/K/mol')

# DISTANCE

# Astronomical Unit
au = Constant(si.au * 1.e2, si.au.error * 1.e2,
si.au.name, si.au.origin, 'cm')

# Parsec
pc = Constant(si.pc * 1.e2, si.pc.error * 1.e2,
si.pc.name, si.pc.origin, 'cm')

# Kiloparsec
kpc = Constant(si.kpc * 1.e2, si.kpc.error * 1.e2,
si.kpc.name, si.kpc.origin, 'cm')

# SOLAR QUANTITIES

# Solar luminosity
L_sun = Constant(si.L_sun * 1.e7, si.L_sun.error * 1.e7,
si.L_sun.name, si.L_sun.origin, 'erg/s')

# Solar mass
M_sun = Constant(si.M_sun * 1.e3, si.M_sun.error * 1.e3,
si.M_sun.name, si.M_sun.origin, 'g')

# Solar radius
R_sun = Constant(si.R_sun * 1.e2, si.R_sun.error * 1.e2,
si.R_sun.name, si.R_sun.origin, 'cm')

# OTHER SOLAR SYSTEM QUANTITIES

# Jupiter mass
M_jup = Constant(si.M_jup * 1.e3, si.M_jup.error * 1.e3,
si.M_jup.name, si.M_jup.origin, 'g')

# Jupiter equatorial radius
R_jup = Constant(si.R_jup * 1.e2, si.R_jup.error * 1.e2,
si.R_jup.name, si.R_jup.origin, 'cm')

# Earth mass
M_earth = Constant(si.M_earth * 1.e3, si.M_earth.error * 1.e3,
si.M_earth.name, si.M_earth.origin, 'g')

# Earth equatorial radius
R_earth = Constant(si.R_earth * 1.e2, si.R_earth.error * 1.e2,
si.R_earth.name, si.R_earth.origin, 'cm')
50 changes: 50 additions & 0 deletions astropy/constants/constant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Constant(float):
"""A physical or astronomical constant.

Objects of this class behave just like `float` values when used in
expressions, but they contain additional information

Attributes
----------
name : str
The name of this constant.
error : float
The uncertainty in the value of this constant.
origin : str
The source used for the value of this constant.
units : str
A string describing the units

.. warning ::
The `units` attribute will be replaced when the Astropy units package
is implemented. Don't count on it being a simple string in the future.

"""

def __init__(self, value, error, name, origin, units):
self.error = error
self.name = name
self.origin = origin
self.units = units
return float.__init__(self, value)

def __new__(self, value, error, name, origin, units):
return float.__new__(self, value)

def __repr__(self):
s = "<Constant: "
s += "name='{0}' ".format(self.name)
s += "value={0} ".format(float.__repr__(self))
s += "error={0} ".format(self.error)
s += "units='{0}' ".format(self.units)
s += "origin='{0}' ".format(self.origin)
s += ">"
return s

def __str__(self):
s = " Name = {0}\n".format(self.name)
s += " Value = {0}\n".format(float.__repr__(self))
s += " Error = {0}\n".format(self.error)
s += " Units = {0}\n".format(self.units)
s += " Origin = {0}".format(self.origin)
return s
124 changes: 124 additions & 0 deletions astropy/constants/si.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Numerical constants, in cgs units

import numpy

from .constant import Constant

# PHYSICAL CONSTANTS

# Planck constant
h = Constant(6.62606957e-34, 0.00000029e-34,
"Planck constant",
'CODATA 2010', 'J.s')

# Reduced Planck constant
hbar = Constant(h * 0.5 / numpy.pi, h.error * 0.5 / numpy.pi,
"Reduced Planck constant",
'CODATA 2010', 'J.s')

# Boltzmann constant
k_B = Constant(1.3806488e-23, 0.0000013e-23,
"Boltzmann constant",
'CODATA 2010', 'J/K')

# Speed of light
c = Constant(2.99792458e8, 0.,
"Speed of light in vacuum",
'CODATA 2010', 'm/s')

# Gravitional constant
G = Constant(6.67384e-11, 0.00080e-11,
"Gravitational constant",
'CODATA 2010', 'm^3/kg/s^2')

# Proton mass
m_p = Constant(1.672621777e-27, 0.000000074e-27,
"Proton mass",
'CODATA 2010', 'kg')

# Neutron mass
m_n = Constant(1.674927351e-27, 0.000000074e-27,
"Neutron mass",
'CODATA 2010', 'kg')

# Electron mass
m_e = Constant(9.10938291e-31, 0.00000040e-31,
"Electron mass",
'CODATA 2010', 'kg')

# Stefan-Boltzmann constant
sigma_sb = Constant(5.670373e-8, 0.000021e-8,
"Stefan-Boltzmann constant",
'CODATA 2010', 'W/m^2/K^4')

# Electron charge
e = Constant(1.602176565e-19, 0.000000035e-19,
"Electron charge",
'CODATA 2010', 'C')

# Avogadro's number
N_A = Constant(6.02214129e23, 0.00000027e23,
"Avogadro's number",
'CODATA 2010', '/mol')

# Gas constant
R = Constant(8.3144621, 0.0000075,
"Gas constant",
'CODATA 2010', 'J/mol/K')

# DISTANCE

# Astronomical Unit
au = Constant(1.4959787066e11, 0.00000000005e11,
"Astronomical Unit",
"Allen's Astrophysical Quantities 4th Ed.", 'm')

# Parsec
pc = Constant(3.0856776e16, 0.00000005e16,
"Parsec",
"Allen's Astrophysical Quantities 4th Ed.", 'm')

# Kiloparsec
kpc = Constant(3.0856776e19, 0.00000005e19,
"Kiloparsec",
"Allen's Astrophysical Quantities 4th Ed.", 'm')

# SOLAR QUANTITIES

# Solar luminosity
L_sun = Constant(3.846e26, 0.0005e26,
"Solar luminosity",
"Allen's Astrophysical Quantities 4th Ed.", 'W')

# Solar mass
M_sun = Constant(1.9891e30, 0.00005e30,
"Solar mass",
"Allen's Astrophysical Quantities 4th Ed.", 'kg')

# Solar radius
R_sun = Constant(6.95508e8, 0.00026e8,
"Solar radius",
"Allen's Astrophysical Quantities 4th Ed.", 'm')


# OTHER SOLAR SYSTEM QUANTITIES

# Jupiter mass
M_jup = Constant(1.8987e27, 0.00005e27,
"Jupiter mass",
"Allen's Astrophysical Quantities 4th Ed.", 'kg')

# Jupiter equatorial radius
R_jup = Constant(7.1492e7, 0.00005e7,
"Jupiter equatorial radius",
"Allen's Astrophysical Quantities 4th Ed.", 'm')

# Earth mass
M_earth = Constant(5.9742e24, 0.00005e24,
"Earth mass",
"Allen's Astrophysical Quantities 4th Ed.", 'kg')

# Earth equatorial radius
R_earth = Constant(6.378136e6, 0.0000005e6,
"Earth equatorial radius",
"Allen's Astrophysical Quantities 4th Ed.", 'm')