Skip to content

Commit

Permalink
Remove unnecessary (since Pyresample 1.18) mask_all_nan/skipna handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pnuu committed Dec 12, 2023
1 parent 7c54a14 commit a7f93eb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 111 deletions.
47 changes: 4 additions & 43 deletions satpy/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,8 @@

import dask.array as da
import numpy as np
import pyresample
import xarray as xr
import zarr
from packaging import version
from pyresample.ewa import DaskEWAResampler, LegacyDaskEWAResampler
from pyresample.geometry import SwathDefinition
from pyresample.gradient import create_gradient_search_resampler
Expand All @@ -177,8 +175,6 @@

resamplers_cache: "WeakValueDictionary[tuple, object]" = WeakValueDictionary()

PR_USE_SKIPNA = version.parse(pyresample.__version__) > version.parse("1.17.0")


def hash_dict(the_dict, the_hash=None):
"""Calculate a hash for a dictionary."""
Expand Down Expand Up @@ -773,33 +769,6 @@ def _get_replicated_chunk_sizes(d_arr, repeats):
return tuple(repeated_chunks)


def _get_arg_to_pass_for_skipna_handling(**kwargs):
"""Determine if skipna can be passed to the compute functions for the average and sum bucket resampler."""
# FIXME this can be removed once Pyresample 1.18.0 is a Satpy requirement

if PR_USE_SKIPNA:
if "mask_all_nan" in kwargs:
warnings.warn(
"Argument mask_all_nan is deprecated. Please use skipna for missing values handling. "
"Continuing with default skipna=True, if not provided differently.",
DeprecationWarning,
stacklevel=3
)
kwargs.pop("mask_all_nan")
else:
if "mask_all_nan" in kwargs:
warnings.warn(
"Argument mask_all_nan is deprecated."
"Please update Pyresample and use skipna for missing values handling.",
DeprecationWarning,
stacklevel=3
)
kwargs.setdefault("mask_all_nan", False)
kwargs.pop("skipna")

return kwargs


class BucketResamplerBase(PRBaseResampler):
"""Base class for bucket resampling which implements averaging."""

Expand Down Expand Up @@ -832,11 +801,6 @@ def resample(self, data, **kwargs): # noqa: D417
Returns (xarray.DataArray): Data resampled to the target area
"""
if not PR_USE_SKIPNA and "skipna" in kwargs:
raise ValueError("You are trying to set the skipna argument but you are using an old version of"
" Pyresample that does not support it."
"Please update Pyresample to 1.18.0 or higher to be able to use this argument.")

self.precompute(**kwargs)
attrs = data.attrs.copy()
data_arr = data.data
Expand Down Expand Up @@ -910,17 +874,16 @@ def compute(self, data, fill_value=np.nan, skipna=True, **kwargs): # noqa: D417
Returns:
dask.Array
"""
kwargs = _get_arg_to_pass_for_skipna_handling(skipna=skipna, **kwargs)

results = []
if data.ndim == 3:
for i in range(data.shape[0]):
res = self.resampler.get_average(data[i, :, :],
fill_value=fill_value,
skipna=skipna,
**kwargs)
results.append(res)
else:
res = self.resampler.get_average(data, fill_value=fill_value,
res = self.resampler.get_average(data, fill_value=fill_value, skipna=skipna,
**kwargs)
results.append(res)

Expand Down Expand Up @@ -948,16 +911,14 @@ class BucketSum(BucketResamplerBase):

def compute(self, data, skipna=True, **kwargs):
"""Call the resampling."""
kwargs = _get_arg_to_pass_for_skipna_handling(skipna=skipna, **kwargs)

results = []
if data.ndim == 3:
for i in range(data.shape[0]):
res = self.resampler.get_sum(data[i, :, :],
res = self.resampler.get_sum(data[i, :, :], skipna=skipna,
**kwargs)
results.append(res)
else:
res = self.resampler.get_sum(data, **kwargs)
res = self.resampler.get_sum(data, skipna=skipna, **kwargs)
results.append(res)

return da.stack(results)
Expand Down
68 changes: 0 additions & 68 deletions satpy/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,17 +581,10 @@ def test_compute(self):
res = self._compute_mocked_bucket_avg(data, return_data=data[0, :, :], fill_value=2)
assert res.shape == (3, 5, 5)

@mock.patch("satpy.resample.PR_USE_SKIPNA", True)
def test_compute_and_use_skipna_handling(self):
"""Test bucket resampler computation and use skipna handling."""
data = da.ones((5,))

self._compute_mocked_bucket_avg(data, fill_value=2, mask_all_nan=True)
self.bucket.resampler.get_average.assert_called_once_with(
data,
fill_value=2,
skipna=True)

self._compute_mocked_bucket_avg(data, fill_value=2, skipna=False)
self.bucket.resampler.get_average.assert_called_once_with(
data,
Expand All @@ -604,35 +597,6 @@ def test_compute_and_use_skipna_handling(self):
fill_value=2,
skipna=True)

@mock.patch("satpy.resample.PR_USE_SKIPNA", False)
def test_compute_and_not_use_skipna_handling(self):
"""Test bucket resampler computation and not use skipna handling."""
data = da.ones((5,))

self._compute_mocked_bucket_avg(data, fill_value=2, mask_all_nan=True)
self.bucket.resampler.get_average.assert_called_once_with(
data,
fill_value=2,
mask_all_nan=True)

self._compute_mocked_bucket_avg(data, fill_value=2, mask_all_nan=False)
self.bucket.resampler.get_average.assert_called_once_with(
data,
fill_value=2,
mask_all_nan=False)

self._compute_mocked_bucket_avg(data, fill_value=2)
self.bucket.resampler.get_average.assert_called_once_with(
data,
fill_value=2,
mask_all_nan=False)

self._compute_mocked_bucket_avg(data, fill_value=2, skipna=True)
self.bucket.resampler.get_average.assert_called_once_with(
data,
fill_value=2,
mask_all_nan=False)

@mock.patch("pyresample.bucket.BucketResampler")
def test_resample(self, pyresample_bucket):
"""Test bucket resamplers resample method."""
Expand Down Expand Up @@ -712,16 +676,10 @@ def test_compute(self):
res = self._compute_mocked_bucket_sum(data, return_data=data[0, :, :])
assert res.shape == (3, 5, 5)

@mock.patch("satpy.resample.PR_USE_SKIPNA", True)
def test_compute_and_use_skipna_handling(self):
"""Test bucket resampler computation and use skipna handling."""
data = da.ones((5,))

self._compute_mocked_bucket_sum(data, mask_all_nan=True)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
skipna=True)

self._compute_mocked_bucket_sum(data, skipna=False)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
Expand All @@ -732,32 +690,6 @@ def test_compute_and_use_skipna_handling(self):
data,
skipna=True)

@mock.patch("satpy.resample.PR_USE_SKIPNA", False)
def test_compute_and_not_use_skipna_handling(self):
"""Test bucket resampler computation and not use skipna handling."""
data = da.ones((5,))

self._compute_mocked_bucket_sum(data, mask_all_nan=True)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
mask_all_nan=True)

self._compute_mocked_bucket_sum(data, mask_all_nan=False)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
mask_all_nan=False)

self._compute_mocked_bucket_sum(data)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
mask_all_nan=False)

self._compute_mocked_bucket_sum(data, fill_value=2, skipna=True)
self.bucket.resampler.get_sum.assert_called_once_with(
data,
fill_value=2,
mask_all_nan=False)


class TestBucketCount(unittest.TestCase):
"""Test the count bucket resampler."""
Expand Down

0 comments on commit a7f93eb

Please sign in to comment.