Use a shapefile as landmask

import numpy as np
from opendrift.readers import reader_netCDF_CF_generic
from opendrift.readers import reader_shape
from opendrift.models.oceandrift import OceanDrift

o = OceanDrift(loglevel=20)  # Set loglevel to 0 for debug information

# This example works better using hourly input from Thredds than the daily data from test folder
reader_topaz = reader_netCDF_CF_generic.Reader('https://thredds.met.no/thredds/dodsC/cmems/topaz6/dataset-topaz6-arc-15min-3km-be.ncml')
17:13:51 INFO    opendrift:513: OpenDriftSimulation initialised (version 1.14.2 / v1.14.2-88-gf09b7bd)
17:13:51 INFO    opendrift.readers:61: Opening file with xr.open_dataset
17:13:54 INFO    opendrift.readers.reader_netCDF_CF_generic:332: Detected dimensions: {'x': 'x', 'y': 'y', 'time': 'time'}

Use shapes from Cartopy as landmask. These shapefiles are less acurate than those provided by the GSHHG dataset (available though the reader_global_landmask reader).

import cartopy.io.shapereader as shpreader
shpfilename = shpreader.natural_earth(resolution='110m',
                                    category='cultural',
                                    name='admin_0_countries')
reader_natural = reader_shape.Reader.from_shpfiles(shpfilename)

o.add_reader([reader_natural, reader_topaz])
o.set_config('general:use_auto_landmask', False)  # Disabling the automatic GSHHG landmask
o.set_config('general:coastline_action', 'stranding')
17:13:54 INFO    opendrift.readers.reader_shape:99: Pre-processing 177 geometries

Seeding elements on a grid

lons = np.linspace(12, 14.7, 30)
lats = np.linspace(67.5, 68.5, 30)
lons, lats = np.meshgrid(lons, lats)
lon = lons.ravel()
lat = lats.ravel()

time = reader_topaz.start_time
o.seed_elements(lon, lat, radius=0, number=30*30, time=time)

o.run(steps=48, time_step=3600)
17:13:54 INFO    opendrift.models.basemodel.environment:229: Fallback values will be used for the following variables which have no readers:
17:13:54 INFO    opendrift.models.basemodel.environment:232:    x_wind: 0.000000
17:13:54 INFO    opendrift.models.basemodel.environment:232:    y_wind: 0.000000
17:13:54 INFO    opendrift.models.basemodel.environment:232:    upward_sea_water_velocity: 0.000000
17:13:54 INFO    opendrift.models.basemodel.environment:232:    ocean_vertical_diffusivity: 0.000000
17:13:54 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_significant_height: 0.000000
17:13:54 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
17:13:54 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
17:13:54 INFO    opendrift.models.basemodel.environment:232:    ocean_mixed_layer_thickness: 50.000000
17:13:54 INFO    opendrift:1732: Skipping environment variable ocean_vertical_diffusivity because of condition ['drift:vertical_mixing', 'is', False]
17:13:54 INFO    opendrift:1732: Skipping environment variable ocean_mixed_layer_thickness because of condition ['drift:vertical_mixing', 'is', False]
17:13:54 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:54 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:54 INFO    opendrift:1751: Storing previous values of environment variable sea_surface_height because of condition ['drift:vertical_advection', 'is', True]
17:13:54 INFO    opendrift:899: Using existing reader for land_binary_mask
17:13:54 INFO    opendrift.readers.reader_shape:149: Building KDTree from 177 geometries with buffer distance 0.000000e+00
17:13:54 INFO    opendrift:2035: 2023-01-01 00:00:00 - step 1 of 48 - 900 active elements (0 deactivated)
17:13:55 INFO    opendrift:2035: 2023-01-01 01:00:00 - step 2 of 48 - 900 active elements (0 deactivated)
17:13:58 INFO    opendrift:2035: 2023-01-01 02:00:00 - step 3 of 48 - 880 active elements (20 deactivated)
17:13:58 INFO    opendrift:2035: 2023-01-01 03:00:00 - step 4 of 48 - 880 active elements (20 deactivated)
17:13:59 INFO    opendrift:2035: 2023-01-01 04:00:00 - step 5 of 48 - 878 active elements (22 deactivated)
17:14:00 INFO    opendrift:2035: 2023-01-01 05:00:00 - step 6 of 48 - 878 active elements (22 deactivated)
17:14:01 INFO    opendrift:2035: 2023-01-01 06:00:00 - step 7 of 48 - 878 active elements (22 deactivated)
17:14:01 INFO    opendrift:2035: 2023-01-01 07:00:00 - step 8 of 48 - 877 active elements (23 deactivated)
17:14:02 INFO    opendrift:2035: 2023-01-01 08:00:00 - step 9 of 48 - 875 active elements (25 deactivated)
17:14:03 INFO    opendrift:2035: 2023-01-01 09:00:00 - step 10 of 48 - 875 active elements (25 deactivated)
17:14:04 INFO    opendrift:2035: 2023-01-01 10:00:00 - step 11 of 48 - 873 active elements (27 deactivated)
17:14:04 INFO    opendrift:2035: 2023-01-01 11:00:00 - step 12 of 48 - 873 active elements (27 deactivated)
17:14:05 INFO    opendrift:2035: 2023-01-01 12:00:00 - step 13 of 48 - 873 active elements (27 deactivated)
17:14:06 INFO    opendrift:2035: 2023-01-01 13:00:00 - step 14 of 48 - 872 active elements (28 deactivated)
17:14:07 INFO    opendrift:2035: 2023-01-01 14:00:00 - step 15 of 48 - 871 active elements (29 deactivated)
17:14:07 INFO    opendrift:2035: 2023-01-01 15:00:00 - step 16 of 48 - 871 active elements (29 deactivated)
17:14:08 INFO    opendrift:2035: 2023-01-01 16:00:00 - step 17 of 48 - 871 active elements (29 deactivated)
17:14:09 INFO    opendrift:2035: 2023-01-01 17:00:00 - step 18 of 48 - 870 active elements (30 deactivated)
17:14:10 INFO    opendrift:2035: 2023-01-01 18:00:00 - step 19 of 48 - 869 active elements (31 deactivated)
17:14:10 INFO    opendrift:2035: 2023-01-01 19:00:00 - step 20 of 48 - 869 active elements (31 deactivated)
17:14:11 INFO    opendrift:2035: 2023-01-01 20:00:00 - step 21 of 48 - 869 active elements (31 deactivated)
17:14:12 INFO    opendrift:2035: 2023-01-01 21:00:00 - step 22 of 48 - 869 active elements (31 deactivated)
17:14:12 INFO    opendrift:2035: 2023-01-01 22:00:00 - step 23 of 48 - 869 active elements (31 deactivated)
17:14:13 INFO    opendrift:2035: 2023-01-01 23:00:00 - step 24 of 48 - 868 active elements (32 deactivated)
17:14:14 INFO    opendrift:2035: 2023-01-02 00:00:00 - step 25 of 48 - 867 active elements (33 deactivated)
17:14:15 INFO    opendrift:2035: 2023-01-02 01:00:00 - step 26 of 48 - 867 active elements (33 deactivated)
17:14:16 INFO    opendrift:2035: 2023-01-02 02:00:00 - step 27 of 48 - 864 active elements (36 deactivated)
17:14:16 INFO    opendrift:2035: 2023-01-02 03:00:00 - step 28 of 48 - 864 active elements (36 deactivated)
17:14:17 INFO    opendrift:2035: 2023-01-02 04:00:00 - step 29 of 48 - 864 active elements (36 deactivated)
17:14:18 INFO    opendrift:2035: 2023-01-02 05:00:00 - step 30 of 48 - 864 active elements (36 deactivated)
17:14:18 INFO    opendrift:2035: 2023-01-02 06:00:00 - step 31 of 48 - 864 active elements (36 deactivated)
17:14:19 INFO    opendrift:2035: 2023-01-02 07:00:00 - step 32 of 48 - 863 active elements (37 deactivated)
17:14:20 INFO    opendrift:2035: 2023-01-02 08:00:00 - step 33 of 48 - 863 active elements (37 deactivated)
17:14:20 INFO    opendrift:2035: 2023-01-02 09:00:00 - step 34 of 48 - 861 active elements (39 deactivated)
17:14:21 INFO    opendrift:2035: 2023-01-02 10:00:00 - step 35 of 48 - 860 active elements (40 deactivated)
17:14:22 INFO    opendrift:2035: 2023-01-02 11:00:00 - step 36 of 48 - 858 active elements (42 deactivated)
17:14:22 INFO    opendrift:2035: 2023-01-02 12:00:00 - step 37 of 48 - 858 active elements (42 deactivated)
17:14:23 INFO    opendrift:2035: 2023-01-02 13:00:00 - step 38 of 48 - 857 active elements (43 deactivated)
17:14:24 INFO    opendrift:2035: 2023-01-02 14:00:00 - step 39 of 48 - 857 active elements (43 deactivated)
17:14:24 INFO    opendrift:2035: 2023-01-02 15:00:00 - step 40 of 48 - 856 active elements (44 deactivated)
17:14:25 INFO    opendrift:2035: 2023-01-02 16:00:00 - step 41 of 48 - 855 active elements (45 deactivated)
17:14:26 INFO    opendrift:2035: 2023-01-02 17:00:00 - step 42 of 48 - 854 active elements (46 deactivated)
17:14:27 INFO    opendrift:2035: 2023-01-02 18:00:00 - step 43 of 48 - 854 active elements (46 deactivated)
17:14:27 INFO    opendrift:2035: 2023-01-02 19:00:00 - step 44 of 48 - 852 active elements (48 deactivated)
17:14:28 INFO    opendrift:2035: 2023-01-02 20:00:00 - step 45 of 48 - 851 active elements (49 deactivated)
17:14:29 INFO    opendrift:2035: 2023-01-02 21:00:00 - step 46 of 48 - 851 active elements (49 deactivated)
17:14:30 INFO    opendrift:2035: 2023-01-02 22:00:00 - step 47 of 48 - 851 active elements (49 deactivated)
17:14:31 INFO    opendrift:2035: 2023-01-02 23:00:00 - step 48 of 48 - 850 active elements (50 deactivated)
<xarray.Dataset> Size: 4MB
Dimensions:                                   (trajectory: 900, time: 49)
Coordinates:
  * trajectory                                (trajectory) int64 7kB 0 1 ... 899
  * time                                      (time) datetime64[ns] 392B 2023...
