Ribasim
Ribasim copied to clipboard
Improve run_delwaq
Reproduce
- Download this ribasim-delwaq schematization (the basic example from docs): https://we.tl/t-inUYMigfZv
- run this code, make sure model_dir points to the delwaq sub-dir
from ribasim.delwaq import run_delwaq
from pathlib import Path
model_dir = Path(r"d:\repositories\ribasim_delwaq_aam\data\basic_delwaq\delwaq")
run_delwaq(model_dir=model_dir, d3d_home=Path(r"c:\Program Files\Deltares\D-HYDRO Suite 2025.02 1D2D\plugins\DeltaShell.Dimr\kernels\x64"))
- Notice:
- No delwaq_map.nc is generated, that seems to be necessary to run ribasim.delwaq.parse() later
- No logging to console (Jupyter Interactive Window in this case)
Suggested improvements
This code will use dimr_config.xml, create the delwaq_map.nc and log to console:
import os
import subprocess
import sys
from collections import namedtuple
from pathlib import Path
RunSpecs = namedtuple("RunSpecs", ["exit_code"])
def run_delwaq(dimr_config: Path, run_dimr_bat: Path):
"""To run a Delwaq model
Args:
dimr_config (Path): path to dimr_config.xml
run_dimr_bat (Path): path to run_dimr.bat as part of your DHydro installation
"""
dimr_config = dimr_config.absolute().resolve()
run_dimr_bat = run_dimr_bat.absolute().resolve()
env = os.environ.copy()
args = [run_dimr_bat.as_posix(), dimr_config.as_posix()]
proc = subprocess.Popen(
args,
cwd=dimr_config.parent.as_posix(),
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=1,
universal_newlines=True,
encoding="utf-8",
)
with proc:
for line in proc.stdout:
print(line, end="") # Standard line
sys.stdout.flush() # Flush to Jupyter outs = None
return RunSpecs(proc.returncode)
At least on main I do get a delwaq_map.nc out of Delwaq with your example. Could also be related to me using a different Delwaq release? I use:
DELWAQ Version 7.0-c157fbb752a7c3b24c15c95e665b274b10b07d7b, Jul 18 2024, 20:02:39
In your files I see in delwaq.lst:
found -p command line switch
error opening nefis file(s):c:\Program.dat
files do not exist
ERROR: Could not read the process definition file.
Check if the filename after -p is correct, and exists.
Use -np if you want to run without processes.
The proposed logging seems fine to me, but do you have a good reason for wanting to switch from run_delwaq.bat delwaq.inp to run_dimr.bat? The latter seems more direct, and I think we chose it for a reason.