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)
20:17:31 INFO    opendrift.models.basemodel:515: OpenDriftSimulation initialised (version 1.12.0 / v1.12.0-26-g390e945)
20:17:31 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...
20:17:35 INFO    opendrift.models.basemodel.environment:245: Fallback values will be used for the following variables which have no readers:
20:17:35 INFO    opendrift.models.basemodel.environment:248:    x_sea_water_velocity: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_height: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    x_wind: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    y_wind: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    upward_sea_water_velocity: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    ocean_vertical_diffusivity: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_significant_height: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_period_at_variance_spectral_density_maximum: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_swell_wave_to_direction: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_swell_wave_peak_period_from_variance_spectral_density: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_swell_wave_significant_height: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wind_wave_to_direction: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wind_wave_mean_period: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wind_wave_significant_height: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    surface_downward_x_stress: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    surface_downward_y_stress: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    turbulent_kinetic_energy: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    turbulent_generic_length_scale: 0.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    ocean_mixed_layer_thickness: 50.000000
20:17:35 INFO    opendrift.models.basemodel.environment:248:    sea_floor_depth_below_sea_level: 10000.000000
20:17:37 INFO    opendrift.models.basemodel:935: Using existing reader for land_binary_mask
20:17:37 INFO    opendrift.models.basemodel:946: All points are in ocean
20:17:37 INFO    opendrift.models.basemodel:2035: 2024-12-12 20:17:31.295947 - step 1 of 15 - 10000 active elements (0 deactivated)
20:17:37 INFO    opendrift.models.basemodel:2035: 2024-12-12 21:17:31.295947 - step 2 of 15 - 10000 active elements (0 deactivated)
20:17:37 INFO    opendrift.models.basemodel:2035: 2024-12-12 22:17:31.295947 - step 3 of 15 - 10000 active elements (0 deactivated)
20:17:37 INFO    opendrift.models.basemodel:2035: 2024-12-12 23:17:31.295947 - step 4 of 15 - 10000 active elements (0 deactivated)
20:17:38 INFO    opendrift.models.basemodel:2035: 2024-12-13 00:17:31.295947 - step 5 of 15 - 10000 active elements (0 deactivated)
20:17:38 INFO    opendrift.models.basemodel:2035: 2024-12-13 01:17:31.295947 - step 6 of 15 - 10000 active elements (0 deactivated)
20:17:38 INFO    opendrift.models.basemodel:2035: 2024-12-13 02:17:31.295947 - step 7 of 15 - 10000 active elements (0 deactivated)
20:17:38 INFO    opendrift.models.basemodel:2035: 2024-12-13 03:17:31.295947 - step 8 of 15 - 10000 active elements (0 deactivated)
20:17:38 INFO    opendrift.models.basemodel:2035: 2024-12-13 04:17:31.295947 - step 9 of 15 - 10000 active elements (0 deactivated)
20:17:38 INFO    opendrift.models.basemodel:2035: 2024-12-13 05:17:31.295947 - step 10 of 15 - 10000 active elements (0 deactivated)
20:17:38 INFO    opendrift.models.basemodel:2035: 2024-12-13 06:17:31.295947 - step 11 of 15 - 10000 active elements (0 deactivated)
20:17:39 INFO    opendrift.models.basemodel:2035: 2024-12-13 07:17:31.295947 - step 12 of 15 - 10000 active elements (0 deactivated)
20:17:39 INFO    opendrift.models.basemodel:2035: 2024-12-13 08:17:31.295947 - step 13 of 15 - 10000 active elements (0 deactivated)
20:17:39 INFO    opendrift.models.basemodel:2035: 2024-12-13 09:17:31.295947 - step 14 of 15 - 10000 active elements (0 deactivated)
20:17:39 INFO    opendrift.models.basemodel:2035: 2024-12-13 10:17:31.295947 - step 15 of 15 - 10000 active elements (0 deactivated)
20:17:39 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'
20:17:41 DEBUG   opendrift.config:168: Adding 18 config items from __init__
20:17:41 DEBUG   opendrift.config:178:   Overwriting config item readers:max_number_of_fails
20:17:41 DEBUG   opendrift.config:168: Adding 5 config items from __init__
20:17:41 INFO    opendrift.models.basemodel:515: OpenDriftSimulation initialised (version 1.12.0 / v1.12.0-26-g390e945)
20:17:41 DEBUG   opendrift.config:168: Adding 15 config items from oceandrift
20:17:41 DEBUG   opendrift.config:178:   Overwriting config item seed:z
20:17:41 DEBUG   opendrift.export.io_netcdf:239: Importing with Xarray from test.nc
20:17:41 INFO    opendrift:118: Returning <class 'opendrift.models.oceandrift.OceanDrift'> object
20:17:42 INFO    opendrift.models.basemodel:3892:       calculating for origin_marker 0...
20:17:42 INFO    opendrift.models.basemodel:3892:       calculating for origin_marker 1...
20:17:42 INFO    opendrift.models.basemodel:3892:       calculating for origin_marker 2...
20:17:42 INFO    opendrift.models.basemodel:3892:       calculating for origin_marker 3...
20:17:42 INFO    opendrift.models.basemodel:3892:       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)
20:17:42 DEBUG   opendrift.models.basemodel:2364: Setting up map: corners=None, fast=False, lscale=None
20:17:43 DEBUG   opendrift.readers.reader_global_landmask:78: 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)
20:18:08 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:10 DEBUG   opendrift.models.basemodel:3044: Saving animation..
20:18:10 INFO    opendrift.models.basemodel:4613: Saving animation to /root/project/docs/source/gallery/animations/example_dominating_0.gif...
20:18:11 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:13 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:15 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:16 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:18 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:20 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:22 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:24 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:26 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:27 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:29 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:31 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:32 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:34 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:35 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:37 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:39 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:40 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:42 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:43 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:45 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:46 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:48 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:49 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:51 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:52 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:54 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:55 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:57 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:18:58 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:19:00 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:19:01 DEBUG   opendrift.readers.reader_global_landmask:78: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
20:19:03 DEBUG   opendrift.models.basemodel:4651: MPLBACKEND = agg
20:19:03 DEBUG   opendrift.models.basemodel:4652: DISPLAY = None
20:19:03 DEBUG   opendrift.models.basemodel:4653: Time to save animation: 0:00:52.611482
20:19:03 INFO    opendrift.models.basemodel:3037: Time to make animation: 0:01:20.693039
../_images/example_dominating_0.gif
os.remove(of)

Total running time of the script: (1 minutes 37.247 seconds)

Gallery generated by Sphinx-Gallery