Skip to content

Commit

Permalink
modified examples
Browse files Browse the repository at this point in the history
  • Loading branch information
schwemro committed Aug 7, 2023
1 parent 8003618 commit ede5d40
Show file tree
Hide file tree
Showing 27 changed files with 1,528 additions and 3,717 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ examples/*/*/*/RK4/
examples/*/*/hydrus_benchmark/*.txt
examples/*/*/hydrus_benchmark/*.py
input_examples/
examples/plot_scale/reckenholz/svat_crop_sobol
examples/plot_scale/reckenholz/svat_bromide
examples/plot_scale/reckenholz/svat_nitrate
examples/plot_scale/reckenholz/svat_crop_bromide
examples/plot_scale/reckenholz/svat_crop_nitrate
examples/plot_scale/reckenholz/svat_crop_bromide_sobol
examples/plot_scale/reckenholz/svat_crop_nitrate_sobol
examples/plot_scale/reckenholz/svat_crop_bromide_monte_carlo
examples/plot_scale/reckenholz/svat_crop_nitrate_monte_carlo
examples/plot_scale/reckenholz/svat_nitrate_monte_carlo
examples/plot_scale/reckenholz/input
examples/plot_scale/reckenholz/output
examples/plot_scale/reckenholz/observations
Expand Down
134 changes: 75 additions & 59 deletions examples/plot_scale/reckenholz/svat_crop/merge_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,81 @@
import h5netcdf
import datetime
import numpy as onp
import click
import roger

base_path = Path(__file__).parent

# identifiers for simulations
lys_experiments = ["lys1", "lys2", "lys3", "lys4", "lys8", "lys9", "lys2_bromide", "lys8_bromide", "lys9_bromide"]
@click.command("main")
def main():
base_path = Path(__file__).parent

# merge model output into single file
for lys_experiment in lys_experiments:
path = str(base_path.parent / "output" / "svat_monte_carlo" / f"SVAT_{lys_experiment}.*.nc")
output_hm_file = base_path.parent / "output" / "svat_monte_carlo" / f"SVAT_{lys_experiment}.nc"
if not os.path.exists(output_hm_file):
diag_files = glob.glob(path)
with h5netcdf.File(output_hm_file, 'w', decode_vlen_strings=False) as f:
f.attrs.update(
date_created=datetime.datetime.today().isoformat(),
title=f'RoGeR Monte Carlo simulations at Reckenholz lysimeter {lys_experiment}',
institution='University of Freiburg, Chair of Hydrology',
references='',
comment='First timestep (t=0) contains initial values. Simulations start are written from second timestep (t=1) to last timestep (t=N).',
model_structure='SVAT model with free drainage',
)
# collect dimensions
for dfs in diag_files:
with h5netcdf.File(dfs, 'r', decode_vlen_strings=False) as df:
f.attrs.update(
roger_version=df.attrs['roger_version']
)
# set dimensions with a dictionary
if not dfs.split('/')[-1].split('.')[1] == 'constant':
dict_dim = {'x': len(df.variables['x']), 'y': len(df.variables['y']), 'Time': len(df.variables['Time'])}
time = onp.array(df.variables.get('Time'))
for dfs in diag_files:
with h5netcdf.File(dfs, 'r', decode_vlen_strings=False) as df:
if not f.dimensions:
f.dimensions = dict_dim
v = f.create_variable('x', ('x',), float, compression="gzip", compression_opts=1)
v.attrs['long_name'] = 'model run'
v.attrs['units'] = ''
v[:] = onp.arange(dict_dim["x"])
v = f.create_variable('y', ('y',), float, compression="gzip", compression_opts=1)
v.attrs['long_name'] = ''
v.attrs['units'] = ''
v[:] = onp.arange(dict_dim["y"])
v = f.create_variable('Time', ('Time',), float, compression="gzip", compression_opts=1)
var_obj = df.variables.get('Time')
v.attrs.update(time_origin=var_obj.attrs["time_origin"],
units=var_obj.attrs["units"])
v[:] = time
for var_sim in list(df.variables.keys()):
var_obj = df.variables.get(var_sim)
if var_sim not in list(f.dimensions.keys()) and var_obj.ndim == 3 and var_obj.shape[0] > 2:
v = f.create_variable(var_sim, ('x', 'y', 'Time'), float, compression="gzip", compression_opts=1)
vals = onp.array(var_obj)
v[:, :, :] = vals.swapaxes(0, 2)
v.attrs.update(long_name=var_obj.attrs["long_name"],
units=var_obj.attrs["units"])
elif var_sim not in list(f.dimensions.keys()) and var_obj.ndim == 3 and var_obj.shape[0] <= 2:
v = f.create_variable(var_sim, ('x', 'y'), float, compression="gzip", compression_opts=1)
vals = onp.array(var_obj)
v[:, :] = vals.swapaxes(0, 2)[:, :, 0]
v.attrs.update(long_name=var_obj.attrs["long_name"],
units=var_obj.attrs["units"])
# identifiers for simulations
lys_experiments = ["lys1", "lys2", "lys3", "lys4", "lys8", "lys9", "lys2_bromide", "lys8_bromide", "lys9_bromide"]

# merge model output into single file
for lys_experiment in lys_experiments:
path = str(base_path.parent / "output" / "svat_monte_carlo" / f"SVAT_{lys_experiment}.*.nc")
output_hm_file = base_path.parent / "output" / "svat_monte_carlo" / f"SVAT_{lys_experiment}.nc"
if not os.path.exists(output_hm_file):
diag_files = glob.glob(path)
with h5netcdf.File(output_hm_file, "w", decode_vlen_strings=False) as f:
f.attrs.update(
date_created=datetime.datetime.today().isoformat(),
title=f"RoGeR Monte Carlo simulations at Reckenholz lysimeter {lys_experiment}",
institution="University of Freiburg, Chair of Hydrology",
references="",
comment="First timestep (t=0) contains initial values. Simulations start are written from second timestep (t=1) to last timestep (t=N).",
model_structure="SVAT model with free drainage",
roger_version=f"{roger.__version__}",
)
# collect dimensions
for dfs in diag_files:
with h5netcdf.File(dfs, "r", decode_vlen_strings=False) as df:
f.attrs.update(roger_version=df.attrs["roger_version"])
# set dimensions with a dictionary
if not dfs.split("/")[-1].split(".")[1] == "constant":
dict_dim = {
"x": len(df.variables["x"]),
"y": len(df.variables["y"]),
"Time": len(df.variables["Time"]),
}
time = onp.array(df.variables.get("Time"))
for dfs in diag_files:
with h5netcdf.File(dfs, "r", decode_vlen_strings=False) as df:
if not f.dimensions:
f.dimensions = dict_dim
v = f.create_variable("x", ("x",), float, compression="gzip", compression_opts=1)
v.attrs["long_name"] = "model run"
v.attrs["units"] = ""
v[:] = onp.arange(dict_dim["x"])
v = f.create_variable("y", ("y",), float, compression="gzip", compression_opts=1)
v.attrs["long_name"] = ""
v.attrs["units"] = ""
v[:] = onp.arange(dict_dim["y"])
v = f.create_variable("Time", ("Time",), float, compression="gzip", compression_opts=1)
var_obj = df.variables.get("Time")
v.attrs.update(time_origin=var_obj.attrs["time_origin"], units=var_obj.attrs["units"])
v[:] = time
for var_sim in list(df.variables.keys()):
var_obj = df.variables.get(var_sim)
if var_sim not in list(f.dimensions.keys()) and var_obj.ndim == 3 and var_obj.shape[0] > 2:
v = f.create_variable(
var_sim, ("x", "y", "Time"), float, compression="gzip", compression_opts=1
)
vals = onp.array(var_obj)
v[:, :, :] = vals.swapaxes(0, 2)
v.attrs.update(long_name=var_obj.attrs["long_name"], units=var_obj.attrs["units"])
elif (
var_sim not in list(f.dimensions.keys()) and var_obj.ndim == 3 and var_obj.shape[0] <= 2
):
v = f.create_variable(
var_sim, ("x", "y"), float, compression="gzip", compression_opts=1
)
vals = onp.array(var_obj)
v[:, :] = vals.swapaxes(0, 2)[:, :, 0]
v.attrs.update(long_name=var_obj.attrs["long_name"], units=var_obj.attrs["units"])
return


if __name__ == "__main__":
main()
Loading

0 comments on commit ede5d40

Please sign in to comment.