Show dominating source

import os
from datetime import datetime
import xarray as xr
import opendrift
from opendrift.models.oceandrift import OceanDrift

of = 'test.nc'

Seed elements at 5 different locations/longitudes

lons = [4, 4.2, 4.3, 4.32, 4.6]
t = datetime.now()

o = OceanDrift(loglevel=20)

for i, lon in enumerate(lons):
    o.seed_elements(lon=lon, lat=60, radius=3000, number=2000, time=t, origin_marker_name='Lon %f' % lon)
o.set_config('environment:constant:y_sea_water_velocity', .1)
o.run(steps=15, outfile=of)

Out:

16:12:57 INFO    opendrift.models.basemodel: OpenDriftSimulation initialised (version 1.7.1 / v1.7.1-145-gdf95ed3)
16:12:58 INFO    opendrift.models.basemodel: Fallback values will be used for the following variables which have no readers:
16:12:58 INFO    opendrift.models.basemodel:    x_sea_water_velocity: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    x_wind: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    y_wind: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    upward_sea_water_velocity: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    ocean_vertical_diffusivity: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    sea_surface_wave_significant_height: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    sea_surface_wave_period_at_variance_spectral_density_maximum: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    surface_downward_x_stress: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    surface_downward_y_stress: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    turbulent_kinetic_energy: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    turbulent_generic_length_scale: 0.000000
16:12:58 INFO    opendrift.models.basemodel:    ocean_mixed_layer_thickness: 50.000000
16:12:58 INFO    opendrift.models.basemodel:    sea_floor_depth_below_sea_level: 10000.000000
16:12:58 INFO    opendrift.models.basemodel: Adding a dynamical landmask with max. priority based on assumed maximum speed of 1 m/s. Adding a customised landmask may be faster...
16:13:00 INFO    opendrift.models.basemodel: Using existing reader for land_binary_mask
16:13:00 INFO    opendrift.models.basemodel: All points are in ocean
16:13:01 INFO    opendrift.models.basemodel: 2021-10-23 16:12:57.686858 - step 1 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-23 17:12:57.686858 - step 2 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-23 18:12:57.686858 - step 3 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-23 19:12:57.686858 - step 4 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-23 20:12:57.686858 - step 5 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-23 21:12:57.686858 - step 6 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-23 22:12:57.686858 - step 7 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-23 23:12:57.686858 - step 8 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-24 00:12:57.686858 - step 9 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-24 01:12:57.686858 - step 10 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-24 02:12:57.686858 - step 11 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-24 03:12:57.686858 - step 12 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-24 04:12:57.686858 - step 13 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-24 05:12:57.686858 - step 14 of 15 - 10000 active elements (0 deactivated)
16:13:01 INFO    opendrift.models.basemodel: 2021-10-24 06:12:57.686858 - step 15 of 15 - 10000 active elements (0 deactivated)
16:13:02 INFO    opendrift.export.io_netcdf: Wrote 16 steps to file test.nc

Calculate spatial density of elements at 1500m grid spacing

oa = opendrift.open_xarray(of)
oa.ds = oa.ds.where(oa.ds.status==0)
d = oa.get_histogram(pixelsize_m=1500, weights=None)
dom = d.argmax(dim='origin_marker', skipna=True)
dom = dom.where(d.sum(dim='origin_marker')>0)
dom.name = 'Dominating source'

Out:

16:13:03 DEBUG   opendrift.models.basemodel: Adding 17 config items from basemodel
16:13:03 DEBUG   opendrift.models.basemodel: Adding 4 config items from basemodel
16:13:03 DEBUG   opendrift.models.basemodel: Adding 36 config items from basemodel
16:13:03 INFO    opendrift.models.basemodel: OpenDriftSimulation initialised (version 1.7.1 / v1.7.1-145-gdf95ed3)
16:13:03 DEBUG   opendrift.models.basemodel: Adding 14 config items from oceandrift
16:13:03 DEBUG   opendrift.models.basemodel:   Overwriting config item seed:z
16:13:03 DEBUG   opendrift.export.io_netcdf: Importing with Xarray from test.nc
16:13:03 INFO    opendrift: Returning <class 'opendrift.models.oceandrift.OceanDrift'> object
16:13:03 INFO    opendrift.models.basemodel:    calculating for origin_marker 0...
16:13:03 INFO    opendrift.models.basemodel:    calculating for origin_marker 1...
16:13:04 INFO    opendrift.models.basemodel:    calculating for origin_marker 2...
16:13:04 INFO    opendrift.models.basemodel:    calculating for origin_marker 3...
16:13:04 INFO    opendrift.models.basemodel:    calculating for origin_marker 4...

Show which of the 5 sources are dominating within each grid cell

oa.animation(background=dom, show_elements=False, bgalpha=1,
             legend=oa.origin_marker, colorbar=False, vmin=0, vmax=4)

Out:

16:13:04 DEBUG   opendrift.readers.reader_global_landmask: Using existing GSHHS shapes..
16:13:04 DEBUG   opendrift_landmask_data.mask: mask already memmapped
16:13:22 INFO    opendrift.models.basemodel: Saving animation to /root/project/docs/source/gallery/animations/example_dominating_0.gif...
16:13:22 INFO    opendrift.models.basemodel: Making animated gif...
MovieWriter imagemagick unavailable; using Pillow instead.
16:13:35 DEBUG   opendrift.models.basemodel: Time to make animation: 0:00:31.047339
../_images/example_dominating_0.gif
os.remove(of)

Total running time of the script: ( 0 minutes 37.460 seconds)

Gallery generated by Sphinx-Gallery