Skip to content

Commit

Permalink
Merge pull request #1451 from knutfrode/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
knutfrode authored Nov 29, 2024
2 parents 74c8764 + 89bc9d9 commit 610e1c3
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
11 changes: 5 additions & 6 deletions opendrift/models/openoil/adios/dirjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#
# Copyright 2021, Gaute Hope, MET Norway

from importlib import resources
#from importlib import resources
from importlib_resources import files
from pathlib import Path
import logging
import json
Expand All @@ -23,7 +24,6 @@
from adios_db.models.oil.oil import Oil as AdiosOil
from adios_db.computation import gnome_oil


from .oil import OpendriftOil

logger = logging.getLogger(__name__)
Expand All @@ -35,10 +35,9 @@ def __get_archive__():

oils = []

with resources.open_binary('opendrift.models.openoil.adios',
'oils.xz') as archive:
with lzma.open(archive, 'rt') as c:
oils = json.load(c)
oil_file = files('opendrift.models.openoil.adios').joinpath('oils.xz')
with lzma.open(oil_file, 'rt') as archive:
oils = json.load(archive)

# Add additional oils
#for f in resources.contents('opendrift.models.openoil.adios.extra_oils'):
Expand Down
11 changes: 5 additions & 6 deletions opendrift/models/openoil/adios/harvest_oils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import tarfile
import lzma
from importlib_resources import files
from adios_db.models.oil.oil import Oil
from adios_db.computation import gnome_oil

Expand Down Expand Up @@ -67,12 +68,10 @@ def download():
json.dump(oils, c)

def list_oils():
from importlib import resources
import lzma
with resources.open_binary('opendrift.models.openoil.adios',
'oils.xz') as archive:
with lzma.open(archive, 'rt') as c:
oils = json.load(c)
oil_file = files('opendrift.models.openoil.adios').joinpath('oils.xz')
with lzma.open(oil_file, 'rt') as archive:
oils = json.load(archive)

print(oils)
print(len(oils))
for o in oils:
Expand Down
6 changes: 3 additions & 3 deletions opendrift/models/openoil/openoil.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@
"""

from io import open
from importlib_resources import files
import numpy as np
from datetime import datetime
import pyproj
import matplotlib.pyplot as plt
import logging
import json
from importlib import resources

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -684,8 +684,8 @@ def prepare_run(self):
logger.info('Oil-water surface tension is %f Nm' %
self.oil_water_interfacial_tension)
try:
max_water_fractions = json.loads(
resources.read_text('opendrift.models.openoil.adios', 'max_water_fraction.json'))
with open(files('opendrift.models.openoil.adios').joinpath('max_water_fraction.json')) as f:
max_water_fractions = json.loads(f.read())
if self.oil_name in max_water_fractions:
self.max_water_fraction = max_water_fractions[self.oil_name]
T = self.max_water_fraction['temperatures']
Expand Down
8 changes: 8 additions & 0 deletions opendrift/readers/operators/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import pyproj
import xarray as xr
import numpy as np


class Combine:
"""Combining two readers into a third one. You can use usual operators,
but also more complex ones such as gaussian combining.
Expand All @@ -21,13 +23,19 @@ def __add__(self, other):
else:
return NotImplemented

def __radd__(self, other):
return self.__add__(other)

def __mul__(self, other):
from .numops import Combined as NumCombined
if isinstance(other, Number):
return NumCombined.mul(other, self)
else:
return NotImplemented

def __rmul__(self, other):
return self.__mul__(other)

def __truediv__(self, other):
from .numops import Combined as NumCombined

Expand Down
6 changes: 3 additions & 3 deletions opendrift/scripts/opendrift_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from PIL import ImageTk, Image
import tkinter as tk
from tkinter import ttk
from importlib import resources
from importlib_resources import files
import opendrift
from opendrift.models.oceandrift import OceanDrift
from opendrift.models.openoil import OpenOil
Expand Down Expand Up @@ -364,7 +364,7 @@ def __init__(self):
##############
self.set_model(list(self.opendrift_models)[0])

with resources.open_text('opendrift.scripts', 'data_sources.txt') as fd:
with open(files('opendrift.scripts').joinpath('data_sources.txt')) as fd:
forcingfiles = fd.readlines()

print(forcingfiles)
Expand Down Expand Up @@ -794,7 +794,7 @@ def run_opendrift(self):
nothing
self.o.set_config(se, val)

with resources.path('opendrift.scripts', 'data_sources.txt') as f:
with files('opendrift.scripts').joinpath('data_sources.txt') as f:
self.o.add_readers_from_file(f)

self.o.seed_cone(lon=lon, lat=lat, radius=radius,
Expand Down
11 changes: 7 additions & 4 deletions tests/models/openoil/test_openoil_custom_type.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import json
from importlib import resources
from importlib_resources import files

from opendrift.models.openoil import OpenOil

oiljson = files('opendrift.models.openoil.adios.extra_oils').joinpath('AD04011.json')

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

d = json.loads(resources.read_text('opendrift.models.openoil.adios.extra_oils', 'AD04011.json'))
with open(oiljson) as f:
d = json.loads(f.read())

o.set_oiltype_by_json(d)
assert o.oiltype.name == 'GENERIC FUEL OIL No. 6'
Expand All @@ -14,8 +18,7 @@ def test_set_oil_type_json():
def test_set_oil_type_file():
o = OpenOil(loglevel=50)

with resources.path('opendrift.models.openoil.adios.extra_oils', 'AD04011.json') as f:
o.set_oiltype_from_file(f)
o.set_oiltype_from_file(oiljson)

assert o.oiltype.name == 'GENERIC FUEL OIL No. 6'
print(o.oiltype)
Expand Down
4 changes: 4 additions & 0 deletions tests/readers/test_grib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ def test_get_variables(benchmark, test_data):

assert u.shape == (2 * r.buffer , 2 * r.buffer)

def test_clean_up(test_data):
import glob
for f in glob.glob(os.path.join(test_data, caps1) + '*idx'):
os.remove(f)

0 comments on commit 610e1c3

Please sign in to comment.