{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# NCL_radar_1.py\nThis script illustrates the following concepts:\n   - Fitting radial data to a cartesian grid\n   - Creating a horizontal colorbar\n   - Adding a background behind plotted data\n   - Creating a square aspect ratio\n\nSee following URLs to see the reproduced NCL plot & script:\n    - Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/radar_1.ncl\n    - Original NCL plots: https://www.ncl.ucar.edu/Applications/Images/radar_1_1_lg.png, https://www.ncl.ucar.edu/Applications/Images/radar_1_2_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 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": [
        "ds = xr.open_dataset(gdf.get(\"netcdf_files/dz.nc\"), decode_times=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Convert data to radial form:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Designate center of radial data\nxcenter = 0.0\nycenter = 0.0\n\n# construct radial array from netcdf metadata\nkm_between_cells = 0.25\nradius = ds.DZ.data.shape[1] * km_between_cells\nr = np.arange(0, radius, 0.25)\n\n# Convert reflectivity factor\nvalues = ds.DZ.data\nvalues = values * 100\n\n# Make angles monotonic\ntheta = ds.Azimuth.data\ntheta[0:63] = theta[0:63] - 360\n\n# Make a cartesian mesh grid\nradius_matrix, theta_matrix = np.meshgrid(r, theta)\nX = radius_matrix * np.cos(np.deg2rad(theta_matrix))\nY = radius_matrix * np.sin(np.deg2rad(theta_matrix))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plotting helper function\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def radar_plot(X, Y, values, bg_color=None):\n    # Create a figure and axes using subplots\n    fig, ax = plt.subplots(figsize=(6, 8))\n\n    # Choose default colormap\n    cmap = gvcmaps.gui_default\n\n    # Plot using contourf\n    p = plt.contourf(X,\n                     Y,\n                     values,\n                     cmap=cmap,\n                     levels=np.arange(-20, 70, 5) * 100,\n                     zorder=3)\n\n    # Change orientation and tick marks of colorbar\n    plt.colorbar(p,\n                 orientation=\"horizontal\",\n                 ticks=np.arange(-15, 65, 15) * 100,\n                 drawedges=True,\n                 aspect=12)\n\n    # Use geocat.viz.util convenience function to add minor and major tick lines\n    gvutil.add_major_minor_ticks(ax, labelsize=12)\n\n    # Use geocat.viz.util convenience function to add titles to left and right of the plot axis.\n    gvutil.set_titles_and_labels(ax,\n                                 lefttitle=ds.DZ.long_name,\n                                 lefttitlefontsize=16,\n                                 righttitle=ds.DZ.units,\n                                 righttitlefontsize=16,\n                                 xlabel=\"\",\n                                 ylabel=\"\")\n\n    # Use geocat.viz.util convenience function to set axes limits & tick values\n    gvutil.set_axes_limits_and_ticks(ax,\n                                     xlim=(-240, 240),\n                                     ylim=(-240, 240),\n                                     xticks=np.arange(-200, 201, 100),\n                                     yticks=np.arange(-200, 201, 100))\n\n    # Use geocat.viz.util convenience function to set tick placements\n    gvutil.add_major_minor_ticks(ax,\n                                 x_minor_per_major=5,\n                                 y_minor_per_major=5,\n                                 labelsize=14)\n\n    # Set aspect ratio\n    ax.set_aspect('equal')\n\n    # Allow optional background circle to be set\n    if (bg_color is not None):\n        circle_bg = plt.Circle((0, 0), 240, color=bg_color, zorder=1)\n        ax.add_artist(circle_bg)\n\n    # Show plot\n    plt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Generate first plot without a background using the helper function\nradar_plot(X, Y, values)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Alternative plot:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Generate alternative plot with a background\nradar_plot(X, Y, values, bg_color=\"lightgrey\")"
      ]
    }
  ],
  "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
}