Skip to content

Commit 19b2077

Browse files
committed
update simulation, documentation and tests with revived ship_config
1 parent 5f71ec0 commit 19b2077

File tree

9 files changed

+28
-27
lines changed

9 files changed

+28
-27
lines changed

docs/user-guide/quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ virtualship init EXPEDITION_NAME --from-mfp CoordinatesExport.xlsx
4646
The `CoordinatesExport.xlsx` in the `virtualship init` command refers to the .xlsx file exported from MFP. Replace the filename with the name of your exported .xlsx file (and make sure to move it from the Downloads to the folder/directory in which you are running the expedition).
4747
```
4848

49-
This will create a folder/directory called `EXPEDITION_NAME` with a single file: `expedition.yaml` containing details on the ship speed and instrument configurations, as well as the expedition schedule based on the sampling site coordinates that you specified in your MFP export. The `--from-mfp` flag indicates that the exported coordinates will be used.
49+
This will create a folder/directory called `EXPEDITION_NAME` with a single file: `expedition.yaml` containing details on the ship and instrument configurations, as well as the expedition schedule based on the sampling site coordinates that you specified in your MFP export. The `--from-mfp` flag indicates that the exported coordinates will be used.
5050

5151
```{note}
5252
For advanced users: it is also possible to run the expedition initialisation step without an MFP .xlsx export file. In this case you should simply run `virtualship init EXPEDITION_NAME` in the CLI. This will write an example `expedition.yaml` file in the `EXPEDITION_NAME` folder/directory. This file contains example waypoints, timings, instrument selections, and ship configuration, but can be edited or propagated through the rest of the workflow unedited to run a sample expedition.

src/virtualship/cli/_fetch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _fetch(path: str | Path, username: str | None, password: str | None) -> None
5151
expedition = _get_expedition(path)
5252

