def test_valid_mask(): xx = np.zeros((4, 8), dtype='float32') mm = valid_mask(xx, 0) assert mm.dtype == 'bool' assert mm.shape == xx.shape assert not mm.all() assert not mm.any() mm = valid_mask(xx, 13) assert mm.dtype == 'bool' assert mm.shape == xx.shape assert mm.all() mm = valid_mask(xx, None) assert mm.dtype == 'bool' assert mm.shape == xx.shape assert mm.all() mm = valid_mask(xx, np.nan) assert mm.dtype == 'bool' assert mm.shape == xx.shape assert mm.all() xx[0, 0] = np.nan mm = valid_mask(xx, np.nan) assert not mm[0, 0] assert mm.sum() == (4*8-1)
def valid_data_mask(data): """ Returns bool arrays where the data is not `nodata` :param Dataset or DataArray data: :return: Dataset or DataArray """ if isinstance(data, Dataset): return data.apply(valid_data_mask) if not isinstance(data, DataArray): raise TypeError('valid_data_mask not supported for type {}'.format(type(data))) nodata = data.attrs.get('nodata', None) return xarray.apply_ufunc(lambda xx: valid_mask(xx, nodata), data, dask='parallelized', output_dtypes=[numpy.bool])