Skip to content

Commit 3ee00ad

Browse files
committed
Add function to add CF axis attr to Z axis
- This is a temporary workaround for variables that have Z axes without CF axis attr, which is required for xCDAT to map to for bounds
1 parent 22cd0f5 commit 3ee00ad

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

e3sm_diags/driver/utils/dataset_xr.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,11 @@ def _get_climo_dataset(self, season: str) -> xr.Dataset:
393393
filepath = self._get_climo_filepath(season)
394394
ds = self._open_climo_dataset(filepath)
395395

396+
# Add CF attributes to Z axes if they are missing.
397+
# NOTE: This is a temporary workaround for xCDAT.
398+
# Refer to https://github.com/xCDAT/xcdat/pull/708
399+
ds = self._add_cf_attrs_to_z_axes(ds)
400+
396401
if self.var in self.derived_vars_map:
397402
ds = self._get_dataset_with_derived_climo_var(ds)
398403
elif self.var in ds.data_vars.keys():
@@ -408,6 +413,32 @@ def _get_climo_dataset(self, season: str) -> xr.Dataset:
408413

409414
return ds
410415

416+
def _add_cf_attrs_to_z_axes(self, ds: xr.Dataset) -> xr.Dataset:
417+
"""Add CF attributes to the Z axes of the dataset.
418+
419+
This method is a temporary solution to enable xCDAT to properly
420+
retrieve bounds for Z axes that do not have CF attributes, which
421+
is required for downstream regridding operations.
422+
423+
Parameters
424+
----------
425+
ds : xr.Dataset
426+
The dataset.
427+
428+
Returns
429+
-------
430+
xr.Dataset
431+
The dataset with CF attributes added to the Z axes.
432+
"""
433+
dim = xc.get_dim_keys(ds, axis="Z")
434+
435+
axis_attr = ds[dim].attrs.get("axis")
436+
437+
if axis_attr is None:
438+
ds[dim].attrs["axis"] = "Z"
439+
440+
return ds
441+
411442
def _open_climo_dataset(self, filepath: str) -> xr.Dataset:
412443
"""Open a climatology dataset.
413444
@@ -449,7 +480,7 @@ def _open_climo_dataset(self, filepath: str) -> xr.Dataset:
449480
args = {
450481
"paths": filepath,
451482
"decode_times": True,
452-
"add_bounds": ["X", "Y"],
483+
"add_bounds": ["X", "Y", "Z"],
453484
"coords": "minimal",
454485
"compat": "override",
455486
}

0 commit comments

Comments
 (0)