Skip to content

Commit

Permalink
Updated environment_mapping mechanism so that wind components can be …
Browse files Browse the repository at this point in the history
…calculated from speed and either wind_from_direction or wind_to_direction. Added new test.
  • Loading branch information
knutfrode committed Mar 10, 2021
1 parent 48280ae commit c23669c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion opendrift/models/basemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,7 @@ def run(self, time_step=None, steps=None, time_step_output=None,
self.add_metadata(keyword, self.fallback_values[var])
else:
readers = self.priority_list[var]
if readers[0].startswith('constant_reader'):
if readers[0].startswith('constant_reader') and var in self.readers[readers[0]]._parameter_value_map:
self.add_metadata(keyword, self.readers[readers[
0]]._parameter_value_map[var][0])
else:
Expand Down
20 changes: 13 additions & 7 deletions opendrift/readers/basereader/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,16 @@ class Variables(ReaderDomain):

environment_mappers = []
environment_mappings = {
'wind_from_speed_and_direction': {
'wind_from_speed_and_direction_from': {
'input': ['wind_speed', 'wind_from_direction'],
'output': ['x_wind', 'y_wind'],
'method':
lambda reader, var: reader.wind_from_speed_and_direction(var)
},
lambda reader, var: reader.wind_from_speed_and_direction(var)},
'wind_from_speed_and_direction_to': {
'input': ['wind_speed', 'wind_to_direction'],
'output': ['x_wind', 'y_wind'],
'method':
lambda reader, var: reader.wind_from_speed_and_direction(var)},
'testvar': {
'input': ['sea_ice_thickness'],
'output': ['istjukkleik']
Expand Down Expand Up @@ -428,10 +432,12 @@ def set_buffer_size(self, max_speed, max_vertical_speed=None):
(self.buffer, self.name, max_speed))

def wind_from_speed_and_direction(self, env):
north_wind = -env['wind_speed'] * np.cos(
np.radians(env['wind_from_direction']))
east_wind = -env['wind_speed'] * np.sin(
np.radians(env['wind_from_direction']))
if 'wind_from_direction' in env:
wfd = env['wind_from_direction']
else:
wfd = -env['wind_to_direction']
north_wind = -env['wind_speed']*np.cos(np.radians(wfd))
east_wind = -env['wind_speed']*np.sin(np.radians(wfd))
env['x_wind'] = east_wind
env['y_wind'] = north_wind
# Rotating might be necessary generally
Expand Down
24 changes: 24 additions & 0 deletions tests/readers/test_variables.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import numpy as np
import pytest
from . import *
from datetime import datetime, timedelta
from opendrift.readers import reader_netCDF_CF_generic
from opendrift.readers import reader_constant
from opendrift.models.oceandrift import OceanDrift


def test_covers_positions(test_data):
Expand All @@ -19,4 +22,25 @@ def test_covers_positions(test_data):
np.testing.assert_almost_equal(x_wind[0], 2.615, 2)
np.testing.assert_almost_equal(x_wind[-1], -0.222, 2)

def test_environment_mapping(test_data):

# Wind from NE
r = reader_constant.Reader({'wind_speed':5, 'wind_from_direction': 45,
'land_binary_mask': 0})
o = OceanDrift(loglevel=50)
o.set_config('general:use_auto_landmask', False)
o.add_reader(r)
o.seed_elements(lon=4, lat=60, time=datetime.now())
o.run(steps=15)
np.testing.assert_almost_equal(o.elements.lon, 3.932, 3)
np.testing.assert_almost_equal(o.elements.lat, 59.966, 3)
# Wind from SW
r = reader_constant.Reader({'wind_speed':5, 'wind_from_direction': 225,
'land_binary_mask': 0})
o = OceanDrift(loglevel=50)
o.set_config('general:use_auto_landmask', False)
o.add_reader(r)
o.seed_elements(lon=4, lat=60, time=datetime.now())
o.run(steps=15)
np.testing.assert_almost_equal(o.elements.lon, 4.068, 3)
np.testing.assert_almost_equal(o.elements.lat, 60.034, 3)

0 comments on commit c23669c

Please sign in to comment.