diff --git a/satpy/resample.py b/satpy/resample.py index 617e760ea8..e7338b8dd8 100644 --- a/satpy/resample.py +++ b/satpy/resample.py @@ -616,7 +616,6 @@ def precompute(self, mask=None, radius_of_influence=50000, epsilon=0, reduce_data=reduce_data) self.resampler = XArrayResamplerBilinear(**kwargs) - try: self.load_bil_info(cache_dir, **kwargs) LOG.debug("Loaded bilinear parameters") diff --git a/satpy/tests/test_resample.py b/satpy/tests/test_resample.py index 9c2814d0bb..92956f7e47 100644 --- a/satpy/tests/test_resample.py +++ b/satpy/tests/test_resample.py @@ -387,13 +387,11 @@ def test_expand_without_dims_4D(self): class TestBilinearResampler(unittest.TestCase): """Test the bilinear resampler.""" - @mock.patch('satpy.resample.BilinearResampler.load_bil_info') @mock.patch('satpy.resample.np.savez') @mock.patch('satpy.resample.np.load') @mock.patch('satpy.resample.BilinearResampler._create_cache_filename') @mock.patch('satpy.resample.XArrayResamplerBilinear') - def test_bil_resampling(self, resampler, create_filename, load, savez, - load_bil_info): + def test_bil_resampling(self, resampler, create_filename, load, savez): """Test the bilinear resampler.""" import numpy as np import dask.array as da @@ -406,7 +404,7 @@ def test_bil_resampling(self, resampler, create_filename, load, savez, # Test that bilinear resampling info calculation is called, # and the info is saved - load_bil_info.side_effect = IOError() + load.side_effect = IOError() resampler = BilinearResampler(source_swath, target_area) resampler.precompute( mask=da.arange(5, chunks=5).astype(np.bool)) @@ -414,7 +412,8 @@ def test_bil_resampling(self, resampler, create_filename, load, savez, resampler.resampler.get_bil_info.assert_called_with() self.assertFalse(len(savez.mock_calls), 1) resampler.resampler.reset_mock() - load_bil_info.reset_mock() + load.reset_mock() + load.side_effect = None # Test that get_sample_from_bil_info is called properly data = mock.MagicMock() @@ -425,6 +424,55 @@ def test_bil_resampling(self, resampler, create_filename, load, savez, resampler.resampler.get_sample_from_bil_info.assert_called_with( data, fill_value=fill_value, output_shape=target_area.shape) + # Test that the resampling info is tried to read from the disk + resampler = BilinearResampler(source_swath, target_area) + resampler.precompute(cache_dir='.') + load.assert_called() + + # Test caching the resampling info + try: + the_dir = tempfile.mkdtemp() + resampler = BilinearResampler(source_area, target_area) + create_filename.return_value = os.path.join(the_dir, 'test_cache.npz') + load.reset_mock() + load.side_effect = IOError() + + resampler.precompute(cache_dir=the_dir) + savez.assert_called() + # assert data was saved to the on-disk cache + self.assertEqual(len(savez.mock_calls), 1) + # assert that load was called to try to load something from disk + self.assertEqual(len(load.mock_calls), 1) + + nbcalls = len(resampler.resampler.get_bil_info.mock_calls) + # test reusing the resampler + load.side_effect = None + + class FakeNPZ(dict): + def close(self): + pass + + load.return_value = FakeNPZ(bilinear_s=1, + bilinear_t=2, + valid_input_index=3, + index_array=4) + resampler.precompute(cache_dir=the_dir) + # we already have things cached in-memory, no need to save again + self.assertEqual(len(savez.mock_calls), 1) + # we already have things cached in-memory, don't need to load + # self.assertEqual(len(load.mock_calls), 1) + self.assertEqual(len(resampler.resampler.get_bil_info.mock_calls), nbcalls) + + # test loading saved resampler + resampler = BilinearResampler(source_area, target_area) + resampler.precompute(cache_dir=the_dir) + self.assertEqual(len(load.mock_calls), 2) + self.assertEqual(len(resampler.resampler.get_bil_info.mock_calls), nbcalls) + # we should have cached things in-memory now + # self.assertEqual(len(resampler._index_caches), 1) + finally: + shutil.rmtree(the_dir) + def suite(): """The test suite for test_scene.