Note
Go to the end to download the full example code.
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
17:13:53 INFO opendrift:513: OpenDriftSimulation initialised (version 1.14.2 / v1.14.2-88-gf09b7bd)
Using live data from Thredds
o.add_readers_from_list([
'https://thredds.met.no/thredds/dodsC/sea/norkyst800m/1h/aggregate_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)
17:13:53 INFO opendrift.models.basemodel.environment:206: Adding a global landmask from GSHHG
17:13:57 INFO opendrift.models.basemodel.environment:229: Fallback values will be used for the following variables which have no readers:
17:13:57 INFO opendrift.models.basemodel.environment:232: x_sea_water_velocity: 0.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: y_sea_water_velocity: 0.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: sea_surface_height: 0.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: x_wind: 0.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: y_wind: 0.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: upward_sea_water_velocity: 0.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: ocean_vertical_diffusivity: 0.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: sea_surface_wave_significant_height: 0.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: sea_surface_wave_stokes_drift_x_velocity: 0.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: sea_surface_wave_stokes_drift_y_velocity: 0.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: ocean_mixed_layer_thickness: 50.000000
17:13:57 INFO opendrift.models.basemodel.environment:232: sea_floor_depth_below_sea_level: 10000.000000
===========================
Model: OceanDrift (OpenDrift version 1.14.2)
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/sea/norkyst800m/1h/aggregate_be
Discarded readers:
===========================
Running model
o.run(duration=timedelta(hours=24), time_step=1800)
17:13:57 INFO opendrift:1732: Skipping environment variable ocean_vertical_diffusivity because of condition ['drift:vertical_mixing', 'is', False]
17:13:57 INFO opendrift:1732: Skipping environment variable ocean_mixed_layer_thickness because of condition ['drift:vertical_mixing', 'is', False]
17:13:57 INFO opendrift:1743: Storing previous values of element property lon because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous']))
17:13:57 INFO opendrift:1743: Storing previous values of element property lat because of condition (('general:coastline_action', 'in', ['stranding', 'previous']), 'or', ('general:seafloor_action', 'in', ['previous']))
17:13:57 INFO opendrift:1751: Storing previous values of environment variable sea_surface_height because of condition ['drift:vertical_advection', 'is', True]
17:13:57 INFO opendrift:899: Using existing reader for land_binary_mask
17:13:57 INFO opendrift:928: All points are in ocean
17:13:57 INFO opendrift:2035: 2025-07-11 17:13:53.466005 - step 1 of 48 - 2000 active elements (0 deactivated)
17:13:57 INFO opendrift.readers:61: Opening file with xr.open_dataset
17:13:59 INFO opendrift.readers.reader_netCDF_CF_generic:332: Detected dimensions: {'x': 'X', 'y': 'Y', 'z': 'depth', 'time': 'time'}
17:14:03 INFO opendrift:2035: 2025-07-11 17:43:53.466005 - step 2 of 48 - 2000 active elements (0 deactivated)
17:14:03 INFO opendrift:2035: 2025-07-11 18:13:53.466005 - step 3 of 48 - 2000 active elements (0 deactivated)
17:14:05 INFO opendrift:2035: 2025-07-11 18:43:53.466005 - step 4 of 48 - 2000 active elements (0 deactivated)
17:14:05 INFO opendrift:2035: 2025-07-11 19:13:53.466005 - step 5 of 48 - 2000 active elements (0 deactivated)
17:14:06 INFO opendrift:2035: 2025-07-11 19:43:53.466005 - step 6 of 48 - 2000 active elements (0 deactivated)
17:14:06 INFO opendrift:2035: 2025-07-11 20:13:53.466005 - step 7 of 48 - 2000 active elements (0 deactivated)
17:14:08 INFO opendrift:2035: 2025-07-11 20:43:53.466005 - step 8 of 48 - 2000 active elements (0 deactivated)
17:14:08 INFO opendrift:2035: 2025-07-11 21:13:53.466005 - step 9 of 48 - 2000 active elements (0 deactivated)
17:14:10 INFO opendrift:2035: 2025-07-11 21:43:53.466005 - step 10 of 48 - 2000 active elements (0 deactivated)
17:14:10 INFO opendrift:2035: 2025-07-11 22:13:53.466005 - step 11 of 48 - 2000 active elements (0 deactivated)
17:14:11 INFO opendrift:2035: 2025-07-11 22:43:53.466005 - step 12 of 48 - 2000 active elements (0 deactivated)
17:14:11 INFO opendrift:2035: 2025-07-11 23:13:53.466005 - step 13 of 48 - 2000 active elements (0 deactivated)
17:14:13 INFO opendrift:2035: 2025-07-11 23:43:53.466005 - step 14 of 48 - 2000 active elements (0 deactivated)
17:14:13 INFO opendrift:2035: 2025-07-12 00:13:53.466005 - step 15 of 48 - 2000 active elements (0 deactivated)
17:14:14 INFO opendrift:2035: 2025-07-12 00:43:53.466005 - step 16 of 48 - 2000 active elements (0 deactivated)
17:14:14 INFO opendrift:2035: 2025-07-12 01:13:53.466005 - step 17 of 48 - 2000 active elements (0 deactivated)
17:14:15 INFO opendrift:2035: 2025-07-12 01:43:53.466005 - step 18 of 48 - 2000 active elements (0 deactivated)
17:14:15 INFO opendrift:2035: 2025-07-12 02:13:53.466005 - step 19 of 48 - 2000 active elements (0 deactivated)
17:14:16 INFO opendrift:2035: 2025-07-12 02:43:53.466005 - step 20 of 48 - 2000 active elements (0 deactivated)
17:14:16 INFO opendrift:2035: 2025-07-12 03:13:53.466005 - step 21 of 48 - 2000 active elements (0 deactivated)
17:14:18 INFO opendrift:2035: 2025-07-12 03:43:53.466005 - step 22 of 48 - 2000 active elements (0 deactivated)
17:14:18 INFO opendrift:2035: 2025-07-12 04:13:53.466005 - step 23 of 48 - 2000 active elements (0 deactivated)
17:14:19 INFO opendrift:2035: 2025-07-12 04:43:53.466005 - step 24 of 48 - 2000 active elements (0 deactivated)
17:14:19 INFO opendrift:2035: 2025-07-12 05:13:53.466005 - step 25 of 48 - 2000 active elements (0 deactivated)
17:14:21 INFO opendrift:2035: 2025-07-12 05:43:53.466005 - step 26 of 48 - 2000 active elements (0 deactivated)
17:14:21 INFO opendrift:2035: 2025-07-12 06:13:53.466005 - step 27 of 48 - 2000 active elements (0 deactivated)
17:14:22 INFO opendrift:2035: 2025-07-12 06:43:53.466005 - step 28 of 48 - 2000 active elements (0 deactivated)
17:14:22 INFO opendrift:2035: 2025-07-12 07:13:53.466005 - step 29 of 48 - 2000 active elements (0 deactivated)
17:14:23 INFO opendrift:2035: 2025-07-12 07:43:53.466005 - step 30 of 48 - 2000 active elements (0 deactivated)
17:14:23 INFO opendrift:2035: 2025-07-12 08:13:53.466005 - step 31 of 48 - 2000 active elements (0 deactivated)
17:14:24 INFO opendrift:2035: 2025-07-12 08:43:53.466005 - step 32 of 48 - 2000 active elements (0 deactivated)
17:14:24 INFO opendrift:2035: 2025-07-12 09:13:53.466005 - step 33 of 48 - 2000 active elements (0 deactivated)
17:14:26 INFO opendrift:2035: 2025-07-12 09:43:53.466005 - step 34 of 48 - 2000 active elements (0 deactivated)
17:14:26 INFO opendrift:2035: 2025-07-12 10:13:53.466005 - step 35 of 48 - 2000 active elements (0 deactivated)
17:14:27 INFO opendrift:2035: 2025-07-12 10:43:53.466005 - step 36 of 48 - 2000 active elements (0 deactivated)
17:14:27 INFO opendrift:2035: 2025-07-12 11:13:53.466005 - step 37 of 48 - 2000 active elements (0 deactivated)
17:14:28 INFO opendrift:2035: 2025-07-12 11:43:53.466005 - step 38 of 48 - 2000 active elements (0 deactivated)
17:14:28 INFO opendrift:2035: 2025-07-12 12:13:53.466005 - step 39 of 48 - 2000 active elements (0 deactivated)
17:14:30 INFO opendrift:2035: 2025-07-12 12:43:53.466005 - step 40 of 48 - 2000 active elements (0 deactivated)
17:14:30 INFO opendrift:2035: 2025-07-12 13:13:53.466005 - step 41 of 48 - 2000 active elements (0 deactivated)
17:14:31 INFO opendrift:2035: 2025-07-12 13:43:53.466005 - step 42 of 48 - 2000 active elements (0 deactivated)
17:14:31 INFO opendrift:2035: 2025-07-12 14:13:53.466005 - step 43 of 48 - 2000 active elements (0 deactivated)
17:14:33 INFO opendrift:2035: 2025-07-12 14:43:53.466005 - step 44 of 48 - 2000 active elements (0 deactivated)
17:14:33 INFO opendrift:2035: 2025-07-12 15:13:53.466005 - step 45 of 48 - 2000 active elements (0 deactivated)
17:14:34 INFO opendrift:2035: 2025-07-12 15:43:53.466005 - step 46 of 48 - 2000 active elements (0 deactivated)
17:14:34 INFO opendrift:2035: 2025-07-12 16:13:53.466005 - step 47 of 48 - 2000 active elements (0 deactivated)
17:14:35 INFO opendrift:2035: 2025-07-12 16:43:53.466005 - step 48 of 48 - 2000 active elements (0 deactivated)
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)

===========================
--------------------
Reader performance:
--------------------
global_landmask
0:00:00.0 total
0:00:00.0 preparing
0:00:00.0 reading
0:00:00.0 masking
--------------------
https://thredds.met.no/thredds/dodsC/sea/norkyst800m/1h/aggregate_be
0:00:34.9 total
0:00:00.0 preparing
0:00:34.6 reading
0:00:00.5 interpolation
0:00:00.0 interpolation_time
0:00:00.3 rotating vectors
0:00:00.0 masking
--------------------
Performance:
42.5 total time
3.9 configuration
0.0 preparing main loop
0.0 moving elements to ocean
38.5 main loop
0.1 updating elements
0.0 cleaning up
--------------------
===========================
Model: OceanDrift (OpenDrift version 1.14.2)
2000 active Lagrangian3DArray particles (0 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/sea/norkyst800m/1h/aggregate_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-07-11 17:13:53.466005 UTC
Present: 2025-07-12 17:13:53.466005 UTC
Calculation steps: 48 * 0:30:00 - total time: 1 day, 0:00:00
Output steps: 49 * 0:30:00
===========================
17:15:36 INFO opendrift:4553: Saving animation to /root/project/docs/source/gallery/animations/example_depth_0.gif...
17:16:08 INFO opendrift:2994: Time to make animation: 0:00:33.174178

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