{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e54115a3-ecaa-4131-bcb3-77f84831f91f",
   "metadata": {
    "tags": []
   },
   "source": [
    "<div>\n",
    "    <h3><center><a href=\"https://its-live.jpl.nasa.gov/\"><img align=\"middle\" src=\"https://its-live-data.s3.amazonaws.com/documentation/ITS_LIVE_logo.png\" width=\"400px\"/></a></center></h3>\n",
    "    <h2><center>Global Glacier Velocity Point Data Access</center></h2>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "330a2f00",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%capture\n",
    "%matplotlib widget\n",
    "\n",
    "import matplotlib\n",
    "import markdown\n",
    "import io\n",
    "from ipywidgets import widgets, HTML, Output, FileUpload\n",
    "\n",
    "from velocity_widget import ITSLIVE\n",
    "\n",
    "matplotlib.rcParams['figure.figsize'] = [9, 5]\n",
    "matplotlib.rcParams[\"figure.autolayout\"] = True\n",
    "\n",
    "matplotlib.pyplot.style.use('ggplot')\n",
    "\n",
    "\n",
    "velocity_widget = ITSLIVE()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9ad345d-087a-43e8-bf24-1e0be4ba6565",
   "metadata": {},
   "source": [
    "**Instructions**: Click and drag on the map to pan the field of view. Select locations by double-clicking on the map then press Plot. Once plotted you can change the Variable that is being shown and how the markers are colored using Plot By. You can drag individual points after they are placed to relocate them, and then Plot again or Clear markers to start over.\n",
    "You can also single-click on the map to populate the Lat and Lon boxes then add a point using the Add Point. Lat and Lon can also be edited manually.\n",
    "Hovering your cursor over the plot reveals tools to zoom, pan, and save the figure.\n",
    "\n",
    "Press Export Data to generate comma separated value (.csv) files of the data. Press Download Data to retrieve locally. Export Data must be pressed each time new data is requested.\n",
    "Check out the video tutorial if you're a visual learner:\n",
    "\n",
    "<center><a href=\"https://www.youtube.com/watch?v=VYKsVvpVbmU\" target=\"_blank\"><img width=\"35%\" src=\"https://its-live-data.s3.amazonaws.com/documentation/ITS_LIVE_widget_youtube.jpg\"></a></center>\n",
    "\n",
    "\n",
    "Data are Version 2 of the ITS_LIVE global glacier velocity dataset that provides up-to-date velocities from Sentinel-1, Sentinel-2, Landsat-8 and Landsat-9 data. Version 2 annual mosaics are coming soon, and will be followed by Landsat 7 and improved Landsat 9 velocities.\n",
    "Please refer to the <b>[project website](https://its-live.jpl.nasa.gov/)</b> for known issues, citation and other product information.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdc8e3f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# we display the UI\n",
    "velocity_widget.display(mobile=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d134742-c10f-47ce-9930-cf049e37b2f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# we can access the zarr cubes via \n",
    "cubes = velocity_widget.dct.open_cubes\n",
    "[cube for cube in cubes]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c401b1c6-d2e0-4bbc-b5f8-b0e803e395b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# then load them with xarray e.g.\n",
    "# ds = cubes[\"s3://its-live-data/datacubes/v02/N70W040/ITS_LIVE_vel_EPSG3413_G0120_X-150000_Y-2150000.zarr\"\n",
    "# ds"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58e60370-c092-4405-a3ae-c0d5d7bf9c8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Antarctic elevation change data cube\n",
    "import xarray as xr\n",
    "ds = xr.open_dataset(\"s3://its-live-data/elevation/v01/ANT_G1920V01_GroundedIceHeight.zarr\",\n",
    "                     engine=\"zarr\",\n",
    "                     storage_options={\"anon\": True})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52c105a0-ba5d-4ee2-8e93-f3027849c048",
   "metadata": {},
   "outputs": [],
   "source": [
    "# lon, lat\n",
    "x, y = velocity_widget.dct.transformer_3031.transform(-87.34, -74.56)\n",
    "df = ds[\"dh\"].sel(x=x, y=y, method=\"nearest\").to_dataframe().dropna()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec46ef32-ca33-4ad9-b715-b982c14133e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = df[\"dh\"]\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44a1ca05-87d6-47d0-a639-fc53bf66d330",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "fig, ax = plt.subplots(figsize=(5, 3))\n",
    "\n",
    "# plot the cumulative histogram\n",
    "ts.plot(ax=ax)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}