{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_proj_3.py\n\nThis script illustrates the following concepts:\n   - Drawing filled contours over an orthographic map\n   - Changing the center latitude and longitude for an orthographic projection\n   - Turning off map fill\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/proj_3.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/proj_3_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\nimport geocat.datafiles as gdf\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)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Fix the artifact of not-shown-data around 0 and 360-degree longitudes\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "wrap_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 with\nax = plt.axes(\n    projection=ccrs.Orthographic(central_longitude=-120, central_latitude=50))\n\n# Set extent to include latitudes between 0 and 90, and longitude between\n# 0 and -180 only\nax.set_extent([0, -180, 0, 90], ccrs.PlateCarree())\nax.set_global()\nax.coastlines(linewidths=0.5)\n\n# Plot data and add a colorbar\ntemp = wrap_t.plot.contourf(\n    ax=ax,\n    transform=ccrs.PlateCarree(),\n    levels=11,\n    cmap='coolwarm',\n    add_colorbar=False)\n\ncbar_ticks = np.arange(210, 311, 10)\ncbar = plt.colorbar(temp, \n                    orientation='horizontal', \n                    shrink=0.75, \n                    pad=0.05, \n                    extendrect=True,\n                    ticks=cbar_ticks)\n\ncbar.ax.tick_params(labelsize=10)\n\n# Use geocat.viz.util convenience function to add titles to left and right\n# of the plot axis.\ngvutil.set_titles_and_labels(ax,\n                             maintitle=\"Example of Orthogonal 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
}