Skip to content

Commit

Permalink
Move deployments and postprocessing into sailship function and make d…
Browse files Browse the repository at this point in the history
…ata directory a parameter instead of hardcoded
  • Loading branch information
surgura committed Apr 30, 2024
1 parent 6958101 commit 16159dc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 39 deletions.
15 changes: 7 additions & 8 deletions virtual_ship/argo_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)


def argo_deployments(config, argo_time):
def argo_deployments(config, argo_time, data_dir: str):
"""
Deploy argo floats.
Expand All @@ -28,7 +28,7 @@ def argo_deployments(config, argo_time):

if len(config.argo_deploylocations) > 0:

fieldset = create_argo_fieldset(config)
fieldset = create_argo_fieldset(config, data_dir)

# Define the new Kernel that mimics Argo vertical movement
def ArgoVerticalMovement(particle, fieldset, time):
Expand Down Expand Up @@ -148,19 +148,18 @@ class ArgoParticle(JITParticle):
)


def create_argo_fieldset(config):
def create_argo_fieldset(config, data_dir: str):
"""
Create a fieldset from netcdf files for argo floats, return fieldset with negative depth values.
:param config: The cruise configuration.
:returns: The fieldset.
"""
datadirname = os.path.dirname(__file__)
filenames = {
"U": os.path.join(datadirname, "argodata_UV.nc"),
"V": os.path.join(datadirname, "argodata_UV.nc"),
"S": os.path.join(datadirname, "argodata_S.nc"),
"T": os.path.join(datadirname, "argodata_T.nc"),
"U": os.path.join(data_dir, "argodata_UV.nc"),
"V": os.path.join(data_dir, "argodata_UV.nc"),
"S": os.path.join(data_dir, "argodata_S.nc"),
"T": os.path.join(data_dir, "argodata_T.nc"),
}
variables = {"U": "uo", "V": "vo", "S": "so", "T": "thetao"}
dimensions = {
Expand Down
13 changes: 6 additions & 7 deletions virtual_ship/drifter_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from parcels import AdvectionRK4, FieldSet, JITParticle, ParticleSet, Variable


def drifter_deployments(config, drifter_time):
def drifter_deployments(config, drifter_time, data_dir: str):
"""
Deploy drifters.
Expand All @@ -17,7 +17,7 @@ def drifter_deployments(config, drifter_time):
"""
if len(config.drifter_deploylocations) > 0:

fieldset = create_drifter_fieldset(config)
fieldset = create_drifter_fieldset(config, data_dir)

# Create particle to sample water underway
class DrifterParticle(JITParticle):
Expand Down Expand Up @@ -74,18 +74,17 @@ def CheckError(particle, fieldset, time):
)


def create_drifter_fieldset(config):
def create_drifter_fieldset(config, data_dir: str):
"""
Create a fieldset from netcdf files for drifters, returns fieldset with negative depth values.
:param config: The cruise configuration.
:returns: The fieldset.
"""
datadirname = os.path.dirname(__file__)
filenames = {
"U": os.path.join(datadirname, "drifterdata_UV.nc"),
"V": os.path.join(datadirname, "drifterdata_UV.nc"),
"T": os.path.join(datadirname, "drifterdata_T.nc"),
"U": os.path.join(data_dir, "drifterdata_UV.nc"),
"V": os.path.join(data_dir, "drifterdata_UV.nc"),
"T": os.path.join(data_dir, "drifterdata_T.nc"),
}
variables = {"U": "uo", "V": "vo", "T": "thetao"}
dimensions = {
Expand Down
39 changes: 30 additions & 9 deletions virtual_ship/sailship.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import pyproj
from parcels import Field, FieldSet, JITParticle, ParticleSet, Variable
from shapely.geometry import Point, Polygon
from .drifter_deployments import drifter_deployments
from .argo_deployments import argo_deployments
from .postprocess import postprocess
from .costs import costs


def sailship(config):
Expand All @@ -16,8 +20,12 @@ def sailship(config):
:param config: The cruise configuration.
:returns: drifter_time, argo_time, total_time
"""
# hardcoding this until we refactor more
DATA_DIR = "data"
data_dir = DATA_DIR

