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

Using cf_xarray to infer attributes #193

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions cmip6_preprocessing/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def _invert_dict(rdict):
return exploded_dict


def _parse_cf(dataset):
dataset = dataset.cf.guess_coord_axis()
return dataset

def rename_cmip6(ds, rename_dict=None):
"""Homogenizes cmip6 dataasets to common naming"""
ds = ds.copy()
Expand Down Expand Up @@ -109,6 +113,8 @@ def _maybe_rename(obj, rdict):
# restore attributes
ds.attrs = attrs

# use cf conventions where they can be inferred by cf_xarray
ds = _parse_cf(ds)
return ds


Expand Down
6 changes: 6 additions & 0 deletions tests/test_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def test_rename_cmip6(xname, yname, zname, missing_dim):
assert ylen == len(ds_renamed.y)
if not missing_dim == "z":
assert zlen == len(ds_renamed.lev)

# check if cf conventions were inferred
for dim, axis in [('x', 'X'), ('y', 'Y'), ('lev','Z'), ('time', 'T')]:
if dim in ds_renamed.dims:
#check that the axis is in the cf object
assert axis in ds_renamed.cf.axes.keys()


@pytest.mark.parametrize("xname", ["i", "x"])
Expand Down
6 changes: 6 additions & 0 deletions tests/test_preprocessing_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ def test_check_dim_coord_values_wo_intake(
if unit:
assert unit == expected_unit

# check if cf conventions were inferred
for dim, axis in [('x', 'X'), ('y', 'Y'), ('lev','Z'), ('time', 'T')]:
if dim in ds.dims:
#check that the axis is in the cf object
assert axis in ds.cf.axes.keys()


# this fixture has to be redifined every time to account for different fail cases for each test
@pytest.fixture
Expand Down