def test_load_dataset_with_area_for_single_areas(self, ldwa): """Test _load_dataset_with_area() for single area definitions.""" import xarray as xr import numpy as np from pyresample.geometry import AreaDefinition from satpy.readers.yaml_reader import GEOFlippableFileYAMLReader reader = GEOFlippableFileYAMLReader() dsid = MagicMock() coords = MagicMock() # create a dummy upright xarray original_area_extent = (-1500, -1000, 1500, 1000) original_array = np.arange(6).reshape((2, 3)) area_def = AreaDefinition( 'test', 'test', 'test', { 'proj': 'geos', 'h': 35785831, 'type': 'crs' }, 3, 2, original_area_extent, ) dummy_ds_xr = xr.DataArray(original_array, dims=('y', 'x'), attrs={'area': area_def}) # assign the dummy xr as return for the super _load_dataset_with_area method ldwa.return_value = dummy_ds_xr # check no input, nothing should change res = reader._load_dataset_with_area(dsid, coords) np.testing.assert_equal(res.values, original_array) np.testing.assert_equal(res.attrs['area'].area_extent, original_area_extent) # check wrong input with self.assertRaises(ValueError): _ = reader._load_dataset_with_area(dsid, coords, 'wronginput') # check native orientation, nothing should change res = reader._load_dataset_with_area(dsid, coords, 'native') np.testing.assert_equal(res.values, original_array) np.testing.assert_equal(res.attrs['area'].area_extent, original_area_extent) # check upright orientation, nothing should change since area is already upright res = reader._load_dataset_with_area(dsid, coords, 'NE') np.testing.assert_equal(res.values, original_array) np.testing.assert_equal(res.attrs['area'].area_extent, original_area_extent) # check that left-right image is flipped correctly dummy_ds_xr.attrs['area'] = area_def.copy(area_extent=(1500, -1000, -1500, 1000)) ldwa.return_value = dummy_ds_xr.copy() res = reader._load_dataset_with_area(dsid, coords, 'NE') np.testing.assert_equal(res.values, np.fliplr(original_array)) np.testing.assert_equal(res.attrs['area'].area_extent, original_area_extent) # check that upside down image is flipped correctly dummy_ds_xr.attrs['area'] = area_def.copy(area_extent=(-1500, 1000, 1500, -1000)) ldwa.return_value = dummy_ds_xr.copy() res = reader._load_dataset_with_area(dsid, coords, 'NE') np.testing.assert_equal(res.values, np.flipud(original_array)) np.testing.assert_equal(res.attrs['area'].area_extent, original_area_extent) # check different projection than geos, nothing should be changed area_def = AreaDefinition( 'test', 'test', 'test', { 'proj': 'lcc', 'lat_1': 25.0, 'type': 'crs' }, 3, 2, original_area_extent, ) dummy_ds_xr = xr.DataArray(original_array, dims=('y', 'x'), attrs={'area': area_def}) ldwa.return_value = dummy_ds_xr res = reader._load_dataset_with_area(dsid, coords, 'NE') np.testing.assert_equal(res.values, original_array) np.testing.assert_equal(res.attrs['area'].area_extent, original_area_extent)
def test_load_dataset_with_area_for_stacked_areas(self, ldwa): """Test _load_dataset_with_area() for stacked area definitions.""" import xarray as xr import numpy as np from pyresample.geometry import AreaDefinition, StackedAreaDefinition from satpy.readers.yaml_reader import GEOFlippableFileYAMLReader reader = GEOFlippableFileYAMLReader() dsid = MagicMock() coords = MagicMock() # create a dummy upright xarray original_area_extents = [(-1500, -1000, 1500, 1000), (3000, 5000, 7000, 8000)] original_array = np.arange(12).reshape((4, 3)) area_def0 = AreaDefinition( 'test', 'test', 'test', { 'proj': 'geos', 'h': 35785831, 'type': 'crs' }, 3, 2, original_area_extents[0], ) area_def1 = area_def0.copy(area_extent=original_area_extents[1]) dummy_ds_xr = xr.DataArray( original_array, dims=('y', 'x'), attrs={'area': StackedAreaDefinition(area_def0, area_def1)}) # check that left-right image is flipped correctly dummy_ds_xr.attrs['area'].defs[0] = area_def0.copy(area_extent=(1500, -1000, -1500, 1000)) dummy_ds_xr.attrs['area'].defs[1] = area_def1.copy(area_extent=(7000, 5000, 3000, 8000)) ldwa.return_value = dummy_ds_xr.copy() res = reader._load_dataset_with_area(dsid, coords, 'NE') np.testing.assert_equal(res.values, np.fliplr(original_array)) np.testing.assert_equal(res.attrs['area'].defs[0].area_extent, original_area_extents[0]) np.testing.assert_equal(res.attrs['area'].defs[1].area_extent, original_area_extents[1]) # check that upside down image is flipped correctly dummy_ds_xr.attrs['area'].defs[0] = area_def0.copy(area_extent=(-1500, 1000, 1500, -1000)) dummy_ds_xr.attrs['area'].defs[1] = area_def1.copy(area_extent=(3000, 8000, 7000, 5000)) ldwa.return_value = dummy_ds_xr.copy() res = reader._load_dataset_with_area(dsid, coords, 'NE') np.testing.assert_equal(res.values, np.flipud(original_array)) # note that the order of the stacked areadefs is flipped here, as expected np.testing.assert_equal(res.attrs['area'].defs[1].area_extent, original_area_extents[0]) np.testing.assert_equal(res.attrs['area'].defs[0].area_extent, original_area_extents[1])
area_vis_exp = AreaDefinition( area_id='geos_mviri_4x4', proj_id='geos_mviri_4x4', description='MVIRI Geostationary Projection', projection={ 'proj': 'geos', 'lon_0': 57.0, 'h': ALTITUDE, 'a': EQUATOR_RADIUS, 'b': POLE_RADIUS }, width=4, height=4, area_extent=[5621229.74392, 5621229.74392, -5621229.74392, -5621229.74392]) area_ir_wv_exp = area_vis_exp.copy(area_id='geos_mviri_2x2', proj_id='geos_mviri_2x2', width=2, height=2) @pytest.fixture(name='fake_dataset') def fixture_fake_dataset(): """Create fake dataset.""" count_ir = da.linspace(0, 255, 4, dtype=np.uint8).reshape(2, 2) count_wv = da.linspace(0, 255, 4, dtype=np.uint8).reshape(2, 2) count_vis = da.linspace(0, 255, 16, dtype=np.uint8).reshape(4, 4) sza = da.from_array(np.array([[45, 90], [0, 45]], dtype=np.float32)) mask = da.from_array( np.array( [ [0, 0, 0, 0], [0, 0, 0, 0],