{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_xy_10.py\nThis script illustrates the following concepts:\n   - Filling the area between two curves in an XY plot\n   - Drawing Greek characters on 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_10.ncl\n    - Original NCL plots: https://www.ncl.ucar.edu/Applications/Images/xy_10_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\nimport math\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/80.nc\"))\n# Extract slice of data\nTS = ds.isel(time=0, lon=21, drop=True).TS"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Define bounds for region centered on the data with a width of 2 sigma\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "nlat = np.shape(TS.lat)[0]\ntop = np.empty(nlat)\nbottom = np.empty(nlat)\n\nfor k in range(0, nlat):\n    dx = math.sqrt(TS[k])\n    top[k] = TS[k] + dx\n    bottom[k] = TS[k] - dx"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "plt.figure(figsize=(8, 8))\nax = plt.axes()\n\n# Plot data\nTS.plot.line(ax=ax, color='black', _labels=False)\n\n# Plot curves that bound the region to be colored\nplt.plot(TS.lat, top, color='SlateBlue')\nplt.plot(TS.lat, bottom, color='SlateBlue')\n\n# Fill the area between the bounds\nax.fill_between(TS.lat, top, bottom, color='SlateBlue')\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=4,\n                             labelsize=14)\n\n# Use geocat.viz.util convenience function to set axes parameters\ngvutil.set_axes_limits_and_ticks(\n    ax,\n    ylim=(220, 320),\n    xlim=(-90, 90),\n    xticks=np.arange(-90, 91, 30),\n    xticklabels=['90S', '60S', '30S', '0', '30N', '60N', '90N'])\n\n# Use geocat.viz.util convenience function to set titles and labels\ngvutil.set_titles_and_labels(\n    ax,\n    maintitle=\"A Title with $\\\\eta\\epsilon\\lambda\\\\alpha\\sigma$ Characters\",\n    ylabel=TS.long_name)\n\n# Draw sigma on axes\nax.text(0.15, 0.15, \"$\\sigma$\", fontsize=40, transform=ax.transAxes)\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
}