Skip to content

Commit b21463d

Browse files
committed
better handling of old problems/checkpoint files if simulation fails for unexpected errors
1 parent 650cb21 commit b21463d

3 files changed

Lines changed: 41 additions & 39 deletions

File tree

src/virtualship/cli/_run.py

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import copernicusmarine
1010

11-
from virtualship.errors import ProblemsError
1211
from virtualship.expedition.simulate_schedule import (
1312
MeasurementsToSimulate,
1413
ScheduleProblem,
@@ -155,50 +154,55 @@ def _run(
155154
print("\nSimulating measurements. This may take a while...\n")
156155

157156
for itype in instruments_in_expedition:
158-
# get instrument class
159-
instrument_class = get_instrument_class(itype)
160-
if instrument_class is None:
161-
raise RuntimeError(f"No instrument class found for type {itype}.")
162-
163-
# execute problem simulations for this instrument type
164-
if problems:
165-
# TODO: this print statement is helpful for user to see so it makes sense when a relevant instrument-related problem occurs; but ideally would be overwritten when the actual measurement simulation spinner starts (try and address this in future PR which improves log output)
166-
print(
167-
""
168-
if hasattr(problems["problem_class"][0], "pre_departure")
169-
and problems["problem_class"][0].pre_departure
170-
else f"\033[4mUp next\033[0m: {itype.name} measurements...\n"
171-
)
172-
try:
157+
try:
158+
# get instrument class
159+
instrument_class = get_instrument_class(itype)
160+
if instrument_class is None:
161+
raise RuntimeError(f"No instrument class found for type {itype}.")
162+
163+
# execute problem simulations for this instrument type
164+
if problems:
165+
# TODO: this print statement is helpful for user to see so it makes sense when a relevant instrument-related problem occurs; but ideally would be overwritten when the actual measurement simulation spinner starts (try and address this in future PR which improves log output)
166+
print(
167+
""
168+
if hasattr(problems["problem_class"][0], "pre_departure")
169+
and problems["problem_class"][0].pre_departure
170+
else f"\033[4mUp next\033[0m: {itype.name} measurements...\n"
171+
)
173172
problem_simulator.execute(
174173
problems,
175174
instrument_type_validation=itype,
176175
problems_dir=problems_dir,
177176
)
178177

179-
except Exception as e:
180-
os.removedirs(problems_dir) # clean up if fails
178+
# get measurements to simulate
179+
attr = MeasurementsToSimulate.get_attr_for_instrumenttype(itype)
180+
measurements = getattr(schedule_results.measurements_to_simulate, attr)
181+
182+
# initialise instrument
183+
instrument = instrument_class(
184+
expedition=expedition,
185+
from_data=Path(from_data) if from_data is not None else None,
186+
)
187+
188+
# execute simulation
189+
instrument.execute(
190+
measurements=measurements,
191+
out_path=expedition_dir.joinpath(
192+
"results", f"{itype.name.lower()}.zarr"
193+
),
194+
)
195+
except Exception as e:
196+
# clean up if unexpected error occurs
197+
if os.path.exists(problems_dir):
198+
shutil.rmtree(problems_dir)
199+
if expedition_dir.joinpath(CHECKPOINT).exists():
181200
os.remove(expedition_dir.joinpath(CHECKPOINT))
182-
raise ProblemsError(
183-
f"An error occurred while simulating problems: {e}. Please report this issue, with a description and the traceback, "
184-
"to the VirtualShip issue tracker at: https://github.com/OceanParcels/virtualship/issues"
185-
) from e
186-
187-
# get measurements to simulate
188-
attr = MeasurementsToSimulate.get_attr_for_instrumenttype(itype)
189-
measurements = getattr(schedule_results.measurements_to_simulate, attr)
190-
191-
# initialise instrument
192-
instrument = instrument_class(
193-
expedition=expedition,
194-
from_data=Path(from_data) if from_data is not None else None,
195-
)
196201

197-
# execute simulation
198-
instrument.execute(
199-
measurements=measurements,
200-
out_path=expedition_dir.joinpath("results", f"{itype.name.lower()}.zarr"),
201-
)
202+
raise RuntimeError(
203+
f"An unexpected error occurred while simulating measurements: {e}. Please report this issue, with a description and the traceback, "
204+
"to the VirtualShip issue tracker at: https://github.com/OceanParcels/virtualship/issues"
205+
) from e
202206

203207
print("\nAll measurement simulations are complete.")
204208

src/virtualship/models/checkpoint.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ def verify(self, expedition: Expedition, problems_dir: Path) -> None:
111111

112112
# problem at a later waypoint: check new scheduled time exceeds sail time + delay duration + instrument deployment time (rather whole delay duration add-on, as there may be _some_ contingency time already scheduled)
113113
else:
114-
breakpoint()
115114
failed_waypoint = new_schedule.waypoints[self.failed_waypoint_i]
116115

117116
scheduled_time = failed_waypoint.time - problem_waypoint.time

src/virtualship/utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,6 @@ def _calc_wp_stationkeeping_time(
650650
# get wp total stationkeeping time
651651
cumulative_stationkeeping_time = timedelta()
652652
for iconfig in wp_instrument_configs:
653-
breakpoint()
654653
if (
655654
both_ctd_and_bgc
656655
and iconfig.__class__.__name__

0 commit comments

Comments
 (0)