Data variables: (12/21)
    status                                    (trajectory, time) float32 176kB ...
    moving                                    (trajectory, time) float32 176kB ...
    age_seconds                               (trajectory, time) float32 176kB ...
    origin_marker                             (trajectory, time) float32 176kB ...
    lon                                       (trajectory, time) float32 176kB ...
    lat                                       (trajectory, time) float32 176kB ...
    ...                                        ...
    upward_sea_water_velocity                 (trajectory, time) float32 176kB ...
    sea_surface_wave_significant_height       (trajectory, time) float32 176kB ...
    sea_surface_wave_stokes_drift_x_velocity  (trajectory, time) float32 176kB ...
    sea_surface_wave_stokes_drift_y_velocity  (trajectory, time) float32 176kB ...
    sea_floor_depth_below_sea_level           (trajectory, time) float32 176kB ...
    land_binary_mask                          (trajectory, time) float32 176kB ...
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:                                               0.0
    geospatial_vertical_max:                                               0.0
    geospatial_vertical_positive:                                          up


The custom landmask from shapefile, which is used for the simulation, is also shown in the map instead of the default GSHHG

o.plot()

o.animation()
OpenDrift - OceanDrift 2023-01-01 00:00 to 2023-01-03 00:00 UTC (49 steps)
17:14:36 INFO    opendrift:4553: Saving animation to /root/project/docs/source/gallery/animations/example_shape_landmask_0.gif...
17:15:04 INFO    opendrift:2994: Time to make animation: 0:00:28.689190
../_images/example_shape_landmask_0.gif

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

Gallery generated by Sphinx-Gallery