{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SlideRule ATL03 Subsetting: Interactive Tutorial\n", "\n", "SlideRule is an on-demand science data processing service that runs in on Amazon Web Services and responds to REST API calls to process and return science results. SlideRule was designed to enable researchers and other data systems to have low-latency access to custom-generated, high-level, analysis-ready data products using processing parameters supplied at the time of the request. \n", "\n", "[Documentation for using SlideRule](https://slideruleearth.io/rtd) is available from the [project website](https://slideruleearth.io) \n", "\n", "### Background\n", "SlideRule can subset ATL03 geolocated photon height data _on-demand_ and calculate photon classifications to suit different needs.\n", "\n", "### Jupyter and SlideRule\n", "[Jupyter widgets](https://ipywidgets.readthedocs.io) are used to set parameters for the SlideRule API. \n", "\n", "Regions of interest for submitting to SlideRule are drawn on a [ipyleaflet](https://ipyleaflet.readthedocs.io) map. \n", "The results from SlideRule can be displayed on the interactive [ipyleaflet](https://ipyleaflet.readthedocs.io) map along with additional contextual layers." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Load necessary packages" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "import warnings\n", "# turn off warnings for demo\n", "warnings.filterwarnings('ignore')# autoreload\n", "\n", "from sliderule import icesat2, ipysliderule, sliderule, io, earthdata\n", "import geopandas\n", "import logging\n", "\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initiate SlideRule API\n", "- Sets the URL for accessing the SlideRule service\n", "- Builds a table of servers available for processing data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# set the url for the sliderule service\n", "# set the logging level\n", "icesat2.init(\"slideruleearth.io\", loglevel=logging.WARNING)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set options for making science data processing requests to SlideRule\n", "\n", "SlideRule can provide different sources for photon classification. This is useful for example in cases using the photon returns to estimate ocean or lake bathymetry, vegetated canopy heights, or ground position in the presence of a vegetated canopy. \n", "- ATL03 photon confidence values, based on algorithm-specific classification types for land, ocean, sea-ice, land-ice, or inland water\n", "- [ATL08 Land and Vegetation Height product](https://nsidc.org/data/atl08) photon classification\n", "- Experimental YAPC (Yet Another Photon Classification) photon-density-based classification" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# display widgets for setting SlideRule parameters\n", "SRwidgets = ipysliderule.widgets()\n", "SRwidgets.set_atl03_defaults()\n", "SRwidgets.VBox(SRwidgets.atl03())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Interactive Mapping with Leaflet\n", "\n", "Interactive maps within the SlideRule python API are built upon [ipyleaflet](https://ipyleaflet.readthedocs.io).\n", "\n", "#### Leaflet Basemaps and Layers\n", "\n", "There are 3 projections available within SlideRule for mapping ([Global](https://epsg.io/3857), [North](https://epsg.io/5936) and [South](https://epsg.io/3031)). There are also contextual layers available for each projection.\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Global (Web Mercator, EPSG:3857)North (Alaska Polar Stereographic, EPSG:5936)South (Antarctic Polar Stereographic, EPSG:3031)
\n", " \n", " \n", " \n", " \n", " \n", "
\n", "\n", "In addition, most [xyzservice providers](https://xyzservices.readthedocs.io/en/stable/introduction.html) can be added as contextual layers to the global Web Mercator maps" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "SRwidgets.VBox([\n", " SRwidgets.projection,\n", " SRwidgets.layers,\n", " SRwidgets.raster_functions\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Select regions of interest for submitting to SlideRule\n", "\n", "Here, we create polygons or bounding boxes for our regions of interest. \n", "This map is also our viewer for inspecting our SlideRule ICESat-2 data returns." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# create ipyleaflet map in specified projection\n", "m = ipysliderule.leaflet(SRwidgets.projection.value)\n", "m.map" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "m.add_layer(\n", " layers=SRwidgets.layers.value,\n", " rendering_rule=SRwidgets.rendering_rule\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Inspect list of available granules\n", "- SlideRule will query the [NASA Common Metadata Repository (CMR)](https://cmr.earthdata.nasa.gov/) for ATL03 data within our region of interest" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%time\n", "# sliderule asset and data release\n", "asset = SRwidgets.asset.value\n", "release = SRwidgets.release.value\n", "# find granule for each region of interest\n", "granules_list = []\n", "# for each region of interest\n", "sliderule.logger.warning('No valid regions to run') if not m.regions else None\n", "for poly in m.regions:\n", " granules = earthdata.cmr(short_name=\"ATL03\",\n", " polygon=poly,\n", " time_start=SRwidgets.time_start,\n", " time_end=SRwidgets.time_end,\n", " version=release)\n", " granules_list.extend(granules)\n", "# inspect granules list\n", "print(f'Available granules: {len(granules_list)}')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Transmit requests to SlideRule\n", "- When using the `icesat2` asset, the ICESat-2 ATL03 data are then accessed from the NSIDC AWS s3 bucket in `us-west-2`\n", "- The ATL03 granules is spatially subset within SlideRule to our exact region of interest\n", "- Photon classification parameters can then be extracted or calculated for our ATL03 data\n", "- The completed data is streamed concurrently back and combined into a geopandas GeoDataFrame within the Python client" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# build sliderule parameters using latest values from widget\n", "parms = SRwidgets.build_atl03()\n", "\n", "# clear existing geodataframe results\n", "elevations = [sliderule.emptyframe()]\n", "\n", "# for each region of interest\n", "for poly in m.regions:\n", " # add polygon from map to sliderule parameters\n", " parms[\"poly\"] = poly\n", " # make the request to the SlideRule (ATL03-SR) endpoint\n", " # and pass it the request parameters to request ATL03 Data\n", " elevations.append(icesat2.atl03sp(parms, resources=granules_list))\n", "\n", "gdf = geopandas.pd.concat(elevations)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Review GeoDataFrame output\n", "Can inspect the columns, number of returns and returns at the top of the GeoDataFrame.\n", "\n", "See the [ICESat-2 documentation](https://slideruleearth.io/rtd/user_guide/ICESat-2.html#elevations) for descriptions of each column" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "print(f'Returned {gdf.shape[0]} records')\n", "gdf.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add GeoDataFrame to map\n", "\n", "For stability of the leaflet map, SlideRule will as a default limit the plot to have up to 10000 points from the GeoDataFrame\n", "\n", "GeoDataFrames can be plotted in any available [matplotlib colormap](https://matplotlib.org/stable/tutorials/colors/colormaps.html)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "SRwidgets.VBox([\n", " SRwidgets.variable,\n", " SRwidgets.cmap,\n", " SRwidgets.reverse,\n", "])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%matplotlib inline\n", "# ATL03 fields for hover tooltip\n", "fields = gdf.leaflet.default_atl03_fields()\n", "gdf.leaflet.GeoData(m.map, column_name=SRwidgets.variable.value, cmap=SRwidgets.colormap,\n", " max_plot_points=10000, tooltip=True, colorbar=True, fields=fields)\n", "# install handlers and callbacks\n", "gdf.leaflet.set_observables(SRwidgets)\n", "gdf.leaflet.add_selected_callback(SRwidgets.atl03_click_handler)\n", "m.add_region_callback(gdf.leaflet.handle_region)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create plots for a single track\n", "- scatter: Will plot data returned by SlideRule for a single RGT, ground track and cycle" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "SRwidgets.VBox([\n", " SRwidgets.plot_classification,\n", " SRwidgets.rgt,\n", " SRwidgets.ground_track,\n", " SRwidgets.cycle,\n", "])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%matplotlib widget\n", "gdf.icesat2.plot(data_type='atl03', kind='scatter', title='Photon Cloud',\n", " cmap=SRwidgets.colormap, legend=True, legend_frameon=True,\n", " classification=SRwidgets.plot_classification.value,\n", " segments=False, **SRwidgets.plot_kwargs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save GeoDataFrame to output file" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "display(SRwidgets.filesaver)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# append sliderule api version to attributes\n", "version = sliderule.get_version()\n", "parms['version'] = version['icesat2']['version']\n", "parms['commit'] = version['icesat2']['commit']\n", "# save to file in format (HDF5 or netCDF)\n", "io.to_file(gdf, SRwidgets.file,\n", " format=SRwidgets.format,\n", " parameters=parms,\n", " regions=m.regions,\n", " verbose=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read GeoDataFrame from input file" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "display(SRwidgets.fileloader)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# read from file in format (HDF5 or netCDF)\n", "gdf,parms,regions = io.from_file(SRwidgets.file,\n", " format=SRwidgets.format,\n", " return_parameters=True,\n", " return_regions=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Review GeoDataFrame input from file" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "gdf.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set parameters and add saved regions to map" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "SRwidgets.set_values(parms)\n", "m.add_region(regions)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" }, "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.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }