Skip to content

Commit 81ba042

Browse files
committed
Add parse_rv to extract command value from RV file. Use this to get the RunName in run function to fix the overwrite logic.
1 parent 638fed9 commit 81ba042

5 files changed

Lines changed: 46 additions & 20 deletions

File tree

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Changelog
55
v0.18.3 (unreleased)
66
--------------------
77

8+
New features
9+
^^^^^^^^^^^^
10+
* Added `parsers.parse_rv` to extract a Command value from an RV file.
11+
812
Bug fixes
913
^^^^^^^^^
1014
* Fixed bug affecting `GriddedForcing`, where `station_idx` in the call to ``nc_specs`` was set to ``1`` instead of ``None``. (PR #501)

src/ravenpy/config/parsers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,26 @@
1010
from .conventions import RAVEN_OUTPUT_FMT
1111

1212

13+
def parse_rv(rv: str, command: str) -> str:
14+
"""Parse an RV file to find the value of a Command (one liner).
15+
16+
Parameters
17+
----------
18+
rv : str
19+
The Raven command string, e.g. "RunName".
20+
command : str
21+
Valid Raven command.
22+
23+
Returns
24+
-------
25+
str:
26+
Command value. None if not found.
27+
"""
28+
pat = re.compile(rf":{command}\s+(.+)")
29+
if match := re.search(pat, rv):
30+
return match.groups()[0]
31+
32+
1333
def parse_diagnostics(fn: Path):
1434
"""Return dictionary of performance metrics."""
1535
import csv

src/ravenpy/ravenpy.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ def run(self, overwrite: bool = False) -> "OutputReader":
6464
overwrite : bool
6565
If True, overwrite existing files.
6666
"""
67-
if not (self.workdir / f"{self.modelname}.rvi").exists():
68-
# FIXME: No attribute 'write_rv' on Emulator [attribute-error]
69-
self.write_rv(overwrite=overwrite)
70-
7167
self._output_path = run(
7268
self.modelname, self.workdir, "output", overwrite=overwrite
7369
)
@@ -296,21 +292,26 @@ def run(
296292
if not outputdir.is_absolute():
297293
outputdir = (configdir / outputdir).absolute()
298294

299-
if modelname is None:
300-
results = [f for f in outputdir.rglob(f"*.*") if "_" not in f.stem]
301-
else:
302-
results = [f for f in outputdir.rglob(f"{modelname}_*.*")]
303-
if len(results) > 0:
304-
if overwrite:
305-
for f in results:
306-
Path(f).unlink()
307-
else:
308-
msg = f"Output files already exist in {outputdir}. Use `overwrite=True` to overwrite."
309-
raise FileExistsError(msg)
310-
311295
if not outputdir.exists():
312296
Path(str(outputdir)).mkdir(parents=True)
313297

298+
# Parse RunName
299+
rvi = (configdir / f"{modelname}.rvi").read_text()
300+
run_name = parsers.parse_rv(rvi, "RunName") or ""
301+
302+
# Existing output files with the same :RunName - they would be overwritten
303+
files = outputdir.glob(f"{run_name}*.*")
304+
305+
# Remove existing output files if overwrite is True
306+
for f in files:
307+
if f.is_file():
308+
if overwrite:
309+
f.unlink()
310+
else:
311+
raise FileExistsError(
312+
"Output files using this `modelname` already exist. Use `overwrite=True` to remove them."
313+
)
314+
314315
# Launch executable, wait for completion.
315316
cmd = [RAVEN_EXEC_PATH, modelname, "-o", str(outputdir)]
316317

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
)
2626

2727
RAVEN_TESTING_DATA_BRANCH = os.getenv("RAVEN_TESTING_DATA_BRANCH", "master")
28-
SKIP_TEST_DATA = os.getenv("RAVENPY_SKIP_TEST_DATA")
28+
SKIP_TEST_DATA = True # os.getenv("RAVENPY_SKIP_TEST_DATA")
2929
DEFAULT_CACHE = Path(_default_cache_dir)
3030

3131

tests/test_emulators.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,12 @@ def test_run(numeric_config, tmp_path):
5959
assert isinstance(out.storage, xr.Dataset)
6060

6161

62-
def test_run_overwrite(numeric_config, tmp_path):
62+
def test_run_overwrite(gr4jcn_config, tmp_path):
6363
"""Test that the emulator actually runs and returns the expected NSE."""
64-
name, conf = numeric_config
64+
gr4jcn, params = gr4jcn_config
65+
gr4jcn = gr4jcn.set_params(params)
6566

66-
e = Emulator(config=conf, workdir=tmp_path)
67+
e = Emulator(config=gr4jcn, workdir=tmp_path)
6768
e.run()
6869

6970
with pytest.raises(FileExistsError):

0 commit comments

Comments
 (0)