diff --git a/pyresample/bucket/__init__.py b/pyresample/bucket/__init__.py index 317c2b64..cb99d94c 100644 --- a/pyresample/bucket/__init__.py +++ b/pyresample/bucket/__init__.py @@ -202,8 +202,7 @@ def _get_indices(self): target_shape = self.target_area.shape self.idxs = self.y_idxs * target_shape[1] + self.x_idxs - def get_sum(self, data, fill_value=np.nan, skipna=True, - set_empty_bucket_to=0): + def get_sum(self, data, fill_value=np.nan, skipna=True, empty_bucket_value=0): """Calculate sums for each bin with drop-in-a-bucket resampling. Parameters @@ -215,12 +214,12 @@ def get_sum(self, data, fill_value=np.nan, skipna=True, Default: np.nan skipna : boolean (optional) If True, skips missing values (as marked by NaN or `fill_value`) for the sum calculation - (similarly to Numpy's `nansum`). Buckets containing only missing values are set to `set_empty_bucket_to`. + (similarly to Numpy's `nansum`). Buckets containing only missing values are set to `empty_bucket_value`. If False, sets the bucket to fill_value if one or more missing values are present in the bucket (similarly to Numpy's `sum`). - In both cases, empty buckets are set to `set_empty_bucket_to`. + In both cases, empty buckets are set to `empty_bucket_value`. Default: True - set_empty_bucket_to : float + empty_bucket_value : float Set empty buckets to the given value. Empty buckets are considered as the buckets with value 0. Note that a bucket could become 0 as the result of a sum of positive and negative values. If the user needs to identify these zero-buckets reliably, @@ -255,7 +254,9 @@ def get_sum(self, data, fill_value=np.nan, skipna=True, # TODO remove following line in favour of weights = data when dask histogram bug (issue #6935) is fixed sums = self._mask_bins_with_nan_if_not_skipna(skipna, data, out_size, sums, fill_value) - sums = da.where(sums == 0, set_empty_bucket_to, sums) + + if empty_bucket_value != 0: + sums = da.where(sums == 0, empty_bucket_value, sums) return sums.reshape(self.target_area.shape) diff --git a/pyresample/test/test_bucket.py b/pyresample/test/test_bucket.py index d7797a19..a36c5bd9 100644 --- a/pyresample/test/test_bucket.py +++ b/pyresample/test/test_bucket.py @@ -205,24 +205,24 @@ def test_get_sum_non_default_fill_value_skipna_true(self): self.assertEqual(np.count_nonzero(result == 255), 0) self.assertEqual(np.nanmin(result), 0) - def test_nonzero_set_empty_bucket_to_number(self): - """Test drop-in-a-bucket sum for non-zero set_empty_bucket_to np.nan.""" + def test_nonzero_empty_bucket_value_number(self): + """Test drop-in-a-bucket sum for non-zero empty_bucket_value set as number.""" data = da.from_array(np.array([[2., np.nan], [5., np.nan]]), chunks=self.chunks) - result = self._get_sum_result(data, skipna=True, set_empty_bucket_to=4095) + result = self._get_sum_result(data, skipna=True, empty_bucket_value=4095) # 5 is untouched in a single bin self.assertEqual(np.count_nonzero(result == 5.), 1) # all-nan and rest is 4095 self.assertEqual(np.count_nonzero(result == 4095), 2048 * 2560 - 2) self.assertEqual(np.nanmin(result), 2) - def test_nonzero_set_empty_bucket_to_npnan(self): - """Test drop-in-a-bucket sum for non-zero set_empty_bucket_to np.nan.""" + def test_nonzero_empty_bucket_valueo_npnan(self): + """Test drop-in-a-bucket sum for non-zero empty_bucket_value set as np.nan.""" data = da.from_array(np.array([[2., np.nan], [5., np.nan]]), chunks=self.chunks) - result = self._get_sum_result(data, skipna=True, set_empty_bucket_to=np.nan) + result = self._get_sum_result(data, skipna=True, empty_bucket_value=np.nan) # 5 is untouched in a single bin self.assertEqual(np.count_nonzero(result == 5.), 1) # all-nan and rest is np.nan