Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dropping bound coordinate values #183

Merged
merged 2 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmip6_preprocessing/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from cmip6_preprocessing.utils import _maybe_make_list


_drop_coords = ["bnds", "vertex"]


def cmip6_renaming_dict():
"""a universal renaming dict. Keys correspond to source id (model name)
and valuse are a dict of target name (key) and a list of variables that
Expand Down Expand Up @@ -441,7 +444,6 @@ def fix_metadata(ds):


def combined_preprocessing(ds):
ds = ds.copy()
# fix naming
ds = rename_cmip6(ds)
# promote empty dims to actual coordinates
Expand All @@ -462,4 +464,5 @@ def combined_preprocessing(ds):
ds = maybe_convert_bounds_to_vertex(ds)
ds = maybe_convert_vertex_to_bounds(ds)
ds = fix_metadata(ds)
ds = ds.drop_vars(_drop_coords, errors="ignore")
return ds
20 changes: 20 additions & 0 deletions tests/test_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,26 @@ def test_fix_metadata():
### Combination test - involving###


@pytest.mark.parametrize("add_coords", [True, False])
@pytest.mark.parametrize("shift", [0, 10])
def test_combined_preprocessing_dropped_coords(add_coords, shift):
"""Check if coordinates are properly dropped"""
# create a 2d dataset
xlen, ylen, zlen = (10, 5, 1)
ds = (
create_test_ds("x", "y", "dummy", xlen, ylen, zlen).squeeze().drop_vars("dummy")
)
x_bnds = xr.concat([ds.x, ds.x], "bnds")
ds = ds.assign_coords(x_bounds=x_bnds)

if add_coords:
ds = ds.assign_coords(bnds=np.arange(len(ds.bnds)) + shift)

ds = combined_preprocessing(ds)

assert "bnds" not in ds.coords


def test_combined_preprocessing_mislabeled_coords():
"""Test if the renaming is applied to datavariables and then if they are moved to the coords."""
# create a 2d dataset
Expand Down
8 changes: 7 additions & 1 deletion tests/test_preprocessing_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import xarray as xr

from cmip6_preprocessing.grids import combine_staggered_grid
from cmip6_preprocessing.preprocessing import combined_preprocessing
from cmip6_preprocessing.preprocessing import _drop_coords, combined_preprocessing
from cmip6_preprocessing.utils import google_cmip_col, model_id_match


Expand Down Expand Up @@ -221,6 +221,9 @@ def test_check_dim_coord_values_wo_intake(
# make sure lon and lat are 2d
assert len(ds.lon.shape) == 2
assert len(ds.lat.shape) == 2
for co in _drop_coords:
if co in ds.dims:
assert co not in ds.coords


# this fixture has to be redifined every time to account for different fail cases for each test
Expand Down Expand Up @@ -288,6 +291,9 @@ def test_check_dim_coord_values(
# make sure lon and lat are 2d
assert len(ds.lon.shape) == 2
assert len(ds.lat.shape) == 2
for co in _drop_coords:
if co in ds.dims:
assert co not in ds.coords


############################### Specific Bound Coords Test ###############################
Expand Down