{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_scatter_3.py\nThis script illustrates the following concepts:\n    - Drawing a scatter plot over a map\n    - Choosing marker color, size, and style\n    - Drawing markers on a map indicating the locations of station data\n   \nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/scatter_3.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/scatter_3_1_lg.png and https://www.ncl.ucar.edu/Applications/Images/scatter_3_2_lg.png\n                         \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\nimport cartopy.crs as ccrs\nimport cartopy.feature as cfeature\nfrom cartopy.mpl.gridliner import LongitudeFormatter, LatitudeFormatter\nimport matplotlib.pyplot as plt\n\nfrom geocat.viz import util as gvutil\nimport geocat.datafiles as gdf"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Open a netCDF data file using xarray default engine and load the data into xarrays\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "ds = xr.open_dataset(gdf.get(\"netcdf_files/95031800_sao.cdf\"),\n                     decode_times=False)\nlat = ds.lat.isel()\nlon = ds.lon.isel()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def Plots(xlim, ylim, xtic, ytic, xminor, yminor, size, color):\n    '''\n    Creates plot using user specified variables.\n    \n    Parameters\n    ----------\n    xext : 'tuple'\n        Inclusive extent of projection in format (xstart, xend) with values between -180 \n        and 180.\n    yext : 'tuple'\n        Inclusive extent of projection in format (ystart, yend) with values between -90 \n        and 90.\n    xtic : 'int'\n        Step number of major x tick label instances in format of number between each tick.\n        This is passed to range() as the 'step' argument.\n    ytic : 'int'\n        Step number of major y tick label instances in format of number between each tick.\n        This is passed to range() as the 'step' argument.\n    xminor : 'int'\n        Exclusive number of minor ticks between each major x-axis tick mark\n    yminor : 'int'\n        Exclusive number of minor ticks between each major y-axis tick mark\n    size : 'int'\n        Size of marker being used in format of font size number.\n    color : 'str'\n        Matplotlib color of marker being used in format 'color'.\n\n    '''\n\n    # Generate figure (set its size (width, height) in inches) and axes using Cartopy projection\n    plt.figure(figsize=(12, 12))\n\n    # Generate axes using Cartopy\n    ax = plt.axes(projection=ccrs.PlateCarree())\n\n    # Use geocat.viz.util convenience function to add minor and major tick lines\n    gvutil.add_major_minor_ticks(ax,\n                                 x_minor_per_major=xminor,\n                                 y_minor_per_major=yminor,\n                                 labelsize=14)\n\n    # Use geocat.viz.util convenience function to make plots look like NCL plots by using latitude, longitude tick labels\n    gvutil.add_lat_lon_ticklabels(ax)\n\n    # Use geocat.viz.util convenience function to set axes limits & tick values without calling several matplotlib functions\n    gvutil.set_axes_limits_and_ticks(ax,\n                                     xlim=xlim,\n                                     ylim=ylim,\n                                     xticks=range(-180, 180, xtic),\n                                     yticks=range(-90, 90, ytic))\n\n    # Remove the degree symbol from tick labels\n    ax.yaxis.set_major_formatter(LatitudeFormatter(degree_symbol=''))\n    ax.xaxis.set_major_formatter(LongitudeFormatter(degree_symbol=''))\n\n    # Turn on continent shading\n    ax.add_feature(cfeature.LAND,\n                   edgecolor='lightgray',\n                   facecolor='lightgray',\n                   zorder=0)\n    ax.add_feature(cfeature.LAKES,\n                   edgecolor='white',\n                   facecolor='white',\n                   zorder=0)\n\n    # Scatter-plot the location data on the map\n    plt.scatter(lon, lat, s=size, c=color, marker='+', linewidth=0.5, zorder=1)\n\n    plt.title(\"Locations of stations\",\n              loc=\"center\",\n              y=1.03,\n              size=15,\n              fontweight=\"bold\")\n\n    plt.show()\n\n\nPlots((-180, 160), (-20, 90), 30, 30, 3, 3, 50, 'firebrick')\nPlots((-125, -65), (21, 60), 20, 10, 4, 5, 50, 'blue')"
      ]
    }
  ],
  "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
}