{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_vector_3.py\nPlot U & V vectors globally\n\nThis script illustrates the following concepts:\n  - Drawing a black-and-white vector plot over a PlateCarree map\n  - Adding a time stamp to a plot\n  - Moving the vector reference annotation to the top right of the plot\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/vector_3.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/vector_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 xarray as xr\nfrom matplotlib import pyplot as plt\nimport cartopy\nimport cartopy.crs as ccrs\nfrom datetime import datetime\n\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\nfile_in = xr.open_dataset(gdf.get(\"netcdf_files/uv300.nc\"))\n\n# Extract slices of lon and lat\n# Read in data from netCDF file.\n# Note that when we extract ``u`` and ``v`` from the file,\n# we only read every third latitude and longitude.\n\nds = file_in.isel(time=1, lon=slice(0, -1, 3), lat=slice(1, -1, 3))"
      ]
    },
    {
      "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.subplots(figsize=(10, 5.25))\n\n# Generate axes using Cartopy projection\nax = plt.axes(projection=ccrs.PlateCarree())\nz = gvutil.set_vector_density(ds, 0.017)\n\n# Draw vector plot\n# Notes\n\n# 1. We are using `set_vector_density` on line 47 as a replacement for NCL's vcMinDistanceF\n# 2. There is no matplotlib equivalent to \"CurlyVector\"\nQ = plt.quiver(z['lon'],\n               z['lat'],\n               z['U'].data,\n               z['V'].data,\n               color='black',\n               zorder=1,\n               pivot=\"middle\",\n               width=0.0007,\n               headwidth=10)\n\n# Draw legend for vector plot\nqk = ax.quiverkey(Q,\n                  167.5,\n                  72.5,\n                  20,\n                  r'20',\n                  labelpos='N',\n                  coordinates='data',\n                  color='black',\n                  zorder=2)\n\n# Turn on continent shading\nax.add_feature(cartopy.feature.LAND,\n               edgecolor='lightgray',\n               facecolor='lightgray',\n               zorder=0)\n\n# Draw the key for the quiver plot as a rectangle patch\nax.add_patch(\n    plt.Rectangle((155, 65),\n                  25,\n                  25,\n                  facecolor='white',\n                  edgecolor='black',\n                  zorder=1))\n\n# Use geocat.viz.util convenience function to set axes tick values\ngvutil.set_axes_limits_and_ticks(ax,\n                                 xticks=range(-180, 181, 30),\n                                 yticks=range(-90, 91, 30))\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 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 titles to left and right of the plot axis.\ngvutil.set_titles_and_labels(ax,\n                             lefttitle=ds['U'].long_name,\n                             righttitle=ds['U'].units)\n\n# Add timestamp\nax.text(-200, -115, f'Created: {datetime.now()}')\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
}