|
18 | 18 | from e3sm_diags.logger import _setup_child_logger |
19 | 19 | from e3sm_diags.plot import arm_diags_plot |
20 | 20 |
|
| 21 | + |
21 | 22 | if TYPE_CHECKING: |
22 | 23 | from e3sm_diags.parameter.arm_diags_parameter import ARMDiagsParameter |
23 | 24 |
|
@@ -278,6 +279,11 @@ def _run_diag_annual_cycle(parameter: ARMDiagsParameter) -> ARMDiagsParameter: |
278 | 279 | vars_funcs = _get_vars_funcs_for_derived_var(ds_ref, var) |
279 | 280 | target_var = list(vars_funcs.keys())[0][0] |
280 | 281 |
|
| 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 | + |
281 | 287 | ds_ref_climo = ds_ref.temporal.climatology(target_var, "month") |
282 | 288 | da_ref = vars_funcs[(target_var,)](ds_ref_climo[target_var]).rename( |
283 | 289 | var |
@@ -530,3 +536,33 @@ def _save_metrics_to_json(parameter: ARMDiagsParameter, metrics_dict: Dict[str, |
530 | 536 | json.dump(metrics_dict, outfile) |
531 | 537 |
|
532 | 538 | 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