Physical units and constants module
The astrophysix
package also provides a Unit
helper class to handle physical constants
and units. In addition, a large set of physical quantities and constants are defined in this module.
See also
The
Unit
API reference.
Basic use cases
Unit information
You can have access to unit parameters with the name
, description
,
coeff
, dimensions
,
latex
and physical_type
properties :
>>> from astrophysix import units as U
>>> mass_unit = U.Msun
>>> mass_unit.name
Msun
>>> mass_unit.dimensions
array([1, 0, 0, 0, 0, 0, 0, 0], dtype=int32)
>>> mass_unit.coeff
1.9889e+30
>>> mass_unit.description
'Solar mass'
>>> mass_unit.latex
'\\textrm{M}_{\\odot}'
>>> mass_unit.physical_type
'mass
A summary of any Unit
can be displayed using the
Unit.info
method :
>>> from astrophysix import units as U
>>> U.ly.info()
Unit : ly
---------
Light year
Value
-----
9460730472580800.0 m
Equivalent units
----------------
* m : 1.057e-16 ly
* um : 1.057e-22 ly
* mm : 1.057e-19 ly
* cm : 1.057e-18 ly
* nm : 1.057e-25 ly
* km : 1.057e-13 ly
* Angstrom : 1.057e-26 ly
* au : 1.58125e-05 ly
* pc : 3.26156 ly
* kpc : 3261.56 ly
* Mpc : 3.26156e+06 ly
* Gpc : 3.26156e+09 ly
* Rsun : 7.35153e-08 ly
Unit retrieval
The built-in units and constants defined in the astrophysix
package are directly accessible
as variables of the package :
>>> from astrophysix import units as U
>>> U.Msun.description
'Solar mass'
>>> U.kHz.description
'kilo-Hertz : frequency unit'
For custom units retrieval, see Custom Units
Unit operations
You can create composite units by multiplying or dividing Unit
objects by float
values or other Unit
objects. You can also raise Unit
objects to a given (integer) power :
>>> from astrophysix import units as U
>>> u = U.km/U.s
>>> print(u)
(1000 m.s^-1)
>>> joule = kg*(m/s)**2
>>> joule == U.J
True
>>> my_p = 250.0 * J * m**-3
>>> my_p.physical_type
'pressure'
Custom Units
If you want to create your own units and use them elsewhere in your code, you can create a Unit
instance that will be included in the astrophysix
unit registry, use Unit.create_unit
method to add a new unit, and Unit.from_name
to retrieve it :
>>> from astrophysix import units as U
>>> U.km_s == U.km/U.s # Soft equality => same coefficient and same dimensions
True
>>> U.km_s.identical(U.km/U.s) # Strict equality => they do not share the same names/LaTEX formulae, descriptions
False
# Create a custom Solar mass per square kiloparsec surface density unit
>>> u = U.Unit.create_unit(name="Msun_pc2", base_unit=U.Msun/U.pc**2, descr="Solar mass per square parsec",
latex="\\textrm{M}_{\\odot}\\cdot\\textrm{pc}^{-2}")
>>> u.identical(U.Msun/U.pc**2)
False
>>> # Later in your Python script ...
>>> surf_dens_unit = U.Unit.from_name("Msun_pc2")
surf_dens_unit == U.Msun/U.pc**2
True
>>>surf_dens_unit.description
"Solar mass per square parsec"
Unit search
You can browse the units available in the astrophysix
unit registry using the
Unit.equivalent_unit_list
method, the
Unit.appropriate_unit
method or the
Unit.iterate_units
iterator :
>>> # Equivalent units
>>> U.km.equivalent_unit_list()
[m : (1 m),
um : (1e-06 m),
mm : (0.001 m),
cm : (0.01 m),
nm : (1e-09 m),
Angstrom : (1e-10 m),
au : (1.49598e+11 m),
pc : (3.08568e+16 m),
kpc : (3.08568e+19 m),
Mpc : (3.08568e+22 m),
Gpc : (3.08568e+25 m),
Rsun : (6.95508e+08 m),
ly : (9.46073e+15 m)]
>>> # Most appropriate unit
>>> u = 0.3 * U.pc
>>> f, best_unit = u.appropriate_unit()
>>> print("{f:g} {bu:s}".format(f=f, bu=best_unit.name))
0.978469 ly
>>> # Unit iterator
>>> for u in Unit.iterate_units(phys_type="time"):
print(u.name)
s
min
hour
day
sid_day
year
kyr
Myr
Gyr
Unit conversion
Unit conversion can be done with the Unit.express
method :
>>> from astrophysix import units as U
>>> # Basic unit conversion
>>> l = 100.0 * U.kpc
>>> t = 320.0 U. Myr
>>> v = l/t
>>> print("v = {c:g} km/s".format(c=v.express(U.km_s)))
v = 305.56 km/s
Built-in quantities and constants
Base units
Name
Description
A
Ampere : electric intensity base unit
K
Kelvin : base temperature unit
cd
Candela: base luminous intensity unit
kg
Kilogram : base mass unit
m
Meter : base length unit
mol
mole: amount of a chemical substance base unit
none
Unscaled dimensionless unit
rad
radian: angular measurement (ratio lengh / radius of an arc)
s
Second : base time unit
See also
Constants and common units
Name
Value
Decomposition in base units
Description
Angstrom
1e-10
m
Angstrom: 10**-10 m
C
1
s.A
Coulomb
F
1
kg^-1.m^-2.s^4.A^2
Farad
G
6.67428e-11
kg^-1.m^3.s^-2
Graviational constant
GHz
1e+09
s^-1
giga-Hertz : frequency unit
Gauss
0.0001
kg.s^-2.A^-1
Gauss
Gpc
3.08568e+25
m
Gigaparsec
Gyr
3.15576e+16
s
Gigayear : trillion years
H
2.26855e-18
s^-1
Hubble’s constant
H_cc
2.18421e-21
kg.m^-3
Atoms per cubic centimeter
Henry
1
kg.m^2.s^-2.A^-2
Henry
Hz
1
s^-1
Hertz : frequency unit
J
1
kg.m^2.s^-2
Joule : (SI) energy unit
Jy
1e-26
kg.s^-2
Jansky
Lsun
3.846e+26
kg.m^2.s^-3
Solar luminosity
MHz
1e+06
s^-1
mega-Hertz : frequency unit
Mearth
5.9722e+24
kg
Earth mass
Mpc
3.08568e+22
m
Megaparsec
Msun
1.9889e+30
kg
Solar mass
Myr
3.15576e+13
s
Megayear : million years
N
1
kg.m.s^-2
Newton : (SI) force unit
Ohm
1
kg.m^2.s^-3.A^-2
Ohm
Pa
1
kg.m^-1.s^-2
Pascal: (SI) pressure unit
Rsun
6.95508e+08
m
Solar radius
S
1
kg^-1.m^-2.s^3.A^2
Siemens
T
1
kg.s^-2.A^-1
Tesla
V
1
kg.m^2.s^-3.A^-1
Volt
W
1
kg.m^2.s^-3
Watt
a_r
7.56577e-16
kg.m^-1.s^-2.K^-4
Radiation constant
arcmin
0.000290888
rad
arc minute: 1/60 of a hour angle
arcsec
4.84814e-06
rad
arc second: 1/60 of an arcminute
atm
101325
kg.m^-1.s^-2
atm: atmospheric pressure (101 3525 Pa)
au
1.49598e+11
m
Astronomical unit
bar
100000
kg.m^-1.s^-2
Bar
barn
1e-28
m^2
barn: surface unit used in HEP
barye
0.1
kg.m^-1.s^-2
Barye: (CGS) pressure unit
c
2.99792e+08
m.s^-1
Speed of light in vacuum
cm
0.01
m
Centimeter
cm3
1e-06
m^3
Cubic centimeter
day
86400
s
Day
deg
0.0174533
rad
degree: angular measurement corresponding to 1/360 of a full rotation
dyne
1e-05
kg.m.s^-2
dyne : (CGS) force unit
e
1.60218e-19
s.A
e : elementary electric charge carried by a proton
eV
1.60218e-19
kg.m^2.s^-2
electron-Volt
erg
1e-07
kg.m^2.s^-2
erg : (CGS) energy unit
g
0.001
kg
Gram
g_cc
1000
kg.m^-3
Gram per cubic centimeter
h
6.62607e-34
kg.m^2.s^-1
Planck Constant
hPa
100
kg.m^-1.s^-2
Hectopascal
hbar
1.05457e-34
kg.m^2.s^-1
Reduced Planck constant
hour
3600
s
Hour
hourangle
0.261799
rad
hour angle: angular measurement with 24 in a full circle
kB
1.38065e-23
kg.m^2.s^-2.K^-1
Boltzmann constant
kHz
1000
s^-1
kilo-Hertz : frequency unit
kPa
1000
kg.m^-1.s^-2
Kilopascal
km
1000
m
Kilometer
km_s
1000
m.s^-1
kilometers per second
kpc
3.08568e+19
m
Kiloparsec
kpc3
2.938e+49
m^3
Cubic kiloparsec
kyr
3.15576e+10
s
kyr : millenium
lm
1
rad^2.cd
Lumen
lx
1
m^-2.rad^2.cd
Lux
ly
9.46073e+15
m
Light year
m3
1
m^3
Cubic meter
mGauss
1e-07
kg.s^-2.A^-1
Milligauss
mH
1.66e-27
kg
Hydrogen atomic mass
m_s
1
m.s^-1
Meters per second
min
60
s
Minute
mm
0.001
m
Millimeter
nm
1e-09
m
Nanometer
pc
3.08568e+16
m
Parsec
pc3
2.938e+49
m^3
Cubic parsec
percent
0.01
One hundredth of unity
rhoc
9.2039e-27
kg.m^-3
Friedmann’s universe critical density
sid_day
86164.1
s
Sidereal day : Earth full rotation time
sigmaSB
5.6704e-08
kg.s^-3.K^-4
Stefan-Boltzmann constant
sr
1
rad^2
Steradian: solid angle (SI) unit
t
1000
kg
Metric ton
uGauss
1e-10
kg.s^-2.A^-1
Microgauss
um
1e-06
m
Micron
year
3.15576e+07
s
Year
Physical quantities
Quantity
Decomposition in base units
acceleration
m.s^-2
amount of substance
mol
angle
rad
angular acceleration
s^-2.rad
angular momentum
kg.m^2.s^-1
angular velocity
s^-1.rad
area
m^2
dimensionless
dynamic viscosity
kg.m^-1.s^-1
electric capacitance
kg^-1.m^-2.s^4.A^2
electric charge
s.A
electric charge density
m^-3.s.A
electric conductance
kg^-1.m^-2.s^3.A^2
electric conductivity
kg^-1.m^-3.s^3.A^2
electric current
A
electric current density
m^-2.A
electric dipole moment
m.s.A
electric field strength
kg.m.s^-3.A^-1
electric flux density
m^-2.s.A
electric potential
kg.m^2.s^-3.A^-1
electric resistance
kg.m^2.s^-3.A^-2
electric resistivity
kg.m^3.s^-3.A^-2
energy
kg.m^2.s^-2
energy flux density
kg.s^-3
entropy
kg.m^2.s^-2.K^-1
force
kg.m.s^-2
frequency
s^-1
inductance
kg.m^2.s^-2.A^-2
kinematic viscosity
m^2.s^-1
length
m
linear density
kg.m^-1
luminance
m^-2.cd
luminous emittence
m^-2.rad^2.cd
luminous flux
rad^2.cd
luminous intensity
cd
magnetic field strength
m^-1.A
magnetic flux
kg.m^2.s^-2.A^-1
magnetic flux density
kg.s^-2.A^-1
magnetic permeability
kg.m.s^-2.A^-2
mass
kg
molar volume
m^-3.mol
moment of inertia
kg.m^2
momentum/impulse
kg.m.s^-1
permittivity
kg^-1.m^-3.s^4.A^2
power
kg.m^2.s^-3
pressure
kg.m^-1.s^-2
radiant intensity
kg.m^2.s^-3.rad^-2
solid angle
rad^2
specific energy
m^2.s^-2
specific volume
kg^-1.m^3
spectral flux density
kg.s^-2
surface density
kg.m^-2
temperature
K
thermal conductivity
kg.m.s^-3.K^-1
time
s
velocity
m.s^-1
volume
m^3
volume density
kg.m^-3
wavenumber
m^-1