diff --git a/src/mswm/utils/checkpoint_restart.py b/src/mswm/utils/checkpoint_restart.py index 8d26d8b0..943476f9 100644 --- a/src/mswm/utils/checkpoint_restart.py +++ b/src/mswm/utils/checkpoint_restart.py @@ -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 @@ -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 = { diff --git a/tests/test_checkpoint.py b/tests/test_checkpoint.py index 13b07754..a13655df 100644 --- a/tests/test_checkpoint.py +++ b/tests/test_checkpoint.py @@ -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: @@ -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) + )