{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_hov_3.py\nThis script illustrates the following concepts:\n   - Creating a Hovmueller plot\n   - Hatching fill between contours\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/hov_3.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/hov_3_lg.png\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Import packages:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport xarray as xr\n\nimport matplotlib.pyplot as plt\n\nimport geocat.datafiles as gdf\nfrom geocat.viz import util as gvutil"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Read in data:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Open a netCDF data file using xarray default engine\n# and load the data into xarrays\nds = xr.open_dataset(gdf.get('netcdf_files/chi200_ud_smooth.nc'))\n\nlon = ds.lon\ntimes = ds.time\nscale = 1000000\nchi = ds.CHI\nchi = chi / scale"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Initialize figure and axis\nfig, ax = plt.subplots(figsize=(7, 7.5))\n\n# Fill area between level 4 contours and level 10 contours with dot hatching\ncf = ax.contourf(lon,\n                 times,\n                 chi,\n                 levels=[4, 12],\n                 colors='None',\n                 hatches=['....'])\n\n# Make all dot-filled areas light gray so contour lines are still visible\nfor i, collection in enumerate(cf.collections):\n    collection.set_edgecolor('lightgray')\n    collection.set_linewidth(0.)\n\n# Fill area at the lowest contour level, -6, with line hatching\ncf = ax.contourf(lon,\n                 times,\n                 chi,\n                 levels=[-7, -6],\n                 colors='None',\n                 hatches=['///'])\n\n# Draw contour lines at levels [-6, -4, -2, 0, 2, 4, 6, 8, 10]\ncs = ax.contour(lon,\n                times,\n                chi,\n                levels=np.arange(-6, 12, 2),\n                colors='black',\n                linestyles=\"-\",\n                linewidths=.2)\n\n# Set 0 level contour line to a thicker linewidth\n# If you try to access the \"levels\" attribute of cs (cs.levels),\n# the list of levels is: [-6, -4, -2, 0, 2, 4, 6, 8, 10]\n# level 0 is at the 3rd index of that list, so those contour lines\n# can be accessed at cs.collections[3]\ncs.collections[3].set_linewidth(1)\n\n# Label the contour levels -4, 0, and 4\ncl = ax.clabel(cs, fmt='%d', levels=[-4, 0, 4])\n\n# Use geocat.viz.util convenience function to set axes limits & tick values\ngvutil.set_axes_limits_and_ticks(ax,\n                                 xlim=[100, 220],\n                                 ylim=[0, 1.55 * 1e16],\n                                 xticks=[135, 180],\n                                 yticks=np.linspace(0, 1.55 * 1e16, 7),\n                                 xticklabels=['135E', '180'],\n                                 yticklabels=np.linspace(0, 180, 7,\n                                                         dtype='int'))\n\n# Use geocat.viz.util convenience function to add minor and major tick lines\ngvutil.add_major_minor_ticks(ax,\n                             x_minor_per_major=3,\n                             y_minor_per_major=3,\n                             labelsize=16)\n\n# Use geocat.viz.util convenience function to add titles\ngvutil.set_titles_and_labels(ax,\n                             maintitle=\"Pacific Region\",\n                             maintitlefontsize=20,\n                             lefttitle=\"Velocity Potential\",\n                             lefttitlefontsize=18,\n                             righttitle=\"m2/s\",\n                             righttitlefontsize=18,\n                             ylabel=\"elapsed time\",\n                             labelfontsize=18)\n\n# Add lower text box\nax.text(1,\n        -0.12,\n        \"CONTOUR FROM -6 TO 10 BY 2\",\n        horizontalalignment='right',\n        transform=ax.transAxes,\n        bbox=dict(boxstyle='square, pad=0.25',\n                  facecolor='white',\n                  edgecolor='black'))\n\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.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}