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')
09:34:03 INFO    opendrift.models.basemodel:515: OpenDriftSimulation initialised (version 1.11.13 / v1.11.13-48-g5bea84b)
09:34:03 INFO    opendrift.readers.reader_netCDF_CF_generic:102: Opening dataset: https://thredds.met.no/thredds/dodsC/cmems/topaz6/dataset-topaz6-arc-15min-3km-be.ncml
09:34:05 INFO    opendrift.readers.reader_netCDF_CF_generic:325: 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')
09:34:05 INFO    opendrift.readers.reader_shape:91: 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)
09:34:05 INFO    opendrift.models.basemodel.environment:245: Fallback values will be used for the following variables which have no readers:
09:34:05 INFO    opendrift.models.basemodel.environment:248:    x_wind: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    y_wind: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    upward_sea_water_velocity: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    ocean_vertical_diffusivity: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_significant_height: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_period_at_variance_spectral_density_maximum: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_swell_wave_to_direction: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_swell_wave_peak_period_from_variance_spectral_density: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_swell_wave_significant_height: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wind_wave_to_direction: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wind_wave_mean_period: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wind_wave_significant_height: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    surface_downward_x_stress: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    surface_downward_y_stress: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    turbulent_kinetic_energy: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    turbulent_generic_length_scale: 0.000000
09:34:05 INFO    opendrift.models.basemodel.environment:248:    ocean_mixed_layer_thickness: 50.000000
09:34:06 INFO    opendrift.models.basemodel:936: Using existing reader for land_binary_mask
09:34:06 INFO    opendrift.models.basemodel:949: Moving 20 out of 900 points from land to water
09:34:06 WARNING opendrift.models.basemodel:730: Seafloor check not being run because environment is missing. This will happen the first time the function is run but if it happens subsequently there is probably a problem.
09:34:06 INFO    opendrift.models.basemodel:2038: 2018-01-01 00:00:00 - step 1 of 48 - 900 active elements (0 deactivated)
/root/project/opendrift/readers/interpolation/interpolators.py:17: RuntimeWarning: overflow encountered in cast
  data[mask] = np.finfo(np.float64).min
