Skip to content

Commit

Permalink
Merge pull request #1268 from mdboom/unit/enabling
Browse files Browse the repository at this point in the history
Allow sets of units to be enabled
  • Loading branch information
mdboom committed Oct 7, 2013
2 parents 1fb6281 + 8528741 commit a40cb7b
Show file tree
Hide file tree
Showing 19 changed files with 564 additions and 344 deletions.
13 changes: 7 additions & 6 deletions astropy/units/__init__.py
Expand Up @@ -16,20 +16,21 @@

from . import si
from . import cgs
from . import astrophys

from .si import *
from .astrophys import *
from .cgs import *
from .imperial import *
from .physical import *

from .equivalencies import *

# Create a special singleton for the dimensionless unit
dimensionless_unscaled = Unit(1)

# After importing the unit definitions above, set the unit namespace
# to this top-level module so that new units are added here.
from .core import _UnitRegistry
_UnitRegistry().namespace = locals()

del bases

# Enable the set of default units. This notably does *not* include
# Imperial units.

set_enabled_units([si, cgs, astrophys])
56 changes: 28 additions & 28 deletions astropy/units/astrophys.py
Expand Up @@ -17,64 +17,64 @@

from . import si
from ..constants import si as _si
from .core import UnitBase, def_unit, _UnitRegistry
from .core import UnitBase, def_unit

import numpy as _numpy

_UnitRegistry().namespace = globals()
_ns = globals()

###########################################################################
# LENGTH

