yt.units.yt_array.YTQuantity

class yt.units.yt_array.YTQuantity[source]

A scalar associated with a unit.

Parameters:

input_scalar : an integer or floating point scalar

The scalar to attach units to

input_units : String unit specification, unit symbol object, or astropy units

The units of the quantity. Powers must be specified using python syntax (cm**3, not cm^3).

registry : A UnitRegistry object

The registry to create units from. If input_units is already associated with a unit registry and this is specified, this will be used instead of the registry associated with the unit object.

dtype : string or NumPy dtype object

The dtype of the array data.

Examples

>>> from yt import YTQuantity
>>> a = YTQuantity(1, 'cm')
>>> b = YTQuantity(2, 'm')
>>> a + b
201.0 cm
>>> b + a
2.01 m

NumPy ufuncs will pass through units where appropriate.

>>> import numpy as np
>>> a = YTQuantity(12, 'g/cm**3')
>>> np.ones_like(a)
1 g/cm**3

and strip them when it would be annoying to deal with them.

>>> print(np.log10(a))
1.07918124605

YTQuantity is tightly integrated with yt datasets:

>>> import yt
>>> ds = yt.load('IsolatedGalaxy/galaxy0030/galaxy0030')
>>> a = ds.quan(5, 'code_length')
>>> a.in_cgs()
1.543e+25 cm

This is equivalent to:

>>> b = YTQuantity(5, 'code_length', registry=ds.unit_registry)
>>> np.all(a == b)
True

Methods