Note
Go to the end to download the full example code.
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)
09:39:25 INFO opendrift.models.basemodel:515: OpenDriftSimulation initialised (version 1.11.13 / v1.11.13-48-g5bea84b)
09:39:25 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...
09:39:31 INFO opendrift.models.basemodel.environment:245: Fallback values will be used for the following variables which have no readers:
09:39:31 INFO opendrift.models.basemodel.environment:248: x_sea_water_velocity: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_height: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: x_wind: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: y_wind: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: upward_sea_water_velocity: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: ocean_vertical_diffusivity: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_wave_significant_height: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_wave_stokes_drift_x_velocity: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_wave_stokes_drift_y_velocity: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_wave_period_at_variance_spectral_density_maximum: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_swell_wave_to_direction: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_swell_wave_peak_period_from_variance_spectral_density: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_swell_wave_significant_height: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_wind_wave_to_direction: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_wind_wave_mean_period: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_surface_wind_wave_significant_height: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: surface_downward_x_stress: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: surface_downward_y_stress: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: turbulent_kinetic_energy: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: turbulent_generic_length_scale: 0.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: ocean_mixed_layer_thickness: 50.000000
09:39:31 INFO opendrift.models.basemodel.environment:248: sea_floor_depth_below_sea_level: 10000.000000
09:39:32 INFO opendrift.models.basemodel:936: Using existing reader for land_binary_mask
09:39:32 INFO opendrift.models.basemodel:947: All points are in ocean
09:39:32 INFO opendrift.models.basemodel:2038: 2024-10-01 09:39:25.364683 - step 1 of 15 - 10000 active elements (0 deactivated)
09:39:32 INFO opendrift.models.basemodel:2038: 2024-10-01 10:39:25.364683 - step 2 of 15 - 10000 active elements (0 deactivated)
09:39:32 INFO opendrift.models.basemodel:2038: 2024-10-01 11:39:25.364683 - step 3 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 12:39:25.364683 - step 4 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 13:39:25.364683 - step 5 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 14:39:25.364683 - step 6 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 15:39:25.364683 - step 7 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 16:39:25.364683 - step 8 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 17:39:25.364683 - step 9 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 18:39:25.364683 - step 10 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 19:39:25.364683 - step 11 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 20:39:25.364683 - step 12 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 21:39:25.364683 - step 13 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 22:39:25.364683 - step 14 of 15 - 10000 active elements (0 deactivated)
09:39:33 INFO opendrift.models.basemodel:2038: 2024-10-01 23:39:25.364683 - step 15 of 15 - 10000 active elements (0 deactivated)
09:39:34 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'
09:39:35 DEBUG opendrift.config:168: Adding 18 config items from __init__
09:39:35 DEBUG opendrift.config:178: Overwriting config item readers:max_number_of_fails
09:39:35 DEBUG opendrift.config:168: Adding 5 config items from __init__
09:39:35 INFO opendrift.models.basemodel:515: OpenDriftSimulation initialised (version 1.11.13 / v1.11.13-48-g5bea84b)
09:39:35 DEBUG opendrift.config:168: Adding 15 config items from oceandrift
09:39:35 DEBUG opendrift.config:178: Overwriting config item seed:z
09:39:35 DEBUG opendrift.export.io_netcdf:239: Importing with Xarray from test.nc
09:39:35 INFO opendrift:118: Returning <class 'opendrift.models.oceandrift.OceanDrift'> object
09:39:36 INFO opendrift.models.basemodel:3892: calculating for origin_marker 0...
09:39:36 INFO opendrift.models.basemodel:3892: calculating for origin_marker 1...
09:39:36 INFO opendrift.models.basemodel:3892: calculating for origin_marker 2...
09:39:36 INFO opendrift.models.basemodel:3892: calculating for origin_marker 3...
09:39:36 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)
09:39:36 DEBUG opendrift.models.basemodel:2365: Setting up map: corners=None, fast=False, lscale=None
09:39:37 DEBUG opendrift.readers.reader_global_landmask:83: 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:1683: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap', 'vmin', 'vmax' will be ignored
result = super().scatter(*args, **kwargs)
09:40:00 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:01 DEBUG opendrift.models.basemodel:3044: Saving animation..
09:40:01 INFO opendrift.models.basemodel:4608: Saving animation to /root/project/docs/source/gallery/animations/example_dominating_0.gif...
09:40:01 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:03 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:04 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:06 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:07 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:09 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:11 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:12 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:14 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:15 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:17 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:19 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:20 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:22 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:24 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:25 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:27 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:28 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:30 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:32 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:34 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:35 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:37 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:39 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:41 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:43 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:44 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:46 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:48 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:49 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:51 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:53 DEBUG opendrift.readers.reader_global_landmask:83: Adding GSHHG shapes from cartopy, scale: h, extent: (3.435968017578124, 5.1932105064392085, 59.68026428222656, 60.350817871093746)..
09:40:55 DEBUG opendrift.models.basemodel:4646: MPLBACKEND = agg
09:40:55 DEBUG opendrift.models.basemodel:4647: DISPLAY = None
09:40:55 DEBUG opendrift.models.basemodel:4648: Time to save animation: 0:00:53.410637
09:40:55 INFO opendrift.models.basemodel:3037: Time to make animation: 0:01:18.310630
os.remove(of)
Total running time of the script: (1 minutes 36.397 seconds)