Skip to content

Commit cd6e212

Browse files
committed
Add a temporary workaround to rename nv bounds dim
1 parent e60e93a commit cd6e212

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

e3sm_diags/driver/arm_diags_driver.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from e3sm_diags.logger import _setup_child_logger
1919
from e3sm_diags.plot import arm_diags_plot
2020

21+
2122
if TYPE_CHECKING:
2223
from e3sm_diags.parameter.arm_diags_parameter import ARMDiagsParameter
2324

@@ -278,6 +279,11 @@ def _run_diag_annual_cycle(parameter: ARMDiagsParameter) -> ARMDiagsParameter:
278279
vars_funcs = _get_vars_funcs_for_derived_var(ds_ref, var)
279280
target_var = list(vars_funcs.keys())[0][0]
280281

282+
# NOTE: The bounds dimension can be "nv", which is not
283+
# currently recognized as a valid bounds dimension
284+
# by xcdat. We rename it to "bnds" to make it compatible.
285+
ds_ref = _rename_bounds_dim(ds_ref)
286+
281287
ds_ref_climo = ds_ref.temporal.climatology(target_var, "month")
282288
da_ref = vars_funcs[(target_var,)](ds_ref_climo[target_var]).rename(
283289
var
@@ -530,3 +536,33 @@ def _save_metrics_to_json(parameter: ARMDiagsParameter, metrics_dict: Dict[str,
530536
json.dump(metrics_dict, outfile)
531537

532538
logger.info(f"Metrics saved in: {abs_path}")
539+
540+
541+
def _rename_bounds_dim(ds: xr.Dataset) -> xr.Dataset:
542+
"""
543+
Renames the bounds dimension "nv" to "bnds" in the given xarray.Dataset for
544+
xCDAT compatibility.
545+
546+
This is a temporary workaround to ensure compatibility with xCDAT's bounds
547+
handling. The bounds dimension "nv" is commonly used in datasets to
548+
represent the number of vertices in a polygon, but xCDAT expects the
549+
bounds dimension to be in `xcdat.bounds.VALID_BOUNDS_DIMS`. This function
550+
renames "nv" to "bnds" to align with xCDAT's expectations.
551+
552+
Parameters
553+
----------
554+
ds : xr.Dataset
555+
The input xarray.Dataset which may contain a bounds dimension named "nv".
556+
557+
Returns
558+
-------
559+
xr.Dataset
560+
A new xarray.Dataset with the "nv" dimension renamed to "bnds" if it
561+
existed; otherwise, the original dataset copy.
562+
"""
563+
ds_new = ds.copy()
564+
565+
if "nv" in ds_new.dims:
566+
ds_new = ds_new.rename({"nv": "bnds"})
567+
568+
return ds_new

0 commit comments

Comments
 (0)