Skip to content

Commit c3f759a

Browse files
authored
Merge pull request #1629 from knutfrode/dev
[run-ex] New mechanism to skip environment variables based on conditi…
2 parents 60ee835 + b9e65d6 commit c3f759a

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

opendrift/models/basemodel/__init__.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ def __init__(self,
241241
# List to store GeoJSON dicts of seeding commands
242242
self.seed_geojson = []
243243

244+
self.required_variables = self.required_variables.copy() # Avoid modifying the class variable
245+
244246
self.env = Environment(self.required_variables, self._config)
245247

246248
# Make copies of dictionaries so that they are private to each instance
@@ -1807,6 +1809,16 @@ def run(self,
18071809
if self.steps_calculation >= 1:
18081810
self.steps_calculation = 0
18091811

1812+
###################################################################################
1813+
# Remove variables from required_variables based on configuration and conditionals
1814+
###################################################################################
1815+
for vn, var in self.required_variables.copy().items():
1816+
if 'skip_if' in var:
1817+
skip = self.evaluate_conditional(*var['skip_if'])
1818+
if skip is True:
1819+
logger.info(f'Skipping environment variable {vn} because of condition {var["skip_if"]}')
1820+
self.required_variables.pop(vn)
1821+
18101822
########################
18111823
# Simulation time step
18121824
########################
@@ -4755,3 +4767,34 @@ def center_of_gravity(self, onlysurface=False):
47554767
def gui_postproc(self):
47564768
'''To be overloaded by subclasses'''
47574769
pass
4770+
4771+
def evaluate_conditional(self, key, operator, value):
4772+
"""Evaluate a condition as True or False
4773+
4774+
This can be used to:
4775+
- skip required_variables that are not required, based on config setting
4776+
- store previous value of element property or environment variable, based on config setting
4777+
- disable a config setting based on another setting (for dynamic menus)
4778+
4779+
key: config key string
4780+
operator: one from operator_map below
4781+
value: the provided value to be matched with operator against actual config setting
4782+
4783+
Returns: True or False
4784+
"""
4785+
4786+
operator_map = {
4787+
'==': lambda x, y: x == y,
4788+
'!=': lambda x, y: x != y,
4789+
'<': lambda x, y: x < y,
4790+
'<=': lambda x, y: x <= y,
4791+
'>': lambda x, y: x > y,
4792+
'>=': lambda x, y: x >= y,
4793+
'is': lambda x, y: x is y,
4794+
'is not': lambda x, y: x is not y,
4795+
}
4796+
4797+
# Presently assuming that key is a config key
4798+
key = self.get_config(key)
4799+
4800+
return operator_map[operator](key, value)

opendrift/models/oceandrift.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@ class OceanDrift(OpenDriftSimulation):
7373
'sea_surface_height': {'fallback': 0},
7474
'x_wind': {'fallback': 0},
7575
'y_wind': {'fallback': 0},
76-
'upward_sea_water_velocity': {'fallback': 0},
76+
'upward_sea_water_velocity': {'fallback': 0,
77+
'skip_if': ['drift:vertical_advection', 'is', False]},
7778
'ocean_vertical_diffusivity': {'fallback': 0,
79+
'skip_if': ['drift:vertical_mixing', 'is', False],
7880
'profiles': True},
7981
'sea_surface_wave_significant_height': {'fallback': 0},
80-
'sea_surface_wave_stokes_drift_x_velocity': {'fallback': 0},
81-
'sea_surface_wave_stokes_drift_y_velocity': {'fallback': 0},
82+
'sea_surface_wave_stokes_drift_x_velocity': {'fallback': 0,
83+
'skip_if': ['drift:stokes_drift', 'is', False]},
84+
'sea_surface_wave_stokes_drift_y_velocity': {'fallback': 0,
85+
'skip_if': ['drift:stokes_drift', 'is', False]},
8286
'sea_surface_wave_period_at_variance_spectral_density_maximum':
8387
{'fallback': 0},
8488
'sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment': {'fallback': 0},

tests/models/test_basemodel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def test_logging(tmpdir, capsys):
1111
# Accepting small variations in log output,
1212
# depending on machine, and from which folder test is run
13-
accepted = (286, 289, 314)
13+
accepted = (278, 281)
1414

1515
# Logging to console
1616
logfile = None

0 commit comments

Comments
 (0)