{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Demonstrating basic plotting\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import cartopy.crs as ccrs\nimport lzma\nimport matplotlib.pyplot as plt\nimport xarray as xr\nimport trajan as ta\nimport coloredlogs"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Demonstrating how a trajectory dataset (from OpenDrift)\n can be analysed and plotted with Trajan\n##########################################################\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "coloredlogs.install(level='debug')"
      ]
    },
    {
      "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": [
        "with lzma.open('openoil.nc.xz') as oil:\n    d = xr.open_dataset(oil, decode_coords=False)\n    d.load()\n    # Requirement that status>=0 is needed since non-valid points are not masked in OpenDrift output\n    d = d.where(d.status>=0)  # only active particles"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Displaying a basic plot of trajectories\nd.traj.plot()\nplt.title('Basic trajectory plot')\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Demonstrating how the Xarray Dataset can be modified, allowing for\n more flexibility than can be provided through the plotting method of OpenDrift\n#################################################################################\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Extracting only the first 10 elements, and every 4th output time steps:\nd.isel(trajectory=range(0, 10), time=range(0, len(d.time), 4)).traj.plot()\nplt.title('First 10 elements, and every 4th time steps')\nplt.show()\n\n# Plotting a \"mean\" trajectory on top\nd.traj.plot(color='red', alpha=0.01, land='fast')  # Plotting trajectories in red, and with landmask as land.\ndmean = d.mean('trajectory', skipna=True)\ndmean.traj.plot.lines(color='black', linewidth=5)  # Plotting mean trajectory in black\nplt.show()\n\n# Calling set_up_map explicitly\nd.traj.plot.set_up_map(margin=0)\nd.traj.plot(color='red', alpha=0.01)  # Plotting trajectories in red\ndmean.traj.plot(color='black', alpha=1, linewidth=5)  # Plotting mean trajectory in black\n# Plotting the mean trajectory for a sub period in yellow\ndmean17nov = d.sel(time=slice('2015-11-17', '2015-11-17 12')).mean('trajectory', skipna=True)\ndmean17nov.traj.plot(color='yellow', alpha=1, linewidth=5)\nplt.tight_layout()\nplt.savefig('testplot.png')  # TODO: this produces a figure, but with title from previous"
      ]
    }
  ],
  "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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}