def test_area2cf(self, area2gridmapping, area2lonlat): """Test the conversion of an area to CF standards.""" import xarray as xr import pyresample.geometry from satpy.writers.cf_writer import area2cf area2gridmapping.side_effect = lambda x: [1, 2, 3] area2lonlat.side_effect = lambda x: [4, 5, 6] ds_base = xr.DataArray(data=[[1, 2], [3, 4]], dims=('y', 'x'), coords={ 'y': [1, 2], 'x': [3, 4] }, attrs={'name': 'var1'}) # a) Area Definition and strict=False geos = pyresample.geometry.AreaDefinition(area_id='geos', description='geos', proj_id='geos', projection={ 'proj': 'geos', 'h': 35785831., 'a': 6378169., 'b': 6356583.8 }, width=2, height=2, area_extent=[-1, -1, 1, 1]) ds = ds_base.copy(deep=True) ds.attrs['area'] = geos res = area2cf(ds) self.assertEqual(len(res), 4) self.assertListEqual(res[0:3], [1, 2, 3]) self.assertTrue(ds.identical(res[3])) # b) Area Definition and strict=False area2cf(ds, strict=True) area2lonlat.assert_called() # c) Swath Definition swath = pyresample.geometry.SwathDefinition(lons=[[1, 1], [2, 2]], lats=[[1, 2], [1, 2]]) ds = ds_base.copy(deep=True) ds.attrs['area'] = swath res = area2cf(ds) self.assertEqual(len(res), 4) self.assertListEqual(res[0:3], [4, 5, 6]) self.assertTrue(ds.identical(res[3]))
def test_area2cf(self): """Test the conversion of an area to CF standards.""" import xarray as xr import pyresample.geometry from satpy.writers.cf_writer import area2cf ds_base = xr.DataArray(data=[[1, 2], [3, 4]], dims=('y', 'x'), coords={'y': [1, 2], 'x': [3, 4]}, attrs={'name': 'var1'}) # a) Area Definition and strict=False geos = pyresample.geometry.AreaDefinition( area_id='geos', description='geos', proj_id='geos', projection={'proj': 'geos', 'h': 35785831., 'a': 6378169., 'b': 6356583.8}, width=2, height=2, area_extent=[-1, -1, 1, 1]) ds = ds_base.copy(deep=True) ds.attrs['area'] = geos res = area2cf(ds) self.assertEqual(len(res), 2) self.assertEqual(res[0].size, 1) # grid mapping variable self.assertEqual(res[0].name, res[1].attrs['grid_mapping']) # b) Area Definition and strict=False ds = ds_base.copy(deep=True) ds.attrs['area'] = geos res = area2cf(ds, strict=True) # same as above self.assertEqual(len(res), 2) self.assertEqual(res[0].size, 1) # grid mapping variable self.assertEqual(res[0].name, res[1].attrs['grid_mapping']) # but now also have the lon/lats self.assertIn('longitude', res[1].coords) self.assertIn('latitude', res[1].coords) # c) Swath Definition swath = pyresample.geometry.SwathDefinition(lons=[[1, 1], [2, 2]], lats=[[1, 2], [1, 2]]) ds = ds_base.copy(deep=True) ds.attrs['area'] = swath res = area2cf(ds) self.assertEqual(len(res), 1) self.assertIn('longitude', res[0].coords) self.assertIn('latitude', res[0].coords) self.assertNotIn('grid_mapping', res[0].attrs)