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)
17:24:12 INFO    opendrift.models.basemodel:512: OpenDriftSimulation initialised (version 1.12.0 / v1.12.0-55-g4faa301)
17:24:12 INFO    opendrift.models.basemodel.environment:218: Adding a dynamical landmask with max. priority based on assumed maximum speed of 2.0 m/s. Adding a customised landmask may be faster...
17:24:15 INFO    opendrift.models.basemodel.environment:245: Fallback values will be used for the following variables which have no readers:
17:24:15 INFO    opendrift.models.basemodel.environment:248:    x_sea_water_velocity: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_height: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    x_wind: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    y_wind: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    upward_sea_water_velocity: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    ocean_vertical_diffusivity: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_significant_height: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_period_at_variance_spectral_density_maximum: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_swell_wave_to_direction: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_swell_wave_peak_period_from_variance_spectral_density: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_swell_wave_significant_height: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wind_wave_to_direction: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wind_wave_mean_period: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wind_wave_significant_height: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    surface_downward_x_stress: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    surface_downward_y_stress: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    turbulent_kinetic_energy: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    turbulent_generic_length_scale: 0.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    ocean_mixed_layer_thickness: 50.000000
17:24:15 INFO    opendrift.models.basemodel.environment:248:    sea_floor_depth_below_sea_level: 10000.000000
17:24:17 INFO    opendrift.models.basemodel:932: Using existing reader for land_binary_mask
17:24:17 INFO    opendrift.models.basemodel:943: All points are in ocean
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-14 17:24:12.564145 - step 1 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-14 18:24:12.564145 - step 2 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-14 19:24:12.564145 - step 3 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-14 20:24:12.564145 - step 4 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-14 21:24:12.564145 - step 5 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-14 22:24:12.564145 - step 6 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-14 23:24:12.564145 - step 7 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-15 00:24:12.564145 - step 8 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-15 01:24:12.564145 - step 9 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-15 02:24:12.564145 - step 10 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-15 03:24:12.564145 - step 11 of 15 - 10000 active elements (0 deactivated)
17:24:17 INFO    opendrift.models.basemodel:2032: 2025-01-15 04:24:12.564145 - step 12 of 15 - 10000 active elements (0 deactivated)
17:24:18 INFO    opendrift.models.basemodel:2032: 2025-01-15 05:24:12.564145 - step 13 of 15 - 10000 active elements (0 deactivated)
17:24:18 INFO    opendrift.models.basemodel:2032: 2025-01-15 06:24:12.564145 - step 14 of 15 - 10000 active elements (0 deactivated)
17:24:18 INFO    opendrift.models.basemodel:2032: 2025-01-15 07:24:12.564145 - step 15 of 15 - 10000 active elements (0 deactivated)
17:24:18 INFO    opendrift.export.io_netcdf:121: Wrote 16 steps to file test.nc
/opt/conda/envs/opendrift/lib/python3.11/site-packages/numpy/ma/core.py:467: RuntimeWarning: invalid value encountered in cast
  fill_value = np.array(fill_value, copy=False, dtype=ndtype)

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'
17:24:19 DEBUG   opendrift.config:168: Adding 18 config items from __init__
17:24:19 DEBUG   opendrift.config:178:   Overwriting config item readers:max_number_of_fails
17:24:19 DEBUG   opendrift.config:168: Adding 5 config items from __init__
17:24:19 INFO    opendrift.models.basemodel:512: OpenDriftSimulation initialised (version 1.12.0 / v1.12.0-55-g4faa301)
17:24:19 DEBUG   opendrift.config:168: Adding 15 config items from oceandrift
17:24:19 DEBUG   opendrift.config:178:   Overwriting config item seed:z
17:24:19 DEBUG   opendrift.export.io_netcdf:239: Importing with Xarray from test.nc
17:24:19 INFO    opendrift:118: Returning <class 'opendrift.models.oceandrift.OceanDrift'> object
17:24:20 INFO    opendrift.models.basemodel:3885:       calculating for origin_marker 0...
17:24:20 INFO    opendrift.models.basemodel:3885:       calculating for origin_marker 1...
17:24:20 INFO    opendrift.models.basemodel:3885:       calculating for origin_marker 2...
17:24:20 INFO    opendrift.models.basemodel:3885:       calculating for origin_marker 3...
17:24:20 INFO    opendrift.models.basemodel:3885:       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)
17:24:20 DEBUG   opendrift.models.basemodel:2361: Setting up map: corners=None, fast=False, lscale=None
17:24:21 DEBUG   opendrift.readers.reader_global_landmask:84: Loading shapes with Cartopy shapereader...
17:24:32 DEBUG   opendrift.readers.reader_global_landmask:84: Loading shapes with Cartopy shapereader...
17:24:32 DEBUG   opendrift.readers.reader_global_landmask:84: Loading shapes with Cartopy shapereader...
17:24:32 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
/opt/conda/envs/opendrift/lib/python3.11/site-packages/cartopy/mpl/geoaxes.py:1692: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap', 'vmin', 'vmax' will be ignored
  result = super().scatter(*args, **kwargs)
17:24:33 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:33 DEBUG   opendrift.models.basemodel:3041: Saving animation..
17:24:34 INFO    opendrift.models.basemodel:4606: Saving animation to /root/project/docs/source/gallery/animations/example_dominating_0.gif...
17:24:34 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:34 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:34 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:35 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:35 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:35 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:36 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:36 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:36 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:36 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:37 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:37 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:37 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:37 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:38 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:38 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:38 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:38 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:39 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:39 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:39 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:40 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:40 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:40 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:41 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:41 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:41 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:41 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:42 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:42 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:42 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:42 DEBUG   opendrift.readers.reader_global_landmask:123: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
17:24:43 DEBUG   opendrift.models.basemodel:4644: MPLBACKEND = agg
17:24:43 DEBUG   opendrift.models.basemodel:4645: DISPLAY = None
17:24:43 DEBUG   opendrift.models.basemodel:4646: Time to save animation: 0:00:09.292315
17:24:43 INFO    opendrift.models.basemodel:3034: Time to make animation: 0:00:22.952307
../_images/example_dominating_0.gif
os.remove(of)

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

Gallery generated by Sphinx-Gallery