{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_xy_5.py\nThis script illustrates the following concepts:\n   - Draw multiple curves on an XY plot\n   - Drawing a Y reference line in an XY plot\n   - Filling the areas of an XY curve above and below a reference line\n   - Using named colors to indicate a fill color\n   - Converting dates from YYYYMM format to floats\n   - Creating a main title\n   - Setting the mininum/maximum value of the Y axis in an XY plot\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/xy_5.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/xy_5_1_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\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 and load the data into xarrays\nds = xr.open_dataset(gdf.get(\"netcdf_files/soi.nc\"))\ndsoik = ds.DSOI_KET\ndsoid = ds.DSOI_DEC\ndate = ds.date\nnum_months = np.shape(date)[0]\n\n# Dates in the file are represented by year and month (YYYYMM)\n# representing them fractionally will make ploting the data easier\n# This produces the same results as NCL's yyyymm_to_yyyyfrac() function\ndate_frac = np.empty_like(date)\nfor n in np.arange(0, num_months, 1):\n    yyyy = int(date[n] / 100)\n    mon = (date[n] / 100 - yyyy) * 100\n    date_frac[n] = yyyy + (mon - 1) / 12"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Generate figure (set its size (width, height) in inches) and axes\nplt.figure(figsize=(8, 4))\nax = plt.gca()\n\n# Plot reference line\nax.axhline(y=0, color='grey', linewidth=0.75)\n\n# Plot data\n# _labels=False prevents axis labels from being drawn\nax.plot(date_frac, dsoik, color='black', linewidth=0.5)\nax.plot(date_frac, dsoid, color='black')\n\n# Fill above and below the 0 line\nax.fill_between(date_frac, dsoik, where=dsoik > 0, color='red')\nax.fill_between(date_frac, dsoik, where=dsoik < 0, color='blue')\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=4,\n                             y_minor_per_major=5,\n                             labelsize=14)\n\n# Use geocat.viz.util convenience function to set axes parameters\ngvutil.set_axes_limits_and_ticks(ax,\n                                 ylim=(-3, 3),\n                                 yticks=np.linspace(-3, 3, 7),\n                                 yticklabels=np.linspace(-3, 3, 7),\n                                 xlim=(date_frac[0], date_frac[-1]),\n                                 xticks=np.linspace(1880, 1980, 6))\n\n# Use geocat.viz.util convenience function to set titles and labels\ngvutil.set_titles_and_labels(ax, maintitle=\"Darwin Southern Oscillation Index\")\n\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
}