From 66abcbd58d132ba9f82c1bbfe0a1803ceeba9cb8 Mon Sep 17 00:00:00 2001 From: Rachel Wegener Date: Thu, 5 Aug 2021 12:53:22 -0500 Subject: [PATCH 1/5] adding cf convention helper functions --- cmip6_preprocessing/preprocessing.py | 8 ++++++++ tests/test_preprocessing.py | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/cmip6_preprocessing/preprocessing.py b/cmip6_preprocessing/preprocessing.py index 7306dedc..5ad9e064 100644 --- a/cmip6_preprocessing/preprocessing.py +++ b/cmip6_preprocessing/preprocessing.py @@ -109,6 +109,14 @@ def _maybe_rename(obj, rdict): # restore attributes ds.attrs = attrs + # use cf conventions where they can be inferred by cf_xarray + print('CONVERTING WITH CF CONVENION FUNCTIONS ---------') + print('-------------------------------') + print('-----------------------------') + ds = ds.cf.guess_coord_axis() + ds = ds.cf.add_canonical_attributes() + print('ds atrrs keys', ds.attrs.keys()) + return ds diff --git a/tests/test_preprocessing.py b/tests/test_preprocessing.py index 4e2a2a4d..943efe66 100644 --- a/tests/test_preprocessing.py +++ b/tests/test_preprocessing.py @@ -66,6 +66,10 @@ 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 + print('ds attrs keys from test', ds.attrs.keys()) + assert 'history' in ds.attrs.keys() @pytest.mark.parametrize("xname", ["i", "x"]) From 0f4dab9f9409ad795d7014bc9867acc1473e83b7 Mon Sep 17 00:00:00 2001 From: Rachel Wegener Date: Thu, 9 Sep 2021 10:52:04 -0400 Subject: [PATCH 2/5] refactor cf convention test --- cmip6_preprocessing/preprocessing.py | 14 +++++++------- tests/test_preprocessing.py | 6 ++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/cmip6_preprocessing/preprocessing.py b/cmip6_preprocessing/preprocessing.py index 5ad9e064..f41ee8f6 100644 --- a/cmip6_preprocessing/preprocessing.py +++ b/cmip6_preprocessing/preprocessing.py @@ -68,6 +68,11 @@ def _invert_dict(rdict): return exploded_dict +def _parse_cf(dataset): + dataset = dataset.cf.guess_coord_axis() + dataset = dataset.cf.add_canonical_attributes() + return dataset + def rename_cmip6(ds, rename_dict=None): """Homogenizes cmip6 dataasets to common naming""" ds = ds.copy() @@ -110,13 +115,8 @@ def _maybe_rename(obj, rdict): ds.attrs = attrs # use cf conventions where they can be inferred by cf_xarray - print('CONVERTING WITH CF CONVENION FUNCTIONS ---------') - print('-------------------------------') - print('-----------------------------') - ds = ds.cf.guess_coord_axis() - ds = ds.cf.add_canonical_attributes() - print('ds atrrs keys', ds.attrs.keys()) - + ds = _parse_cf(ds) + return ds diff --git a/tests/test_preprocessing.py b/tests/test_preprocessing.py index 943efe66..3d96ab15 100644 --- a/tests/test_preprocessing.py +++ b/tests/test_preprocessing.py @@ -68,8 +68,10 @@ def test_rename_cmip6(xname, yname, zname, missing_dim): assert zlen == len(ds_renamed.lev) # check if cf conventions were inferred - print('ds attrs keys from test', ds.attrs.keys()) - assert 'history' in ds.attrs.keys() + 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"]) From 22a305e9311e5d1f6b29c518ddc9ce8af03b3506 Mon Sep 17 00:00:00 2001 From: Rachel Wegener Date: Thu, 9 Sep 2021 11:02:05 -0400 Subject: [PATCH 3/5] add cf renaming cloud test --- tests/test_preprocessing_cloud.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_preprocessing_cloud.py b/tests/test_preprocessing_cloud.py index d48d1ef6..704c8247 100644 --- a/tests/test_preprocessing_cloud.py +++ b/tests/test_preprocessing_cloud.py @@ -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 From f5bc368e5537253bc684c775e0ac79b73aa6ed7c Mon Sep 17 00:00:00 2001 From: Rachel Wegener Date: Fri, 10 Sep 2021 13:41:00 -0400 Subject: [PATCH 4/5] remove cf.add_canonical_attributes() --- cmip6_preprocessing/preprocessing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cmip6_preprocessing/preprocessing.py b/cmip6_preprocessing/preprocessing.py index f41ee8f6..a4422e39 100644 --- a/cmip6_preprocessing/preprocessing.py +++ b/cmip6_preprocessing/preprocessing.py @@ -70,7 +70,6 @@ def _invert_dict(rdict): def _parse_cf(dataset): dataset = dataset.cf.guess_coord_axis() - dataset = dataset.cf.add_canonical_attributes() return dataset def rename_cmip6(ds, rename_dict=None): From 329f56e82577b200622d4760bdd4a3f5a2eed040 Mon Sep 17 00:00:00 2001 From: Rachel Wegener Date: Mon, 21 Mar 2022 10:29:46 -0400 Subject: [PATCH 5/5] superficial change to trigger checks --- cmip6_preprocessing/preprocessing.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cmip6_preprocessing/preprocessing.py b/cmip6_preprocessing/preprocessing.py index a4422e39..8b855429 100644 --- a/cmip6_preprocessing/preprocessing.py +++ b/cmip6_preprocessing/preprocessing.py @@ -115,7 +115,6 @@ def _maybe_rename(obj, rdict): # use cf conventions where they can be inferred by cf_xarray ds = _parse_cf(ds) - return ds