|
8 | 8 |
|
9 | 9 | import copernicusmarine |
10 | 10 |
|
11 | | -from virtualship.errors import ProblemsError |
12 | 11 | from virtualship.expedition.simulate_schedule import ( |
13 | 12 | MeasurementsToSimulate, |
14 | 13 | ScheduleProblem, |
@@ -155,50 +154,55 @@ def _run( |
155 | 154 | print("\nSimulating measurements. This may take a while...\n") |
156 | 155 |
|
157 | 156 | 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 | + ) |
173 | 172 | problem_simulator.execute( |
174 | 173 | problems, |
175 | 174 | instrument_type_validation=itype, |
176 | 175 | problems_dir=problems_dir, |
177 | 176 | ) |
178 | 177 |
|
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(): |
181 | 200 | 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 | | - ) |
196 | 201 |
|
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 |
202 | 206 |
|
203 | 207 | print("\nAll measurement simulations are complete.") |
204 | 208 |
|
|
0 commit comments