cf.Field.setdata

Field.setdata(x, y, *arg, **kwargs)[source]

Set data array elements depending on a condition.

Elements are set differently depending on where the condition is True or False. Two assignment values are given. From one of them, the field’s data array is set where the condition is True and where the condition is False, the data array is set from the other.

Each assignment value may either contain a single datum, or is an array-like object which is broadcastable shape of the field’s data array.

Missing data

The treatment of missing data elements depends on the value of field’s hardmask attribute. If it is True then masked elements will not unmasked, otherwise masked elements may be set to any value.

In either case, unmasked elements may be set to any value (including missing data).

Unmasked elements may be set to missing data by assignment to the cf.masked constant or by assignment to a value which contains masked elements.

Parameters :
x, y :

Specify the assignment value. Where the condition defined by the arg and kwargs arguments is True, set the field’s data array from x and where the condition is False, set the field’s data array from y. Arguments x and y are each one of:

  • None. The appropriate elements of the field’s data array are unchanged.
  • Any object which is broadcastable to the field’s shape using the metadata-aware cf broadcasting rules (i.e. a cf.Field object or any object, a, for which numpy.size(a) == 1 is True). The appropriate elements of the field’s data array are set to the corresponding values from the object broadcast to the field’s data array shape.
arg : optional

Define the condition which determines how to set the field’s data array. Depending on the presence of any optional keyword arguments, arg is interpreted in one of two ways:

Keyword

arg

arguments?

 

No

Any object which is broadcastable to the field’s shape using the metadata-aware cf broadcasting rules (i.e. a cf.Field object or any object, a, for which numpy.size(a) == 1 is True). The condition is True where the object broadcast to the field’s data array shape is True.

Yes

A modifier for the interpretation of keyword arguments. See kwargs for details.

In the first case, the array-like object explicitly describes where the condition is True or False (if arg is unset then it defaults to a condition of True everywhere); whilst in the second case, the condition is defined by the keyword arguments.

kwargs : optional

Define the condition which determines how to set the field’s data array by tests on the field’s one dimensional coordinate values. Where the tests are passed, the condition is True elsewhere the condition is False.

The condition is True at the data array indices that would be returned by this call of the field’s indices method: f.indices(*arg, **kwargs). See cf.Field.indices for details.

If there are no optional keyword arguments then see the arg argument.

Returns :

None

Examples

Set data array values to 15 everywhere:

>>> f.setdata(15, None)

Set all negative data array values to zero and leave all other elements unchanged:

>>> f.setdata(0, None, f<0)

Multiply all positive data array elements by -1 and set other data array elements to 3.14:

>>> f.setdata(-f, 3.14, f>0)

Set all values less than 280 and greater than 290 to missing data:

>>> f.setdata(cf.masked, None, (f < 280) | (f > 290)

Set data array elements in the northern hemisphere to missing data:

>>> f.setdata(cf.masked, None, latitude=cf.ge(0)

Set a field’s polar rows to their average values and leave it unchanged elsewhere:

>>> a = cf.collapse(f, 'longitude: mean')
>>> f.setdata(a, None, latitude=cf.set([-90, 90])

Set a field to values from another field within the tropics, and to yet another field elsewhere:

>>> f
<CF Field: air_temperature(height(19), time(12), latitude(73), longitude(96)) K>
>>> g
<CF Field: air_temperature(longitude(1), latitude(73)) K>
>>> h
<CF Field: air_temperature(latitude(1), time(1), longitude(96)) K>
>>> f.setdata(g, h, latitude=cf.wi(-30, 30))

Identify a one dimensionsal coordinate object by its abbreviated identity:

>>> f.setdata(cf.Data([23]), numpy.array(54), lat=0)

Identify a one dimensionsal coordinate object by its exact identity:

>>> f.setdata([[15]], f.subspace(height=10), cf.masked, 'exact', latitude=0)

Identify one dimensionsal coordinates by regular expressions (in this case, for example, a one dimensionsional object of “grid_latitude” or “latitude” would be selected):

>>> f.setdata(g, h, 'regex', lat=cf.wi(-30, 30)), long=cf.wi(-30, 30))

Previous topic

cf.Field.setattr

Next topic

cf.Field.setprop

This Page