From 9e36229fc2f4459ecc68e2bfc281c23c5bf61f82 Mon Sep 17 00:00:00 2001 From: David Hoese Date: Wed, 26 Jun 2019 13:26:53 -0500 Subject: [PATCH 1/2] Fix EWA resampling tests not properly testing caching --- satpy/resample.py | 13 ++++++------- satpy/tests/test_resample.py | 26 ++++++++++++++++++++------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/satpy/resample.py b/satpy/resample.py index 6f5fb2b9f2..c3c306006d 100644 --- a/satpy/resample.py +++ b/satpy/resample.py @@ -621,6 +621,11 @@ def _call_ll2cr(self, lons, lats, target_geo_def, swath_usage=0): def precompute(self, cache_dir=None, swath_usage=0, **kwargs): """Generate row and column arrays and store it for later use.""" + if self.cache: + # this resampler should be used for one SwathDefinition + # no need to recompute ll2cr output again + return None + if kwargs.get('mask') is not None: LOG.warning("'mask' parameter has no affect during EWA " "resampling") @@ -950,7 +955,7 @@ def prepare_resampler(source_area, destination_area, resampler=None, **resample_ key = (resampler_class, source_area, destination_area, - hash_dict(resample_kwargs)) + hash_dict(resample_kwargs).hexdigest()) try: resampler_instance = resamplers_cache[key] except KeyError: @@ -962,12 +967,6 @@ def prepare_resampler(source_area, destination_area, resampler=None, **resample_ def resample(source_area, data, destination_area, resampler=None, **kwargs): """Do the resampling.""" - if 'resampler_class' in kwargs: - import warnings - warnings.warn("'resampler_class' is deprecated, use 'resampler'", - DeprecationWarning) - resampler = kwargs.pop('resampler_class') - if not isinstance(resampler, BaseResampler): # we don't use the first argument (cache key) _, resampler_instance = prepare_resampler(source_area, diff --git a/satpy/tests/test_resample.py b/satpy/tests/test_resample.py index 2a35eafc89..753917b586 100644 --- a/satpy/tests/test_resample.py +++ b/satpy/tests/test_resample.py @@ -212,7 +212,8 @@ class TestEWAResampler(unittest.TestCase): @mock.patch('satpy.resample.fornav') @mock.patch('satpy.resample.ll2cr') - def test_2d_ewa(self, ll2cr, fornav): + @mock.patch('satpy.resample.SwathDefinition.get_lonlats') + def test_2d_ewa(self, get_lonlats, ll2cr, fornav): """Test EWA with a 2D dataset.""" import numpy as np import xarray as xr @@ -223,7 +224,9 @@ def test_2d_ewa(self, ll2cr, fornav): fornav.return_value = (100 * 200, np.zeros((200, 100), dtype=np.float32)) _, _, swath_data, source_swath, target_area = get_test_data() + get_lonlats.return_value = (source_swath.lons, source_swath.lats) swath_data.data = swath_data.data.astype(np.float32) + num_chunks = len(source_swath.lons.chunks[0]) * len(source_swath.lons.chunks[1]) new_data = resample_dataset(swath_data, target_area, resampler='ewa') self.assertTupleEqual(new_data.shape, (200, 100)) @@ -232,7 +235,8 @@ def test_2d_ewa(self, ll2cr, fornav): self.assertIs(new_data.attrs['area'], target_area) # make sure we can actually compute everything new_data.compute() - previous_calls = ll2cr.call_count + lonlat_calls = get_lonlats.call_count + ll2cr_calls = ll2cr.call_count # resample a different dataset and make sure cache is used data = xr.DataArray( @@ -240,8 +244,11 @@ def test_2d_ewa(self, ll2cr, fornav): dims=('y', 'x'), attrs={'area': source_swath, 'test': 'test2', 'name': 'test2'}) new_data = resample_dataset(data, target_area, resampler='ewa') - self.assertEqual(ll2cr.call_count, previous_calls) new_data.compute() + # ll2cr will be called once more because of the computation + self.assertEqual(ll2cr.call_count, ll2cr_calls + num_chunks) + # but we should already have taken the lonlats from the SwathDefinition + self.assertEqual(get_lonlats.call_count, lonlat_calls) self.assertIn('y', new_data.coords) self.assertIn('x', new_data.coords) if CRS is not None: @@ -253,7 +260,8 @@ def test_2d_ewa(self, ll2cr, fornav): @mock.patch('satpy.resample.fornav') @mock.patch('satpy.resample.ll2cr') - def test_3d_ewa(self, ll2cr, fornav): + @mock.patch('satpy.resample.SwathDefinition.get_lonlats') + def test_3d_ewa(self, get_lonlats, ll2cr, fornav): """Test EWA with a 3D dataset.""" import numpy as np import xarray as xr @@ -266,6 +274,8 @@ def test_3d_ewa(self, ll2cr, fornav): np.zeros((10, 10), dtype=np.float32)) fornav.return_value = ([100 * 200] * 3, [np.zeros((200, 100), dtype=np.float32)] * 3) + get_lonlats.return_value = (source_swath.lons, source_swath.lats) + num_chunks = len(source_swath.lons.chunks[0]) * len(source_swath.lons.chunks[1]) new_data = resample_dataset(swath_data, target_area, resampler='ewa') self.assertTupleEqual(new_data.shape, (3, 200, 100)) @@ -274,7 +284,8 @@ def test_3d_ewa(self, ll2cr, fornav): self.assertIs(new_data.attrs['area'], target_area) # make sure we can actually compute everything new_data.compute() - previous_calls = ll2cr.call_count + lonlat_calls = get_lonlats.call_count + ll2cr_calls = ll2cr.call_count # resample a different dataset and make sure cache is used swath_data = xr.DataArray( @@ -282,8 +293,11 @@ def test_3d_ewa(self, ll2cr, fornav): dims=('bands', 'y', 'x'), coords={'bands': ['R', 'G', 'B']}, attrs={'area': source_swath, 'test': 'test'}) new_data = resample_dataset(swath_data, target_area, resampler='ewa') - self.assertEqual(ll2cr.call_count, previous_calls) new_data.compute() + # ll2cr will be called once more because of the computation + self.assertEqual(ll2cr.call_count, ll2cr_calls + num_chunks) + # but we should already have taken the lonlats from the SwathDefinition + self.assertEqual(get_lonlats.call_count, lonlat_calls) self.assertIn('y', new_data.coords) self.assertIn('x', new_data.coords) self.assertIn('bands', new_data.coords) From bfeacd0b086717346e30afcd08240188cf2f31ed Mon Sep 17 00:00:00 2001 From: David Hoese Date: Wed, 26 Jun 2019 13:37:56 -0500 Subject: [PATCH 2/2] Fix geoviews only issuing a warning on critical import error --- satpy/scene.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/satpy/scene.py b/satpy/scene.py index 336a9def61..631f62c969 100644 --- a/satpy/scene.py +++ b/satpy/scene.py @@ -1170,13 +1170,8 @@ def to_geoviews(self, gvtype=None, datasets=None, kdims=None, vdims=None, dynami to be passed to geoviews """ - try: - import geoviews as gv - from cartopy import crs # noqa - except ImportError: - import warnings - warnings.warn("This method needs the geoviews package installed.") - + import geoviews as gv + from cartopy import crs # noqa if gvtype is None: gvtype = gv.Image