{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_conLev_4.py\nThis script illustrates the following concepts:\n   - Explicitly setting contour levels\n   - Explicitly setting the fill colors for contours\n   - Reordering an array\n   - Removing the mean\n   - Drawing color-filled contours over a cylindrical equidistant map\n   - Turning off contour line labels\n   - Turning off contour lines\n   - Turning off map fill\n   - Turning on map outlines\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/conLev_4.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/conLev_4_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/b003_TS_200-299.nc\"),\n                     decode_times=False)\nx = ds.TS\n\n# Apply mean reduction from coordinates as performed in NCL's dim_rmvmean_n_Wrap(x,0)\n# Apply this only to x.isel(time=0) because NCL plot plots only for time=0\nnewx = x.mean('time')\nnewx = x.isel(time=0) - newx\n\n# Fix the artifact of not-shown-data around 0 and 360-degree longitudes\nnewx = gvutil.xr_add_cyclic_longitudes(newx, \"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)\nplt.figure(figsize=(12, 7.2))\n\n# Generate axes using Cartopy projection\nprojection = ccrs.PlateCarree()\nax = plt.axes(projection=projection)\n\n# Use global map and draw coastlines\nax.set_global()\nax.coastlines(linewidth=0.5, resolution=\"110m\")\n\n# Import an NCL colormap\nnewcmp = gvcmaps.BlRe\nnewcmp.colors[len(newcmp.colors) //\n              2] = [1, 1, 1]  # Set middle value to white to match NCL\n\n# Contourf-plot data (for filled contours)\np = newx.plot.contourf(\n    ax=ax,\n    vmin=-1,\n    vmax=10,\n    levels=[-12, -10, -8, -6, -4, -2, -1, 1, 2, 4, 6, 8, 10, 12],\n    cmap=newcmp,\n    add_colorbar=False,\n    transform=projection,\n    add_labels=False)\n\n# Add horizontal colorbar\ncbar = plt.colorbar(p, orientation='horizontal', shrink=0.5)\ncbar.ax.tick_params(labelsize=11)\ncbar.set_ticks([-12, -10, -8, -6, -4, -2, -1, 1, 2, 4, 6, 8, 10, 12])\n\n# Use geocat.viz.util convenience function to set axes tick values\ngvutil.set_axes_limits_and_ticks(ax,\n                                 xticks=np.linspace(-180, 180, 13),\n                                 yticks=np.linspace(-90, 90, 7))\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 minor and major tick lines\ngvutil.add_major_minor_ticks(ax, labelsize=12)\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='Anomalies: Surface Temperature',\n                             righttitle='K')\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
}