{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Trajan demo\n\nFrom OpenDrift 2.0, analysis and plotting of results from OpenDrift simulations\nwill be handled by a new, standalone package: Trajan\nhttps://github.com/OpenDrift/trajan\n\nThis example creates a test dataset, and demonstrates its anlysis using Trajan\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import os\nfrom datetime import datetime, timedelta\nimport matplotlib.pyplot as plt\nimport xarray as xr\nimport trajan as ta\nfrom opendrift.readers import reader_netCDF_CF_generic\nfrom opendrift.models.openoil import OpenOil" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create test dataset with OpenDrift\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "o = OpenOil(loglevel=20)\n\n# Add forcing\nreader_arome = reader_netCDF_CF_generic.Reader(o.test_data_folder() +\n '16Nov2015_NorKyst_z_surface/arome_subset_16Nov2015.nc')\nreader_norkyst = reader_netCDF_CF_generic.Reader(o.test_data_folder() +\n '16Nov2015_NorKyst_z_surface/norkyst800_subset_16Nov2015.nc')\no.add_reader([reader_norkyst, reader_arome])\n\n# Seeding some particles\no.seed_elements(lon=4.4, lat=60.1, radius=1000, number=1000,\n time=reader_arome.start_time)\n\n# Running model\no.run(end_time=reader_norkyst.end_time, outfile='openoil.nc')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demonstrating analysis and visualisation of the output dataset, independently of OpenDrift code\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "if not os.path.exists('openoil.nc'):\n raise ValueError('Please run create_test_dataset.py first')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Importing a trajectory dataset from a simulation with OpenDrift\ndecode_coords is needed so that lon and lat are not interpreted as coordinate variables\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d = xr.open_dataset('openoil.nc', decode_coords=False)\n# Requirement that status>=0 is needed since non-valid points are not masked in OpenDrift output\nd = d.where(d.status>=0) # only active particles" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Displaying a basic plot of trajectories\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d.traj.plot(land='mask')\nax = plt.gca()\nax.set_title('Adding custom title')\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Demonstrating how the Xarray Dataset can be modified, allowing for\nmore flexibility than can be provided through the plotting method of OpenDrift\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extracting only the first 100 elements, and every 4th output time steps:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dsub = d.isel(trajectory=range(0, 100), time=range(0, len(d.time), 4))\ndsub.traj.plot(land='h')\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With several plots on the same figure\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "d.traj.plot(color='red', alpha=0.01, land='mask') # Plotting individual trajectories in red\ndmean = d.mean('trajectory', skipna=True) # Overlaying a \"mean\" trajectory in black\ndmean.traj.plot(color='k', linewidth=5)\n# Showing the a sub-period of the mean trajectory in yellow\ndmean.sel(time=slice('2015-11-17', '2015-11-17 12')).traj.plot(color='yellow', linewidth=5)\nplt.tight_layout()\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.6" } }, "nbformat": 4, "nbformat_minor": 0 }