{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using `atl03x` to get ICESat-2 data over the Boulder Watershed\n",
    "\n",
    "Process ATL03 data from the Boulder Watershed region and produce a customized ATL06 elevation dataset.\n",
    "\n",
    "### What is demonstrated\n",
    "\n",
    "* The `atl03x` API is used to process the Boulder Watershed region\n",
    "* The `matplotlib` and `geopandas` packages are used to plot the data returned by SlideRule\n",
    "\n",
    "### Points of interest\n",
    "\n",
    "This is a simple notebook showing how a region of interest can be processed by SlideRule and the results analyzed using geopandas GeoDataFrames and Matplotlib."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import geopandas as gpd\n",
    "import matplotlib.pyplot as plt\n",
    "from sliderule import sliderule, icesat2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SlideRule Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Configure Region of Interest\n",
    "region = [ {\"lon\":-105.82971551223244, \"lat\": 39.81983728534918},\n",
    "           {\"lon\":-105.30742121965137, \"lat\": 39.81983728534918},\n",
    "           {\"lon\":-105.30742121965137, \"lat\": 40.164048017973755},\n",
    "           {\"lon\":-105.82971551223244, \"lat\": 40.164048017973755},\n",
    "           {\"lon\":-105.82971551223244, \"lat\": 39.81983728534918} ]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Execute ATL06 Algorithm using SlideRule"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "\n",
    "# Build ATL06 Request\n",
    "parms = {\n",
    "    \"poly\": region,\n",
    "    \"srt\": icesat2.SRT_LAND,\n",
    "    \"cnf\": icesat2.CNF_SURFACE_HIGH,\n",
    "    \"ats\": 10.0,\n",
    "    \"cnt\": 10,\n",
    "    \"len\": 40.0,\n",
    "    \"res\": 20.0,\n",
    "    \"fit\": {}\n",
    "}\n",
    "\n",
    "# Request ATL06 Data\n",
    "gdf = sliderule.run(\"atl03x\", parms)\n",
    "\n",
    "# Display Statistics\n",
    "print(\"Reference Ground Tracks: {}\".format(gdf[\"rgt\"].unique()))\n",
    "print(\"Cycles: {}\".format(gdf[\"cycle\"].unique()))\n",
    "print(\"Received {} elevations\".format(len(gdf)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot Region"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Calculate Extent\n",
    "lons = [p[\"lon\"] for p in region]\n",
    "lats = [p[\"lat\"] for p in region]\n",
    "lon_margin = (max(lons) - min(lons)) * 0.1\n",
    "lat_margin = (max(lats) - min(lats)) * 0.1\n",
    "\n",
    "# Create Plot\n",
    "fig,ax = plt.subplots(num=None, ncols=1, figsize=(12, 6))\n",
    "box_lon = [e[\"lon\"] for e in region]\n",
    "box_lat = [e[\"lat\"] for e in region]\n",
    "\n",
    "# Plot SlideRule Ground Tracks\n",
    "ax.set_title(\"SlideRule Zoomed Ground Tracks\")\n",
    "gdf.plot(ax=ax, column=gdf[\"h_mean\"], cmap='winter_r', s=1.0, zorder=3)\n",
    "ax.plot(box_lon, box_lat, linewidth=1.5, color='r', zorder=2)\n",
    "ax.set_xlim(min(lons) - lon_margin, max(lons) + lon_margin)\n",
    "ax.set_ylim(min(lats) - lat_margin, max(lats) + lat_margin)\n",
    "ax.set_aspect('equal', adjustable='box')\n",
    "\n",
    "# Show Plot\n",
    "plt.tight_layout()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "sliderule_env",
   "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.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
