{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b0831525-6547-40bd-9d7f-11f9082168ab",
   "metadata": {},
   "source": [
    "# Running the PhoREAL algorithm over Grand Mesa, CO\n",
    "\n",
    "Demonstrate running the PhoREAL algorithm in SlideRule to produce canopy metrics over the Grand Mesa, Colorado region."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b0d9805-5b1a-4bce-b828-6f2fee432725",
   "metadata": {},
   "source": [
    "#### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bcd3a1dc-2d8d-4f88-b715-059497f4d52d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import logging\n",
    "import sliderule\n",
    "from sliderule import icesat2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9cdeed4-810c-4540-869b-78e09591b68c",
   "metadata": {
    "user_expressions": []
   },
   "source": [
    "#### Initialize Client"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8b5b11b-eea7-4922-abbc-997a670e03e6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "sliderule.init(verbose=True, loglevel=logging.INFO)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4c96101-53cf-4614-80a2-e50afcf65a03",
   "metadata": {
    "user_expressions": []
   },
   "source": [
    "#### Processing parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "504049cb-de86-4f6b-98a4-3a3237b17ca6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "parms = {\n",
    "    \"poly\": sliderule.toregion('grandmesa.geojson')['poly'], # subset to Grand Mesa area of interest\n",
    "    \"t0\": '2019-11-14T00:00:00Z', # time range is one day - November 14, 2019\n",
    "    \"t1\": '2019-11-15T00:00:00Z',\n",
    "    \"srt\": icesat2.SRT_LAND, # use the land surface type for ATL03 photon confidence levels\n",
    "    \"len\": 100, # generate statistics over a 100m segment\n",
    "    \"res\": 100, # generate statistics every 100m\n",
    "    \"pass_invalid\": True, # do not perform any segment-level filtering\n",
    "    \"atl08_class\": [\"atl08_ground\", \"atl08_canopy\", \"atl08_top_of_canopy\"], # exclude noise and unclassified photons\n",
    "    \"atl08_fields\": [\"h_dif_ref\"], # include these fields as extra columns in the dataframe\n",
    "    \"phoreal\": {\"binsize\": 1.0, \"geoloc\": \"center\"} # run the PhoREAL algorithm\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9bd5e72-f5d5-4686-94b2-c65f6715a877",
   "metadata": {
    "user_expressions": []
   },
   "source": [
    "#### Make Proessing Request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26d37002-0505-4175-9657-2f8bc6aa956c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "atl08 = sliderule.run(\"atl03x\", parms)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60702b08-c333-4502-b948-26015c1520d5",
   "metadata": {
    "user_expressions": []
   },
   "source": [
    "#### Print Resulting GeoDataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2237bae5-78e9-4edf-8b24-4162039cc2be",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "atl08"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23a8280a-844e-4405-a8a2-53e2dd51f5e0",
   "metadata": {
    "user_expressions": []
   },
   "source": [
    "#### Plot Canopy Height"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92188f98-1d76-4807-9f36-eba16354afea",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "canopy_gt1l = atl08[atl08['gt'] == icesat2.GT1L]\n",
    "canopy_gt1l.plot.scatter(x='x_atc', y='h_canopy')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3665d73-2745-4ba6-b209-5cf7a6abe693",
   "metadata": {
    "user_expressions": []
   },
   "source": [
    "#### Plot Landcover"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7f8fe31-ad77-4ea1-9165-6e295434f125",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "atl08.plot('landcover', legend=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fc1e14d-8936-4f26-a6ca-a2361f54ef4d",
   "metadata": {
    "user_expressions": []
   },
   "source": [
    "#### Create and Plot 75th percentile Across All Ground Tracks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5127ea05-56f9-4c4d-88d4-2a74f9ef5eee",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "atl08['75'] = atl08.apply(lambda row : row[\"canopy_h_metrics\"][icesat2.P['75']], axis = 1)\n",
    "atl08.plot.scatter(x='x_atc', y='75')"
   ]
  }
 ],
 "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
}
