{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_color_1.py\nThis script illustrates the following concepts:\n   - Drawing a horizonal color bar\n   - Adjusting a colorbar position relative to plot axes\n   - Recreating a default NCL colormap\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/color_1.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/color_1_lg.png\n\nNote:\n    This may not be the best colormap to interpret the information, but was included here in order to\n    demonstrate how to recreate the original NCL colormap. For more information on colormap choices, see the\n    Colors examples in the GeoCAT-examples documentation.\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 matplotlib.pyplot as plt\nimport xarray as xr\nimport cartopy.crs as ccrs\n\nimport geocat.datafiles as gdf\nfrom geocat.viz import cmaps as gvcmaps\nimport geocat.viz.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 xarray\nds = xr.open_dataset(gdf.get(\"netcdf_files/uv300.nc\")).isel(time=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Generate figure and set its size in (width, height)\nfig = plt.figure(figsize=(10, 8))\n\n# Generate axes using Cartopy to draw coastlines\nax = plt.axes(projection=ccrs.PlateCarree())\nax.coastlines(linewidth=0.5, alpha=0.6)\n\n# Use geocat.viz.util convenience function to set axes limits & tick values\ngvutil.set_axes_limits_and_ticks(ax,\n                                 xlim=(-180, 180),\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 latitude, longitude tick labels\ngvutil.add_lat_lon_ticklabels(ax)\n\n# Import the default color map\nnewcmp = gvcmaps.ncl_default\n\n# Define contour levels\nlevels = np.arange(-16, 48, 4)\n\n# Define dictionary for kwargs\nkwargs = dict(\n    levels=levels,\n    xticks=np.arange(-180, 181, 30),  # nice x ticks\n    yticks=np.arange(-90, 91, 30),  # nice y ticks\n    add_colorbar=False,  # allow for colorbar specification later\n    transform=ccrs.PlateCarree(),  # ds projection\n)\n\n# Contouf-plot U data (for filled contours)\nfillplot = ds.U.plot.contourf(ax=ax, cmap=newcmp, **kwargs)\n\n# Create horizonal color bar\n# By changing the kwarg `pad`, the colorbar can be moved closer to or farther away from\n# the axis parallel to it.\n# `pad` defaults to 0.15 for horizontal colorbars\nfig.colorbar(fillplot,\n             orientation=\"horizontal\",\n             ticks=np.arange(-12, 44, 4),\n             label='',\n             shrink=0.75,\n             pad=0.11)\n\n# Plot line contours\nds.U.plot.contour(ax=ax,\n                  colors='black',\n                  alpha=0.8,\n                  linewidths=0.4,\n                  linestyles='solid',\n                  add_labels=False,\n                  levels=levels,\n                  transform=ccrs.PlateCarree())\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=\"Default Color\",\n                             lefttitle=ds.U.long_name,\n                             lefttitlefontsize=16,\n                             righttitle=ds.U.units,\n                             righttitlefontsize=16,\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
}