yt.units.yt_array.YTArray

class yt.units.yt_array.YTArray[source]

An ndarray subclass that attaches a symbolic unit object to the array data.

Parameters:

input_array : iterable

A tuple, list, or array to attach units to

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

The units of the array. 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 YTArray
>>> a = YTArray([1, 2, 3], 'cm')
>>> b = YTArray([4, 5, 6], 'm')
>>> a + b
YTArray([ 401.,  502.,  603.]) cm
>>> b + a
YTArray([ 4.01,  5.02,  6.03]) m

NumPy ufuncs will pass through units where appropriate.

>>> import numpy as np
>>> a = YTArray(np.arange(8), 'g/cm**3')
>>> np.ones_like(a)
YTArray([1, 1, 1, 1, 1, 1, 1, 1]) g/cm**3

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

>>> np.log10(a)
array([       -inf,  0.        ,  0.30103   ,  0.47712125,  0.60205999,
        0.69897   ,  0.77815125,  0.84509804])

YTArray is tightly integrated with yt datasets:

>>> import yt
>>> ds = yt.load('IsolatedGalaxy/galaxy0030/galaxy0030')
>>> a = ds.arr(np.ones(5), 'code_length')
>>> a.in_cgs()
YTArray([  3.08600000e+24,   3.08600000e+24,   3.08600000e+24,
         3.08600000e+24,   3.08600000e+24]) cm

This is equivalent to:

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

Methods