Drift at different depths

from datetime import datetime, timedelta
import numpy as np
from opendrift.models.oceandrift import OceanDrift

o = OceanDrift(loglevel=20)  # Set loglevel to 0 for debug information
14:11:34 INFO    opendrift:569: OpenDriftSimulation initialised (version 1.14.3 / v1.14.3-25-gcd89af5)

Using live data from Thredds

o.add_readers_from_list([
    'https://thredds.met.no/thredds/dodsC/fou-hi/norkystv3_800m_m00_be'])

Adding some diffusion

o.set_config('drift:horizontal_diffusivity', 10)  # m2/s

Seed 1000 elements at random depths

z = -np.random.rand(2000)*50
o.seed_elements(lon=4.8, lat=60.0, z=z, radius=0, number=2000,
                time=datetime.now())

print(o)
14:11:34 INFO    opendrift.models.basemodel.environment:206: Adding a global landmask from GSHHG
14:11:38 INFO    opendrift.models.basemodel.environment:229: Fallback values will be used for the following variables which have no readers:
14:11:38 INFO    opendrift.models.basemodel.environment:232:    x_sea_water_velocity: 0.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    y_sea_water_velocity: 0.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    sea_surface_height: 0.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    x_wind: 0.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    y_wind: 0.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    upward_sea_water_velocity: 0.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    ocean_vertical_diffusivity: 0.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_significant_height: 0.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    ocean_mixed_layer_thickness: 50.000000
14:11:38 INFO    opendrift.models.basemodel.environment:232:    sea_floor_depth_below_sea_level: 10000.000000
===========================
Model:  OceanDrift     (OpenDrift version 1.14.3)
        0 active Lagrangian3DArray particles  (0 deactivated, 2000 scheduled)
-------------------
Environment variables:
  -----
  land_binary_mask
     1) global_landmask
  -----
Readers not added for the following variables:
  ocean_mixed_layer_thickness
  ocean_vertical_diffusivity
  sea_floor_depth_below_sea_level
  sea_surface_height
  sea_surface_wave_significant_height
  sea_surface_wave_stokes_drift_x_velocity
  sea_surface_wave_stokes_drift_y_velocity
  upward_sea_water_velocity
  x_sea_water_velocity
  x_wind
  y_sea_water_velocity
  y_wind
---
Lazy readers:
  LazyReader: https://thredds.met.no/thredds/dodsC/fou-hi/norkystv3_800m_m00_be

Discarded readers:
===========================

Running model

