dask: `Data.clip` by davidhassell · Pull Request #375 · NCAS-CMS/cf-python

Expand Up @@ -8361,24 +8361,25 @@ def binary_mask(self): m.override_units(_units_1, inplace=True) return m
@daskified(_DASKIFIED_VERBOSE) @_deprecated_kwarg_check("i") @_inplace_enabled(default=False) def clip(self, a_min, a_max, units=None, inplace=False, i=False): """Clip (limit) the values in the data array in place.
Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified then values smaller than 0 become 0 and values larger than 1 become 1. Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified then values smaller than 0 become 0 and values larger than 1 become 1.
:Parameters:
a_min: a_min: number Minimum value. If `None`, clipping is not performed on lower interval edge. Not more than one of `a_min` and `a_max` may be `None`.
a_max: a_max: number Maximum value. If `None`, clipping is not performed on upper interval edge. Not more than one of `a_min` and `a_max` may be `None`. Expand All @@ -8398,31 +8399,35 @@ def clip(self, a_min, a_max, units=None, inplace=False, i=False): `None` is returned.

**Examples:** **Examples**
>>> g = f.clip(-90, 90) >>> g = f.clip(-90, 90, 'degrees_north') >>> d = cf.Data(np.arange(12).reshape(3, 4), 'm') >>> print(d.array) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] >>> print(d.clip(2, 10).array) [[ 2 2 2 3] [ 4 5 6 7] [ 8 9 10 10]] >>> print(d.clip(0.003, 0.009, 'km').array) [[3. 3. 3. 3.] [4. 5. 6. 7.] [8. 9. 9. 9.]]
""" d = _inplace_enabled_define_and_cleanup(self)
if units is not None: # Convert the limits to the same units as the data array units = Units(units) self_units = d.Units self_units = self.Units if self_units != units: a_min = Units.conform(a_min, units, self_units) a_max = Units.conform(a_max, units, self_units) # --- End: if
config = d.partition_configuration(readonly=False)
for partition in d.partitions.matrix.flat: partition.open(config) array = partition.array array.clip(a_min, a_max, out=array) partition.close() a_min = Units.conform(np.asanyarray(a_min), units, self_units) a_max = Units.conform(np.asanyarray(a_max), units, self_units)
d = _inplace_enabled_define_and_cleanup(self) dx = self._get_dask() dx = da.clip(dx, a_min, a_max) d._set_dask(dx, reset_mask_hardness=False) return d
@classmethod Expand Down