Unit Support¶
To ensure correct calculations, MetPy relies upon the pint
library to enforce
unit-correctness. This simplifies the MetPy API by eliminating the need to specify units
various functions. Instead, only the final results need to be converted to desired units. For
more information on unit support, see the documentation for
Pint. Particular attention should be paid to the support
for temperature units.
Construction¶
To use units, the first step is to import the default MetPy units registry from the
units
module:
import numpy as np
from metpy.units import units
The unit registry encapsulates all of the available units, as well as any pertinent settings.
The registry also understands unit prefixes and suffixes; this allows the registry to
understand 'kilometer'
and 'meters'
in addition to the base 'meter'
unit.
In general, using units is only a small step on top of using the numpy.ndarray
object.
The easiest way to attach units to an array is to multiply by the units:
distance = np.arange(1, 5) * units.meters
It is also possible to directly construct a pint.Quantity
, with a full units string:
time = units.Quantity(np.arange(2, 10, 2), 'sec')
Compound units can be constructed by the direct mathematical operations necessary:
g = 9.81 * units.meter / (units.second * units.second)
This verbose syntax can be reduced by using the unit registry’s support for parsing units:
g = 9.81 * units('m/s^2')
Operations¶
With units attached, it is possible to perform mathematical operations, resulting in the proper units:
print(distance / time)
[ 0.5 0.5 0.5 0.5] meter / second
For multiplication and division, units can combine and cancel. For addition and subtraction, instead the operands must have compatible units. For instance, this works:
print(distance + distance)
[0 2 4 6 8] meter
But this does not:
print(distance + time)
DimensionalityError: Cannot convert from 'meter' ([length]) to 'second' ([time])
Even if the units are not identical, as long as they are dimensionally equivalent, the operation can be performed:
print(3 * units.inch + 5 * units.cm)
4.968503937007874 inch
Conversion¶
Converting a Quantity
between units can be accomplished by using the
to()
method call, which constructs a new Quantity
in the
desired units:
print((1 * units.inch).to(units.mm))
25.400000000000002 millimeter
There is also the ito()
method which performs the same operation in place.
To simplify units, there is also the to_base_units()
method, which
converts a quantity to SI units, performing any needed cancellation:
Lf = 3.34e6 * units('J/kg')
print(Lf, Lf.to_base_units(), sep='\n')
3340000.0 joule / kilogram
3340000.0 meter ** 2 / second ** 2
to_base_units()
can also be done in place via the
ito_base_units()
method.