{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ab0f8a76",
   "metadata": {},
   "source": [
    "# Accessing ATL13 data using lake names, reference ids, and contained coordinates \n",
    "\n",
    "SlideRule provides an `Asset Metadata Service` to lookup ATL13 granules using different variables:\n",
    "* reference id\n",
    "* lake name\n",
    "* coordinate within the lake\n",
    "\n",
    "SlideRule can also be used to directly subset ATL13 using the above variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0e71af9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports\n",
    "from sliderule import sliderule\n",
    "\n",
    "# Setup\n",
    "sliderule.init(verbose=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "00746863",
   "metadata": {},
   "source": [
    "### Retrieve ATL13 from ___SlideRule___ using `Reference ID`\n",
    "\n",
    "##### Metadata from a `shell` script"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faa5e509",
   "metadata": {},
   "source": [
    "```bash\n",
    "curl -X GET -d \"{\\\"asset\\\": \\\"icesat2-atl13\\\", \\\"atl13\\\": {\\\"refid\\\": 5952002394}}\" https://sliderule.slideruleearth.io/source/earthdata\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27fe98a2",
   "metadata": {},
   "source": [
    "##### Metadata from a `python` script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc7b35bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = sliderule.source(\"earthdata\", {\"asset\": \"icesat2-atl13\", \"atl13\": {\"refid\": 5952002394}})\n",
    "response"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdd01f7a",
   "metadata": {},
   "source": [
    "##### Data from a `python` script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f44659c",
   "metadata": {},
   "outputs": [],
   "source": [
    "parms = { \"atl13\": { \"refid\": 5952002394 } }\n",
    "gdf = sliderule.run(\"atl13x\", parms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "965860ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate a plot of the results\n",
    "gdf.plot(column='ht_ortho', cmap='viridis', legend=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d05abd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Display the returned GeoDataFrame\n",
    "gdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff25378f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Display mapping of srcid to granule names\n",
    "gdf.attrs[\"meta\"][\"srctbl\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c344e242",
   "metadata": {},
   "source": [
    "### Retrieve ATL13 from ___SlideRule___ using `Lake Name`\n",
    "\n",
    "##### Metadata from a `shell` script"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93022504",
   "metadata": {},
   "source": [
    "```bash\n",
    "curl -X GET -d \"{\\\"asset\\\": \\\"icesat2-atl13\\\", \\\"atl13\\\": {\\\"name\\\": \\\"Caspian Sea\\\"}, \\\"max_resources\\\": 1500}\" https://sliderule.slideruleearth.io/source/earthdata\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d85ea5d9",
   "metadata": {},
   "source": [
    "##### Metadata from a `python` script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b735d24",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = sliderule.source(\"earthdata\", {\"asset\": \"icesat2-atl13\", \"atl13\": {\"name\": \"Caspian Sea\"}, \"max_resources\": 1500})\n",
    "response"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "65b5bbbf",
   "metadata": {},
   "source": [
    "##### Data from a `python` script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de9f71e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# define area of interest as a geojson object because the Lake being request is very large and we only want a subset of it\n",
    "area_of_interest = {\n",
    "  \"type\": \"FeatureCollection\",\n",
    "  \"features\": [ { \"type\": \"Feature\", \"properties\": {}, \"geometry\": { \"coordinates\": [ [ [49.63305859097062, 43.517023064094445], [49.63305859097062, 43.26673730943335], [50.39096571145933, 43.26673730943335], [50.39096571145933, 43.517023064094445], [49.63305859097062, 43.517023064094445] ] ], \"type\": \"Polygon\" } } ]\n",
    "}\n",
    "# process geojson into a format sliderule can understand\n",
    "region = sliderule.toregion(area_of_interest)\n",
    "# make atl13x request\n",
    "parms = { \"atl13\": { \"name\": \"Caspian Sea\" }, \"poly\": region[\"poly\"], \"max_resources\": 500, \"t0\": '2022-01-01', \"t1\": '2023-01-01' }\n",
    "gdf = sliderule.run(\"atl13x\", parms)\n",
    "gdf"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "047ed1d5",
   "metadata": {},
   "source": [
    "### Retrieve ATL13 from ___SlideRule___ using `Coordinate`\n",
    "\n",
    "##### Metadata from a `shell` script"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c1d1679",
   "metadata": {},
   "source": [
    "```bash\n",
    "curl -X GET -d \"{\\\"asset\\\": \\\"icesat2-atl13\\\", \\\"atl13\\\": {\\\"coord\\\":{\\\"lon\\\":-77.40162711974297,\\\"lat\\\":38.48769543754824}}}\" https://sliderule.slideruleearth.io/source/earthdata\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea91365d",
   "metadata": {},
   "source": [
    "##### Metadata from a `python` script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25fce937",
   "metadata": {},
   "outputs": [],
   "source": [
    "coordinates = [-77.40162711974297, 38.48769543754824]\n",
    "response = sliderule.source(\"earthdata\", {\"asset\": \"icesat2-atl13\", \"atl13\": {\"coord\": {\"lon\": coordinates[0], \"lat\": coordinates[1]}}})\n",
    "response"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc9d88ce",
   "metadata": {},
   "source": [
    "##### Data from a `python` script"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2c063257",
   "metadata": {},
   "outputs": [],
   "source": [
    "coordinates = [-77.40162711974297, 38.48769543754824]\n",
    "parms = { \"atl13\": {\"coord\": {\"lon\": coordinates[0], \"lat\": coordinates[1]}} }\n",
    "gdf = sliderule.run(\"atl13x\", parms)\n",
    "gdf"
   ]
  }
 ],
 "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
}