o.run(duration=timedelta(hours=24), time_step=1800)
14:11:38 INFO    opendrift:1779: Skipping environment variable ocean_vertical_diffusivity because of condition ['drift:vertical_mixing', 'is', False]
14:11:38 INFO    opendrift:1779: Skipping environment variable ocean_mixed_layer_thickness because of condition ['drift:vertical_mixing', 'is', False]
14:11:38 INFO    opendrift:1790: Storing previous values of element property lon because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous']))
14:11:38 INFO    opendrift:1790: Storing previous values of element property lat because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous']))
14:11:38 INFO    opendrift:1798: Storing previous values of environment variable sea_surface_height because of condition ['drift:vertical_advection', 'is', True]
14:11:38 INFO    opendrift:945: Using existing reader for land_binary_mask to move elements to ocean
14:11:38 INFO    opendrift:975: All points are in ocean
14:11:38 INFO    opendrift:2086: 2025-10-03 14:11:34.538743 - step 1 of 48 - 2000 active elements (0 deactivated)
14:11:38 INFO    opendrift.readers:63: Opening file with xr.open_dataset
14:11:41 INFO    opendrift.readers.reader_netCDF_CF_generic:332: Detected dimensions: {'x': 'X', 'y': 'Y', 'z': 'depth', 'time': 'time'}
14:11:41 INFO    opendrift.readers.basereader:176: Variable x_sea_water_velocity will be rotated from eastward_sea_water_velocity
14:11:41 INFO    opendrift.readers.basereader:176: Variable y_sea_water_velocity will be rotated from northward_sea_water_velocity
14:11:41 INFO    opendrift.readers.basereader:176: Variable x_wind will be rotated from eastward_wind
14:11:41 INFO    opendrift.readers.basereader:176: Variable y_wind will be rotated from northward_wind
14:11:44 INFO    opendrift:2086: 2025-10-03 14:41:34.538743 - step 2 of 48 - 2000 active elements (0 deactivated)
14:11:44 INFO    opendrift:2086: 2025-10-03 15:11:34.538743 - step 3 of 48 - 2000 active elements (0 deactivated)
14:11:45 INFO    opendrift:2086: 2025-10-03 15:41:34.538743 - step 4 of 48 - 2000 active elements (0 deactivated)
14:11:45 INFO    opendrift:2086: 2025-10-03 16:11:34.538743 - step 5 of 48 - 2000 active elements (0 deactivated)
14:11:46 INFO    opendrift:2086: 2025-10-03 16:41:34.538743 - step 6 of 48 - 2000 active elements (0 deactivated)
14:11:46 INFO    opendrift:2086: 2025-10-03 17:11:34.538743 - step 7 of 48 - 2000 active elements (0 deactivated)
14:11:48 INFO    opendrift:2086: 2025-10-03 17:41:34.538743 - step 8 of 48 - 2000 active elements (0 deactivated)
14:11:48 INFO    opendrift:2086: 2025-10-03 18:11:34.538743 - step 9 of 48 - 2000 active elements (0 deactivated)
14:11:49 INFO    opendrift:2086: 2025-10-03 18:41:34.538743 - step 10 of 48 - 2000 active elements (0 deactivated)
14:11:49 INFO    opendrift:2086: 2025-10-03 19:11:34.538743 - step 11 of 48 - 2000 active elements (0 deactivated)
14:11:50 INFO    opendrift:2086: 2025-10-03 19:41:34.538743 - step 12 of 48 - 2000 active elements (0 deactivated)
14:11:50 INFO    opendrift:2086: 2025-10-03 20:11:34.538743 - step 13 of 48 - 2000 active elements (0 deactivated)
14:11:51 INFO    opendrift:2086: 2025-10-03 20:41:34.538743 - step 14 of 48 - 2000 active elements (0 deactivated)
14:11:51 INFO    opendrift:2086: 2025-10-03 21:11:34.538743 - step 15 of 48 - 2000 active elements (0 deactivated)
14:11:52 INFO    opendrift:2086: 2025-10-03 21:41:34.538743 - step 16 of 48 - 2000 active elements (0 deactivated)
14:11:52 INFO    opendrift:2086: 2025-10-03 22:11:34.538743 - step 17 of 48 - 2000 active elements (0 deactivated)
14:11:54 INFO    opendrift:2086: 2025-10-03 22:41:34.538743 - step 18 of 48 - 2000 active elements (0 deactivated)
14:11:54 INFO    opendrift:2086: 2025-10-03 23:11:34.538743 - step 19 of 48 - 2000 active elements (0 deactivated)
14:11:55 INFO    opendrift:2086: 2025-10-03 23:41:34.538743 - step 20 of 48 - 2000 active elements (0 deactivated)
14:11:55 WARNING opendrift.readers.basereader.structured:324: Data block from https://thredds.met.no/thredds/dodsC/fou-hi/norkystv3_800m_m00_be not large enough to cover element positions within timestep. Buffer size (11) must be increased. See `Variables.set_buffer_size`.
14:11:55 INFO    opendrift:2086: 2025-10-04 00:11:34.538743 - step 21 of 48 - 2000 active elements (0 deactivated)
14:11:56 INFO    opendrift:2086: 2025-10-04 00:41:34.538743 - step 22 of 48 - 2000 active elements (0 deactivated)
14:11:56 WARNING opendrift.readers.basereader.structured:324: Data block from https://thredds.met.no/thredds/dodsC/fou-hi/norkystv3_800m_m00_be not large enough to cover element positions within timestep. Buffer size (11) must be increased. See `Variables.set_buffer_size`.
14:11:56 INFO    opendrift:2086: 2025-10-04 01:11:34.538743 - step 23 of 48 - 2000 active elements (0 deactivated)
14:11:58 INFO    opendrift:2086: 2025-10-04 01:41:34.538743 - step 24 of 48 - 2000 active elements (0 deactivated)
14:11:58 INFO    opendrift:2086: 2025-10-04 02:11:34.538743 - step 25 of 48 - 2000 active elements (0 deactivated)
14:12:03 INFO    opendrift:2086: 2025-10-04 02:41:34.538743 - step 26 of 48 - 1999 active elements (1 deactivated)
14:12:03 INFO    opendrift:2086: 2025-10-04 03:11:34.538743 - step 27 of 48 - 1997 active elements (3 deactivated)
14:12:05 INFO    opendrift:2086: 2025-10-04 03:41:34.538743 - step 28 of 48 - 1991 active elements (9 deactivated)
14:12:05 INFO    opendrift:2086: 2025-10-04 04:11:34.538743 - step 29 of 48 - 1984 active elements (16 deactivated)
14:12:06 INFO    opendrift:2086: 2025-10-04 04:41:34.538743 - step 30 of 48 - 1978 active elements (22 deactivated)
14:12:07 INFO    opendrift:2086: 2025-10-04 05:11:34.538743 - step 31 of 48 - 1970 active elements (30 deactivated)
14:12:08 INFO    opendrift:2086: 2025-10-04 05:41:34.538743 - step 32 of 48 - 1964 active elements (36 deactivated)
14:12:08 INFO    opendrift:2086: 2025-10-04 06:11:34.538743 - step 33 of 48 - 1953 active elements (47 deactivated)
14:12:09 INFO    opendrift:2086: 2025-10-04 06:41:34.538743 - step 34 of 48 - 1949 active elements (51 deactivated)
14:12:09 INFO    opendrift:2086: 2025-10-04 07:11:34.538743 - step 35 of 48 - 1945 active elements (55 deactivated)
14:12:11 INFO    opendrift:2086: 2025-10-04 07:41:34.538743 - step 36 of 48 - 1943 active elements (57 deactivated)
14:12:11 INFO    opendrift:2086: 2025-10-04 08:11:34.538743 - step 37 of 48 - 1936 active elements (64 deactivated)
14:12:13 INFO    opendrift:2086: 2025-10-04 08:41:34.538743 - step 38 of 48 - 1930 active elements (70 deactivated)
14:12:13 INFO    opendrift:2086: 2025-10-04 09:11:34.538743 - step 39 of 48 - 1924 active elements (76 deactivated)
14:12:15 INFO    opendrift:2086: 2025-10-04 09:41:34.538743 - step 40 of 48 - 1919 active elements (81 deactivated)
14:12:15 INFO    opendrift:2086: 2025-10-04 10:11:34.538743 - step 41 of 48 - 1904 active elements (96 deactivated)
14:12:16 INFO    opendrift:2086: 2025-10-04 10:41:34.538743 - step 42 of 48 - 1894 active elements (106 deactivated)
14:12:17 INFO    opendrift:2086: 2025-10-04 11:11:34.538743 - step 43 of 48 - 1886 active elements (114 deactivated)
14:12:18 INFO    opendrift:2086: 2025-10-04 11:41:34.538743 - step 44 of 48 - 1879 active elements (121 deactivated)
14:12:18 INFO    opendrift:2086: 2025-10-04 12:11:34.538743 - step 45 of 48 - 1871 active elements (129 deactivated)
14:12:20 INFO    opendrift:2086: 2025-10-04 12:41:34.538743 - step 46 of 48 - 1867 active elements (133 deactivated)
14:12:20 INFO    opendrift:2086: 2025-10-04 13:11:34.538743 - step 47 of 48 - 1865 active elements (135 deactivated)
14:12:21 INFO    opendrift:2086: 2025-10-04 13:41:34.538743 - step 48 of 48 - 1859 active elements (141 deactivated)
<xarray.Dataset> Size: 8MB
Dimensions:                                   (trajectory: 2000, time: 49)
Coordinates:
  * trajectory                                (trajectory) int64 16kB 0 ... 1999
  * time                                      (time) datetime64[ns] 392B 2025...
