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 : |
|
||||||||
---|---|---|---|---|---|---|---|---|---|
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))