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)
o.set_config('environment:constant:y_sea_water_velocity', .1)

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.run(steps=15, outfile=of)
15:21:40 INFO    opendrift:568: OpenDriftSimulation initialised (version 1.14.3 / v1.14.3-4-ga7b402c)
15:21:40 INFO    opendrift.models.basemodel.environment:206: Adding a global landmask from GSHHG
15:21:44 INFO    opendrift.models.basemodel.environment:229: Fallback values will be used for the following variables which have no readers:
15:21:44 INFO    opendrift.models.basemodel.environment:232:    x_sea_water_velocity: 0.000000
15:21:44 INFO    opendrift.models.basemodel.environment:232:    sea_surface_height: 0.000000
15:21:44 INFO    opendrift.models.basemodel.environment:232:    x_wind: 0.000000
15:21:44 INFO    opendrift.models.basemodel.environment:232:    y_wind: 0.000000
15:21:44 INFO    opendrift.models.basemodel.environment:232:    upward_sea_water_velocity: 0.000000
15:21:44 INFO    opendrift.models.basemodel.environment:232:    ocean_vertical_diffusivity: 0.000000
15:21:44 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_significant_height: 0.000000
15:21:44 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
15:21:44 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
15:21:44 INFO    opendrift.models.basemodel.environment:232:    ocean_mixed_layer_thickness: 50.000000
15:21:44 INFO    opendrift.models.basemodel.environment:232:    sea_floor_depth_below_sea_level: 10000.000000
15:21:44 INFO    opendrift:1778: Skipping environment variable ocean_vertical_diffusivity because of condition ['drift:vertical_mixing', 'is', False]
15:21:44 INFO    opendrift:1778: Skipping environment variable ocean_mixed_layer_thickness because of condition ['drift:vertical_mixing', 'is', False]
15:21:44 INFO    opendrift:1789: Storing previous values of element property lon because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous']))
15:21:44 INFO    opendrift:1789: Storing previous values of element property lat because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous']))
15:21:44 INFO    opendrift:1797: Storing previous values of environment variable sea_surface_height because of condition ['drift:vertical_advection', 'is', True]
15:21:44 INFO    opendrift:944: Using existing reader for land_binary_mask to move elements to ocean
15:21:44 INFO    opendrift:974: All points are in ocean
15:21:44 INFO    opendrift:2085: 2025-09-12 15:21:40.023463 - step 1 of 15 - 10000 active elements (0 deactivated)
15:21:44 INFO    opendrift:2085: 2025-09-12 16:21:40.023463 - step 2 of 15 - 10000 active elements (0 deactivated)
15:21:44 INFO    opendrift:2085: 2025-09-12 17:21:40.023463 - step 3 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-12 18:21:40.023463 - step 4 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-12 19:21:40.023463 - step 5 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-12 20:21:40.023463 - step 6 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-12 21:21:40.023463 - step 7 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-12 22:21:40.023463 - step 8 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-12 23:21:40.023463 - step 9 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-13 00:21:40.023463 - step 10 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-13 01:21:40.023463 - step 11 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-13 02:21:40.023463 - step 12 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-13 03:21:40.023463 - step 13 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-13 04:21:40.023463 - step 14 of 15 - 10000 active elements (0 deactivated)
15:21:45 INFO    opendrift:2085: 2025-09-13 05:21:40.023463 - step 15 of 15 - 10000 active elements (0 deactivated)
<xarray.Dataset> Size: 15MB
Dimensions:                                   (trajectory: 10000, time: 16)
Coordinates:
  * trajectory                                (trajectory) int32 40kB 0 ... 9999
  * time                                      (time) datetime64[ns] 128B 2025...
Data variables: (12/21)
    status                                    (trajectory, time) float64 1MB ...
    moving                                    (trajectory, time) float64 1MB ...
    age_seconds                               (trajectory, time) float32 640kB ...
    origin_marker                             (trajectory, time) float64 1MB ...
    lon                                       (trajectory, time) float32 640kB ...
    lat                                       (trajectory, time) float32 640kB ...
    ...                                        ...
    upward_sea_water_velocity                 (trajectory, time) float32 640kB ...
    sea_surface_wave_significant_height       (trajectory, time) float32 640kB ...
    sea_surface_wave_stokes_drift_x_velocity  (trajectory, time) float32 640kB ...
    sea_surface_wave_stokes_drift_y_velocity  (trajectory, time) float32 640kB ...
    sea_floor_depth_below_sea_level           (trajectory, time) float32 640kB ...
    land_binary_mask                          (trajectory, time) float32 640kB ...
Attributes: (12/119)
    Conventions:                                                           CF...
    standard_name_vocabulary:                                              CF...
    featureType:                                                           tr...
    title:                                                                 Op...
    summary:                                                               Ou...
    keywords:                                                              tr...
    ...                                                                    ...
    geospatial_lon_units:                                                  de...
    geospatial_lon_resolution:                                             point
    runtime:                                                               0:...
    geospatial_vertical_min:                                               0.0
    geospatial_vertical_max:                                               0.0
    geospatial_vertical_positive:                                          up


Calculate spatial density of elements at 1500m grid spacing

d = o.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'
15:21:46 INFO    opendrift:3892:        calculating for origin_marker 0...
15:21:46 INFO    opendrift:3892:        calculating for origin_marker 1...
15:21:46 INFO    opendrift:3892:        calculating for origin_marker 2...
15:21:46 INFO    opendrift:3892:        calculating for origin_marker 3...
15:21:46 INFO    opendrift:3892:        calculating for origin_marker 4...

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

o.animation(background=dom, show_elements=False, bgalpha=1,
             legend=list(o.origin_marker.values()), colorbar=False, vmin=0, vmax=4)
/opt/conda/envs/opendrift/lib/python3.13/site-packages/cartopy/mpl/geoaxes.py:1677: UserWarning: No data for colormapping provided via 'c'. Parameters 'vmin', 'vmax' will be ignored
  result = super().scatter(*args, **kwargs)
15:22:02 INFO    opendrift:4612: Saving animation to /root/project/docs/source/gallery/animations/example_dominating_0.gif...
15:22:17 INFO    opendrift:3053: Time to make animation: 0:00:30.666375
../_images/example_dominating_0.gif
os.remove(of)

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

Gallery generated by Sphinx-Gallery