def precompute(self, mask=None, radius_of_influence=50000, epsilon=0, reduce_data=True, nprocs=1, cache_dir=False, **kwargs): """Create bilinear coefficients and store them for later use. Note: The `mask` keyword should be provided if geolocation may be valid where data points are invalid. This defaults to the `mask` attribute of the `data` numpy masked array passed to the `resample` method. """ del kwargs if self.resampler is None: kwargs = dict(source_geo_def=self.source_geo_def, target_geo_def=self.target_geo_def, radius_of_influence=radius_of_influence, neighbours=32, epsilon=epsilon, reduce_data=reduce_data) self.resampler = XArrayResamplerBilinear(**kwargs) try: self.load_bil_info(cache_dir, **kwargs) LOG.debug("Loaded bilinear parameters") except IOError: LOG.debug("Computing bilinear parameters") self.resampler.get_bil_info() self.save_bil_info(cache_dir, **kwargs)
def test_query_resample_kdtree(self, qnd): """Test that query_no_distance is called in _query_resample_kdtree().""" from pyresample.bilinear.xarr import XArrayResamplerBilinear resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) res, none = resampler._query_resample_kdtree(1, 2, 3, 4, reduce_data=5) qnd.assert_called_with(2, 3, 4, 1, resampler.neighbours, resampler.epsilon, resampler.radius_of_influence)
def test_create_resample_kdtree(self, KDTree): """Test that KDTree creation is called.""" from pyresample.bilinear.xarr import XArrayResamplerBilinear resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) vii, kdtree = resampler._create_resample_kdtree() self.assertEqual(np.sum(vii), 2700) self.assertEqual(vii.size, self.source_def.size) KDTree.assert_called_once()
def test_add_missing_coordinates(self): """Test coordinate updating.""" import dask.array as da from xarray import DataArray from pyresample.bilinear.xarr import XArrayResamplerBilinear resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) bands = ['R', 'G', 'B'] data = DataArray(da.ones((3, 10, 10)), dims=('bands', 'y', 'x'), coords={ 'bands': bands, 'y': np.arange(10), 'x': np.arange(10) }) resampler._add_missing_coordinates(data) # X and Y coordinates should not change self.assertIsNone(resampler.out_coords_x) self.assertIsNone(resampler.out_coords_y) self.assertIsNone(resampler._out_coords['x']) self.assertIsNone(resampler._out_coords['y']) self.assertTrue('bands' in resampler._out_coords) self.assertTrue(np.all(resampler._out_coords['bands'] == bands)) # Available coordinates from self.out_coords_x and self.out_coords_y # should be set to self._out_coords resampler.out_coords_x = [1] resampler.out_coords_y = [2] resampler._add_missing_coordinates(data) self.assertEqual(resampler._out_coords['x'], resampler.out_coords_x) self.assertEqual(resampler._out_coords['y'], resampler.out_coords_y)
def test_create_empty_bil_info(self): """Test creation of empty bilinear info.""" from pyresample.bilinear.xarr import XArrayResamplerBilinear resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) resampler._create_empty_bil_info() self.assertEqual(resampler.bilinear_t.shape, (self.target_def.size, )) self.assertEqual(resampler.bilinear_s.shape, (self.target_def.size, )) self.assertEqual(resampler._index_array.shape, (self.target_def.size, 4)) self.assertTrue(resampler._index_array.dtype == np.int32) self.assertEqual(resampler._valid_input_index.shape, (self.source_def.size, )) self.assertTrue(resampler._valid_input_index.dtype == np.bool)
def resample_dataset(data, target_grid, radius_of_influence=100e3, resample_cache=None, return_neighbor_info=False, neighbours=1, epsilon=0, interp='nearest'): # first get the source grid definition try: if 'area' in data.attrs: source_grid = data.attrs['area'] else: raise RuntimeError except RuntimeError: print('Must include pyresample.gemoetry in the data.attrs area_def or ' 'area') return # check for SwathDefinition or AreaDefinition # if swath ensure it is xarray.DataArray and not numpy for chunking source_grid = _check_swath_or_area(source_grid) # set kwargs for XArrayResamplerNN kwargs = dict(source_geo_def=source_grid, target_geo_def=target_grid, radius_of_influence=radius_of_influence, neighbours=neighbours, epsilon=epsilon) if interp is 'nearest': resampler = XArrayResamplerNN(**kwargs) else: resampler = XArrayResamplerBilinear(**kwargs) # check if resample cash is none else assume it is a dict with keys #[valid_input_index, valid_output_index, index_array, distance_array] # else generate the data if resample_cache is None: valid_input_index, valid_output_index, index_array, distance_array = resampler.get_neighbour_info( ) else: resampler.valid_input_index = resample_cache['valid_input_index'] resampler.valid_output_index = resample_cache['valid_output_index'] resampler.index_array = resample_cache['index_array'] resampler.distance_array = resample_cache['distance_array'] # now store the resampled data temporarily in temp temp = resampler.get_sample_from_neighbour_info(data) # reformat data from temp out = _reformat_resampled_data(data, temp, target_grid) if return_neighbor_info: resample_cache = dict(valid_input_index=valid_input_index, valid_output_index=valid_output_index, index_array=index_array, distance_array=distance_array) return out, resample_cache else: return out
def test_init(self): """Test that the resampler has been initialized correctly.""" from pyresample.bilinear.xarr import XArrayResamplerBilinear # With defaults resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) self.assertTrue(resampler.source_geo_def == self.source_def) self.assertTrue(resampler.target_geo_def == self.target_def) self.assertEqual(resampler.radius_of_influence, self.radius) self.assertEqual(resampler.neighbours, 32) self.assertEqual(resampler.epsilon, 0) self.assertTrue(resampler.reduce_data) # These should be None self.assertIsNone(resampler.valid_input_index) self.assertIsNone(resampler.valid_output_index) self.assertIsNone(resampler.index_array) self.assertIsNone(resampler.distance_array) self.assertIsNone(resampler.bilinear_t) self.assertIsNone(resampler.bilinear_s) self.assertIsNone(resampler.slices_x) self.assertIsNone(resampler.slices_y) self.assertIsNone(resampler.mask_slices) self.assertIsNone(resampler.out_coords_x) self.assertIsNone(resampler.out_coords_y) # self.slices_{x,y} are used in self.slices dict self.assertTrue(resampler.slices['x'] is resampler.slices_x) self.assertTrue(resampler.slices['y'] is resampler.slices_y) # self.out_coords_{x,y} are used in self.out_coords dict self.assertTrue(resampler.out_coords['x'] is resampler.out_coords_x) self.assertTrue(resampler.out_coords['y'] is resampler.out_coords_y) # Override defaults resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius, neighbours=16, epsilon=0.1, reduce_data=False) self.assertEqual(resampler.neighbours, 16) self.assertEqual(resampler.epsilon, 0.1) self.assertFalse(resampler.reduce_data)
def test_get_sample_from_bil_info(self): """Test bilinear interpolation as a whole.""" from pyresample.bilinear.xarr import XArrayResamplerBilinear resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) _ = resampler.get_bil_info() # Sample from data1 res = resampler.get_sample_from_bil_info(self.data1) res = res.compute() # Check couple of values self.assertEqual(res.values[1, 1], 1.) self.assertTrue(np.isnan(res.values[0, 3])) # Check that the values haven't gone down or up a lot self.assertAlmostEqual(np.nanmin(res.values), 1.) self.assertAlmostEqual(np.nanmax(res.values), 1.) # Check that dimensions are the same self.assertEqual(res.dims, self.data1.dims) # Sample from data1, custom fill value res = resampler.get_sample_from_bil_info(self.data1, fill_value=-1.0) res = res.compute() self.assertEqual(np.nanmin(res.values), -1.) # Sample from integer data res = resampler.get_sample_from_bil_info(self.data1.astype(np.uint8), fill_value=None) res = res.compute() # Five values should be filled with zeros, which is the # default fill_value for integer data self.assertEqual(np.sum(res == 0), 6)
def test_get_sample_from_bil_info(self): """Test bilinear interpolation as a whole.""" import dask.array as da from xarray import DataArray from pyresample.bilinear.xarr import XArrayResamplerBilinear resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) resampler.get_bil_info() # Sample from data1 res = resampler.get_sample_from_bil_info(self.data1) res = res.compute() # Check couple of values self.assertEqual(res.values[1, 1], 1.) self.assertTrue(np.isnan(res.values[0, 3])) # Check that the values haven't gone down or up a lot self.assertAlmostEqual(np.nanmin(res.values), 1.) self.assertAlmostEqual(np.nanmax(res.values), 1.) # Check that dimensions are the same self.assertEqual(res.dims, self.data1.dims) # Sample from data1, custom fill value res = resampler.get_sample_from_bil_info(self.data1, fill_value=-1.0) res = res.compute() self.assertEqual(np.nanmin(res.values), -1.) # Sample from integer data res = resampler.get_sample_from_bil_info(self.data1.astype(np.uint8), fill_value=None) res = res.compute() # Five values should be filled with zeros, which is the # default fill_value for integer data self.assertEqual(np.sum(res == 0), 6) # Output coordinates should have been set self.assertTrue(isinstance(resampler._out_coords, dict)) self.assertTrue( np.all(resampler._out_coords['x'] == resampler.out_coords_x)) self.assertTrue( np.all(resampler._out_coords['y'] == resampler.out_coords_y)) # 3D data data = da.moveaxis(da.dstack((self.data1, self.data1)), -1, 0) data = DataArray(data, dims=('bands', 'y', 'x')) res = resampler.get_sample_from_bil_info(data) assert res.shape == (2, ) + self.target_def.shape assert res.dims == data.dims
def test_get_index_array(self, qnd, ria): """Test that query_no_distance is called in __get_index_array().""" from pyresample.bilinear.xarr import XArrayResamplerBilinear qnd.return_value = 'foo' resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) resampler._target_lons = 1 resampler._target_lats = 2 resampler._resample_kdtree = 3 resampler._get_index_array() qnd.assert_called_with(1, 2, True, 3, resampler._neighbours, resampler._epsilon, resampler._radius_of_influence) ria.assert_called_with(qnd.return_value)
def test_compute_indices(self, mock_setattr): """Test running .compute() for indices.""" from pyresample.bilinear.xarr import (XArrayResamplerBilinear, CACHE_INDICES) resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) # Set indices to Numpy arrays for idx in CACHE_INDICES: setattr(resampler, idx, np.array([])) resampler._compute_indices() # None of the indices shouldn't have been reassigned mock_setattr.assert_not_called() # Set indices to a Mock object arr = mock.MagicMock() for idx in CACHE_INDICES: setattr(resampler, idx, arr) resampler._compute_indices() # All the indices should have been reassigned self.assertEqual(mock_setattr.call_count, len(CACHE_INDICES)) # The compute should have been called the same amount of times self.assertEqual(arr.compute.call_count, len(CACHE_INDICES))
def test_get_bil_info(self): """Test calculation of bilinear info.""" from pyresample.bilinear.xarr import XArrayResamplerBilinear def _check_ts(t__, s__, nans): for i, _ in enumerate(t__): # Just check the exact value for one pixel if i == 5: self.assertAlmostEqual(t__[i], 0.730659147133, 5) self.assertAlmostEqual(s__[i], 0.310314173004, 5) # These pixels are outside the area elif i in nans: self.assertTrue(np.isnan(t__[i])) self.assertTrue(np.isnan(s__[i])) # All the others should have values between 0.0 and 1.0 else: self.assertTrue(t__[i] >= 0.0) self.assertTrue(s__[i] >= 0.0) self.assertTrue(t__[i] <= 1.0) self.assertTrue(s__[i] <= 1.0) # Data reduction enabled (default) resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius, reduce_data=True) (t__, s__, slices, mask_slices, out_coords) = resampler.get_bil_info() _check_ts(t__.compute(), s__.compute(), [3, 10, 12, 13, 14, 15]) # Nothing should be masked based on coordinates self.assertTrue(np.all(~mask_slices)) # Four values per output location self.assertEqual(mask_slices.shape, (self.target_def.size, 4)) # self.slices_{x,y} are used in self.slices dict so they # should be the same (object) self.assertTrue(isinstance(slices, dict)) self.assertTrue(resampler.slices['x'] is resampler.slices_x) self.assertTrue(np.all(resampler.slices['x'] == slices['x'])) self.assertTrue(resampler.slices['y'] is resampler.slices_y) self.assertTrue(np.all(resampler.slices['y'] == slices['y'])) # self.slices_{x,y} are used in self.slices dict so they # should be the same (object) self.assertTrue(isinstance(out_coords, dict)) self.assertTrue(resampler.out_coords['x'] is resampler.out_coords_x) self.assertTrue(np.all(resampler.out_coords['x'] == out_coords['x'])) self.assertTrue(resampler.out_coords['y'] is resampler.out_coords_y) self.assertTrue(np.all(resampler.out_coords['y'] == out_coords['y'])) # Also some other attributes should have been set self.assertTrue(t__ is resampler.bilinear_t) self.assertTrue(s__ is resampler.bilinear_s) self.assertIsNotNone(resampler.valid_output_index) self.assertIsNotNone(resampler.index_array) self.assertIsNotNone(resampler.valid_input_index) # Data reduction disabled resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius, reduce_data=False) (t__, s__, slices, mask_slices, out_coords) = resampler.get_bil_info() _check_ts(t__.compute(), s__.compute(), [10, 12, 13, 14, 15])
class BilinearResampler(BaseResampler): """Resample using bilinear.""" def __init__(self, source_geo_def, target_geo_def): super(BilinearResampler, self).__init__(source_geo_def, target_geo_def) self.resampler = None def precompute(self, mask=None, radius_of_influence=50000, epsilon=0, reduce_data=True, nprocs=1, cache_dir=False, **kwargs): """Create bilinear coefficients and store them for later use. Note: The `mask` keyword should be provided if geolocation may be valid where data points are invalid. This defaults to the `mask` attribute of the `data` numpy masked array passed to the `resample` method. """ del kwargs if self.resampler is None: kwargs = dict(source_geo_def=self.source_geo_def, target_geo_def=self.target_geo_def, radius_of_influence=radius_of_influence, neighbours=32, epsilon=epsilon, reduce_data=reduce_data) self.resampler = XArrayResamplerBilinear(**kwargs) try: self.load_bil_info(cache_dir, **kwargs) LOG.debug("Loaded bilinear parameters") except IOError: LOG.debug("Computing bilinear parameters") self.resampler.get_bil_info() self.save_bil_info(cache_dir, **kwargs) def load_bil_info(self, cache_dir, **kwargs): if cache_dir: filename = self._create_cache_filename(cache_dir, prefix='resample_lut_bil_', **kwargs) cache = np.load(filename) for elt in ['bilinear_s', 'bilinear_t', 'valid_input_index', 'index_array']: if isinstance(cache[elt], tuple): setattr(self.resampler, elt, cache[elt][0]) else: setattr(self.resampler, elt, cache[elt]) cache.close() else: raise IOError def save_bil_info(self, cache_dir, **kwargs): if cache_dir: filename = self._create_cache_filename(cache_dir, prefix='resample_lut_bil_', **kwargs) LOG.info('Saving kd_tree neighbour info to %s', filename) cache = {'bilinear_s': self.resampler.bilinear_s, 'bilinear_t': self.resampler.bilinear_t, 'valid_input_index': self.resampler.valid_input_index, 'index_array': self.resampler.index_array} np.savez(filename, **cache) def compute(self, data, fill_value=None, **kwargs): """Resample the given data using bilinear interpolation""" del kwargs if fill_value is None: fill_value = data.attrs.get('_FillValue') target_shape = self.target_geo_def.shape res = self.resampler.get_sample_from_bil_info(data, fill_value=fill_value, output_shape=target_shape) return res
class BilinearResampler(BaseResampler): """Resample using bilinear.""" def __init__(self, source_geo_def, target_geo_def): """Init BilinearResampler.""" super(BilinearResampler, self).__init__(source_geo_def, target_geo_def) self.resampler = None def precompute(self, mask=None, radius_of_influence=50000, epsilon=0, reduce_data=True, cache_dir=False, **kwargs): """Create bilinear coefficients and store them for later use. Note: The `mask` keyword should be provided if geolocation may be valid where data points are invalid. This defaults to the `mask` attribute of the `data` numpy masked array passed to the `resample` method. """ del kwargs if self.resampler is None: kwargs = dict(source_geo_def=self.source_geo_def, target_geo_def=self.target_geo_def, radius_of_influence=radius_of_influence, neighbours=32, epsilon=epsilon, reduce_data=reduce_data) self.resampler = XArrayResamplerBilinear(**kwargs) try: self.load_bil_info(cache_dir, **kwargs) LOG.debug("Loaded bilinear parameters") except IOError: LOG.debug("Computing bilinear parameters") self.resampler.get_bil_info() self.save_bil_info(cache_dir, **kwargs) def load_bil_info(self, cache_dir, **kwargs): """Load bilinear resampling info from cache directory.""" if cache_dir: filename = self._create_cache_filename(cache_dir, prefix='bil_lut-', **kwargs) for val in BIL_COORDINATES.keys(): try: cache = da.from_zarr(filename, val) if val == 'valid_input_index': # valid input index array needs to be boolean cache = cache.astype(np.bool) # Compute the cache arrays cache = cache.compute() except ValueError: raise IOError setattr(self.resampler, val, cache) else: raise IOError def save_bil_info(self, cache_dir, **kwargs): """Save bilinear resampling info to cache directory.""" if cache_dir: filename = self._create_cache_filename(cache_dir, prefix='bil_lut-', **kwargs) LOG.info('Saving BIL neighbour info to %s', filename) zarr_out = xr.Dataset() for idx_name, coord in BIL_COORDINATES.items(): var = getattr(self.resampler, idx_name) if isinstance(var, np.ndarray): var = da.from_array(var, chunks=CHUNK_SIZE) var = var.rechunk(CHUNK_SIZE) zarr_out[idx_name] = (coord, var) zarr_out.to_zarr(filename) def compute(self, data, fill_value=None, **kwargs): """Resample the given data using bilinear interpolation.""" del kwargs if fill_value is None: fill_value = data.attrs.get('_FillValue') target_shape = self.target_geo_def.shape res = self.resampler.get_sample_from_bil_info(data, fill_value=fill_value, output_shape=target_shape) return update_resampled_coords(data, res, self.target_geo_def)
def test_get_bil_info(self): """Test calculation of bilinear info.""" from pyresample.bilinear.xarr import XArrayResamplerBilinear def _check_ts(t__, s__, nans): for i, _ in enumerate(t__): # Just check the exact value for one pixel if i == 5: self.assertAlmostEqual(t__[i], 0.730659147133, 5) self.assertAlmostEqual(s__[i], 0.310314173004, 5) # These pixels are outside the area elif i in nans: self.assertTrue(np.isnan(t__[i])) self.assertTrue(np.isnan(s__[i])) # All the others should have values between 0.0 and 1.0 else: self.assertTrue(t__[i] >= 0.0) self.assertTrue(s__[i] >= 0.0) self.assertTrue(t__[i] <= 1.0) self.assertTrue(s__[i] <= 1.0) # Data reduction enabled (default) resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius, reduce_data=True) resampler.get_bil_info() t__ = resampler.bilinear_t s__ = resampler.bilinear_s mask_slices = resampler.mask_slices _check_ts(t__, s__, [3, 10, 12, 13, 14, 15]) # Nothing should be masked based on coordinates self.assertTrue(np.all(~mask_slices)) # Four values per output location self.assertEqual(mask_slices.shape, (self.target_def.size, 4)) # Also some other attributes should have been set self.assertTrue(t__ is resampler.bilinear_t) self.assertTrue(s__ is resampler.bilinear_s) self.assertIsNotNone(resampler._index_array) self.assertIsNotNone(resampler._valid_input_index) self.assertIsNotNone(resampler.out_coords_x) self.assertIsNotNone(resampler.out_coords_y) self.assertTrue( np.allclose(resampler.out_coords_x, [-1070912.72, -470912.72, 129087.28, 729087.28])) self.assertTrue( np.allclose(resampler.out_coords_y, [1190031.36, 590031.36, -9968.64, -609968.64])) # Data reduction disabled resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius, reduce_data=False) resampler.get_bil_info() t__ = resampler.bilinear_t s__ = resampler.bilinear_s mask_slices = resampler.mask_slices _check_ts(t__, s__, [10, 12, 13, 14, 15]) # Target area and source data do not overlap resampler = XArrayResamplerBilinear(self.source_def, self.target_def_outside, self.radius, reduce_data=False) resampler.get_bil_info() self.assertEqual(resampler.bilinear_t.shape, (self.target_def_outside.size, )) self.assertEqual(resampler.bilinear_s.shape, (self.target_def_outside.size, )) self.assertEqual(resampler.slices_x.shape, (self.target_def_outside.size, 4)) self.assertEqual(resampler.slices_y.shape, (self.target_def_outside.size, 4)) self.assertEqual(resampler.out_coords_x.shape, (self.target_def_outside.shape[1], )) self.assertEqual(resampler.out_coords_y.shape, (self.target_def_outside.shape[0], )) self.assertEqual(resampler.mask_slices.shape, (self.target_def_outside.size, 4))
def test_get_slices(self, meshgrid): """Test slice array creation.""" from pyresample.bilinear.xarr import XArrayResamplerBilinear meshgrid.return_value = (self.cols, self.lines) resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) resampler.valid_input_index = self.valid_input_index resampler.index_array = self.index_array resampler._get_slices() self.assertIsNotNone(resampler.out_coords_x) self.assertIsNotNone(resampler.out_coords_y) self.assertTrue(resampler.out_coords_x is resampler.out_coords['x']) self.assertTrue(resampler.out_coords_y is resampler.out_coords['y']) self.assertTrue( np.allclose(resampler.out_coords_x, [-1070912.72, -470912.72, 129087.28, 729087.28])) self.assertTrue( np.allclose(resampler.out_coords_y, [1190031.36, 590031.36, -9968.64, -609968.64])) self.assertIsNotNone(resampler.slices_x) self.assertIsNotNone(resampler.slices_y) self.assertTrue(resampler.slices_x is resampler.slices['x']) self.assertTrue(resampler.slices_y is resampler.slices['y']) self.assertTrue(resampler.slices_x.shape == (self.target_def.size, 32)) self.assertTrue(resampler.slices_y.shape == (self.target_def.size, 32)) self.assertEqual(np.sum(resampler.slices_x), 12471) self.assertEqual(np.sum(resampler.slices_y), 2223) self.assertFalse(np.any(resampler.mask_slices)) # Ensure that source geo def is used in masking # Setting target_geo_def to 0-size shouldn't cause any masked values resampler.target_geo_def = np.array([]) resampler._get_slices() self.assertFalse(np.any(resampler.mask_slices)) # Setting source area def to 0-size should mask all values resampler.source_geo_def = np.array([[]]) resampler._get_slices() self.assertTrue(np.all(resampler.mask_slices))
def test_slice_data(self): """Test slicing the data.""" import dask.array as da from xarray import DataArray from pyresample.bilinear.xarr import XArrayResamplerBilinear resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) # Too many dimensions data = DataArray(da.ones((1, 3, 10, 10))) with self.assertRaises(ValueError): _ = resampler._slice_data(data, np.nan) # 2D data data = DataArray(da.ones((10, 10))) resampler.slices_x = np.random.randint(0, 10, (100, 4)) resampler.slices_y = np.random.randint(0, 10, (100, 4)) resampler.mask_slices = np.zeros((100, 4), dtype=np.bool) p_1, p_2, p_3, p_4 = resampler._slice_data(data, np.nan) self.assertEqual(p_1.shape, (100, )) self.assertTrue(p_1.shape == p_2.shape == p_3.shape == p_4.shape) self.assertTrue( np.all(p_1 == 1.0) and np.all(p_2 == 1.0) and np.all(p_3 == 1.0) and np.all(p_4 == 1.0)) # 2D data with masking resampler.mask_slices = np.ones((100, 4), dtype=np.bool) p_1, p_2, p_3, p_4 = resampler._slice_data(data, np.nan) self.assertTrue( np.all(np.isnan(p_1)) and np.all(np.isnan(p_2)) and np.all(np.isnan(p_3)) and np.all(np.isnan(p_4))) # 3D data data = DataArray(da.ones((3, 10, 10))) resampler.slices_x = np.random.randint(0, 10, (100, 4)) resampler.slices_y = np.random.randint(0, 10, (100, 4)) resampler.mask_slices = np.zeros((100, 4), dtype=np.bool) p_1, p_2, p_3, p_4 = resampler._slice_data(data, np.nan) self.assertEqual(p_1.shape, (3, 100)) self.assertTrue(p_1.shape == p_2.shape == p_3.shape == p_4.shape) # 3D data with masking resampler.mask_slices = np.ones((100, 4), dtype=np.bool) p_1, p_2, p_3, p_4 = resampler._slice_data(data, np.nan) self.assertTrue( np.all(np.isnan(p_1)) and np.all(np.isnan(p_2)) and np.all(np.isnan(p_3)) and np.all(np.isnan(p_4)))
def test_slice_data(self): """Test slicing the data.""" import dask.array as da from xarray import DataArray from pyresample.bilinear.xarr import XArrayResamplerBilinear resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) resampler.get_bil_info() # Too many dimensions data = DataArray(da.ones((1, 3) + self.source_def.shape)) with self.assertRaises(ValueError): _ = resampler._slice_data(data, np.nan) # 2D data data = DataArray(da.ones(self.source_def.shape)) p_1, p_2, p_3, p_4 = resampler._slice_data(data, np.nan) self.assertEqual(p_1.shape, resampler.bilinear_s.shape) self.assertTrue(p_1.shape == p_2.shape == p_3.shape == p_4.shape) self.assertTrue( np.all(p_1 == 1.0) and np.all(p_2 == 1.0) and np.all(p_3 == 1.0) and np.all(p_4 == 1.0)) # 2D data with masking resampler.mask_slices[:, :] = True p_1, p_2, p_3, p_4 = resampler._slice_data(data, np.nan) self.assertTrue( np.all(np.isnan(p_1)) and np.all(np.isnan(p_2)) and np.all(np.isnan(p_3)) and np.all(np.isnan(p_4))) # 3D data data = DataArray(da.ones((3, ) + self.source_def.shape)) resampler.mask_slices[:, :] = False p_1, p_2, p_3, p_4 = resampler._slice_data(data, np.nan) self.assertEqual(p_1.shape, (3, ) + resampler.bilinear_s.shape) self.assertTrue(p_1.shape == p_2.shape == p_3.shape == p_4.shape) # 3D data with masking resampler.mask_slices[:, :] = True p_1, p_2, p_3, p_4 = resampler._slice_data(data, np.nan) self.assertTrue( np.all(np.isnan(p_1)) and np.all(np.isnan(p_2)) and np.all(np.isnan(p_3)) and np.all(np.isnan(p_4)))
def test_get_slices(self, meshgrid): """Test slice array creation.""" from pyresample.bilinear.xarr import XArrayResamplerBilinear meshgrid.return_value = (self.cols, self.lines) resampler = XArrayResamplerBilinear(self.source_def, self.target_def, self.radius) resampler._valid_input_index = self._valid_input_index resampler._index_array = self._index_array resampler._get_slices() self.assertIsNotNone(resampler.slices_x) self.assertIsNotNone(resampler.slices_y) self.assertTrue(resampler.slices_x.shape == (self.target_def.size, 32)) self.assertTrue(resampler.slices_y.shape == (self.target_def.size, 32)) self.assertEqual(np.sum(resampler.slices_x), 12471) self.assertEqual(np.sum(resampler.slices_y), 2223) self.assertFalse(np.any(resampler.mask_slices)) # Ensure that source geo def is used in masking # Setting target_geo_def to 0-size shouldn't cause any masked values resampler._target_geo_def = np.array([]) resampler._get_slices() self.assertFalse(np.any(resampler.mask_slices)) # Setting source area def to 0-size should mask all values resampler._source_geo_def = np.array([[]]) resampler._get_slices() self.assertTrue(np.all(resampler.mask_slices))