Customising Xarray Dataset

from datetime import datetime, timedelta
import xarray as xr
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
15:24:14 INFO    opendrift:509: OpenDriftSimulation initialised (version 1.13.1 / v1.13.1-82-gbc545aa)

Opening the currents netCDF file with xarray

ds = xr.open_dataset(o.test_data_folder() + '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
15:24:14 INFO    opendrift.readers.reader_netCDF_CF_generic:332: Detected dimensions: {'x': 'X', 'y': 'Y', 'z': 'depth', 'time': 'time'}
===========================
Reader: <xarray.Dataset> Size: 33MB
Dimensions:           (X: 301, Y: 201, depth: 1, time: 67)
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)
OpenDrift - OceanDrift 2015-11-16 00:00 to 2015-11-17 00:00 UTC (25 steps)
15:24:14 INFO    opendrift.models.basemodel.environment:229: Fallback values will be used for the following variables which have no readers:
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_height: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    x_wind: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    y_wind: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    upward_sea_water_velocity: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    ocean_vertical_diffusivity: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_significant_height: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_period_at_variance_spectral_density_maximum: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_swell_wave_to_direction: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_swell_wave_peak_period_from_variance_spectral_density: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_swell_wave_significant_height: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wind_wave_to_direction: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wind_wave_mean_period: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_surface_wind_wave_significant_height: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    surface_downward_x_stress: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    surface_downward_y_stress: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    turbulent_kinetic_energy: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    turbulent_generic_length_scale: 0.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    ocean_mixed_layer_thickness: 50.000000
15:24:14 INFO    opendrift.models.basemodel.environment:232:    sea_floor_depth_below_sea_level: 10000.000000
15:24:14 INFO    opendrift:907: Using existing reader for land_binary_mask
15:24:14 INFO    opendrift:936: All points are in ocean
15:24:14 INFO    opendrift:2069: 2015-11-16 00:00:00 - step 1 of 66 - 1 active elements (0 deactivated)
15:24:14 INFO    opendrift:2069: 2015-11-16 01:00:00 - step 2 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 02:00:00 - step 3 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 03:00:00 - step 4 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 04:00:00 - step 5 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 05:00:00 - step 6 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 06:00:00 - step 7 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 07:00:00 - step 8 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 08:00:00 - step 9 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 09:00:00 - step 10 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 10:00:00 - step 11 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 11:00:00 - step 12 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 12:00:00 - step 13 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 13:00:00 - step 14 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 14:00:00 - step 15 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 15:00:00 - step 16 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 16:00:00 - step 17 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 17:00:00 - step 18 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 18:00:00 - step 19 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 19:00:00 - step 20 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 20:00:00 - step 21 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 21:00:00 - step 22 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 22:00:00 - step 23 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-16 23:00:00 - step 24 of 66 - 1 active elements (0 deactivated)
15:24:15 INFO    opendrift:2069: 2015-11-17 00:00:00 - step 25 of 66 - 1 active elements (0 deactivated)
15:24:17 WARNING opendrift:2167: The simulation stopped before requested end time was reached.
15:24:17 INFO    opendrift:2169: ========================
15:24:17 INFO    opendrift:2170: End of simulation:
15:24:17 INFO    opendrift:2171: No more active or scheduled elements, quitting.
15:24:17 INFO    opendrift:2172: Traceback (most recent call last):
  File "/root/project/opendrift/models/basemodel/__init__.py", line 2134, in run
    raise ValueError(
        'No more active or scheduled elements, quitting.')
ValueError: No more active or scheduled elements, quitting.

15:24:17 INFO    opendrift:2173: 'The simulation stopped before requested end time was reached.'

15:24:17 INFO    opendrift:2176: ========================
15:24:17 WARNING opendrift:2446: 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 14.892 seconds)

Gallery generated by Sphinx-Gallery