{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_conOncon_1.py\nThis script illustrates the following concepts:\n   - Drawing pressure/height contours on top of another set of contours\n   - Drawing negative contour lines as dashed lines\n   - Drawing the zero contour line thicker\n   - Changing the color of a contour line\n   - Overlaying dashed contours on solid line contours\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/conOncon_1.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/conOncon_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\n\nimport matplotlib.pyplot as plt\nfrom matplotlib.ticker import ScalarFormatter\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/mxclim.nc\"))\n# Extract variables\nU = ds.U[0, :, :]\nV = ds.V[0, :, :]"
      ]
    },
    {
      "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=(12, 12))\nax = plt.gca()\n\n# Set y-axis to have log-scale\nplt.yscale('log')\n\n# Contour-plot U-data\np = U.plot.contour(ax=ax, levels=16, colors='red', extend='neither')\nax.clabel(p, fmt='%d', inline=1, fontsize=14)\n\n# Contour-plot V-data\np = V.plot.contour(ax=ax, levels=16, colors='blue', extend='neither')\nax.clabel(p, fmt='%d', inline=1, fontsize=14)\n\n# Use geocat.viz.util convenience function to set axes tick values\n# Set y-lim inorder for y-axis to have descending values\ngvutil.set_axes_limits_and_ticks(ax,\n                                 xticks=np.linspace(-60, 60, 5),\n                                 xticklabels=['60S', '30S', '0', '30N', '60N'],\n                                 ylim=ax.get_ylim()[::-1],\n                                 yticks=U[\"lev\"])\n\n# Change formatter or else we tick values formatted in exponential form\nax.yaxis.set_major_formatter(ScalarFormatter())\n\n# Tweak label sizes, etc.\nax.yaxis.label.set_size(20)\nax.tick_params('both', length=20, width=2, which='major', labelsize=18)\nax.minorticks_off()\n\n# Use geocat.viz.util convenience function to add titles to left and right of the plot axis.\ngvutil.set_titles_and_labels(ax,\n                             maintitle=\"Ensemble Average 1987-89\",\n                             maintitlefontsize=20,\n                             lefttitle=U.long_name,\n                             lefttitlefontsize=18,\n                             righttitle=U.units,\n                             righttitlefontsize=18,\n                             xlabel=\"\")\n\n# Create second y-axis to show geo-potential height.\n# Currently we're using bogus values for height, cause we haven't figured out how to make this work.\naxRHS = ax.twinx()\ndummy = 10\nmn, mx = ax.get_ylim()\naxRHS.set_ylim(mn * dummy, mx * dummy)\naxRHS.set_ylim(axRHS.get_ylim()[::-1])\naxRHS.set_ylabel('Height (km)')\naxRHS.yaxis.label.set_size(20)\naxRHS.tick_params('both', length=20, width=2, which='major', labelsize=18)\n\n# Show the plot\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
}