def test_rename_grid_attrs_skip_scalar_dim(ds_with_time_bounds): phalf_dim = 'phalf' assert phalf_dim not in ds_with_time_bounds assert phalf_dim in GRID_ATTRS[PHALF_STR] ds_copy = ds_with_time_bounds.copy() ds_copy[phalf_dim] = 4 ds_copy = ds_copy.set_coords(phalf_dim) result = grid_attrs_to_aospy_names(ds_copy) xr.testing.assert_identical(result[phalf_dim], ds_copy[phalf_dim])
def test_rename_grid_attrs_skip_scalar_dim(ds): phalf_dim = 'phalf' assert phalf_dim not in ds assert phalf_dim in GRID_ATTRS[PHALF_STR] ds_copy = ds.copy() ds_copy[phalf_dim] = 4 ds_copy = ds_copy.set_coords(phalf_dim) result = grid_attrs_to_aospy_names(ds_copy) xr.testing.assert_identical(result[phalf_dim], ds_copy[phalf_dim])
def test_rename_grid_attrs_skip_scalar_dim(self): phalf_dim = 'phalf' assert phalf_dim not in self.ds assert phalf_dim in GRID_ATTRS[PHALF_STR] ds = self.ds.copy() ds[phalf_dim] = 4 ds = ds.set_coords(phalf_dim) result = grid_attrs_to_aospy_names(ds) xr.testing.assert_identical(result[phalf_dim], ds[phalf_dim])
def test_set_grid_attrs_as_coords(ds_with_time_bounds, var_name): ds = grid_attrs_to_aospy_names(ds_with_time_bounds) sfc_area = ds[var_name].isel(**{TIME_STR: 0}).drop_vars(TIME_STR) ds[SFC_AREA_STR] = sfc_area assert SFC_AREA_STR not in ds.coords ds = set_grid_attrs_as_coords(ds) assert SFC_AREA_STR in ds.coords assert TIME_BOUNDS_STR in ds.coords
def test_set_grid_attrs_as_coords_no_times(self): ds = grid_attrs_to_aospy_names(self.ds) sfc_area = ds[self.var_name].isel(**{TIME_STR: 0}).drop(TIME_STR) ds[SFC_AREA_STR] = sfc_area assert SFC_AREA_STR not in ds.coords ds = set_grid_attrs_as_coords(ds, set_time_vars=False) assert SFC_AREA_STR in ds.coords assert TIME_BOUNDS_STR not in ds.coords
def test_set_grid_attrs_as_coords(ds, var_name): ds = grid_attrs_to_aospy_names(ds) sfc_area = ds[var_name].isel(**{TIME_STR: 0}).drop(TIME_STR) ds[SFC_AREA_STR] = sfc_area assert SFC_AREA_STR not in ds.coords ds = set_grid_attrs_as_coords(ds) assert SFC_AREA_STR in ds.coords assert TIME_BOUNDS_STR in ds.coords
def test_rename_grid_attrs_dim_no_coord(ds_with_time_bounds, var_name): bounds_dim = 'nv' assert bounds_dim not in ds_with_time_bounds assert bounds_dim in GRID_ATTRS[BOUNDS_STR] # Create DataArray with all dims lacking coords. arr = xr.DataArray(ds_with_time_bounds[var_name].values, name='dummy') # Insert name to be replaced (its physical meaning doesn't matter here) ds = arr.rename({'dim_0': bounds_dim}).to_dataset() assert not ds[bounds_dim].coords result = grid_attrs_to_aospy_names(ds) assert not result[BOUNDS_STR].coords
def test_rename_grid_attrs_dim_no_coord(ds, var_name): bounds_dim = 'nv' assert bounds_dim not in ds assert bounds_dim in GRID_ATTRS[BOUNDS_STR] # Create DataArray with all dims lacking coords values = ds[var_name].values arr = xr.DataArray(values, name='dummy') # Insert name to be replaced (its physical meaning doesn't matter here) ds = arr.rename({'dim_0': bounds_dim}).to_dataset() assert not ds[bounds_dim].coords result = grid_attrs_to_aospy_names(ds) assert not result[BOUNDS_STR].coords
def test_rename_grid_attrs_ds(ds, alt_lat_str): assert LAT_STR not in ds assert alt_lat_str in ds ds = grid_attrs_to_aospy_names(ds) assert LAT_STR in ds
def test_rename_grid_attrs_custom_error(ds_with_time_bounds, alt_lat_str): assert LAT_STR not in ds_with_time_bounds ds = ds_with_time_bounds.rename({alt_lat_str: 'custom_lat_name'}) with pytest.raises(ValueError): ds = grid_attrs_to_aospy_names(ds, {alt_lat_str: 'custom_lat_name'})
def test_rename_grid_attrs_custom(ds_with_time_bounds, alt_lat_str): assert LAT_STR not in ds_with_time_bounds ds = ds_with_time_bounds.rename({alt_lat_str: 'custom_lat_name'}) ds = grid_attrs_to_aospy_names(ds, {LAT_STR: 'custom_lat_name'}) assert LAT_STR in ds assert 'custom_lat_name' not in ds
def test_rename_grid_attrs_copy_attrs(ds_with_time_bounds, alt_lat_str): orig_attrs = {'dummy_key': 'dummy_val'} ds_orig = ds_with_time_bounds.copy() ds_orig[alt_lat_str].attrs = orig_attrs ds = grid_attrs_to_aospy_names(ds_orig) assert ds[LAT_STR].attrs == orig_attrs
def test_rename_grid_attrs_copy_attrs(ds, alt_lat_str): orig_attrs = {'dummy_key': 'dummy_val'} ds_orig = ds.copy() ds_orig[alt_lat_str].attrs = orig_attrs ds = grid_attrs_to_aospy_names(ds_orig) assert ds[LAT_STR].attrs == orig_attrs
def test_rename_grid_attrs_ds(ds_with_time_bounds, alt_lat_str): assert LAT_STR not in ds_with_time_bounds assert alt_lat_str in ds_with_time_bounds ds = grid_attrs_to_aospy_names(ds_with_time_bounds) assert LAT_STR in ds
def test_rename_grid_attrs_copy_attrs(self): orig_attrs = {'dummy_key': 'dummy_val'} ds_orig = self.ds.copy() ds_orig[self.ALT_LAT_STR].attrs = orig_attrs ds = grid_attrs_to_aospy_names(ds_orig) self.assertEqual(ds[LAT_STR].attrs, orig_attrs)
def test_rename_grid_attrs_ds(self): assert LAT_STR not in self.ds assert self.ALT_LAT_STR in self.ds ds = grid_attrs_to_aospy_names(self.ds) assert LAT_STR in ds
def test_rename_grid_attrs_custom_error(ds, alt_lat_str): assert LAT_STR not in ds ds = ds.rename({alt_lat_str: 'custom_lat_name'}) with pytest.raises(ValueError): ds = grid_attrs_to_aospy_names(ds, {alt_lat_str: 'custom_lat_name'})
def test_rename_grid_attrs_custom(ds, alt_lat_str): assert LAT_STR not in ds ds = ds.rename({alt_lat_str: 'custom_lat_name'}) ds = grid_attrs_to_aospy_names(ds, {LAT_STR: 'custom_lat_name'}) assert LAT_STR in ds assert 'custom_lat_name' not in ds