Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions src/mswm/utils/checkpoint_restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,14 @@ def checkpoint_restart(
logger.critical(msg)
raise FileNotFoundError(msg)

# Validate checkpoint state path exists
if not checkpoint_state.exists():
msg = f"Checkpoint state path does not exist: {checkpoint_state}"
logger.critical(msg)
raise FileNotFoundError(msg)

# Fild realization file in the destination folder
realization_files = list(dst.rglob("*realization*.json"))
if not realization_files:
msg = f"No realization file found in destination folder: {dst}"
logger.critical(msg)
raise FileNotFoundError(msg)
if len(realization_files) > 1:
logger.warning("More than one realization file found in source folder")
realization_file = realization_files[0]

# Read realization file
Expand All @@ -84,7 +80,16 @@ def checkpoint_restart(
except json.JSONDecodeError as e:
msg = f"Error parsing realization file: {realization_file}\n{e}"
logger.critical(msg)
raise json.JSONDecodeError(msg) from e
raise ValueError(msg) from e

# Check that run used NetCDF catchment output writing
output_format = real_config.get("output_format", [])
if output_format == ["CSV"]:
msg = "output_format is set to ['CSV'] in realization file. Run cannot be restarted as catchment outputs will be incomplete."
logger.critical(msg)
raise ValueError(msg)
elif "NetCDF" in output_format and "CSV" in output_format:
logger.warning("output_format includes both 'NetCDF' and 'CSV' in realization file. CSV catchment outputs will be incomplete for restarted run.")

# Build checkpoint state loading configuration
load_config = {
Expand Down
28 changes: 28 additions & 0 deletions tests/test_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def checkpoint_run_folder(region_build, tmp_path):
state.mkdir(exist_ok=True)
(state / "cat-1_state").write_text("{}")

# Ensure output_format doesn't trigger CSV-only validation error
real_file = Path(region_build.realization_file)
with open(real_file) as f:
data = json.load(f)
data["output_format"] = ["NetCDF"]
with open(real_file, "w") as f:
json.dump(data, f)

checkpoint_restart(str(src), str(dst))
real_file = list(dst.rglob("*realization*.json"))[0]
with open(real_file) as f:
Expand Down Expand Up @@ -189,3 +197,23 @@ def test_no_realization_file_raises(self, tmp_path):
str(empty_src),
str(tmp_path / "dst2")
)

def test_csv_output_format_raises(self, region_build, tmp_path):
real_file = Path(region_build.realization_file)
with open(real_file) as f:
data = json.load(f)
data["output_format"] = ["CSV"]
with open(real_file, "w") as f:
json.dump(data, f)

src = Path(region_build.work_dir)
state = src / "checkpoint"
state.mkdir(exist_ok=True)
(state / "cat-1_state").write_text("{}")

dst = tmp_path / "dst_csv_only"

with pytest.raises(ValueError):
checkpoint_restart(
str(src), str(dst)
)
Loading