Skip to content

Commit 2aeee3d

Browse files
authored
Merge pull request #1606 from knutfrode/dev
[run-ex] Updated OpenBerg example
2 parents 36b25ff + f595bbd commit 2aeee3d

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

examples/example_openberg.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,52 @@
44
====================
55
"""
66

7+
from datetime import datetime, timedelta
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
import opendrift
711
from opendrift.models.openberg import OpenBerg
8-
from datetime import datetime,timedelta
9-
10-
o = OpenBerg()
1112

12-
# The user can overwrite the default setup using set_config method
13-
o.set_config('drift:vertical_profile', False) # use surface currents for this test
13+
#%%
14+
# Currents and wind forcing
15+
forcing = ['https://thredds.met.no/thredds/dodsC/fou-hi/barents_eps_zdepth_be',
16+
'https://pae-paha.pacioos.hawaii.edu/thredds/dodsC/ncep_global/NCEP_Global_Atmospheric_Model_best.ncd']
1417

15-
o.add_readers_from_list([
16-
'https://thredds.met.no/thredds/dodsC/cmems/topaz6/dataset-topaz6-arc-15min-3km-be.ncml',
17-
'https://pae-paha.pacioos.hawaii.edu/thredds/dodsC/ncep_global/NCEP_Global_Atmospheric_Model_best.ncd'])
18+
#%%
19+
# A permutation of iceberg sizes/dimensions
20+
n = 10
21+
lengths = np.linspace(50, 500, n)
22+
widths = np.linspace(20, 200, n)
23+
sails = np.linspace(5, 50, n)
24+
drafts = np.linspace(2, 120, n)
25+
lengths, widths, sails, drafts = np.meshgrid(lengths, widths, sails, drafts)
1826

27+
icebergs = {'lon': 19.8, 'lat': 74.3, 'time': datetime.now(),
28+
'number': lengths.size, 'radius': 500,
29+
'sail': sails, 'draft': drafts, 'length': lengths, 'width': widths}
1930

20-
o.seed_elements(lon=-56, lat=72, time=datetime.now(),
21-
number=100, radius=500,
22-
sail=10,draft=50,length=90,width=40)
31+
#%%
32+
# Simulating drift for 48 hours
33+
outfile = 'icebergs.nc'
34+
if False:
35+
o = OpenBerg()
36+
o.set_config('drift:vertical_profile', False)
37+
o.add_readers_from_list(forcing)
38+
o.seed_elements(**icebergs)
39+
o.run(duration=timedelta(days=2), outfile=outfile)
40+
else:
41+
o = opendrift.open(outfile)
2342

24-
o.run(duration=timedelta(days=3))
25-
o.plot(fast=True)
26-
o.plot_property('draft')
43+
o.animation(color='draft')
44+
45+
#%%
46+
# .. image:: /gallery/animations/example_openberg_0.gif
47+
48+
o.plot()
49+
50+
#%%
51+
# Plotting the speed of icebergs
52+
iceberg_speed = np.sqrt(o.result.iceb_x_velocity**2 + o.result.iceb_y_velocity**2)
53+
iceberg_speed.plot.line(x='time', add_legend=False, color='gray')
54+
plt.ylabel('Iceberg speed [m/s]')
55+
plt.show()

opendrift/elements/elements.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#
1515
# Copyright 2015, Knut-Frode Dagestad, MET Norway
1616

17+
import logging; logger = logging.getLogger(__name__)
1718
from collections import OrderedDict
1819
import numpy as np
1920

@@ -122,6 +123,12 @@ def __init__(self, **kwargs):
122123
raise TypeError('Redundant arguments: %s' %
123124
str(list(redundant_args)))
124125

126+
# Ravel any mulitidimensional kwargs
127+
for kname in kwargs:
128+
if hasattr(kwargs[kname], 'ndim') and kwargs[kname].ndim > 1:
129+
logger.debug(f'array {kname} is multidimensional -> flattening with ravel')
130+
kwargs[kname] = kwargs[kname].ravel()
131+
125132
# Check that input arrays have same length
126133
array_lengths = [1]*len(kwargs)
127134
for i, input_value in enumerate(kwargs.values()):

0 commit comments

Comments
 (0)