Skip to content

Commit

Permalink
Updating gotm tests expected values and regen script
Browse files Browse the repository at this point in the history
  • Loading branch information
wathen committed May 3, 2024
1 parent 74e7000 commit 46345d1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion github-actions/gotm-fabm-ersem/expected.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import argparse
import json
import netCDF4 as nc
from numpy import ndarray
from numpy import ndarray, interp

class NumpyEncoder(json.JSONEncoder):
def default(self, obj):
Expand Down Expand Up @@ -36,17 +36,40 @@ def default(self, obj):
"G2_o_deep" , "G3_c" , "ben_nit_G4n" , "H1_c" , "H2_c" , "Y2_c" , "Y3_c" ,
"Y4_c"]

data = nc.Dataset(data_path, 'r')
expected_results = {}
for v in state_vars:
if data.variables[v].ndim == 4:
expected_results[v] = data.variables[v][:].squeeze()[-1, :]
elif data.variables[v].ndim == 3:
expected_results[v] = float(data.variables[v][:].squeeze()[-1])
else:
raise RuntimeError
gotm_vars_test = ["dates", "N1_p", "N3_n", "N5_s"]

data_dict = {"expected": gotm_vars_test, "expected_state": state_vars}

with open('data.json', 'w') as f:
json.dump(expected_results, f, cls=NumpyEncoder)
for key, items in data_dict.items():
data = nc.Dataset(data_path, 'r')
expected_results = {}
for v in items:
if key == "expected":
if v == "dates":
times = data.variables['time']
dates = nc.num2date(times[:],
units=times.units,
calendar=times.calendar)
dates = [str(d).split(" ")[0] for d in dates]
expected_results[v] = dates
else:
depth = 0.0
var = data.variables[v]
zi = data.variables['zi'][:].squeeze()
z = data.variables['z'][:].squeeze()
var_time_series = []
for i in range(var.shape[0]):
depth_offset = depth + zi[i, -1]
var_time_series.append(interp(depth_offset, z[i, :], var[i, :].squeeze()))
expected_results[v] = var_time_series

elif data.variables[v].ndim == 4:
expected_results[v] = data.variables[v][:].squeeze()[-1, :]
elif data.variables[v].ndim == 3:
expected_results[v] = float(data.variables[v][:].squeeze()[-1])
else:
raise RuntimeError

with open(f'{key}.json', 'w') as f:
json.dump(expected_results, f, cls=NumpyEncoder)

0 comments on commit 46345d1

Please sign in to comment.