Skip to content

Commit

Permalink
parse oil in json format as in extra_oils (+ add tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
gauteh committed Oct 1, 2022
1 parent e5ef1fe commit 7589037
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
15 changes: 7 additions & 8 deletions opendrift/models/openoil/openoil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,11 @@ def set_oiltype_by_json(self, json):
Sets the oil type by specifing a JSON dict. The format should be the same as the ADIOS database. See the `ADIOS database <https://adios.orr.noaa.gov/oils>`_ for a list.
"""
if self.oil_weathering_model == 'noaa':
self.oiltype = adios.oil.OpendriftOil(json)
o = { 'data': { 'attributes' : json } }
o['data']['_id'] = o['data']['attributes']['oil_id']
o['data']['attributes']['metadata']['location'] = 'NORWAY'

self.oiltype = adios.oil.OpendriftOil(o)
self.oil_name = self.oiltype.name
if not self.oiltype.valid():
logger.error(
Expand All @@ -1469,13 +1473,8 @@ def set_oiltype_from_file(self, path):
with open(path, 'r') as fd:
j = json.load(fd)

self.oiltype = adios.oil.OpendriftOil(j)
self.oil_name = self.oiltype.name
if not self.oiltype.valid():
logger.error(
f"{self.oiltype} is not a valid oil for Opendrift simulations"
)
raise ValueError()
self.set_oiltype_by_json(j)

else:
raise ValueError("unsupported oil weathering model")

Expand Down
22 changes: 22 additions & 0 deletions tests/models/openoil/test_openoil_custom_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json
from importlib import resources
from opendrift.models.openoil import OpenOil

def test_set_oil_type_json():
o = OpenOil(loglevel=50)

d = json.loads(resources.read_text('opendrift.models.openoil.adios.extra_oils', 'AD03128.json'))

o.set_oiltype_by_json(d)
assert o.oiltype.name == 'VEGA CONDENSATE 2015'
print(o.oiltype)

def test_set_oil_type_file():
o = OpenOil(loglevel=50)

f = resources.path('opendrift.models.openoil.adios.extra_oils', 'AD03128.json')

o.set_oiltype_from_file(f)
assert o.oiltype.name == 'VEGA CONDENSATE 2015'
print(o.oiltype)

1 comment on commit 7589037

@knutfrode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.