Skip to content

Commit 3c23366

Browse files
authored
Cache: added shorthand for model ds attributes (#345)
* Cache: added shorthand for model ds attributes * Update cache.py * Please Codacy * Update cache.py * Treat transport flag as required model ds attribute * Cache: Exclude "created_on" attr
1 parent 73a550f commit 3c23366

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

nlmod/cache.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def cache_netcdf(
5757
coords_2d=False,
5858
coords_3d=False,
5959
coords_time=False,
60+
attrs_ds=False,
6061
datavars=None,
6162
coords=None,
6263
attrs=None,
@@ -101,6 +102,8 @@ def cache_netcdf(
101102
Shorthand for adding 3D coordinates. The default is False.
102103
coords_time : bool, optional
103104
Shorthand for adding time coordinates. The default is False.
105+
attrs_ds : bool, optional
106+
Shorthand for adding model dataset attributes. The default is False.
104107
datavars : list, optional
105108
List of data variables to check for. The default is an empty list.
106109
coords : list, optional
@@ -139,6 +142,7 @@ def wrapper(*args, cachedir=None, cachename=None, **kwargs):
139142
coords_2d=coords_2d,
140143
coords_3d=coords_3d,
141144
coords_time=coords_time,
145+
attrs_ds=attrs_ds,
142146
datavars=datavars,
143147
coords=coords,
144148
attrs=attrs,
@@ -156,6 +160,7 @@ def wrapper(*args, cachedir=None, cachename=None, **kwargs):
156160
coords_2d=coords_2d,
157161
coords_3d=coords_3d,
158162
coords_time=coords_time,
163+
attrs_ds=attrs_ds,
159164
datavars=datavars,
160165
coords=coords,
161166
attrs=attrs,
@@ -632,6 +637,7 @@ def ds_contains(
632637
coords_2d=False,
633638
coords_3d=False,
634639
coords_time=False,
640+
attrs_ds=False,
635641
datavars=None,
636642
coords=None,
637643
attrs=None,
@@ -650,6 +656,8 @@ def ds_contains(
650656
Shorthand for adding 3D coordinates. The default is False.
651657
coords_time : bool, optional
652658
Shorthand for adding time coordinates. The default is False.
659+
attrs_ds : bool, optional
660+
Shorthand for adding model dataset attributes. The default is False.
653661
datavars : list, optional
654662
List of data variables to check for. The default is an empty list.
655663
coords : list, optional
@@ -666,7 +674,10 @@ def ds_contains(
666674
if ds is None:
667675
msg = "No dataset provided"
668676
raise ValueError(msg)
669-
if not coords_2d and not coords_3d and not datavars and not coords and not attrs:
677+
isdefault_args = not any(
678+
[coords_2d, coords_3d, coords_time, attrs_ds, datavars, coords, attrs]
679+
)
680+
if isdefault_args:
670681
return ds
671682

672683
isvertex = ds.attrs["gridtype"] == "vertex"
@@ -699,7 +710,9 @@ def ds_contains(
699710
datavars.remove("delc")
700711

701712
if "angrot" in ds.attrs:
702-
attrs.append("angrot")
713+
# set by `nlmod.base.to_model_ds()` and `nlmod.dims.resample._set_angrot_attributes()`
714+
attrs_angrot_required = ["angrot", "xorigin", "yorigin"]
715+
attrs.extend(attrs_angrot_required)
703716

704717
if coords_3d:
705718
coords.append("layer")
@@ -712,6 +725,11 @@ def ds_contains(
712725
datavars.append("nstp")
713726
datavars.append("tsmult")
714727

728+
if attrs_ds:
729+
# set by `nlmod.base.to_model_ds()` and `nlmod.base.set_ds_attrs()`, excluding "created_on"
730+
attrs_ds_required = ["model_name", "mfversion", "exe_name", "model_ws", "figdir", "cachedir", "transport"]
731+
attrs.extend(attrs_ds_required)
732+
715733
# User-friendly error messages if missing from ds
716734
if "northsea" in datavars and "northsea" not in ds.data_vars:
717735
msg = "Northsea not in dataset. Run nlmod.read.rws.add_northsea() first."
@@ -721,7 +739,7 @@ def ds_contains(
721739
if "time" not in ds.coords:
722740
msg = "time not in dataset. Run nlmod.time.set_ds_time() first."
723741
raise ValueError(msg)
724-
742+
725743
# Check if time-coord is complete
726744
time_attrs_required = ["start", "time_units"]
727745

@@ -731,6 +749,12 @@ def ds_contains(
731749
"Run nlmod.time.set_ds_time() to set time."
732750
raise ValueError(msg)
733751

752+
if attrs_ds:
753+
for attr in attrs_ds_required:
754+
if attr not in ds.attrs:
755+
msg = f"{attr} not in dataset.attrs. Run nlmod.set_ds_attrs() first."
756+
raise ValueError(msg)
757+
734758
# User-unfriendly error messages
735759
for datavar in datavars:
736760
if datavar not in ds.data_vars:

0 commit comments

Comments
 (0)