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)
21:59:58 INFO    opendrift:509: OpenDriftSimulation initialised (version 1.13.0 / v1.13.0-61-gf300067)
21:59:58 INFO    opendrift.models.basemodel.environment:206: Adding a dynamical landmask with max. priority based on assumed maximum speed of 2.0 m/s. Adding a customised landmask may be faster...
22:00:02 INFO    opendrift.models.basemodel.environment:233: Fallback values will be used for the following variables which have no readers:
22:00:02 INFO    opendrift.models.basemodel.environment:236:    x_sea_water_velocity: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_height: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    x_wind: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    y_wind: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    upward_sea_water_velocity: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    ocean_vertical_diffusivity: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_wave_significant_height: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_wave_period_at_variance_spectral_density_maximum: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_swell_wave_to_direction: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_swell_wave_peak_period_from_variance_spectral_density: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_swell_wave_significant_height: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_wind_wave_to_direction: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_wind_wave_mean_period: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_surface_wind_wave_significant_height: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    surface_downward_x_stress: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    surface_downward_y_stress: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    turbulent_kinetic_energy: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    turbulent_generic_length_scale: 0.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    ocean_mixed_layer_thickness: 50.000000
22:00:02 INFO    opendrift.models.basemodel.environment:236:    sea_floor_depth_below_sea_level: 10000.000000
22:00:02 INFO    opendrift:919: Using existing reader for land_binary_mask
22:00:02 INFO    opendrift:930: All points are in ocean
22:00:02 INFO    opendrift:2056: 2025-03-05 21:59:58.138967 - step 1 of 15 - 10000 active elements (0 deactivated)
22:00:02 INFO    opendrift:2056: 2025-03-05 22:59:58.138967 - step 2 of 15 - 10000 active elements (0 deactivated)
22:00:02 INFO    opendrift:2056: 2025-03-05 23:59:58.138967 - step 3 of 15 - 10000 active elements (0 deactivated)
22:00:02 INFO    opendrift:2056: 2025-03-06 00:59:58.138967 - step 4 of 15 - 10000 active elements (0 deactivated)
22:00:02 INFO    opendrift:2056: 2025-03-06 01:59:58.138967 - step 5 of 15 - 10000 active elements (0 deactivated)
22:00:02 INFO    opendrift:2056: 2025-03-06 02:59:58.138967 - step 6 of 15 - 10000 active elements (0 deactivated)
22:00:02 INFO    opendrift:2056: 2025-03-06 03:59:58.138967 - step 7 of 15 - 10000 active elements (0 deactivated)
22:00:02 INFO    opendrift:2056: 2025-03-06 04:59:58.138967 - step 8 of 15 - 10000 active elements (0 deactivated)
22:00:02 INFO    opendrift:2056: 2025-03-06 05:59:58.138967 - step 9 of 15 - 10000 active elements (0 deactivated)
22:00:03 INFO    opendrift:2056: 2025-03-06 06:59:58.138967 - step 10 of 15 - 10000 active elements (0 deactivated)
22:00:03 INFO    opendrift:2056: 2025-03-06 07:59:58.138967 - step 11 of 15 - 10000 active elements (0 deactivated)
22:00:03 INFO    opendrift:2056: 2025-03-06 08:59:58.138967 - step 12 of 15 - 10000 active elements (0 deactivated)
22:00:03 INFO    opendrift:2056: 2025-03-06 09:59:58.138967 - step 13 of 15 - 10000 active elements (0 deactivated)
22:00:03 INFO    opendrift:2056: 2025-03-06 10:59:58.138967 - step 14 of 15 - 10000 active elements (0 deactivated)
22:00:03 INFO    opendrift:2056: 2025-03-06 11:59:58.138967 - step 15 of 15 - 10000 active elements (0 deactivated)
<xarray.Dataset> Size: 24MB
Dimensions:                                                                              (
                                                                                          trajectory: 10000,
                                                                                          time: 16)
Coordinates:
  * trajectory                                                                           (trajectory) int32 40kB ...
  * time                                                                                 (time) datetime64[ns] 128B ...
Data variables: (12/35)
    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 ...
    ...                                                                                   ...
    surface_downward_y_stress                                                            (trajectory, time) float32 640kB ...
    turbulent_kinetic_energy                                                             (trajectory, time) float32 640kB ...
    turbulent_generic_length_scale                                                       (trajectory, time) float32 640kB ...
    ocean_mixed_layer_thickness                                                          (trajectory, time) float32 640kB ...
    sea_floor_depth_below_sea_level                                                      (trajectory, time) float32 640kB ...
    land_binary_mask                                                                     (trajectory, time) float32 640kB ...
Attributes: (12/153)
    Conventions:                                                             ...
    standard_name_vocabulary:                                                ...
    featureType:                                                             ...
    title:                                                                   ...
    summary:                                                                 ...
    keywords:                                                                ...
    ...                                                                                                               ...
    geospatial_lon_units:                                                    ...
    geospatial_lon_resolution:                                               ...
    runtime:                                                                 ...
    geospatial_vertical_min:                                                 ...
    geospatial_vertical_max:                                                 ...
    geospatial_vertical_positive:                                            ...


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'
22:00:04 INFO    opendrift:3887:        calculating for origin_marker 0...
22:00:04 INFO    opendrift:3887:        calculating for origin_marker 1...
22:00:04 INFO    opendrift:3887:        calculating for origin_marker 2...
22:00:04 INFO    opendrift:3887:        calculating for origin_marker 3...
22:00:04 INFO    opendrift:3887:        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.11/site-packages/cartopy/mpl/geoaxes.py:1692: UserWarning: No data for colormapping provided via 'c'. Parameters 'vmin', 'vmax' will be ignored
  result = super().scatter(*args, **kwargs)
22:00:18 INFO    opendrift:4600: Saving animation to /root/project/docs/source/gallery/animations/example_dominating_0.gif...
22:00:29 INFO    opendrift:3036: Time to make animation: 0:00:24.428647
../_images/example_dominating_0.gif
os.remove(of)

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

Gallery generated by Sphinx-Gallery