Note
Go to the end to download the full example code.
Customising Xarray Dataset
from datetime import datetime, timedelta
import xarray as xr
from opendrift import test_data_folder as tdf
from opendrift.readers import reader_netCDF_CF_generic
from opendrift.models.oceandrift import OceanDrift
o = OceanDrift(loglevel=20) # Set loglevel to 0 for debug information
07:26:02 INFO opendrift:513: OpenDriftSimulation initialised (version 1.14.2 / v1.14.2-90-g1dd1995)
Opening the currents netCDF file with xarray
ds = xr.open_dataset(tdf + '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')
Creating and adding a landmask DataArray (variable) based on the u-current component
ds['landmask'] = ds.u.isel(time=0).isel(depth=0) * 0 # 0 (ocean) where current is finite
ds['landmask'] = ds.landmask.fillna(1) # 1 (land) where current is NaN
ds['landmask'] = ds.landmask.assign_attrs(standard_name='land_binary_mask') # Adding attribute standard_name so that this variable is recognised as landmask
Creating an OpenDrift reader from this modified xarray dataset, and confirming that landmask is recognised
r = reader_netCDF_CF_generic.Reader(ds)
print(r)
o.add_reader(r)
o.set_config('general:use_auto_landmask', False) # Disable the automatic GSHHG landmask, so that the custom landmask is used
07:26:03 INFO opendrift.readers.reader_netCDF_CF_generic:332: Detected dimensions: {'x': 'X', 'y': 'Y', 'z': 'depth', 'time': 'time'}
===========================
Reader: <xarray.Dataset> Size: 33MB
Dimensions: (time: 67, depth: 1, Y: 201, X: 301)
Coordinates:
* X (X) float64 2kB -2.953e+06 -2.952e+06 ... -2.713e+06
* Y (Y) float64 2kB -1.384e+06 -1.383e+06 ... -1.224e+06
* depth (depth) float64 8B 0.0
* time (time) datetime64[ns] 536B 2015-11-16 ... 2015-11-18T18...
Data variables:
projection_stere int32 4B ...
u (time, depth, Y, X) float32 16MB ...
v (time, depth, Y, X) float32 16MB ...
landmask (Y, X) float32 242kB 0.0 -0.0 -0.0 -0.0 ... 0.0 0.0 0.0
Attributes: (12/32)
file: /prod/forecast/run/NorKyst-800m/ocean_his.nc
type: ROMS/TOMS history file
title: ROMS 3.5 - NorKyst-800m
rst_file: /prod/forecast/run/NorKyst-800m/ocean_rst.nc
his_file: /prod/forecast/run/NorKyst-800m/ocean_his.nc
avg_file: /prod/forecast/run/NorKyst-800m/ocean_avg.nc
... ...
tiling: 032x016
history: Mon Nov 16 14:46:04 2015: ncks -O -4 -v u,v,projection...
ana_file: ROMS/Functionals/ana_btflux.h, /prod/forecast/sea/ROMS...
CPP_options: NORKYST800M, ADD_FSOBC, ADD_M2OBC, ALBEDO, ATM_PRESS, ...
Conventions: CF-1.4
NCO: 4.0.8
Projection:
+proj=stere +lat_0=90 +lon_0=70 +k=0.933012701892219 +x_0=0 +y_0=0 +R=6371000 +units=m +no_defs +type=crs
Coverage: [degrees]
xmin: -2952800.000000 xmax: -2712800.000000 step: 800 numx: 301
ymin: -1384000.000000 ymax: -1224000.000000 step: 800 numy: 201
Corners (lon, lat):
( 2.52, 59.90) ( 4.28, 61.89)
( 5.11, 59.32) ( 7.03, 61.26)
Vertical levels [m]:
[-0.]
Available time range:
start: 2015-11-16 00:00:00 end: 2015-11-18 18:00:00 step: 1:00:00
67 times (0 missing)
Variables:
x_sea_water_velocity
y_sea_water_velocity
land_binary_mask
sea_water_speed - derived from ['x_sea_water_velocity', 'y_sea_water_velocity']
===========================
Seeding a particle and running simulation, and confirming that the custom landmask is used for stranding
o.seed_elements(lon=4.9, lat=60.0, time=r.start_time)
o.run(end_time=r.end_time)
o.plot(fast=True)

