{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "29ec1570-d65b-4e52-9d4d-d93604882190",
   "metadata": {},
   "source": [
    "# Sampling the ArcticDEM Mosaic\n",
    "\n",
    "### Purpose\n",
    "Demonstrate how to sample the ArcticDEM at generated ATL06-SR points"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e29fa51f-77bf-4c55-a99e-a4f166833755",
   "metadata": {},
   "source": [
    "#### Import Packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc573692-19b2-41d2-9575-9b42d1ea1031",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "import sliderule\n",
    "from sliderule import icesat2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53e68348-2d49-4e22-b665-1acd8b367dcf",
   "metadata": {},
   "source": [
    "#### Initialize SlideRule Python Client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93edfc47-1cd5-4927-962c-fd447c9e807a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "sliderule.init(verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c588e3ea-8ab8-452b-8f5a-9fd8d6364ca9",
   "metadata": {
    "tags": []
   },
   "source": [
    "#### Make Processing Request to SlideRule\n",
    "ATL06-SR request includes the `samples` parameter to specify that ArcticDEM Mosiac dataset should be sampled at each generated ATL06 elevation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ebef6dc-c05d-4b97-973c-05da9565e841",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "resource = \"ATL03_20190314093716_11600203_007_01.h5\"\n",
    "region = sliderule.toregion(\"dicksonfjord.geojson\")\n",
    "parms = { \"poly\": region['poly'],\n",
    "          \"cnf\": \"atl03_high\",\n",
    "          \"ats\": 5.0,\n",
    "          \"cnt\": 5,\n",
    "          \"len\": 20.0,\n",
    "          \"res\": 10.0,\n",
    "          \"samples\": {\"mosaic\": {\"asset\": \"arcticdem-mosaic\", \"radius\": 10.0, \"zonal_stats\": True}} }\n",
    "gdf = icesat2.atl06p(parms, resources=[resource])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b779ddf2-f9ea-41c2-bb9a-1db92e277fe7",
   "metadata": {},
   "source": [
    "#### Display GeoDataFrame\n",
    "Notice the columns that start with \"mosaic\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e19bae20-140e-4d55-bb73-64a9630096d1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "gdf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6178683e-2d08-4ccb-a80e-4bb997876330",
   "metadata": {},
   "source": [
    "#### Print Out File Directory\n",
    "When a GeoDataFrame includes samples from rasters, each sample value has a file id that is used to look up the file name of the source raster for that value."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4c99349-c44e-4e59-bd31-ad6121df2f80",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf.attrs['file_directory']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13185f3c-23f8-4334-a300-68c39284711c",
   "metadata": {},
   "source": [
    "#### Demonstrate How To Access Source Raster Filename for Entry in GeoDataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "02ddb59c-b63b-4fef-b8c4-ec4b3d7580c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "filedir = gdf.attrs['file_directory']\n",
    "filedir[gdf['mosaic.file_id'].iloc[0]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88c529c1-9d72-4628-8b34-d850ae9e262d",
   "metadata": {},
   "source": [
    "#### Difference the Sampled Value from ArcticDEM with SlideRule ATL06-SR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3476592-b5b2-470e-bd35-d63e23e42ca0",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf[\"value_delta\"] = gdf[\"h_mean\"] - gdf[\"mosaic.value\"]\n",
    "gdf[\"value_delta\"].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5447dd00-69fa-4ab7-a2f3-674bf72126e9",
   "metadata": {},
   "source": [
    "#### Difference the Zonal Statistic Mean from ArcticDEM with SlideRule ATL06-SR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54621607-cdbc-4849-8e65-530957c2adc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf[\"mean_delta\"] = gdf[\"h_mean\"] - gdf[\"mosaic.mean\"]\n",
    "gdf[\"mean_delta\"].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "279dded2-5165-4b88-b28d-971fa303966d",
   "metadata": {},
   "source": [
    "#### Difference the Zonal Statistic Mdeian from ArcticDEM with SlideRule ATL06-SR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fafc2593-f6b4-44c1-8fb9-a9d345c6561e",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf[\"median_delta\"] = gdf[\"h_mean\"] - gdf[\"mosaic.median\"]\n",
    "gdf[\"median_delta\"].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32beb064-f10f-46e1-8756-a03756e069fd",
   "metadata": {},
   "source": [
    "#### Plot the Different ArcticDEM Values against the SlideRule ATL06-SR Values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12645d05-fda6-44bd-878b-37b0aa217065",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup Plot\n",
    "fig,ax = plt.subplots(num=None, figsize=(10, 8))\n",
    "fig.set_facecolor('white')\n",
    "fig.canvas.header_visible = False\n",
    "ax.set_title(\"SlideRule vs. ArcticDEM Elevations\")\n",
    "ax.set_xlabel('UTC')\n",
    "ax.set_ylabel('height (m)')\n",
    "legend_elements = []\n",
    "\n",
    "# Plot SlideRule ATL06 Elevations\n",
    "df = gdf[(gdf['rgt'] == 1160) & (gdf['gt'] == 10) & (gdf['cycle'] == 2)]\n",
    "sc1 = ax.scatter(df.index.values, df[\"h_mean\"].values, c='red', s=2.5)\n",
    "legend_elements.append(matplotlib.lines.Line2D([0], [0], color='red', lw=6, label='ATL06-SR'))\n",
    "\n",
    "# Plot ArcticDEM Elevations\n",
    "sc2 = ax.scatter(df.index.values, df[\"mosaic.value\"].values, c='blue', s=2.5)\n",
    "legend_elements.append(matplotlib.lines.Line2D([0], [0], color='blue', lw=6, label='ArcticDEM'))\n",
    "\n",
    "# Display Legend\n",
    "lgd = ax.legend(handles=legend_elements, loc=3, frameon=True)\n",
    "lgd.get_frame().set_alpha(1.0)\n",
    "lgd.get_frame().set_edgecolor('white')\n",
    "\n",
    "# Show Plot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "343ad4b0-e94b-48bb-ae23-ca57867597fb",
   "metadata": {},
   "source": [
    "#### Plot the Sampled Value and Zonal Statistic Mean Deltas to SlideRule ATL06-SR Values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7154e9db-ff4d-4b17-ac8c-62c3d12d7d54",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup Plot\n",
    "fig,ax = plt.subplots(num=None, figsize=(10, 8))\n",
    "fig.set_facecolor('white')\n",
    "fig.canvas.header_visible = False\n",
    "ax.set_title(\"Delta Elevations between SlideRule and ArcticDEM\")\n",
    "ax.set_xlabel('UTC')\n",
    "ax.set_ylabel('height (m)')\n",
    "ax.yaxis.grid(True)\n",
    "\n",
    "# Plot Deltas\n",
    "df1 = gdf[(gdf['rgt'] == 1160) & (gdf['gt'] == 10) & (gdf['cycle'] == 2)]\n",
    "sc1 = ax.scatter(df1.index.values, df1[\"value_delta\"].values, c='blue', s=2.5)\n",
    "\n",
    "# Plot Deltas\n",
    "df2 = gdf[(gdf['rgt'] == 1160) & (gdf['gt'] == 10) & (gdf['cycle'] == 2)]\n",
    "sc2 = ax.scatter(df2.index.values, df2[\"mean_delta\"].values, c='green', s=2.5)\n",
    "\n",
    "# Show Plot\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eed8f243-dd0c-4473-a952-fcb2bb863e3c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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": 5
}
