ytree.data_structures.arbor.Arbor.add_derived_field
- Arbor.add_derived_field(name, function, units=None, dtype=None, description=None, vector_components=None, force_add=True)[source]
Add a field that is a function of other fields.
- Parameters:
name (string) – Field name.
function (callable) – The function to be called to generate the field. This function should take two arguments, the arbor and the data structure containing the dependent fields. See below for an example.
units (optional, string) – The units in which the field will be returned.
dtype (optional, type) – The data type of the field array. If none, use the default type set by Arbor._default_dtype.
description (optional, string) – A short description of the field.
vector_components (optional, list of strings) – If given, the field is assumed to be a vector field with the x/y/z component fields provided here. Default: None.
force_add (optional, bool) – If True, add field even if it already exists and warn the user and raise an exception if dependencies do not exist. If False, silently do nothing in both instances. Default: True.
Examples
>>> import ytree >>> a = ytree.load("tree_0_0_0.dat") >>> def _redshift(field, data): ... return 1. / data["scale"] - 1 ... >>> a.add_derived_field("redshift", _redshift) >>> print (a["redshift"])