def test_da2cf(self): """Test the conversion of a DataArray to a CF-compatible DataArray.""" from satpy.writers.cf_writer import CFWriter import xarray as xr # Create set of test attributes attrs, attrs_expected, attrs_expected_flat = self.get_test_attrs() attrs['area'] = 'some_area' attrs['prerequisites'] = [DatasetID('hej')] # Adjust expected attributes expected_prereq = ( "DatasetID(name='hej', wavelength=None, resolution=None, polarization=None, " "calibration=None, level=None, modifiers=())") update = { 'prerequisites': [expected_prereq], 'long_name': attrs['name'] } attrs_expected.update(update) attrs_expected_flat.update(update) attrs_expected.pop('name') attrs_expected_flat.pop('name') # Create test data array arr = xr.DataArray(np.array([[1, 2], [3, 4]]), attrs=attrs, dims=('y', 'x'), coords={ 'y': [0, 1], 'x': [1, 2], 'acq_time': ('y', [3, 4]) }) # Test conversion to something cf-compliant res = CFWriter.da2cf(arr) self.assertTrue(np.all(res['x'] == arr['x'])) self.assertTrue(np.all(res['y'] == arr['y'])) self.assertTrue(np.all(res['acq_time'] == arr['acq_time'])) self.assertDictEqual(res['x'].attrs, { 'units': 'm', 'standard_name': 'projection_x_coordinate' }) self.assertDictEqual(res['y'].attrs, { 'units': 'm', 'standard_name': 'projection_y_coordinate' }) self.assertDictWithArraysEqual(res.attrs, attrs_expected) # Test attribute kwargs res_flat = CFWriter.da2cf(arr, flatten_attrs=True, exclude_attrs=['int']) attrs_expected_flat.pop('int') self.assertDictWithArraysEqual(res_flat.attrs, attrs_expected_flat)
def test_collect_datasets(self, link_coords, assert_xy_unique, make_alt_coords_unique, da2cf, area2cf, *mocks): """Test collecting CF datasets from a DataArray objects.""" from satpy.writers.cf_writer import CFWriter import xarray as xr # Patch methods def identity(arg, **kwargs): return arg def raise_key_error(arg, **kwargs): raise KeyError da2cf.side_effect = identity area2cf.side_effect = raise_key_error make_alt_coords_unique.return_value = 'unique_coords' # Define test datasets data = [[1, 2], [3, 4]] y = [1, 2] x = [1, 2] time = [1, 2] tstart = datetime(2019, 4, 1, 12, 0) tend = datetime(2019, 4, 1, 12, 15) datasets = [xr.DataArray(data=data, dims=('y', 'x'), coords={'y': y, 'x': x, 'acq_time': ('y', time)}, attrs={'name': 'var1', 'start_time': tstart, 'end_time': tend}), xr.DataArray(data=data, dims=('y', 'x'), coords={'y': y, 'x': x, 'acq_time': ('y', time)}, attrs={'name': 'var2'})] expected = {'var1': datasets[0], 'var2': datasets[1]} # Collect datasets writer = CFWriter() datas, start_times, end_times = writer._collect_datasets(datasets, include_lonlats=True) # Test results self.assertEqual(datas, 'unique_coords') self.assertListEqual(start_times, [tstart, None]) self.assertListEqual(end_times, [tend, None]) # Test method calls self.assertEqual(len(area2cf.call_args_list), 2) for call_args, ds in zip(area2cf.call_args_list, datasets): self.assertEqual(call_args, mock.call(ds, strict=True)) for func in (assert_xy_unique, link_coords, make_alt_coords_unique): func.assert_called() call_arg = func.call_args[0][0] self.assertIsInstance(call_arg, dict) self.assertSetEqual(set(call_arg.keys()), {'var1', 'var2'}) for key, ds in expected.items(): self.assertTrue(call_arg[key].identical(ds))
def test_init(self): """Test initializing the CFWriter class.""" from satpy.writers.cf_writer import CFWriter import satpy.config CFWriter(config_files=[ os.path.join(satpy.config.CONFIG_PATH, 'writers', 'cf.yaml') ])
def test_collect_datasets(self, *mocks): """Test collecting CF datasets from a DataArray objects.""" from satpy.writers.cf_writer import CFWriter import xarray as xr import pyresample.geometry 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]) # Define test datasets data = [[1, 2], [3, 4]] y = [1, 2] x = [1, 2] time = [1, 2] tstart = datetime(2019, 4, 1, 12, 0) tend = datetime(2019, 4, 1, 12, 15) datasets = [xr.DataArray(data=data, dims=('y', 'x'), coords={'y': y, 'x': x, 'acq_time': ('y', time)}, attrs={'name': 'var1', 'start_time': tstart, 'end_time': tend, 'area': geos}), xr.DataArray(data=data, dims=('y', 'x'), coords={'y': y, 'x': x, 'acq_time': ('y', time)}, attrs={'name': 'var2', 'long_name': 'variable 2'})] # Collect datasets writer = CFWriter() datas, start_times, end_times = writer._collect_datasets(datasets, include_lonlats=True) # Test results self.assertEqual(len(datas), 3) self.assertEqual(set(datas.keys()), {'var1', 'var2', 'geos'}) self.assertListEqual(start_times, [None, tstart, None]) self.assertListEqual(end_times, [None, tend, None]) var1 = datas['var1'] var2 = datas['var2'] self.assertEqual(var1.name, 'var1') self.assertEqual(var1.attrs['grid_mapping'], 'geos') self.assertEqual(var1.attrs['start_time'], '2019-04-01 12:00:00') self.assertEqual(var1.attrs['end_time'], '2019-04-01 12:15:00') self.assertEqual(var1.attrs['long_name'], 'var1') # variable 2 self.assertNotIn('grid_mapping', var2.attrs) self.assertEqual(var2.attrs['long_name'], 'variable 2')
def test_init(self): from satpy.writers.cf_writer import CFWriter import satpy.config CFWriter(config_files=[ os.path.join(satpy.config.CONFIG_PATH, 'writers', 'cf.yaml') ])
def test_init(self): from satpy.writers.cf_writer import CFWriter w = CFWriter()