{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_lb_2.py\nThis script illustrates the following concepts:\n   - Making a vertical colorbar\n   - Changing the colorbar labels\n   - Setting color maps using the new standard\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/lb_2.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/lb_2_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 cartopy.crs as ccrs\nimport matplotlib.pyplot as plt\n\nimport geocat.datafiles as gdf\nfrom geocat.viz import cmaps as gvcmaps\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/atmos.nc\"), decode_times=False)\n# Extract variable\nv = ds.V.isel(time=0, lev=3)\n\n# Fix the artifact of not-shown-data around 0 and 360-degree longitudes\nwrap_v = gvutil.xr_add_cyclic_longitudes(v, \"lon\")"
      ]
    },
    {
      "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)\nfig = plt.figure(figsize=(10, 10))\n\n# Generate axes using Cartopy and draw coastlines\nax = plt.axes(projection=ccrs.PlateCarree())\nax.coastlines(linewidths=0.5)\n\n# Import an NCL colormap\nnewcmp = gvcmaps.wgne15\n\n# Contourf-plot data (for filled contours)\na = wrap_v.plot.contourf(levels=14,\n                         cmap=newcmp,\n                         add_colorbar=False,\n                         add_labels=False)\n# Contour-plot data (for borderlines)\nwrap_v.plot.contour(levels=14, linewidths=0.5, cmap='black', add_labels=False)\n\n# Add vertical colorbar\nclabels = [\n    \"-70\", \"-50\", \"-30\", \"-10\", \"10\", \"30\", \"50\", \"70\", \"90\", \"110\", \"130\",\n    \"150\"\n]\ncbar = fig.colorbar(a, label='', ticks=np.linspace(-24, 24, 12), shrink=0.4)\ncbar.ax.set_yticklabels(clabels)\n\n# Use geocat.viz.util convenience function to set axes limits & tick values without calling several matplotlib functions\ngvutil.set_axes_limits_and_ticks(ax,\n                                 ylim=(-90, 90),\n                                 xticks=np.linspace(-180, 180, 13),\n                                 yticks=np.linspace(-90, 90, 7))\n\n# Use geocat.viz.util convenience function to add minor and major tick lines\ngvutil.add_major_minor_ticks(ax, labelsize=10)\n\n# Use geocat.viz.util convenience function to make plots look like NCL plots by using latitude, longitude tick labels\ngvutil.add_lat_lon_ticklabels(ax)\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                             lefttitle=\"meridional wind component\",\n                             lefttitlefontsize=14,\n                             righttitle=\"m/s\",\n                             righttitlefontsize=14,\n                             xlabel=\"\",\n                             ylabel=\"\")\n\n# Show the plot\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
}