5353
expedition.schedule.verify(
54-
expedition.ship_speed_knots,
54+
expedition.ship_config.ship_speed_knots,
5555
input_data=None,
5656
check_space_time_region=True,
5757
ignore_missing_fieldsets=True,

src/virtualship/cli/_plan.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
Expedition,
3939
InstrumentType,
4040
Location,
41+
ShipConfig,
4142
ShipUnderwaterSTConfig,
4243
SpatialRange,
4344
TimeRange,
@@ -185,12 +186,12 @@ def compose(self) -> ComposeResult:
185186
collapsed=False,
186187
):
187188
attr = "ship_speed_knots"
188-
validators = group_validators(Expedition, attr)
189+
validators = group_validators(ShipConfig, attr)
189190
with Horizontal(classes="ship_speed"):
190191
yield Label("[b]Ship Speed (knots):[/b]")
191192
yield Input(
192193
id="speed",
193-
type=type_to_textual(get_field_type(Expedition, attr)),
194+
type=type_to_textual(get_field_type(ShipConfig, attr)),
194195
validators=[
195196
Function(
196197
validator,
@@ -201,8 +202,8 @@ def compose(self) -> ComposeResult:
201202
classes="ship_speed_input",
202203
placeholder="knots",
203204
value=str(
204-
self.expedition.ship_speed_knots
205-
if self.expedition.ship_speed_knots
205+
self.expedition.ship_config.ship_speed_knots
206+
if self.expedition.ship_config.ship_speed_knots
206207
else ""
207208
),
208209
)
@@ -542,16 +543,12 @@ def save_changes(self) -> bool:
542543

543544
def _update_ship_speed(self):
544545
attr = "ship_speed_knots"
545-
field_type = get_field_type(Expedition, attr)
546+
field_type = get_field_type(type(self.expedition.ship_config), attr)
546547
value = field_type(self.query_one("#speed").value)
547-
try:
548-
if not (value > 0):
549-
raise ValueError("ship_speed_knots must be greater than 0")
550-
except TypeError:
551-
raise UnexpectedError("Invalid ship speed value") from None
552-
553-
# persist to the Expedition instance
554-
self.expedition.ship_speed_knots = value
548+
ShipConfig.model_validate(
549+
{**self.expedition.ship_config.model_dump(), attr: value}
550+
)
551+
self.expedition.ship_config.ship_speed_knots = value
555552

556553
def _update_instrument_configs(self):
557554
for instrument_name, info in INSTRUMENT_FIELDS.items():

src/virtualship/expedition/do_expedition.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ def do_expedition(expedition_dir: str | Path, input_data: Path | None = None) ->
6060
print("\n---- WAYPOINT VERIFICATION ----")
6161

6262
# verify schedule is valid
63-
expedition.schedule.verify(expedition.ship_speed_knots, loaded_input_data)
63+
expedition.schedule.verify(
64+
expedition.ship_config.ship_speed_knots, loaded_input_data
65+
)
6466

6567
# simulate the schedule
6668
schedule_results = simulate_schedule(

src/virtualship/expedition/simulate_schedule.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def _progress_time_traveling_towards(self, location: Location) -> None:
125125
lons2=location.lon,
126126
lats2=location.lat,
127127
)
128-
ship_speed_meter_per_second = self._expedition.ship_speed_knots * 1852 / 3600
128+
ship_speed_meter_per_second = (
129+
self._expedition.ship_config.ship_speed_knots * 1852 / 3600
130+
)
129131
azimuth1 = geodinv[0]
130132
distance_to_next_waypoint = geodinv[2]
131133
time_to_reach = timedelta(

src/virtualship/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def validate_coordinates(coordinates_data):
114114

115115
def mfp_to_yaml(coordinates_file_path: str, yaml_output_path: str): # noqa: D417
116116
"""
117-
Generates an expedition.yaml file with schedule information based on data from MFP excel file. The ship speed and instrument configurations entries in the YAML file are sourced from the static version.
117+
Generates an expedition.yaml file with schedule information based on data from MFP excel file. The ship and instrument configurations entries in the YAML file are sourced from the static version.
118118
119119
Parameters
120120
----------
@@ -189,14 +189,14 @@ def mfp_to_yaml(coordinates_file_path: str, yaml_output_path: str): # noqa: D41
189189
yaml.safe_load(get_example_expedition()).get("instruments_config")
190190
)
191191

192-
# extract ship speed from static
193-
ship_speed_knots = yaml.safe_load(get_example_expedition()).get("ship_speed_knots")
192+
# extract ship config from static
193+
ship_config = yaml.safe_load(get_example_expedition()).get("ship_config")
194194

195195
# combine to Expedition object
196196
expedition = Expedition(
197197
schedule=schedule,
198198
instruments_config=instruments_config,
199-
ship_speed_knots=ship_speed_knots,
199+
ship_config=ship_config,
200200
)
201201

202202
# Save to YAML file

tests/cli/test_plan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ async def test_UI_changes():
106106
with open(expedition_path) as f:
107107
saved_expedition = yaml.safe_load(f)
108108

109-
assert saved_expedition["ship_speed_knots"] == float(NEW_SPEED)
109+
assert saved_expedition["ship_config"]["ship_speed_knots"] == float(NEW_SPEED)
110110

111111
# check schedule.verify() methods are working by purposefully making invalid schedule (i.e. ship speed too slow to reach waypoints)
112112
invalid_speed = "0.0001"

tests/expedition/test_expedition.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_import_export_expedition(tmpdir) -> None:
3434
expedition = Expedition(
3535
schedule=schedule,
3636
instruments_config=get_expedition.instruments_config,
37-
ship_speed_knots=get_expedition.ship_speed_knots,
37+
ship_config=get_expedition.ship_config,
3838
)
3939
expedition.to_yaml(out_path)
4040

@@ -50,7 +50,7 @@ def test_verify_schedule() -> None:
5050
]
5151
)
5252

53-
ship_speed_knots = _get_expedition(expedition_dir).ship_speed_knots
53+
ship_speed_knots = _get_expedition(expedition_dir).ship_config.ship_speed_knots
5454

5555
schedule.verify(ship_speed_knots, None)
5656

@@ -158,7 +158,7 @@ def test_verify_schedule_errors(
158158

159159
with pytest.raises(error, match=match):
160160
schedule.verify(
161-
expedition.ship_speed_knots,
161+
expedition.ship_config.ship_speed_knots,
162162
input_data,
163163
check_space_time_region=check_space_time_region,
164164
)

tests/expedition/test_simulate_schedule.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def test_simulate_schedule_feasible() -> None:
1616

1717
projection = pyproj.Geod(ellps="WGS84")
1818
expedition = Expedition.from_yaml("expedition_dir/expedition.yaml")
19-
expedition.ship_speed_knots = 10.0
19+
expedition.ship_config.ship_speed_knots = 10.0
2020
expedition.schedule = Schedule(
2121
waypoints=[
2222
Waypoint(location=Location(0, 0), time=base_time),
@@ -35,7 +35,7 @@ def test_simulate_schedule_too_far() -> None:
3535

3636
projection = pyproj.Geod(ellps="WGS84")
3737
expedition = Expedition.from_yaml("expedition_dir/expedition.yaml")
38-
expedition.ship_speed_knots = 10.0
38+
expedition.ship_config.ship_speed_knots = 10.0
3939
expedition.schedule = Schedule(
4040
waypoints=[
4141
Waypoint(location=Location(0, 0), time=base_time),

0 commit comments

Comments
 (0)