{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_native_2.py\n\nThis script illustrates the following concepts:\n   - Drawing filled contours over a mercator map\n   - Overlaying contours on a map without having latitude and longitude coordinates\n   - Turning on map tickmark labels with degree symbols\n   - Selecting a different color map\n   - Zooming in on a particular area on a mercator map\n   - Using best practices when choosing plot color scheme to accomodate visual impairments\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/native_2.ncl\n    - Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/native_2_lg.png\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Import packages:\nimport numpy as np\nimport xarray as xr\nimport cartopy.crs as ccrs\nimport cartopy.feature as cfeature\nimport matplotlib.pyplot as plt\nimport matplotlib.ticker as mticker\n\nimport geocat.datafiles as gdf"
      ]
    },
    {
      "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\n# load the data into xarrays\nds = xr.open_dataset(gdf.get(\"netcdf_files/1994_256_FSD.nc\"),\n                     decode_times=False)\nt = ds.FSD.isel(time=0)"
      ]
    },
    {
      "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.Mercator())\nax.coastlines(linewidths=0.5)\nax.add_feature(cfeature.LAND, facecolor=\"lightgray\")\n\n# Set extent to include latitudes from 34 to 52 and longitudes from 128\n# to 144\nax.set_extent([128, 144, 34, 52], ccrs.PlateCarree())\n\n# Plot data and create colorbar\npt = t.plot.contourf(ax=ax,\n                     transform=ccrs.PlateCarree(),\n                     vmin=0,\n                     vmax=70,\n                     levels=15,\n                     cmap=\"inferno\",\n                     add_colorbar=False)\n\ncbar_ticks = np.arange(0, 71, 5)\ncbar = plt.colorbar(pt, \n                    orientation='vertical', \n                    extendrect=True,\n                    ticks=cbar_ticks)\n\n# Draw gridlines\ngl = ax.gridlines(crs=ccrs.PlateCarree(),\n                  draw_labels=True,\n                  dms=False,\n                  x_inline=False,\n                  y_inline=False,\n                  linewidth=1,\n                  color=\"black\",\n                  alpha=0.25)\n\n# Manipulate latitude and longitude gridline numbers and spacing\ngl.top_labels = False\ngl.right_labels = False\ngl.xlocator = mticker.FixedLocator([130, 134, 138, 142])\ngl.ylocator = mticker.FixedLocator([36, 38, 40, 42, 44, 46, 48, 50])\ngl.xlabel_style = {\"rotation\": 0, \"size\": 15}\ngl.ylabel_style = {\"rotation\": 0, \"size\": 15}\n\nplt.title(\"Native Mercator Projection\",\n          loc=\"center\",\n          y=1.05,\n          size=15,\n          fontweight=\"bold\")\nplt.title(t.units, loc=\"right\", y=1.0, size=14)\nplt.title(\"free surface deviation\", loc=\"left\", y=1.0, size=14)\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
}