Skip to content

Commit 40ed1f4

Browse files
authored
Merge pull request #58 from NOC-MSM/57-feature-automated-iperio-nftype-and-name-discovery-during-nemodatatree-creation
57 feature automated iperio nftype and name discovery during nemodatatree creation
2 parents 6a87619 + 34ea4ca commit 40ed1f4

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

nemo_cookbook/nemodatatree.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def from_icechunk(
442442
cls,
443443
repo: icechunk.repository.Repository,
444444
name: str = "NEMO model",
445-
iperio: bool = False,
445+
iperio: bool | None = None,
446446
nftype: str | None = None,
447447
open_kwargs: dict[str, any] | None = None,
448448
**session_kwargs: dict[str, any],
@@ -460,11 +460,15 @@ def from_icechunk(
460460
name : str, optional
461461
Name of the NEMODataTree. Default is "NEMO model".
462462
463-
iperio: bool = False
464-
Zonal periodicity of the parent domain. Default is False.
463+
iperio: bool, optional
464+
Zonal periodicity of the parent domain. Default is None, meaning iperio
465+
will be inferred from the root group global attributes, otherwise False.
465466
466467
nftype: str, optional
467-
Type of north fold lateral boundary condition to apply. Options are 'T' for T-point pivot or 'F' for F-point
468+
Type of north fold lateral boundary condition to apply. Default is None,
469+
meaning nftype will be inferred from the root group global attributes,
470+
otherwise None. Options are 'T' for T-point pivot, 'F' for F-point or
471+
None.
468472
469473
open_kwargs : dict[str, Any], optional
470474
Additional keyword arguments to pass to `xarray.open_datatree`.
@@ -494,7 +498,7 @@ def from_icechunk(
494498
raise TypeError("`repo` must implement readonly_session().")
495499
if not isinstance(name, str):
496500
raise TypeError("`name` must be a string.")
497-
if not isinstance(iperio, bool):
501+
if iperio is not None and not isinstance(iperio, bool):
498502
raise TypeError("zonal periodicity (`iperio`) of parent domain must be a boolean.")
499503
if nftype is not None and nftype not in ("T", "F"):
500504
raise ValueError(
@@ -507,8 +511,10 @@ def from_icechunk(
507511
nemo = super().from_dict(datatree.to_dict())
508512

509513
# -- Update NEMODataTree properties -- #
510-
nemo["/"].attrs.update({"nftype": nftype, "iperio": iperio})
511-
nemo.name = name
514+
nemo["/"].attrs.update({"nftype": nftype or datatree["/"].attrs.get("nftype", None),
515+
"iperio": iperio or datatree["/"].attrs.get("iperio", False)
516+
})
517+
nemo.name = name or datatree["/"].attrs.get("name", None)
512518

513519
# -- Validate NEMO grid node Datasets -- #
514520
for key in [grid for grid in nemo.groups if grid.startswith("grid")]:
@@ -521,7 +527,7 @@ def from_zarr(
521527
cls,
522528
store: str,
523529
name: str = "NEMO model",
524-
iperio: bool = False,
530+
iperio: bool | None = None,
525531
nftype: str | None = None,
526532
**open_kwargs: dict[str, any],
527533
) -> Self:
@@ -536,12 +542,16 @@ def from_zarr(
536542
537543
name : str, optional
538544
Name of the NEMODataTree. Default is "NEMO model".
539-
540-
iperio: bool = False
541-
Zonal periodicity of the parent domain. Default is False.
545+
546+
iperio: bool, optional
547+
Zonal periodicity of the parent domain. Default is None, meaning iperio
548+
will be inferred from the root group global attributes, otherwise False.
542549
543550
nftype: str, optional
544-
Type of north fold lateral boundary condition to apply. Options are 'T' for T-point pivot or 'F' for F-point
551+
Type of north fold lateral boundary condition to apply. Default is None,
552+
meaning nftype will be inferred from the root group global attributes,
553+
otherwise None. Options are 'T' for T-point pivot, 'F' for F-point or
554+
None.
545555
546556
**open_kwargs : dict[str, Any], optional
547557
Additional keyword arguments to pass to `xarray.open_datatree`.
@@ -568,7 +578,7 @@ def from_zarr(
568578
raise TypeError("`store` must be a string.")
569579
if not isinstance(name, str):
570580
raise TypeError("`name` must be a string.")
571-
if not isinstance(iperio, bool):
581+
if iperio is not None and not isinstance(iperio, bool):
572582
raise TypeError("zonal periodicity (`iperio`) of parent domain must be a boolean.")
573583
if nftype is not None and nftype not in ("T", "F"):
574584
raise ValueError(
@@ -580,8 +590,10 @@ def from_zarr(
580590
nemo = super().from_dict(datatree.to_dict())
581591

582592
# -- Update NEMODataTree properties -- #
583-
nemo["/"].attrs.update({"nftype": nftype, "iperio": iperio})
584-
nemo.name = name
593+
nemo["/"].attrs.update({"nftype": nftype or datatree["/"].attrs.get("nftype", None),
594+
"iperio": iperio or datatree["/"].attrs.get("iperio", False)
595+
})
596+
nemo.name = name or datatree["/"].attrs.get("name", None)
585597

586598
# -- Validate NEMO grid node Datasets -- #
587599
for key in [grid for grid in nemo.groups if grid.startswith("grid")]:

0 commit comments

Comments
 (0)