{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b343fcaf-a122-460f-9135-d30f5771589c",
   "metadata": {},
   "source": [
    "# Subsetting and filtering ATL24 data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fca7368a-5248-41ba-8ffa-34f49618a327",
   "metadata": {},
   "source": [
    "### Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b32f76e9-44de-4dd7-bb78-be4adf09c41d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sliderule import sliderule\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1b5abf7-f430-4fb2-b13c-4bd0cb4b962e",
   "metadata": {},
   "source": [
    "### Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbdf1b51-79f0-46b6-ab2d-7eab333c0264",
   "metadata": {},
   "outputs": [],
   "source": [
    "# configure sliderule to output verbose log messages\n",
    "sliderule.init(verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e322cfc5-74d4-4d98-9351-89f91386b926",
   "metadata": {},
   "source": [
    "### Plotting Helper Variables and Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b5e5119-3210-46fc-9920-e82f2f0a8d41",
   "metadata": {},
   "outputs": [],
   "source": [
    "# color map for ATL24 classifications\n",
    "COLORS = {\n",
    "    0: ['gray', 'unclassified'],\n",
    "    40: ['red', 'bathymetry'],\n",
    "    41: ['blue', 'sea_surface']\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efea2b6f-6694-4cdb-9951-949b4e8f0759",
   "metadata": {},
   "outputs": [],
   "source": [
    "# plot ATL24 dataframes\n",
    "def plot_atl24(gdf, x_min, x_max, column=None):\n",
    "    start_xatc = np.min(gdf['x_atc'])\n",
    "    fig,ax = plt.subplots(1, 1, figsize=(18, 6), constrained_layout=True)\n",
    "    if column == None:\n",
    "        for class_val, color_name in COLORS.items():\n",
    "            ii=gdf[\"class_ph\"]==class_val\n",
    "            ax.plot(gdf['x_atc'][ii]-start_xatc, gdf['ortho_h'][ii], 'o', markersize=1, color=color_name[0], label=color_name[1])\n",
    "    else:\n",
    "        sc = ax.scatter(gdf['x_atc']-start_xatc, gdf['ortho_h'], c=gdf[column], cmap='viridis')\n",
    "    ax.set_xlim(x_min, x_max)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7bece2c4-9651-46e1-aed2-6722979978dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# plot ATL06 dataframes\n",
    "def plot_atl06(gdf, x_min, x_max, column=None):\n",
    "    start_xatc = np.min(gdf['x_atc'])\n",
    "    fig,ax = plt.subplots(1, 1, figsize=(18, 6), constrained_layout=True)\n",
    "    ax.plot(gdf['x_atc']-start_xatc, gdf['h_mean'], 'o', markersize=5, color='red')\n",
    "    ax.set_xlim(x_min, x_max)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aad6b211-b8bc-4fcc-a7a8-44f4b9dead96",
   "metadata": {},
   "outputs": [],
   "source": [
    "# plot ATL03 dataframes\n",
    "def plot_atl03(gdf, x_min, x_max, column=None):\n",
    "    start_xatc = np.min(gdf['x_atc'])\n",
    "    fig,ax = plt.subplots(1, 1, figsize=(18, 6), constrained_layout=True)\n",
    "    if column == None:\n",
    "        for class_val, color_name in COLORS.items():\n",
    "            ii=gdf[\"atl24_class\"]==class_val\n",
    "            ax.plot(gdf['x_atc'][ii]-start_xatc, gdf['height'][ii], 'o', markersize=1, color=color_name[0], label=color_name[1])\n",
    "    else:\n",
    "        sc = ax.scatter(gdf['x_atc']-start_xatc, gdf['height'], c=gdf[column], cmap='viridis')\n",
    "    ax.set_xlim(x_min, x_max)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22167d4f-4012-480a-9ceb-a02adbb513f6",
   "metadata": {},
   "source": [
    "### Define Area of Interest (north shore of Dominican Republic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcc82fb6-b470-4b43-a4c1-fa6dc78199cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "aoi = [ { \"lat\": 19.42438470712139, \"lon\": -69.79907695695609  },\n",
    "        { \"lat\": 19.31125534696085,  \"lon\": -69.79907695695609 },\n",
    "        { \"lat\": 19.31125534696085,  \"lon\": -69.33527941905237 },\n",
    "        { \"lat\": 19.42438470712139,  \"lon\": -69.33527941905237 },\n",
    "        { \"lat\": 19.42438470712139,  \"lon\": -69.79907695695609 } ]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "650d2b36-b030-4c59-a5e0-8976cef41e39",
   "metadata": {},
   "source": [
    "## (1) Quick Access"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "809c505e-11a5-44a8-b9f8-88b38c3278ab",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gdf1 = sliderule.run(\"atl24x\", {}, aoi=aoi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4bc036a-843e-4ec3-bbe1-3e5184909cb4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gdf1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ab454da-ae58-4bd3-af38-9042b0ce8ee4",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf1.plot(column='ortho_h', cmap='viridis')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4854f32d-f85e-469b-9059-61646c94adaf",
   "metadata": {},
   "source": [
    "## (2) Access a Single Track"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17f48db5-73d0-4f5a-b8cd-0f5d5a5fe99e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gdf2 = sliderule.run(\"atl24x\", {\"beams\": \"gt3r\", \"rgt\": 202, \"cycle\": 12}, aoi=aoi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a026608b-6bf3-4aad-bcac-2bb4e4446086",
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "242b54ab-3908-472c-8d03-7dfac4b4c654",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_atl24(gdf2, 0, 2500)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b198e55f-e541-4f36-843b-af6d178d893d",
   "metadata": {},
   "source": [
    "## (3) Detailed Access of a Single Track"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52589762-ec37-4c3d-a5ff-9076ebce9ed4",
   "metadata": {},
   "outputs": [],
   "source": [
    "parms = {\n",
    "    \"atl24\": {\n",
    "        \"compact\": False,\n",
    "        \"confidence_threshold\": 0.0,\n",
    "        \"class_ph\": [\"unclassified\", \"sea_surface\", \"bathymetry\"]\n",
    "    },\n",
    "    \"beams\": \"gt3r\",\n",
    "    \"rgt\": 202,\n",
    "    \"cycle\": 12\n",
    "}\n",
    "gdf3 = sliderule.run(\"atl24x\", parms, aoi=aoi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc17db42-b867-4eab-88a4-f776610a2574",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gdf3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8b3f1a5-1aab-41d0-bfd0-87e0cee26fae",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_atl24(gdf3, 0, 2500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7816b113-eb95-4a30-8d72-c4b0bc6a4f2d",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_atl24(gdf3, 0, 2500, \"confidence\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2cbb667b-92c6-400b-8c68-628579fe534e",
   "metadata": {},
   "source": [
    "## (4) Access All ATL03 Photons using ATL24 as a Classifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5bab6acc-0ba1-4ccb-bf8f-4ddda775ef88",
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# UPDATE 2.4.26 - The following code will not work due to ATL03 release 006 being retired.\n",
    "#   ATL03 granules used to generate ATL24 release 001 can no longer be discovered in CMR and accessed in Earthdata Cloud.\n",
    "#\n",
    "parms = {\n",
    "    \"cmr\": {\"version\": \"006\"},\n",
    "    \"atl24\": {\n",
    "        \"class_ph\": [\"unclassified\", \"sea_surface\", \"bathymetry\"]\n",
    "    },\n",
    "    \"cnf\": -1,\n",
    "    \"beams\": \"gt3r\",\n",
    "    \"rgt\": 202,\n",
    "    \"cycle\": 12\n",
    "}\n",
    "gdf4 = sliderule.run(\"atl03x\", parms, aoi=aoi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f01a1236-e73a-44bc-9f51-aba6a9aea0e1",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gdf4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "336d54b9-4821-4285-874d-3c5af8d4f938",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_atl03(gdf4, 0, 2500)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25bd2de0-f4e5-4cbb-9a31-02b4c3951b8f",
   "metadata": {},
   "source": [
    "## (5) Combine ATL03 Filters with ATL24 Classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e41f16a8-f523-467e-9415-87ad5330180d",
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# UPDATE 2.4.26 - The following code will not work due to ATL03 release 006 being retired.\n",
    "#   ATL03 granules used to generate ATL24 release 001 can no longer be discovered in CMR and accessed in Earthdata Cloud.\n",
    "#\n",
    "parms = {\n",
    "    \"cmr\": {\"version\": \"006\"},\n",
    "    \"atl24\": {\n",
    "        \"class_ph\": [\"unclassified\", \"sea_surface\", \"bathymetry\"]\n",
    "    },\n",
    "    \"cnf\": 2,\n",
    "    \"yapc\": {\n",
    "        \"version\": 0,\n",
    "        \"score\": 100\n",
    "    },\n",
    "    \"beams\": \"gt3r\",\n",
    "    \"rgt\": 202,\n",
    "    \"cycle\": 12\n",
    "}\n",
    "gdf5 = sliderule.run(\"atl03x\", parms, aoi=aoi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49475bfa-ea2c-491a-a5b7-e883b63f5ba0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gdf5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d66f145-86f9-42c5-8870-69ac9b5988ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_atl03(gdf5, 0, 2500)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "24edb805-4de0-4203-a064-3cf3b011dd76",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_atl03(gdf5, 0, 2500, \"yapc_score\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b409a40e-5590-4f4d-9d6c-79b8b5b4ffaf",
   "metadata": {},
   "source": [
    "## (6) Run ATL06-SR Surface Fitting Algorithm on ATL24 Classified Photons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77a58553-e5a6-4dec-bcac-dc1cfe4aaa7b",
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "# UPDATE 2.4.26 - The following code will not work due to ATL03 release 006 being retired.\n",
    "#   ATL03 granules used to generate ATL24 release 001 can no longer be discovered in CMR and accessed in Earthdata Cloud.\n",
    "#\n",
    "parms = {\n",
    "    \"cmr\": {\"version\": \"006\"},\n",
    "    \"atl24\": {\n",
    "        \"class_ph\": [\"bathymetry\"]\n",
    "    },\n",
    "    \"fit\": {\n",
    "        \"res\": 10,\n",
    "        \"len\": 20,\n",
    "        \"pass_invalid\": True\n",
    "    },\n",
    "    \"cnf\": -1,\n",
    "    \"beams\": \"gt3r\",\n",
    "    \"rgt\": 202,\n",
    "    \"cycle\": 12\n",
    "}\n",
    "gdf6 = sliderule.run(\"atl03x\", parms, aoi=aoi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e3c3c9d-e11c-4ea4-b1b9-10af53312d4e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gdf6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4992eb34-7e1b-47ee-a513-1eb05e67c050",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_atl06(gdf6, 0, 2500)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4621fb84-68bb-4466-9fe0-74ef39c50be0",
   "metadata": {},
   "source": [
    "## (7) Filtered and Ancillary Access to ATL24"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a61b5825-4781-4e9d-b149-39fdc10c7410",
   "metadata": {},
   "outputs": [],
   "source": [
    "parms = {\n",
    "    \"atl24\": {\n",
    "        \"class_ph\": [\"bathymetry\"],\n",
    "#        \"confidence_threshold\": 0.6,\n",
    "#        \"invalid_kd\": False,\n",
    "#        \"invalid_wind_speed\": False,\n",
    "        \"low_confidence\": False,\n",
    "#        \"night\": True,\n",
    "#        \"sensor_depth_exceeded\": False,\n",
    "        \"anc_fields\": [\"index_ph\", \"index_seg\"]\n",
    "    },\n",
    "    \"beams\": \"gt3r\",\n",
    "    \"rgt\": 202,\n",
    "    \"cycle\": 12\n",
    "}\n",
    "gdf7 = sliderule.run(\"atl24x\", parms, aoi=aoi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5a5b983-0123-4233-97cb-db5692f1e836",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gdf7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da09aa2f-d8fc-4b9b-b4af-75eefce0f3fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_atl24(gdf7, 0, 2500)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "995f1c3c",
   "metadata": {},
   "source": [
    "## (8) Directly Query for Available ATL24 Resources"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40618ad1",
   "metadata": {},
   "outputs": [],
   "source": [
    "poly = [\n",
    "    {\"lat\": 21.222261686673306, \"lon\": -73.78074797284968},\n",
    "    {\"lat\": 21.07228912392266,  \"lon\": -73.78074797284968},\n",
    "    {\"lat\": 21.07228912392266,  \"lon\": -73.51303956051089},\n",
    "    {\"lat\": 21.222261686673306, \"lon\": -73.51303956051089},\n",
    "    {\"lat\": 21.222261686673306, \"lon\": -73.78074797284968}\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68c20954",
   "metadata": {},
   "outputs": [],
   "source": [
    "# find all ATL24 granules that intersect the above polygon and have a maximum mean depth of 10m and are collected in the summer\n",
    "response = sliderule.source(\"earthdata\", {\"asset\": \"icesat2-atl24\", \"atl24\": {\"meandepth1\": 10, \"season\": 2}, \"poly\": poly})\n",
    "response"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "sliderule_notebook",
   "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.14.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