09:34:07 INFO    opendrift.models.basemodel:2038: 2018-01-01 01:00:00 - step 2 of 48 - 900 active elements (0 deactivated)
09:34:08 INFO    opendrift.models.basemodel:2038: 2018-01-01 02:00:00 - step 3 of 48 - 900 active elements (0 deactivated)
09:34:09 INFO    opendrift.models.basemodel:2038: 2018-01-01 03:00:00 - step 4 of 48 - 900 active elements (0 deactivated)
09:34:09 INFO    opendrift.models.basemodel:2038: 2018-01-01 04:00:00 - step 5 of 48 - 900 active elements (0 deactivated)
09:34:10 INFO    opendrift.models.basemodel:2038: 2018-01-01 05:00:00 - step 6 of 48 - 900 active elements (0 deactivated)
09:34:11 INFO    opendrift.models.basemodel:2038: 2018-01-01 06:00:00 - step 7 of 48 - 900 active elements (0 deactivated)
09:34:12 INFO    opendrift.models.basemodel:2038: 2018-01-01 07:00:00 - step 8 of 48 - 900 active elements (0 deactivated)
09:34:12 INFO    opendrift.models.basemodel:2038: 2018-01-01 08:00:00 - step 9 of 48 - 900 active elements (0 deactivated)
09:34:13 INFO    opendrift.models.basemodel:2038: 2018-01-01 09:00:00 - step 10 of 48 - 900 active elements (0 deactivated)
09:34:13 INFO    opendrift.models.basemodel:2038: 2018-01-01 10:00:00 - step 11 of 48 - 897 active elements (3 deactivated)
09:34:14 INFO    opendrift.models.basemodel:2038: 2018-01-01 11:00:00 - step 12 of 48 - 897 active elements (3 deactivated)
09:34:15 INFO    opendrift.models.basemodel:2038: 2018-01-01 12:00:00 - step 13 of 48 - 897 active elements (3 deactivated)
09:34:16 INFO    opendrift.models.basemodel:2038: 2018-01-01 13:00:00 - step 14 of 48 - 897 active elements (3 deactivated)
09:34:17 INFO    opendrift.models.basemodel:2038: 2018-01-01 14:00:00 - step 15 of 48 - 896 active elements (4 deactivated)
09:34:17 INFO    opendrift.models.basemodel:2038: 2018-01-01 15:00:00 - step 16 of 48 - 896 active elements (4 deactivated)
09:34:18 INFO    opendrift.models.basemodel:2038: 2018-01-01 16:00:00 - step 17 of 48 - 896 active elements (4 deactivated)
09:34:19 INFO    opendrift.models.basemodel:2038: 2018-01-01 17:00:00 - step 18 of 48 - 896 active elements (4 deactivated)
09:34:20 INFO    opendrift.models.basemodel:2038: 2018-01-01 18:00:00 - step 19 of 48 - 896 active elements (4 deactivated)
09:34:21 INFO    opendrift.models.basemodel:2038: 2018-01-01 19:00:00 - step 20 of 48 - 896 active elements (4 deactivated)
09:34:21 INFO    opendrift.models.basemodel:2038: 2018-01-01 20:00:00 - step 21 of 48 - 896 active elements (4 deactivated)
09:34:22 INFO    opendrift.models.basemodel:2038: 2018-01-01 21:00:00 - step 22 of 48 - 896 active elements (4 deactivated)
09:34:23 INFO    opendrift.models.basemodel:2038: 2018-01-01 22:00:00 - step 23 of 48 - 896 active elements (4 deactivated)
09:34:23 INFO    opendrift.models.basemodel:2038: 2018-01-01 23:00:00 - step 24 of 48 - 896 active elements (4 deactivated)
09:34:24 INFO    opendrift.models.basemodel:2038: 2018-01-02 00:00:00 - step 25 of 48 - 896 active elements (4 deactivated)
09:34:25 INFO    opendrift.models.basemodel:2038: 2018-01-02 01:00:00 - step 26 of 48 - 896 active elements (4 deactivated)
09:34:26 INFO    opendrift.models.basemodel:2038: 2018-01-02 02:00:00 - step 27 of 48 - 896 active elements (4 deactivated)
09:34:27 INFO    opendrift.models.basemodel:2038: 2018-01-02 03:00:00 - step 28 of 48 - 896 active elements (4 deactivated)
09:34:28 INFO    opendrift.models.basemodel:2038: 2018-01-02 04:00:00 - step 29 of 48 - 896 active elements (4 deactivated)
09:34:28 INFO    opendrift.models.basemodel:2038: 2018-01-02 05:00:00 - step 30 of 48 - 896 active elements (4 deactivated)
09:34:29 INFO    opendrift.models.basemodel:2038: 2018-01-02 06:00:00 - step 31 of 48 - 896 active elements (4 deactivated)
09:34:30 INFO    opendrift.models.basemodel:2038: 2018-01-02 07:00:00 - step 32 of 48 - 896 active elements (4 deactivated)
09:34:31 INFO    opendrift.models.basemodel:2038: 2018-01-02 08:00:00 - step 33 of 48 - 896 active elements (4 deactivated)
09:34:31 INFO    opendrift.models.basemodel:2038: 2018-01-02 09:00:00 - step 34 of 48 - 896 active elements (4 deactivated)
09:34:32 INFO    opendrift.models.basemodel:2038: 2018-01-02 10:00:00 - step 35 of 48 - 896 active elements (4 deactivated)
09:34:33 INFO    opendrift.models.basemodel:2038: 2018-01-02 11:00:00 - step 36 of 48 - 896 active elements (4 deactivated)
09:34:33 INFO    opendrift.models.basemodel:2038: 2018-01-02 12:00:00 - step 37 of 48 - 895 active elements (5 deactivated)
09:34:34 INFO    opendrift.models.basemodel:2038: 2018-01-02 13:00:00 - step 38 of 48 - 894 active elements (6 deactivated)
09:34:35 INFO    opendrift.models.basemodel:2038: 2018-01-02 14:00:00 - step 39 of 48 - 894 active elements (6 deactivated)
09:34:36 INFO    opendrift.models.basemodel:2038: 2018-01-02 15:00:00 - step 40 of 48 - 894 active elements (6 deactivated)
09:34:36 INFO    opendrift.models.basemodel:2038: 2018-01-02 16:00:00 - step 41 of 48 - 894 active elements (6 deactivated)
09:34:37 INFO    opendrift.models.basemodel:2038: 2018-01-02 17:00:00 - step 42 of 48 - 894 active elements (6 deactivated)
09:34:38 INFO    opendrift.models.basemodel:2038: 2018-01-02 18:00:00 - step 43 of 48 - 894 active elements (6 deactivated)
09:34:38 INFO    opendrift.models.basemodel:2038: 2018-01-02 19:00:00 - step 44 of 48 - 894 active elements (6 deactivated)
09:34:39 INFO    opendrift.models.basemodel:2038: 2018-01-02 20:00:00 - step 45 of 48 - 894 active elements (6 deactivated)
09:34:39 INFO    opendrift.models.basemodel:2038: 2018-01-02 21:00:00 - step 46 of 48 - 894 active elements (6 deactivated)
09:34:40 INFO    opendrift.models.basemodel:2038: 2018-01-02 22:00:00 - step 47 of 48 - 894 active elements (6 deactivated)
09:34:41 INFO    opendrift.models.basemodel:2038: 2018-01-02 23:00:00 - step 48 of 48 - 894 active elements (6 deactivated)

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 2018-01-01 00:00 to 2018-01-03 00:00 UTC (49 steps)
/opt/conda/envs/opendrift/lib/python3.11/site-packages/cartopy/mpl/geoaxes.py:1683: UserWarning: No data for colormapping provided via 'c'. Parameters 'cmap' will be ignored
  result = super().scatter(*args, **kwargs)
09:34:47 INFO    opendrift.models.basemodel:4608: Saving animation to /root/project/docs/source/gallery/animations/example_shape_landmask_0.gif...
09:35:09 INFO    opendrift.models.basemodel:3037: Time to make animation: 0:00:22.988705
../_images/example_shape_landmask_0.gif

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

Gallery generated by Sphinx-Gallery