{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_proj_1.py\nThis script illustrates the following concepts:\n   - Drawing filled contours over a Mollweide map\n   - Setting the spacing for latitude/longitude grid lines\n   - Changing the font size of the colorbar's labels\n   - Spanning part of a color map for contour fill\n   - Turning off the map perimeter (boundary)\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/proj_1.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/proj_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\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)\nt = ds.TS.isel(time=0)\n\n# Fix the artifact of not-shown-data around 0 and 360-degree longitudes\nwrap_t = gvutil.xr_add_cyclic_longitudes(t, \"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.Mollweide())\nax.coastlines(linewidths=0.5)\n\n# Draw gridlines\ngl = ax.gridlines(crs=ccrs.PlateCarree(), linewidth=1, color='black', alpha=0.5)\n\n# Import an NCL colormap\nnewcmp = gvcmaps.gui_default\n\n# Contourf-plot data (for filled contours)\ntemp = wrap_t.plot.contourf(ax=ax,\n                            transform=ccrs.PlateCarree(),\n                            levels=11,\n                            cmap=newcmp,\n                            add_colorbar=False)\n\n# Add color bar\ncbar_ticks = np.arange(220, 310, 10)\ncbar = plt.colorbar(temp, \n                    orientation='horizontal', \n                    shrink=0.8, \n                    pad=0.05, \n                    extendrect=True,\n                    ticks=cbar_ticks)\n\ncbar.ax.tick_params(labelsize=10)\n\n# Contour-plot data (for borderlines)\nwrap_t.plot.contour(ax=ax,\n                    transform=ccrs.PlateCarree(),\n                    levels=11,\n                    linewidths=0.5,\n                    cmap='black')\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=\"Example of Mollweide Projection\",\n                             lefttitle=\"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
}