Data variables: (12/21)
    status                                    (trajectory, time) float32 392kB ...
    moving                                    (trajectory, time) float32 392kB ...
    age_seconds                               (trajectory, time) float32 392kB ...
    origin_marker                             (trajectory, time) float32 392kB ...
    lon                                       (trajectory, time) float32 392kB ...
    lat                                       (trajectory, time) float32 392kB ...
    ...                                        ...
    upward_sea_water_velocity                 (trajectory, time) float32 392kB ...
    sea_surface_wave_significant_height       (trajectory, time) float32 392kB ...
    sea_surface_wave_stokes_drift_x_velocity  (trajectory, time) float32 392kB ...
    sea_surface_wave_stokes_drift_y_velocity  (trajectory, time) float32 392kB ...
    sea_floor_depth_below_sea_level           (trajectory, time) float32 392kB ...
    land_binary_mask                          (trajectory, time) float32 392kB ...
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:                                               -1...
    geospatial_vertical_max:                                               0.0
    geospatial_vertical_positive:                                          up


Plot results with lines and particles colored by depth

print(o)
o.plot(linecolor='z', buffer=.1, show_elements=False)
o.animation(color='z', buffer=.1)
OpenDrift - OceanDrift 2025-10-03 14:11 to 2025-10-04 14:11 UTC (49 steps)
===========================
--------------------
Reader performance:
--------------------
global_landmask
 0:00:01.6  total
 0:00:00.0  preparing
 0:00:01.6  reading
 0:00:00.0  masking