07:26:03 INFO opendrift.models.basemodel.environment:229: Fallback values will be used for the following variables which have no readers:
07:26:03 INFO opendrift.models.basemodel.environment:232: sea_surface_height: 0.000000
07:26:03 INFO opendrift.models.basemodel.environment:232: x_wind: 0.000000
07:26:03 INFO opendrift.models.basemodel.environment:232: y_wind: 0.000000
07:26:03 INFO opendrift.models.basemodel.environment:232: upward_sea_water_velocity: 0.000000
07:26:03 INFO opendrift.models.basemodel.environment:232: ocean_vertical_diffusivity: 0.000000
07:26:03 INFO opendrift.models.basemodel.environment:232: sea_surface_wave_significant_height: 0.000000
07:26:03 INFO opendrift.models.basemodel.environment:232: sea_surface_wave_stokes_drift_x_velocity: 0.000000
07:26:03 INFO opendrift.models.basemodel.environment:232: sea_surface_wave_stokes_drift_y_velocity: 0.000000
07:26:03 INFO opendrift.models.basemodel.environment:232: ocean_mixed_layer_thickness: 50.000000
07:26:03 INFO opendrift.models.basemodel.environment:232: sea_floor_depth_below_sea_level: 10000.000000
07:26:03 INFO opendrift:1732: Skipping environment variable ocean_vertical_diffusivity because of condition ['drift:vertical_mixing', 'is', False]
07:26:03 INFO opendrift:1732: Skipping environment variable ocean_mixed_layer_thickness because of condition ['drift:vertical_mixing', 'is', False]
07:26:03 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']))
07:26:03 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']))
07:26:03 INFO opendrift:1751: Storing previous values of environment variable sea_surface_height because of condition ['drift:vertical_advection', 'is', True]
07:26:03 INFO opendrift:899: Using existing reader for land_binary_mask
07:26:03 INFO opendrift:928: All points are in ocean
07:26:03 INFO opendrift:2035: 2015-11-16 00:00:00 - step 1 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 01:00:00 - step 2 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 02:00:00 - step 3 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 03:00:00 - step 4 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 04:00:00 - step 5 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 05:00:00 - step 6 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 06:00:00 - step 7 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 07:00:00 - step 8 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 08:00:00 - step 9 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 09:00:00 - step 10 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 10:00:00 - step 11 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 11:00:00 - step 12 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 12:00:00 - step 13 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 13:00:00 - step 14 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 14:00:00 - step 15 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 15:00:00 - step 16 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 16:00:00 - step 17 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 17:00:00 - step 18 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 18:00:00 - step 19 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 19:00:00 - step 20 of 66 - 1 active elements (0 deactivated)
07:26:03 INFO opendrift:2035: 2015-11-16 20:00:00 - step 21 of 66 - 1 active elements (0 deactivated)
07:26:04 INFO opendrift:2035: 2015-11-16 21:00:00 - step 22 of 66 - 1 active elements (0 deactivated)
07:26:04 INFO opendrift:2035: 2015-11-16 22:00:00 - step 23 of 66 - 1 active elements (0 deactivated)
07:26:04 INFO opendrift:2035: 2015-11-16 23:00:00 - step 24 of 66 - 1 active elements (0 deactivated)
07:26:04 INFO opendrift:2035: 2015-11-17 00:00:00 - step 25 of 66 - 1 active elements (0 deactivated)
07:26:05 WARNING opendrift:2108: The simulation stopped before requested end time was reached.
07:26:05 INFO opendrift:2110: ========================
07:26:05 INFO opendrift:2111: End of simulation:
07:26:05 INFO opendrift:2112: No more active or scheduled elements, quitting.
07:26:05 INFO opendrift:2113: Traceback (most recent call last):
File "/root/project/opendrift/models/basemodel/__init__.py", line 2095, in run
raise ValueError('No more active or scheduled elements, quitting.')
ValueError: No more active or scheduled elements, quitting.
07:26:05 INFO opendrift:2114: 'The simulation stopped before requested end time was reached.'
07:26:05 INFO opendrift:2117: ========================
07:26:05 WARNING opendrift:2391: Plotting fast. This will make your plots less accurate.
(<GeoAxes: title={'center': 'OpenDrift - OceanDrift\n2015-11-16 00:00 to 2015-11-17 00:00 UTC (25 steps)'}>, <Figure size 673.737x1100 with 1 Axes>)
Total running time of the script: (0 minutes 13.309 seconds)