# Create fieldset and retreive final schip route as sample_lons and sample_lats
fieldset = create_fieldset(config)
fieldset = create_fieldset(config, data_dir)

sample_lons, sample_lats = shiproute(config)
print("Arrived in region of interest, starting to gather data.")
Expand Down Expand Up @@ -243,23 +251,36 @@ def SampleT(particle, fieldset, time):
)
print("Cruise has ended. Please wait for drifters and/or Argo floats to finish.")

return drifter_time, argo_time, total_time
# simulate drifter deployments
drifter_deployments(config, drifter_time, data_dir)

# simulate argo deployments
argo_deployments(config, argo_time, data_dir)

# convert CTD data to CSV
postprocess()

print("All data has been gathered and postprocessed, returning home.")

cost = costs(config, total_time)
print(
f"This cruise took {timedelta(seconds=total_time)} and would have cost {cost:,.0f} euros."
)


def create_fieldset(config):
def create_fieldset(config, data_dir: str):
"""
Create fieldset from netcdf files and adds bathymetry data for CTD cast, returns fieldset with negative depth values.
:param config: The cruise configuration.
:returns: The fieldset.
:raises ValueError: If downloaded data is not as expected.
"""
datadirname = os.path.dirname(__file__)
filenames = {
"U": os.path.join(datadirname, "studentdata_UV.nc"),
"V": os.path.join(datadirname, "studentdata_UV.nc"),
"S": os.path.join(datadirname, "studentdata_S.nc"),
"T": os.path.join(datadirname, "studentdata_T.nc"),
"U": os.path.join(data_dir, "studentdata_UV.nc"),
"V": os.path.join(data_dir, "studentdata_UV.nc"),
"S": os.path.join(data_dir, "studentdata_S.nc"),
"T": os.path.join(data_dir, "studentdata_T.nc"),
}
variables = {"U": "uo", "V": "vo", "S": "so", "T": "thetao"}
dimensions = {
Expand All @@ -286,7 +307,7 @@ def create_fieldset(config):
fieldset.add_constant("maxtime", fieldset.U.grid.time_full[-1])

# add bathymetry data to the fieldset for CTD cast
bathymetry_file = os.path.join(datadirname, "GLO-MFC_001_024_mask_bathy.nc")
bathymetry_file = os.path.join(data_dir, "GLO-MFC_001_024_mask_bathy.nc")
bathymetry_variables = ("bathymetry", "deptho")
bathymetry_dimensions = {"lon": "longitude", "lat": "latitude"}
bathymetry_field = Field.from_netcdf(
Expand Down
2 changes: 1 addition & 1 deletion virtual_ship/virtual_ship_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, json_file):
:param json_file: Path to the JSON file to init from.
:raises ValueError: If JSON file not valid.
"""
with open(os.path.join(os.path.dirname(__file__), json_file), "r") as file:
with open(json_file, "r") as file:
json_input = json.loads(file.read())
for key in json_input:
setattr(self, key, json_input[key])
Expand Down
17 changes: 3 additions & 14 deletions virtualship.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
from datetime import timedelta
from virtual_ship.virtual_ship_configuration import VirtualShipConfiguration
from virtual_ship.costs import costs
from virtual_ship.sailship import sailship
from virtual_ship.drifter_deployments import drifter_deployments
from virtual_ship.argo_deployments import argo_deployments
from virtual_ship.postprocess import postprocess

if __name__ == '__main__':
config = VirtualShipConfiguration('student_input.json')
drifter_time, argo_time, total_time = sailship(config)
drifter_deployments(config, drifter_time)
argo_deployments(config, argo_time)
postprocess()
print("All data has been gathered and postprocessed, returning home.")
cost = costs(config, total_time)
print(f"This cruise took {timedelta(seconds=total_time)} and would have cost {cost:,.0f} euros.")
if __name__ == "__main__":
config = VirtualShipConfiguration("student_input.json")
sailship(config)

0 comments on commit 16159dc

Please sign in to comment.