Skip to content

Commit

Permalink
Fix resampling to same area not working
Browse files Browse the repository at this point in the history
  • Loading branch information
mraspaud committed Mar 22, 2023
1 parent 0340c4f commit 9750fcb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pyresample/gradient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ def __init__(self, source_geo_def, target_geo_def):

def precompute(self, **kwargs):
"""Precompute resampling parameters."""
if self.source_geo_def == self.target_geo_def:
return
if self.indices_xy is None:
self.indices_xy = resample_blocks(gradient_resampler_indices_block,
self.source_geo_def, [], self.target_geo_def,
Expand All @@ -509,6 +511,8 @@ def precompute(self, **kwargs):
@ensure_data_array
def compute(self, data, method="bilinear", cache_id=None, **kwargs):
"""Perform the resampling."""
if self.source_geo_def == self.target_geo_def:
return data
if method == "bilinear":
fun = block_bilinear_interpolator
elif method in ["nearest_neighbour", "nn"]:
Expand Down
19 changes: 17 additions & 2 deletions pyresample/test/test_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

from pyresample.area_config import create_area_def
from pyresample.geometry import AreaDefinition, SwathDefinition
from pyresample.gradient import ResampleBlocksGradientSearchResampler


class TestOGradientResampler(unittest.TestCase):
Expand Down Expand Up @@ -266,9 +267,8 @@ def test_resample_swath_to_area_3d(self):
class TestRBGradientSearchResamplerArea2Area:
"""Test RBGradientSearchResampler for the Area to Area case."""

def setup(self):
def setup_method(self):
"""Set up the test case."""
from pyresample.gradient import ResampleBlocksGradientSearchResampler
self.src_area = AreaDefinition('src', 'src area', None,
{'ellps': 'WGS84', 'h': '35785831', 'proj': 'geos'},
100, 100,
Expand All @@ -288,6 +288,12 @@ def test_precompute_generates_indices(self):
self.resampler.precompute()
assert self.resampler.indices_xy.shape == (2, ) + self.dst_area.shape

def test_precompute_does_nothing_when_src_equals_dst(self):
"""Test that precomputing does nothing when src and dst areas are equal."""
resampler = ResampleBlocksGradientSearchResampler(self.src_area, self.src_area)
resampler.precompute()
assert self.resampler.indices_xy is None

def test_resampler_accepts_only_dataarrays_if_not_2d(self):
data = da.ones(self.src_area.shape + (1,), dtype=np.float64, chunks=40)
self.resampler.precompute()
Expand Down Expand Up @@ -485,6 +491,15 @@ def test_resample_area_to_area_nn(self):
np.testing.assert_allclose(res, expected_resampled_data)
assert res.shape == dst_area.shape

def test_resample_does_nothing_when_src_equals_dst(self):
"""Test that resampling does nothing when src and dst areas are equal."""
data = xr.DataArray(da.arange(np.prod(self.src_area.shape), dtype=np.float64).reshape(self.src_area.shape),
dims=['y', 'x'])

resampler = ResampleBlocksGradientSearchResampler(self.src_area, self.src_area)
result = resampler.resample(data)
assert result.data is data.data


class TestRBGradientSearchResamplerArea2Swath:
"""Test RBGradientSearchResampler for the Swath to Area case."""
Expand Down

0 comments on commit 9750fcb

Please sign in to comment.