Note
Click here to download the full example code or to run this example in your browser via Binder
Quantities, Units, and Constants¶
The purpose of this demo is to demonstrate the capabilities of astropy
Unit
, Quantity
, and Constant
.
The astropy Quantity
object
handles defining, converting between, and performing arithmetic
with physical quantities, such as meters, seconds, Hz, etc.
from astropy import units as u
import numpy as np
You can define a Quantity
(a number with a unit) in a number of different ways.
Out:
<Quantity [0., 1., 2., 3., 4., 5., 6., 7., 8., 9.] Hz>
These objects work as you would expect with most Python operators or numpy functions
Out:
<Quantity 4. s2>
If needed you can get the value as well as the unit
Out:
The value is 42.0 and the unit is m
Using the to
function we can easily converted to
another unit.
print(q.to('parsec'))
Out:
1.3611273015666334e-15 pc
and imperial units as also supported
from astropy.units import imperial
print(q.to(imperial.mile))
Out:
0.026097590073968023 mi
Units that “cancel out” become a special unit called the “dimensionless unit”:
Out:
Unit(dimensionless)
More complex conversions are also supported using
equivalencies
.
For example, we can convert the GOES wavelength range to Hz or keV easily using
the spectral
.
print(([0.5, 4.0] * u.angstrom).to('Hz', u.spectral()))
print(([0.5, 4.0] * u.angstrom).to('keV', u.spectral()))
Out:
[5.99584916e+18 7.49481145e+17] Hz
[24.79683969 3.09960496] keV
Astropy provides a number of reference constants
from astropy import constants as astropy_const
SunPy also provides a number of relevant solar reference constants.
from sunpy.sun import constants as sunpy_const
Constant
are simply quantities but they also provide an uncertainty
and a reference
M_earth = astropy_const.M_earth
print("The mass of the Earth is {0} +/- {1} {2} [ref {3}].".format(M_earth.value, M_earth.uncertainty, M_earth.unit, M_earth.reference))
Out:
The mass of the Earth is 5.972167867791379e+24 +/- 1.3422009501651213e+20 kg [ref IAU 2015 Resolution B 3 + CODATA 2018].
The light travel time in minutes from the Sun to the Earth can be calculated
print((sunpy_const.au / astropy_const.c).to('min'))
Out:
8.316746397269274 min
Let’s define a function to calculate the plasma beta, with quantities we don’t have to worry about much beyond getting the equation correct
def plasma_beta(n, T, B):
return (2 * n * astropy_const.k_B * T) / (B ** 2 / (2 * astropy_const.mu0))
The plasma beta for the solar corona using appropriate parameters is given by the following. The decompose function works to simplify the units.
Out:
0.2081969643814698
If the input is given in the wrong units then an error may occur but a better way is to inforce the units on input. Let’s consider a simpler example here to calculate velocity. We use a function annotation to specify the units (this is a Python 3.5+ feature, see the quantity_input documentation for more details and Python 2 instructions):
Total running time of the script: ( 0 minutes 0.010 seconds)