--------------------
https://thredds.met.no/thredds/dodsC/fou-hi/norkystv3_800m_m00_be
 0:00:35.2  total
 0:00:00.0  preparing
 0:00:34.8  reading
 0:00:01.1  interpolation
 0:00:00.0  interpolation_time
 0:00:00.4  rotating vectors
 0:00:00.0  masking
--------------------
Performance:
   47.2 total time
    4.2 configuration
    0.0 preparing main loop
      0.0 moving elements to ocean
   43.0 main loop
      0.1 updating elements
    0.0 cleaning up
--------------------
===========================
Model:  OceanDrift     (OpenDrift version 1.14.3)
        1854 active Lagrangian3DArray particles  (146 deactivated, 0 scheduled)
-------------------
Environment variables:
  -----
  land_binary_mask
     1) global_landmask
  -----
  sea_floor_depth_below_sea_level
  sea_surface_height
  upward_sea_water_velocity
  x_sea_water_velocity
  x_wind
  y_sea_water_velocity
  y_wind
     1) https://thredds.met.no/thredds/dodsC/fou-hi/norkystv3_800m_m00_be
  -----
Readers not added for the following variables:
  sea_surface_wave_significant_height
  sea_surface_wave_stokes_drift_x_velocity
  sea_surface_wave_stokes_drift_y_velocity

Discarded readers:

Time:
        Start: 2025-10-03 14:11:34.538743 UTC
        Present: 2025-10-04 14:11:34.538743 UTC
        Calculation steps: 48 * 0:30:00 - total time: 1 day, 0:00:00
        Output steps: 49 * 0:30:00
===========================

14:13:22 INFO    opendrift:4613: Saving animation to /root/project/docs/source/gallery/animations/example_depth_0.gif...
14:13:54 INFO    opendrift:3054: Time to make animation: 0:00:33.199847
../_images/example_depth_0.gif

Total running time of the script: (2 minutes 28.989 seconds)

Gallery generated by Sphinx-Gallery