{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_conwomap_2.py\nThis script illustrates the following concepts:\n   - Drawing a simple filled contour plot\n   - Selecting a different color map\n   - Changing the size/shape of a contour plot\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/conwomap_2.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/conwomap_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/cone.nc\"))\nu = ds.u.isel(time=4)"
      ]
    },
    {
      "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)\nplt.figure(figsize=(10, 6))\n\n# Generate axes, using Cartopy\nprojection = ccrs.PlateCarree()\nax = plt.axes(projection=projection)\n\n# Import an NCL colormap\nnewcmp = gvcmaps.gui_default\n\n# Contourf-plot data (for filled contours)\np = u.plot.contourf(ax=ax,\n                    vmin=-1,\n                    vmax=10,\n                    levels=12,\n                    cmap=newcmp,\n                    add_colorbar=False,\n                    transform=projection,\n                    extend='neither',\n                    add_labels=False)\n# Contour-plot data (for borderlines)\nu.plot.contour(ax=ax,\n               vmin=-1,\n               vmax=10,\n               levels=12,\n               linewidths=0.5,\n               colors='black',\n               add_colorbar=False,\n               transform=projection,\n               extend='neither',\n               add_labels=False)\n\n# Add horizontal colorbar\ncbar = plt.colorbar(p, orientation='horizontal', shrink=0.5)\ncbar.ax.tick_params(labelsize=16)\ncbar.set_ticks(np.linspace(0, 9, 10))\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                                 xlim=(0, 49),\n                                 ylim=(0, 29),\n                                 xticks=np.linspace(0, 40, 5),\n                                 yticks=np.linspace(0, 25, 6))\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=5,\n                             y_minor_per_major=5,\n                             labelsize=16)\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=\"Cone amplitude\",\n                             lefttitlefontsize=18,\n                             righttitle=\"ndim\",\n                             righttitlefontsize=18,\n                             xlabel=\"X\",\n                             ylabel=\"Y\",\n                             labelfontsize=18)\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
}