Skip to content

Commit eefd6ec

Browse files
committed
bleh
1 parent e8875b7 commit eefd6ec

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

regional_mom6/regional_mom6.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
find_files_by_pattern,
2828
try_pint_convert,
2929
)
30+
from regional_mom6.validate import validate_obc_file, validate_general_file
3031

3132
warnings.filterwarnings("ignore")
3233

@@ -1326,8 +1327,29 @@ def setup_initial_condition(
13261327
self.ic_tracers = tracers_out
13271328
self.ic_vels = vel_out
13281329

1329-
print("done setting up initial condition.")
1330-
1330+
validate_general_file(
1331+
eta_out,
1332+
["eta"],
1333+
{
1334+
"eta_t": {"_FillValue": None},
1335+
},
1336+
)
1337+
validate_general_file(
1338+
tracers_out,
1339+
["temp", "salt"],
1340+
encoding={
1341+
"temp": {"_FillValue": -1e20, "missing_value": -1e20},
1342+
"salt": {"_FillValue": -1e20, "missing_value": -1e20},
1343+
},
1344+
)
1345+
validate_general_file(
1346+
vel_out,
1347+
["u", "v"],
1348+
{
1349+
"u": {"_FillValue": netCDF4.default_fillvals["f4"]},
1350+
"v": {"_FillValue": netCDF4.default_fillvals["f4"]},
1351+
},
1352+
)
13311353
return
13321354

13331355
def get_glorys(self, raw_boundaries_path):

regional_mom6/validate.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,6 @@ def validate_obc_file(
7575
if encoding_dict is None:
7676
encoding_dict = {}
7777
ds = get_file(file)
78-
79-
print(
80-
"This function identifies variables by if they have the word 'segment' in the name and don't start with nz,dz,lon,lat."
81-
)
82-
8378
for var in variable_names:
8479

8580
# check variable name format
@@ -114,5 +109,30 @@ def validate_obc_file(
114109
)
115110

116111

112+
def validate_general_file(
113+
file, variable_names: list, encoding_dict=None, surface_var="eta"
114+
):
115+
"""Validate initial condition file and other domain spanning files"""
116+
if encoding_dict is None:
117+
encoding_dict = {}
118+
ds = get_file(file)
119+
120+
for var in variable_names:
121+
122+
# Add encodings
123+
if var in encoding_dict:
124+
for key, value in encoding_dict[var].items():
125+
ds[var].attrs[key] = value
126+
127+
# Check if there is a non-NaN fill value
128+
_check_fill_value(ds[var])
129+
130+
# check coordinates
131+
_check_coordinates(ds, var_name=var)
132+
133+
# Check the correct number of dimensions
134+
_check_required_dimensions(ds[var], surface=(var == surface_var))
135+
136+
117137
def ends_with_3_digits(s: str) -> bool:
118138
return bool(re.search(r"_\d{3}$", s))

tests/test_validate.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ def test_validate_obc_file_valid(caplog):
138138
ds[var].attrs["_FillValue"] = -999.0
139139
ds[var].attrs["coordinates"] = "lon lat"
140140

141-
validate_obc_file(ds, ["temp_segment_001"], surface_var="eta_segment_001")
141+
validate_obc_file(
142+
ds, ["temp_segment_001", "eta_segment_001"], surface_var="eta_segment_001"
143+
)
142144

143145

144146
def test_validate_obc_file_issues(caplog):
@@ -179,3 +181,25 @@ def test_validate_obc_file_encoding_dict():
179181
validate_obc_file(ds, ["temp_segment_001"], encoding_dict=encoding_dict)
180182

181183
assert ds["temp_segment_001"].attrs["units"] == "celsius"
184+
185+
186+
def test_validate_general_file_valid(caplog):
187+
"""Valid general file with all required attributes passes"""
188+
ds = xr.Dataset(
189+
{
190+
"temp": (["time", "z", "x", "y"], np.random.rand(2, 3, 4, 5)),
191+
"dz_temp": (
192+
["time", "z", "x", "y"],
193+
np.random.rand(2, 3, 4, 5),
194+
),
195+
"eta": (["time", "x", "y"], np.random.rand(2, 4, 5)),
196+
"lon": (["x", "y"], np.random.rand(4, 5)),
197+
"lat": (["x", "y"], np.random.rand(4, 5)),
198+
}
199+
)
200+
201+
for var in ds.data_vars:
202+
ds[var].attrs["_FillValue"] = -999.0
203+
ds[var].attrs["coordinates"] = "lon lat"
204+
205+
validate_obc_file(ds, ["temp", "eta"], surface_var="eta")

0 commit comments

Comments
 (0)