Skip to content

Commit

Permalink
Modification so Reader.get_variables_interpolated() is always called …
Browse files Browse the repository at this point in the history
…with named arguments. Ad some more documentation in ops and readerops
  • Loading branch information
Ugomartinez authored and gauteh committed May 27, 2024
1 parent db68f50 commit 857fb75
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
8 changes: 4 additions & 4 deletions opendrift/models/basemodel/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,10 +639,10 @@ def get_environment(self, variables, time, lon, lat, z, profiles=None, profiles_
profiles_from_reader = None
env_tmp, env_profiles_tmp = \
reader.get_variables_interpolated(
variable_group, profiles_from_reader,
profiles_depth, time,
lon[missing_indices], lat[missing_indices],
z[missing_indices], self.proj_latlon)
variable_group, profiles = profiles_from_reader,
profiles_depth = profiles_depth, time = time,
lon=lon[missing_indices], lat=lat[missing_indices],
z=z[missing_indices], rotate_to_proj=self.proj_latlon)

except NotCoveredError as e:
logger.info(e)
Expand Down
20 changes: 7 additions & 13 deletions opendrift/readers/operators/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
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.
"""

def __add__(self, other):
from .readerops import Combined as ReaderCombined
Expand Down Expand Up @@ -38,6 +40,10 @@ def __sub__(self, other):
return self + (-1 * other)

def combine_gaussian(self, measurement_reader, std):
"""Mix two readers with a gaussian, whose std is the one given as an argument.
The measurment reader have to be of type timeseries, with a lon and lat
attributes that are taken as the center of the measure.
"""
from .readerops import Combined as ReaderCombined

def gaussian_melting(a, b, lon, lat, lon_center, lat_center, std):
Expand All @@ -46,21 +52,9 @@ def gaussian_melting(a, b, lon, lat, lon_center, lat_center, std):
lon_center = lon_center * np.ones(len(lon))
if ( isinstance(lat_center, float) or len(lat_center) == 1 ) and len(lat) != 1 :
lat_center = lat_center * np.ones(len(lat))

print("lon length: {}".format(len(lon)))
print("lat length: {}".format(len(lat)))
print("lon_center length: {}".format(len(lon_center)))
print("lat_center length: {}".format(len(lat_center)))

_, _, distance = geod.inv(lon, lat, lon_center, lat_center)
exponential_factor = np.exp( -np.power(distance/std, 2.) / 2)
res = a * (1 - exponential_factor) + b * exponential_factor
print("==================================")
print("Gaussian calculation")
print("Lon, lat, lon_center, lat_center = {}".format((lon, lat, lon_center, lat_center)))
print("Distance used for calculation: {}".format(distance))
print("a = {}, b = {}, c = {}".format(a, b, res))
print("=================================")
return res

return ReaderCombined(a = self, b = measurement_reader, op = gaussian_melting, op_type= "combine_gaussian", external_params = std)
Expand Down
13 changes: 4 additions & 9 deletions opendrift/readers/operators/readerops.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Combined(BaseReader):
def __init__(self, a, b, op, op_type = "easy", external_params = None):
'''Combine two readers a and b followinf the operator op. If needed,
you can ad an op_type that will enable you to use the external parameters
you want in your op. '''
you want in your op between lines 63 and 73.'''
self.a = a
self.b = b
self.op = op
Expand Down Expand Up @@ -49,16 +49,11 @@ def covers_positions(self, lon, lat):
def covers_time(self, time):
return self.a.covers_time(time) and self.b.covers_time(time)

def get_variables_interpolated(self, variables, *args, **kwargs):
def get_variables_interpolated(self, variables, **kwargs):
assert set(variables).issubset(self.variables), f"{variables} is not subset of {self.variables}"

debug=True
if debug:
print("args passed are: {}".format(args))
print("kwargs passed are: {}".format(kwargs))

env_a, env_profiles_a = self.a.get_variables_interpolated(variables, *args, **kwargs)
env_b, env_profiles_b = self.b.get_variables_interpolated(variables, *args, **kwargs)
env_a, env_profiles_a = self.a.get_variables_interpolated(variables, **kwargs)
env_b, env_profiles_b = self.b.get_variables_interpolated(variables, **kwargs)

variables = [
var for var in env_a.keys() if var not in ['x', 'y', 'z', 'time']
Expand Down

0 comments on commit 857fb75

Please sign in to comment.