如何向yt数组添加标量值?

时间:2014-09-26 17:22:47

标签: python data-analysis yt-project

我正在尝试在yt中的一个派生字段中添加数字楼层。但是,当我尝试:     new_field = 1.0e-10 + data ['metal_density']

我收到以下错误:

In [1]: ad = ds.all_data()

In [2]: floored_density = 1.0e-10 + ad['density'] yt : [INFO     ] 2014-09-26 10:15:21,901 Gathering a field list (this may take a moment.) yt : [INFO     ] 2014-09-26 10:15:22,275 Loading field plugins. yt : [INFO     ] 2014-09-26 10:15:22,275 Loaded angular_momentum (8 new fields) yt : [INFO     ] 2014-09-26 10:15:22,276 Loaded astro (14 new fields) yt : [INFO     ] 2014-09-26 10:15:22,276 Loaded cosmology (20 new fields) yt : [INFO     ] 2014-09-26 10:15:22,276 Loaded fluid (56 new fields) yt : [INFO     ] 2014-09-26 10:15:22,277 Loaded fluid_vector (88 new fields) yt : [INFO ] 2014-09-26 10:15:22,278 Loaded geometric (102 new fields) yt : [INFO ] 2014-09-26 10:15:22,278 Loaded local (102 new fields) yt : [INFO     ] 2014-09-26 10:15:22,278 Loaded magnetic_field (108 new fields) yt : [INFO     ] 2014-09-26 10:15:22,278 Loaded species (108 new fields)
--------------------------------------------------------------------------- YTUnitOperationError                      Traceback (most recent call last) /home/skillman/yt-x86_64/src/yt-3.0/yt/mods.pyc in <module>()
----> 1 floored_density = 1.0e-10 + ad['density']

/home/skillman/yt-x86_64/src/yt-3.0/yt/units/yt_array.pyc in
__radd__(self, left_object)
    674     def __radd__(self, left_object):
    675         """ See __add__. """
--> 676         lo = sanitize_units_add(self, left_object, "addition")
    677         return YTArray(super(YTArray, self).__radd__(lo))
    678 

/home/skillman/yt-x86_64/src/yt-3.0/yt/units/yt_array.pyc in sanitize_units_add(this_object, other_object, op_string)
    159     else:
    160         if not inp.units.is_dimensionless:
--> 161             raise YTUnitOperationError(op_string, inp.units, dimensionless)
    162     return ret
    163 

YTUnitOperationError: The addition operator for YTArrays with units (g/cm**3) and (1) is not well defined.

1 个答案:

答案 0 :(得分:2)

只要您注意要添加的标量与yt数组的单位相同,您就可以执行以下操作:

ad = ds.all_data()

floored_density = 1.0e-10 * ad['density'].uq + ad['density']

.uq代表“单位数量”,以相关字段为单位等于1.0。在这种情况下,ad ['density']。uq等于1.0 g / cm ** 3。

此处描述了挂起yt数组的其他有用属性:http://yt-project.org/doc/analyzing/units/symbolic_units.html?highlight=unit_quantity#Creating-arrays-and-quantities