Skip to content

Commit 60ee835

Browse files
authored
Merge pull request #1628 from knutfrode/dev
[run-ex] Using TrajAn to calculate density in example_leeway, but plo…
2 parents 18aadce + e09cf8a commit 60ee835

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

examples/example_leeway.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from datetime import timedelta
88
import cmocean
99
import xarray as xr
10+
import trajan as ta
1011
from opendrift import test_data_folder as tdf
1112
from opendrift.readers import reader_netCDF_CF_generic
1213
from opendrift.models.leeway import Leeway
@@ -36,7 +37,7 @@
3637

3738
#%%
3839
# Running model
39-
o.run(duration=timedelta(hours=48), time_step=900, time_step_output=3600)
40+
ds = o.run(duration=timedelta(hours=48), time_step=1800, time_step_output=3600)
4041

4142
#%%
4243
# Print and plot results
@@ -56,8 +57,9 @@
5657

5758
#%%
5859
# Plot density of stranded elements
59-
d, dsub, dstr, lon, lat = o.get_density_array(pixelsize_m=3000)
60-
strand_density = xr.DataArray(dstr[-1,:,:], coords={'lon_bin': lon[0:-1], 'lat_bin': lat[0:-1]})
61-
o.plot(fast=True, background=strand_density.where(strand_density>0),
62-
vmin=0, vmax=20, cmap=cmocean.cm.dense, clabel='Density of stranded elements',
60+
ds_stranded = ds.where(ds.status==1)
61+
grid = ds_stranded.traj.make_grid(dx=3000)
62+
ds_conc = ds_stranded.traj.concentration(grid).sum(dim='time')
63+
o.plot(fast=True, background=ds_conc.number.where(ds_conc.number>0),
64+
cmap=cmocean.cm.thermal, clabel='Density of stranded elements',
6365
show_elements=False, linewidth=0)

opendrift/models/basemodel/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3640,9 +3640,14 @@ def plot(self,
36403640
else:
36413641
time = None
36423642
if isinstance(background, xr.DataArray):
3643-
map_x = background.coords['lon_bin']
3644-
map_y = background.coords['lat_bin']
3645-
scalar = background
3643+
if 'lon' in background.coords: # From TrajAn
3644+
map_x = background.coords['lon']
3645+
map_y = background.coords['lat']
3646+
scalar = background.T
3647+
else: # From get_density_array
3648+
map_x = background.coords['lon_bin']
3649+
map_y = background.coords['lat_bin']
3650+
scalar = background
36463651
map_y, map_x = np.meshgrid(map_y, map_x)
36473652
elif background == 'residence':
36483653
scalar, lon_res, lat_res = self.get_residence_time(

0 commit comments

Comments
 (0)