Skip to content

Commit

Permalink
Seperate instruments (#35)
Browse files Browse the repository at this point in the history
* wip drifters

* drifters instrument. just missing test

* drifter test

* Remove old drifter file and fix errors

* codetools

* outputdf wherever i forgot to use it

* added todo list

* ctd instrument

* CTDtest

* use new ctd instrument in sailship

* fix typo

* codetools

* use new parcels api for particle class

* add ctd to init

* comments

* first try adcp

* adcp test

* use adcp instrument in sailship

* cleanup

* instrument ship s t

* use ship st in sailship

* cleanup

* refactor sailship ctd cast

* cleanup

* large cleanup

* add checks that all drifter and argo floats are deployed

* comments

* docstrings and other codetools fixes

* rename SamplePoint to Spacetime and move them one dir up

* rm todo

* run pydocstyle on tests

* fix some names

* Update virtual_ship/instruments/ctd.py

Co-authored-by: Erik van Sebille <[email protected]>

* removed comment about ctd

* fix incorrect docstring

* renamed ship_st to ship_underwater_st

* remove sailship create fieldset commented out function

* ship underway st docstring improvement

* minor docstring change

* minor docstring change

---------

Co-authored-by: Erik van Sebille <[email protected]>
  • Loading branch information
surgura and erikvansebille authored Jun 5, 2024
1 parent 92d0838 commit fcb36d7
Show file tree
Hide file tree
Showing 22 changed files with 740 additions and 452 deletions.
2 changes: 1 addition & 1 deletion codetools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ flake8 ./$PACKAGE ./$TESTS
echo "--------------"
echo "pydocstyle"
echo "--------------"
pydocstyle ./$PACKAGE
pydocstyle ./$PACKAGE ./$TESTS

echo "--------------"
echo "sort-all"
Expand Down
9 changes: 8 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
"""Test configuration that is ran for every test."""

import pytest


# Set the working directory for each test to the directory of that test.
@pytest.fixture(autouse=True)
def change_test_dir(request, monkeypatch):
"""
Set the working directory for each test to the directory of that test.
:param request: -
:param monkeypatch: -
"""
monkeypatch.chdir(request.fspath.dirname)
33 changes: 33 additions & 0 deletions tests/instruments/test_adcp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Test the simulation of ADCP instruments."""

import numpy as np
from parcels import FieldSet

from virtual_ship import Location, Spacetime
from virtual_ship.instruments.adcp import simulate_adcp


def test_simulate_adcp() -> None:
MAX_DEPTH = -1000
MIN_DEPTH = -5
BIN_SIZE = 24

fieldset = FieldSet.from_data(
{"U": 0, "V": 0},
{
"lon": 0,
"lat": 0,
"time": [np.datetime64("1950-01-01") + np.timedelta64(632160, "h")],
},
)

sample_points = [Spacetime(Location(0, 0), 0)]

simulate_adcp(
fieldset=fieldset,
out_file_name="test",
max_depth=MAX_DEPTH,
min_depth=MIN_DEPTH,
bin_size=BIN_SIZE,
sample_points=sample_points,
)
9 changes: 4 additions & 5 deletions tests/instruments/test_argo.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import numpy as np
from parcels import FieldSet

from virtual_ship.instruments import Location
from virtual_ship import Location, Spacetime
from virtual_ship.instruments.argo_float import ArgoFloat, simulate_argo_floats


def test_simulate_argo_floats() -> None:
DRIFT_DEPTH = -1000
MAX_DEPTH = -2000
VERTICLE_SPEED = -0.10
VERTICAL_SPEED = -0.10
CYCLE_DAYS = 10
DRIFT_DAYS = 9

Expand All @@ -29,12 +29,11 @@ def test_simulate_argo_floats() -> None:

argo_floats = [
ArgoFloat(
location=Location(latitude=0, longitude=0),
deployment_time=0,
spacetime=Spacetime(location=Location(latitude=0, longitude=0), time=0),
min_depth=min_depth,
max_depth=MAX_DEPTH,
drift_depth=DRIFT_DEPTH,
vertical_speed=VERTICLE_SPEED,
vertical_speed=VERTICAL_SPEED,
cycle_days=CYCLE_DAYS,
drift_days=DRIFT_DAYS,
)
Expand Down
38 changes: 38 additions & 0 deletions tests/instruments/test_ctd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Test the simulation of CTD instruments."""

from datetime import timedelta

import numpy as np
from parcels import FieldSet

from virtual_ship import Location, Spacetime
from virtual_ship.instruments.ctd import CTD, simulate_ctd


def test_simulate_ctds() -> None:
fieldset = FieldSet.from_data(
{"U": 0, "V": 0, "T": 0, "S": 0, "bathymetry": 100},
{
"lon": 0,
"lat": 0,
"time": [np.datetime64("1950-01-01") + np.timedelta64(632160, "h")],
},
)

min_depth = -fieldset.U.depth[0]
max_depth = -fieldset.U.depth[-1]

ctds = [
CTD(
spacetime=Spacetime(location=Location(latitude=0, longitude=0), time=0),
min_depth=min_depth,
max_depth=max_depth,
)
]

simulate_ctd(
ctds=ctds,
fieldset=fieldset,
out_file_name="test",
outputdt=timedelta(seconds=10),
)
36 changes: 36 additions & 0 deletions tests/instruments/test_drifters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Test the simulation of drifters."""

from datetime import timedelta

import numpy as np
from parcels import FieldSet

from virtual_ship import Location, Spacetime
from virtual_ship.instruments.drifter import Drifter, simulate_drifters


def test_simulate_drifters() -> None:
fieldset = FieldSet.from_data(
{"U": 0, "V": 0, "T": 0},
{
"lon": 0,
"lat": 0,
"time": [np.datetime64("1950-01-01") + np.timedelta64(632160, "h")],
},
)

min_depth = -fieldset.U.depth[0]

drifters = [
Drifter(
spacetime=Spacetime(location=Location(latitude=0, longitude=0), time=0),
min_depth=min_depth,
)
]

simulate_drifters(
drifters=drifters,
fieldset=fieldset,
out_file_name="test",
outputdt=timedelta(minutes=5),
)
29 changes: 29 additions & 0 deletions tests/instruments/test_ship_underwater_st.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Test the simulation of ship salinity temperature measurements."""

import numpy as np
from parcels import FieldSet

from virtual_ship import Location, Spacetime
from virtual_ship.instruments.ship_underwater_st import simulate_ship_underwater_st


def test_simulate_ship_underwater_st() -> None:
DEPTH = -2

fieldset = FieldSet.from_data(
{"U": 0, "V": 0, "S": 0, "T": 0},
{
"lon": 0,
"lat": 0,
"time": [np.datetime64("1950-01-01") + np.timedelta64(632160, "h")],
},
)

sample_points = [Spacetime(Location(0, 0), 0)]

simulate_ship_underwater_st(
fieldset=fieldset,
out_file_name="test",
depth=DEPTH,
sample_points=sample_points,
)
4 changes: 2 additions & 2 deletions tests/sailship_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"bin_size_m": 24
},
"CTD_locations": [
[-23.071289, 63.743631]
[-23.081289, 63.743631]
],
"CTD_settings": {
"max_depth": "max"
},
"drifter_deploylocations": [

[-23.081289, 63.743631]
],
"argo_deploylocations": [
[-23.081289, 63.743631]
Expand Down
22 changes: 16 additions & 6 deletions tests/test_sailship.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,28 @@


def test_sailship() -> None:
adcp_fieldset = FieldSet.from_data(
{"U": 0, "V": 0},
{"lon": 0, "lat": 0},
)

ship_underwater_st_fieldset = FieldSet.from_data(
{"U": 0, "V": 0, "S": 0, "T": 0},
{"lon": 0, "lat": 0},
)

ctd_fieldset = FieldSet.from_data(
{"U": 0, "V": 0, "S": 0, "T": 0, "bathymetry": 0},
{"lon": 0, "lat": 0},
)
ctd_fieldset.add_constant("maxtime", ctd_fieldset.U.grid.time_full[-1])
ctd_fieldset.add_constant("mindepth", -ctd_fieldset.U.depth[0])
ctd_fieldset.add_constant("max_depth", -ctd_fieldset.U.depth[-1])

drifter_fieldset = FieldSet.from_data(
{"U": 0, "V": 0, "T": 0},
{
"U": 0,
"V": 0,
"lon": 0,
"lat": 0,
"time": [np.datetime64("1950-01-01") + np.timedelta64(632160, "h")],
},
{"lon": 0, "lat": 0},
)

argo_float_fieldset = FieldSet.from_data(
Expand All @@ -35,6 +43,8 @@ def test_sailship() -> None:

config = VirtualShipConfiguration(
"sailship_config.json",
adcp_fieldset=adcp_fieldset,
ship_underwater_st_fieldset=ship_underwater_st_fieldset,
ctd_fieldset=ctd_fieldset,
drifter_fieldset=drifter_fieldset,
argo_float_fieldset=argo_float_fieldset,
Expand Down
4 changes: 3 additions & 1 deletion virtual_ship/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Code for the Virtual Ship Classroom, where Marine Scientists can combine Copernicus Marine Data with an OceanParcels ship to go on a virtual expedition."""

from . import instruments, sailship
from .location import Location
from .spacetime import Spacetime

__all__ = ["instruments", "sailship"]
__all__ = ["Location", "Spacetime", "instruments", "sailship"]
10 changes: 7 additions & 3 deletions virtual_ship/costs.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
"""costs function."""

from datetime import timedelta

def costs(config, total_time):
from .virtual_ship_configuration import VirtualShipConfiguration


def costs(config: VirtualShipConfiguration, total_time: timedelta):
"""
Calculate the cost of the virtual ship (in US$).
:param config: The cruise configuration.
:param total_time: Time cruised in seconds.
:param total_time: Time cruised.
:returns: The calculated cost of the cruise.
"""
ship_cost_per_day = 30000
drifter_deploy_cost = 2500
argo_deploy_cost = 15000

ship_cost = ship_cost_per_day / 24 * total_time // 3600
ship_cost = ship_cost_per_day / 24 * total_time.total_seconds() // 3600
argo_cost = len(config.argo_deploylocations) * argo_deploy_cost
drifter_cost = len(config.drifter_deploylocations) * drifter_deploy_cost

Expand Down
Loading

0 comments on commit fcb36d7

Please sign in to comment.