Trajan demo

From OpenDrift 2.0, analysis and plotting of results from OpenDrift simulations will be handled by a new, standalone package: Trajan https://github.com/OpenDrift/trajan

This example creates a test dataset, and demonstrates its anlysis using Trajan

import os
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import xarray as xr
import trajan as ta
from opendrift.readers import reader_netCDF_CF_generic
from opendrift.models.openoil import OpenOil

Create test dataset with OpenDrift

o = OpenOil(loglevel=20)

# Add forcing
reader_arome = reader_netCDF_CF_generic.Reader(o.test_data_folder() +
    '16Nov2015_NorKyst_z_surface/arome_subset_16Nov2015.nc')
reader_norkyst = reader_netCDF_CF_generic.Reader(o.test_data_folder() +
    '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')
o.add_reader([reader_norkyst, reader_arome])

# Seeding some particles
o.seed_elements(lon=4.4, lat=60.1, radius=1000, number=1000,
                time=reader_arome.start_time)

# Running model
o.run(end_time=reader_norkyst.end_time, outfile='openoil.nc')
20:16:24 INFO    opendrift.models.basemodel:515: OpenDriftSimulation initialised (version 1.12.0 / v1.12.0-26-g390e945)
20:16:24 INFO    opendrift.readers:58: Opening file with xr.open_dataset
20:16:24 INFO    opendrift.readers.reader_netCDF_CF_generic:337: Detected dimensions: {'time': 'time', 'x': 'x', 'y': 'y'}
20:16:24 INFO    opendrift.readers:58: Opening file with xr.open_dataset
20:16:24 INFO    opendrift.readers.reader_netCDF_CF_generic:337: Detected dimensions: {'x': 'X', 'y': 'Y', 'z': 'depth', 'time': 'time'}
20:16:24 INFO    opendrift.models.openoil.openoil:1706: Oil type not specified, using default: GENERIC BUNKER C
20:16:24 INFO    opendrift.models.openoil.adios.dirjs:86: Querying ADIOS database for oil: GENERIC BUNKER C
20:16:24 INFO    opendrift.models.openoil.openoil:1715: Using density 988.1 and viscosity 0.021692333877975645 of oiltype GENERIC BUNKER C
20:16:24 INFO    opendrift.models.basemodel.environment:218: Adding a dynamical landmask with max. priority based on assumed maximum speed of 1.3 m/s. Adding a customised landmask may be faster...
20:16:28 INFO    opendrift.models.basemodel.environment:245: Fallback values will be used for the following variables which have no readers:
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_surface_height: 0.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    upward_sea_water_velocity: 0.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_significant_height: 0.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_stokes_drift_x_velocity: 0.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_stokes_drift_y_velocity: 0.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_period_at_variance_spectral_density_maximum: 0.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_surface_wave_mean_period_from_variance_spectral_density_second_frequency_moment: 0.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_ice_area_fraction: 0.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_ice_x_velocity: 0.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_ice_y_velocity: 0.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_water_temperature: 10.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_water_salinity: 34.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    sea_floor_depth_below_sea_level: 10000.000000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    ocean_vertical_diffusivity: 0.020000
20:16:28 INFO    opendrift.models.basemodel.environment:248:    ocean_mixed_layer_thickness: 50.000000
20:16:30 INFO    opendrift.models.basemodel:935: Using existing reader for land_binary_mask
20:16:30 INFO    opendrift.models.basemodel:946: All points are in ocean
20:16:30 INFO    opendrift.models.openoil.openoil:684: Oil-water surface tension is 0.035935 Nm
20:16:30 INFO    opendrift.models.openoil.openoil:697: Max water fraction not available for GENERIC BUNKER C, using default
20:16:30 INFO    opendrift.models.basemodel:2035: 2015-11-16 00:00:00 - step 1 of 66 - 1000 active elements (0 deactivated)
20:16:30 INFO    opendrift.models.basemodel:2035: 2015-11-16 01:00:00 - step 2 of 66 - 1000 active elements (0 deactivated)
20:16:30 INFO    opendrift.models.basemodel:2035: 2015-11-16 02:00:00 - step 3 of 66 - 1000 active elements (0 deactivated)
20:16:30 INFO    opendrift.models.basemodel:2035: 2015-11-16 03:00:00 - step 4 of 66 - 1000 active elements (0 deactivated)
20:16:31 INFO    opendrift.models.basemodel:2035: 2015-11-16 04:00:00 - step 5 of 66 - 1000 active elements (0 deactivated)
20:16:31 INFO    opendrift.models.basemodel:2035: 2015-11-16 05:00:00 - step 6 of 66 - 1000 active elements (0 deactivated)
20:16:31 INFO    opendrift.models.basemodel:2035: 2015-11-16 06:00:00 - step 7 of 66 - 1000 active elements (0 deactivated)
20:16:32 INFO    opendrift.models.basemodel:2035: 2015-11-16 07:00:00 - step 8 of 66 - 1000 active elements (0 deactivated)
20:16:32 INFO    opendrift.models.basemodel:2035: 2015-11-16 08:00:00 - step 9 of 66 - 1000 active elements (0 deactivated)
20:16:32 INFO    opendrift.models.basemodel:2035: 2015-11-16 09:00:00 - step 10 of 66 - 1000 active elements (0 deactivated)
20:16:33 INFO    opendrift.models.basemodel:2035: 2015-11-16 10:00:00 - step 11 of 66 - 1000 active elements (0 deactivated)
20:16:33 INFO    opendrift.models.basemodel:2035: 2015-11-16 11:00:00 - step 12 of 66 - 1000 active elements (0 deactivated)
20:16:33 INFO    opendrift.models.basemodel:2035: 2015-11-16 12:00:00 - step 13 of 66 - 1000 active elements (0 deactivated)
20:16:33 INFO    opendrift.models.basemodel:2035: 2015-11-16 13:00:00 - step 14 of 66 - 1000 active elements (0 deactivated)
20:16:33 INFO    opendrift.models.basemodel:2035: 2015-11-16 14:00:00 - step 15 of 66 - 1000 active elements (0 deactivated)
20:16:33 INFO    opendrift.models.basemodel:2035: 2015-11-16 15:00:00 - step 16 of 66 - 1000 active elements (0 deactivated)
20:16:34 INFO    opendrift.models.basemodel:2035: 2015-11-16 16:00:00 - step 17 of 66 - 1000 active elements (0 deactivated)
20:16:34 INFO    opendrift.models.basemodel:2035: 2015-11-16 17:00:00 - step 18 of 66 - 1000 active elements (0 deactivated)
20:16:34 INFO    opendrift.models.basemodel:2035: 2015-11-16 18:00:00 - step 19 of 66 - 1000 active elements (0 deactivated)
20:16:34 INFO    opendrift.models.basemodel:2035: 2015-11-16 19:00:00 - step 20 of 66 - 1000 active elements (0 deactivated)
20:16:34 INFO    opendrift.models.basemodel:2035: 2015-11-16 20:00:00 - step 21 of 66 - 1000 active elements (0 deactivated)
20:16:35 INFO    opendrift.models.basemodel:2035: 2015-11-16 21:00:00 - step 22 of 66 - 1000 active elements (0 deactivated)
20:16:35 INFO    opendrift.models.basemodel:2035: 2015-11-16 22:00:00 - step 23 of 66 - 1000 active elements (0 deactivated)
20:16:35 INFO    opendrift.models.basemodel:2035: 2015-11-16 23:00:00 - step 24 of 66 - 1000 active elements (0 deactivated)
20:16:35 INFO    opendrift.models.basemodel:2035: 2015-11-17 00:00:00 - step 25 of 66 - 1000 active elements (0 deactivated)
20:16:35 WARNING opendrift.readers.basereader.structured:324: Data block from /root/project/tests/test_data/16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc not large enough to cover element positions within timestep. Buffer size (8) must be increased. See `Variables.set_buffer_size`.
20:16:35 INFO    opendrift.models.basemodel:2035: 2015-11-17 01:00:00 - step 26 of 66 - 1000 active elements (0 deactivated)
20:16:35 WARNING opendrift.readers.basereader.structured:324: Data block from /root/project/tests/test_data/16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc not large enough to cover element positions within timestep. Buffer size (8) must be increased. See `Variables.set_buffer_size`.
20:16:35 WARNING opendrift.readers.basereader.structured:324: Data block from /root/project/tests/test_data/16Nov2015_NorKyst_z_surface/arome_subset_16Nov2015.nc not large enough to cover element positions within timestep. Buffer size (4) must be increased. See `Variables.set_buffer_size`.
20:16:35 INFO    opendrift.models.basemodel:2035: 2015-11-17 02:00:00 - step 27 of 66 - 1000 active elements (0 deactivated)
20:16:35 INFO    opendrift.models.basemodel:2035: 2015-11-17 03:00:00 - step 28 of 66 - 1000 active elements (0 deactivated)
20:16:36 INFO    opendrift.models.basemodel:2035: 2015-11-17 04:00:00 - step 29 of 66 - 1000 active elements (0 deactivated)
20:16:39 INFO    opendrift.models.basemodel:2035: 2015-11-17 05:00:00 - step 30 of 66 - 999 active elements (1 deactivated)
20:16:39 INFO    opendrift.models.basemodel:2035: 2015-11-17 06:00:00 - step 31 of 66 - 999 active elements (1 deactivated)
20:16:39 INFO    opendrift.models.basemodel:2035: 2015-11-17 07:00:00 - step 32 of 66 - 994 active elements (6 deactivated)
20:16:39 INFO    opendrift.models.basemodel:2035: 2015-11-17 08:00:00 - step 33 of 66 - 989 active elements (11 deactivated)
20:16:39 INFO    opendrift.models.basemodel:2035: 2015-11-17 09:00:00 - step 34 of 66 - 962 active elements (38 deactivated)
20:16:39 WARNING opendrift.readers.basereader.structured:324: Data block from /root/project/tests/test_data/16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc not large enough to cover element positions within timestep. Buffer size (8) must be increased. See `Variables.set_buffer_size`.
20:16:39 INFO    opendrift.models.basemodel:2035: 2015-11-17 10:00:00 - step 35 of 66 - 908 active elements (92 deactivated)
20:16:39 INFO    opendrift.models.basemodel:2035: 2015-11-17 11:00:00 - step 36 of 66 - 853 active elements (147 deactivated)
20:16:39 INFO    opendrift.models.basemodel:2035: 2015-11-17 12:00:00 - step 37 of 66 - 804 active elements (196 deactivated)
20:16:39 INFO    opendrift.models.basemodel:2035: 2015-11-17 13:00:00 - step 38 of 66 - 773 active elements (227 deactivated)
20:16:40 INFO    opendrift.models.basemodel:2035: 2015-11-17 14:00:00 - step 39 of 66 - 737 active elements (263 deactivated)
20:16:40 INFO    opendrift.models.basemodel:2035: 2015-11-17 15:00:00 - step 40 of 66 - 692 active elements (308 deactivated)
20:16:40 INFO    opendrift.models.basemodel:2035: 2015-11-17 16:00:00 - step 41 of 66 - 644 active elements (356 deactivated)
20:16:40 INFO    opendrift.models.basemodel:2035: 2015-11-17 17:00:00 - step 42 of 66 - 590 active elements (410 deactivated)
20:16:40 INFO    opendrift.models.basemodel:2035: 2015-11-17 18:00:00 - step 43 of 66 - 545 active elements (455 deactivated)
20:16:40 INFO    opendrift.models.basemodel:2035: 2015-11-17 19:00:00 - step 44 of 66 - 483 active elements (517 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-17 20:00:00 - step 45 of 66 - 401 active elements (599 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-17 21:00:00 - step 46 of 66 - 327 active elements (673 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-17 22:00:00 - step 47 of 66 - 304 active elements (696 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-17 23:00:00 - step 48 of 66 - 286 active elements (714 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-18 00:00:00 - step 49 of 66 - 277 active elements (723 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-18 01:00:00 - step 50 of 66 - 269 active elements (731 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-18 02:00:00 - step 51 of 66 - 263 active elements (737 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-18 03:00:00 - step 52 of 66 - 259 active elements (741 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-18 04:00:00 - step 53 of 66 - 256 active elements (744 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-18 05:00:00 - step 54 of 66 - 254 active elements (746 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-18 06:00:00 - step 55 of 66 - 244 active elements (756 deactivated)
20:16:41 INFO    opendrift.models.basemodel:2035: 2015-11-18 07:00:00 - step 56 of 66 - 236 active elements (764 deactivated)
20:16:42 INFO    opendrift.models.basemodel:2035: 2015-11-18 08:00:00 - step 57 of 66 - 232 active elements (768 deactivated)
20:16:42 INFO    opendrift.models.basemodel:2035: 2015-11-18 09:00:00 - step 58 of 66 - 231 active elements (769 deactivated)
20:16:42 INFO    opendrift.models.basemodel:2035: 2015-11-18 10:00:00 - step 59 of 66 - 228 active elements (772 deactivated)
20:16:42 INFO    opendrift.models.basemodel:2035: 2015-11-18 11:00:00 - step 60 of 66 - 224 active elements (776 deactivated)
20:16:42 INFO    opendrift.models.basemodel:2035: 2015-11-18 12:00:00 - step 61 of 66 - 224 active elements (776 deactivated)
20:16:42 INFO    opendrift.models.basemodel:2035: 2015-11-18 13:00:00 - step 62 of 66 - 220 active elements (780 deactivated)
20:16:42 INFO    opendrift.models.basemodel:2035: 2015-11-18 14:00:00 - step 63 of 66 - 220 active elements (780 deactivated)
20:16:42 INFO    opendrift.models.basemodel:2035: 2015-11-18 15:00:00 - step 64 of 66 - 219 active elements (781 deactivated)
20:16:42 INFO    opendrift.models.basemodel:2035: 2015-11-18 16:00:00 - step 65 of 66 - 219 active elements (781 deactivated)
20:16:42 INFO    opendrift.models.basemodel:2035: 2015-11-18 17:00:00 - step 66 of 66 - 218 active elements (782 deactivated)
20:16:43 INFO    opendrift.export.io_netcdf:121: Wrote 67 steps to file openoil.nc
/opt/conda/envs/opendrift/lib/python3.11/site-packages/numpy/ma/core.py:467: RuntimeWarning: invalid value encountered in cast
  fill_value = np.array(fill_value, copy=False, dtype=ndtype)

Demonstrating analysis and visualisation of the output dataset, independently of OpenDrift code

if not os.path.exists('openoil.nc'):
    raise ValueError('Please run create_test_dataset.py first')

Importing a trajectory dataset from a simulation with OpenDrift decode_coords is needed so that lon and lat are not interpreted as coordinate variables

d = xr.open_dataset('openoil.nc', decode_coords=False)
# Requirement that status>=0 is needed since non-valid points are not masked in OpenDrift output
d = d.where(d.status>=0)  # only active particles

Displaying a basic plot of trajectories

d.traj.plot(land='mask')
ax = plt.gca()
ax.set_title('Adding custom title')
plt.show()
Adding custom title

Demonstrating how the Xarray Dataset can be modified, allowing for more flexibility than can be provided through the plotting method of OpenDrift

Extracting only the first 100 elements, and every 4th output time steps:

dsub = d.isel(trajectory=range(0, 100), time=range(0, len(d.time), 4))
dsub.traj.plot(land='h')
plt.show()
example trajan

With several plots on the same figure

d.traj.plot(color='red', alpha=0.01, land='mask')  # Plotting individual trajectories in red
dmean = d.mean('trajectory', skipna=True) # Overlaying a "mean" trajectory in black
dmean.traj.plot(color='k', linewidth=5)
# Showing the a sub-period of the mean trajectory in yellow
dmean.sel(time=slice('2015-11-17', '2015-11-17 12')).traj.plot(color='yellow', linewidth=5)
plt.tight_layout()
plt.show()
example trajan

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

Gallery generated by Sphinx-Gallery