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_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')