def_unit(['AU', 'au'], _si.au.value * si.m, register=True, prefixes=True,
def_unit(['AU', 'au'], _si.au.value * si.m, namespace=_ns, prefixes=True,
doc="astronomical unit: approximately the mean Earth--Sun "
"distance.")

def_unit(['pc', 'parsec'], _si.pc.value * si.m, register=True, prefixes=True,
def_unit(['pc', 'parsec'], _si.pc.value * si.m, namespace=_ns, prefixes=True,
doc="parsec: approximately 3.26 light-years.")

def_unit(['solRad', 'R_sun', 'Rsun'], _si.R_sun.value * si.m, register=True,
def_unit(['solRad', 'R_sun', 'Rsun'], _si.R_sun.value * si.m, namespace=_ns,
doc="Solar radius",
format={'latex': r'R_{\odot}', 'unicode': 'R⊙'})
def_unit(['lyr', 'lightyear'], _si.c.value * si.yr.to(si.s) * si.m, register=True,
def_unit(['lyr', 'lightyear'], _si.c.value * si.yr.to(si.s) * si.m, namespace=_ns,
doc="Light year")


###########################################################################
# AREAS

def_unit(['barn'], 10 ** -28 * si.m ** 2, register=True, prefixes=True,
def_unit(['barn'], 10 ** -28 * si.m ** 2, namespace=_ns, prefixes=True,
doc="barn: unit of area used in HEP")


###########################################################################
# ANGULAR MEASUREMENTS

def_unit(['cycle', 'cy'], 2.0 * _numpy.pi * si.rad,
register=True, prefixes=False,
namespace=_ns, prefixes=False,
doc="cycle: angular measurement, a full turn or rotation")

###########################################################################
# MASS

def_unit(['solMass', 'M_sun', 'Msun'], _si.M_sun.value * si.kg, register=True,
def_unit(['solMass', 'M_sun', 'Msun'], _si.M_sun.value * si.kg, namespace=_ns,
prefixes=True, doc="Solar mass",
format={'latex': r'M_{\odot}', 'unicode': 'M⊙'})
def_unit(['M_p'], _si.m_p.value * si.kg, register=True,
def_unit(['M_p'], _si.m_p.value * si.kg, namespace=_ns,
doc="Proton mass",
format={'latex': r'M_{p}', 'unicode': 'Mₚ'})
def_unit(['M_e'], _si.m_e.value * si.kg, register=True,
def_unit(['M_e'], _si.m_e.value * si.kg, namespace=_ns,
doc="Electron mass",
format={'latex': r'M_{e}', 'unicode': 'Mₑ'})
# Unified atomic mass unit
def_unit(['u', 'Da', 'Dalton'], 1.6605387e-27 * si.kg, register=True,
def_unit(['u', 'Da', 'Dalton'], 1.6605387e-27 * si.kg, namespace=_ns,
doc="Unified atomic mass unit")


##########################################################################
# ENERGY

def_unit(['Ry', 'rydberg'], 13.605692 * si.eV, register=True,
def_unit(['Ry', 'rydberg'], 13.605692 * si.eV, namespace=_ns,
doc="Rydberg: Energy of a photon whose wavenumber is the Rydberg "
"constant",
format={'latex': r'R_{\infty}', 'unicode': 'R∞'})
Expand All @@ -83,25 +83,25 @@
###########################################################################
# ILLUMINATION

def_unit(['solLum', 'L_sun', 'Lsun'], _si.L_sun.value * si.W, register=True,
def_unit(['solLum', 'L_sun', 'Lsun'], _si.L_sun.value * si.W, namespace=_ns,
prefixes=True, doc="Solar luminance",
format={'latex': r'L_{\odot}', 'unicode': 'L⊙'})


###########################################################################
# SPECTRAL DENSITY

def_unit(['ph', 'photon'], register=True)
def_unit(['ph', 'photon'], namespace=_ns)
def_unit(['Jy', 'Jansky', 'jansky'], 1e-26 * si.W / si.m ** 2 / si.Hz,
register=True, prefixes=True,
namespace=_ns, prefixes=True,
doc="Jansky: spectral flux density")
def_unit(['R', 'Rayleigh', 'rayleigh'],
(1e10 / (4 * _numpy.pi)) *
ph * si.m ** -2 * si.s ** -1 * si.sr ** -1,
register=True, prefixes=True,
namespace=_ns, prefixes=True,
doc="Rayleigh: photon flux")

def_unit(['mag'], register=True, prefixes=True,
def_unit(['mag'], namespace=_ns, prefixes=True,
doc="Astronomical magnitude.")


Expand All @@ -112,26 +112,26 @@
# Maybe they should be moved into the FITS format class?
# TODO: This is defined by the FITS standard as "relative to the sun".
# Is that mass, volume, what?
def_unit(['Sun'], register=True)
def_unit(['Sun'], namespace=_ns)


###########################################################################
# EVENTS

def_unit(['ct', 'count'], register=True)
def_unit(['pix', 'pixel'], register=True)
def_unit(['ct', 'count'], namespace=_ns)
def_unit(['pix', 'pixel'], namespace=_ns)


###########################################################################
# MISCELLANEOUS

def_unit(['chan'], register=True)
def_unit(['bin'], register=True)
def_unit(['vox', 'voxel'], register=True)
def_unit(['bit'], register=True, prefixes=True)
def_unit(['byte'], register=True, prefixes=True)
def_unit(['adu'], register=True)
def_unit(['beam'], register=True)
def_unit(['chan'], namespace=_ns)
def_unit(['bin'], namespace=_ns)
def_unit(['vox', 'voxel'], namespace=_ns)
def_unit(['bit'], namespace=_ns, prefixes=True)
def_unit(['byte'], namespace=_ns, prefixes=True)
def_unit(['adu'], namespace=_ns)
def_unit(['beam'], namespace=_ns)


###########################################################################
Expand Down
32 changes: 16 additions & 16 deletions astropy/units/cgs.py
Expand Up @@ -10,12 +10,12 @@
from ..utils.compat.fractions import Fraction

from . import si
from .core import UnitBase, def_unit, _UnitRegistry
from .core import UnitBase, def_unit


_UnitRegistry().namespace = globals()
_ns = globals()

def_unit(['cm', 'centimeter'], si.cm, register=True, prefixes=False)
def_unit(['cm', 'centimeter'], si.cm, namespace=_ns, prefixes=False)
g = si.g
s = si.s
C = si.C
Expand All @@ -29,54 +29,54 @@
##########################################################################
# ACCELERATION

def_unit(['Gal', 'gal'], cm / s ** 2, register=True, prefixes=True,
def_unit(['Gal', 'gal'], cm / s ** 2, namespace=_ns, prefixes=True,
doc="Gal: CGS unit of acceleration")


##########################################################################
# ENERGY

# Use CGS definition of erg
def_unit(['erg'], g * cm ** 2 / s ** 2, register=True, prefixes=True,
def_unit(['erg'], g * cm ** 2 / s ** 2, namespace=_ns, prefixes=True,
doc="erg: CGS unit of energy")


##########################################################################
# FORCE

def_unit(['dyn', 'dyne'], g * cm / s ** 2, register=True,
def_unit(['dyn', 'dyne'], g * cm / s ** 2, namespace=_ns,
prefixes=True,
doc="dyne: CGS unit of force")


##########################################################################
# PRESSURE

def_unit(['Ba', 'Barye', 'barye'], g / (cm * s ** 2), register=True,
def_unit(['Ba', 'Barye', 'barye'], g / (cm * s ** 2), namespace=_ns,
prefixes=True,
doc="Barye: CGS unit of pressure")


##########################################################################
# DYNAMIC VISCOSITY

def_unit(['P', 'poise'], g / (cm * s), register=True,
def_unit(['P', 'poise'], g / (cm * s), namespace=_ns,
prefixes=True,
doc="poise: CGS unit of dynamic viscosity")


##########################################################################
# KINEMATIC VISCOSITY

def_unit(['St', 'stokes'], cm ** 2 / s, register=True,
def_unit(['St', 'stokes'], cm ** 2 / s, namespace=_ns,
prefixes=True,
doc="stokes: CGS unit of kinematic viscosity")


##########################################################################
# WAVENUMBER

def_unit(['k', 'Kayser', 'kayser'], cm ** -1, register=True,
def_unit(['k', 'Kayser', 'kayser'], cm ** -1, namespace=_ns,
prefixes=True,
doc="kayser: CGS unit of wavenumber")

Expand All @@ -85,28 +85,28 @@
# ELECTRICAL

def_unit(['D', 'Debye', 'debye'], Fraction(1, 3) * 1e-29 * C * si.m,
register=True,
namespace=_ns,
doc="Debye: CGS unit of electric dipole moment")

def_unit(['Fr', 'Franklin', 'statcoulomb', 'statC', 'esu'],
g ** Fraction(1, 2) * cm ** Fraction(3, 2) * s ** -1,
register=True,
namespace=_ns,
doc='Franklin: CGS (ESU) unit of charge')

def_unit(['statA', 'statampere'], Fr * s ** -1, register=True,
def_unit(['statA', 'statampere'], Fr * s ** -1, namespace=_ns,
doc='statampere: CGS (ESU) unit of current')

def_unit(['Bi', 'Biot', 'abA', 'abampere', 'emu'],
g ** Fraction(1, 2) * cm ** Fraction(1, 2) * s ** -1, register=True,
g ** Fraction(1, 2) * cm ** Fraction(1, 2) * s ** -1, namespace=_ns,
doc='Biot: CGS (EMU) unit of current')

def_unit(['abC', 'abcoulomb'], Bi * s, register=True,
def_unit(['abC', 'abcoulomb'], Bi * s, namespace=_ns,
doc='abcoulomb: CGS (EMU) of charge')

###########################################################################
# MAGNETIC

def_unit(['G', 'Gauss', 'gauss'], 1e-4 * si.T, register=True, prefixes=True,
def_unit(['G', 'Gauss', 'gauss'], 1e-4 * si.T, namespace=_ns, prefixes=True,
doc="Gauss: CGS unit for magnetic field")


Expand Down

0 comments on commit a40cb7b

Please sign in to comment.