{ "cells": [ { "cell_type": "markdown", "id": "ce46eaf7-1d78-4afe-859f-661c58ffbee1", "metadata": {}, "source": [ "{{title_s1_4}}\n", "\n", "This notebook demonstrates how to access Sentinel-1 RTC imagery from [Microsoft Planetary Computer](https://planetarycomputer.microsoft.com/) using [`stackstac`](https://stackstac.readthedocs.io/en/latest/). [STAC](https://stacspec.org/en) stands for Spatio-Temporal Asset Catalog, it is a common framework to describe geospatial information and a way for data providers, developers, and users to work and exchange information efficiently. " ] }, { "cell_type": "markdown", "id": "fd085da9", "metadata": {}, "source": [ "::::{tab-set}\n", ":::{tab-item} Outline \n", "\n", "(content.Section_A)=\n", "**[A. Connect to Microsoft Planetary Computer](#a-connect-to-microsoft-planetary-computer)**\n", "- {{a1_s1_nb4}}\n", "\n", "(content.Section_B)=\n", "**[B. Read data and create Xarray data cube](#b-read-data-with-xarray)**\n", "- {{b1_s1_nb4}}\n", "- {{b2_s1_nb4}}\n", "- {{b3_s1_nb4}}\n", "\n", "(content.Section_C)= \n", "**[C. Visualize data](#c-visualize-data)**\n", "- {{c1_s1_nb4}}\n", "- {{c2_s1_nb4}}\n", "- {{c3_s1_nb4}}\n", "\n", ":::\n", ":::{tab-item} Learning goals\n", "\n", "{{concepts}}\n", "- Querying large cloud-hosted dataset\n", "- Accessing cloud-hosted data stored as COGs (cloud-optimized GeoTIFFs)\n", "- Extracting and organizing metadata\n", "\n", "{{techniques}}\n", "- Introduction to working with STAC data\n", "- Using `pystac_client` to query cloud-hosted datasets, observe metadata\n", "- Using `stackstac` to read cloud-hosted data as xarray objects\n", "- Using `xarray` to manipulate and organize Sentinel-1 SAR data\n", "- Performing grouping and reductions on `xarrray` objects\n", "- Visualizing `xarray` objects using `FacetGrid`\n", ":::\n", "\n", ":::{tab-item} Relevant Concepts\n", "1. [Sentinel-1 RTC imagery](../../background/4_tutorial_data.md#sentinel-1-radiometric-terrain-corrected-rtc-imagery)\n", "2. {term}`Spatio-temporal Asset Catalog (STAC)`\n", "::::" ] }, { "cell_type": "code", "execution_count": 2, "id": "329bb298-9f22-47e1-afba-af9a7ef77978", "metadata": { "tags": [ "hide-input", "hide-output" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Exception reporting mode: Minimal\n" ] } ], "source": [ "%xmode minimal\n", "import geopandas as gpd\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import planetary_computer\n", "from pystac_client import Client\n", "import rich.table\n", "import stackstac\n", "\n", "import s1_tools" ] }, { "cell_type": "markdown", "id": "44cd0c50", "metadata": {}, "source": [ "{{break}}" ] }, { "cell_type": "markdown", "id": "8907c79c-b4fe-4e38-a858-60b6d731eb46", "metadata": {}, "source": [ "## A. Connect to Microsoft Planetary Computer \n", "\n", "We use the [`pystac_client`](https://pystac-client.readthedocs.io/) package to interact with and query the Microsoft Planetary Computer [Sentinel-1 RTC dataset](https://planetarycomputer.microsoft.com/dataset/group/sentinel-1). In the cell below, we will create an object called `catalog` by calling the `.open()` method of the `Client` class. This establishes a connection with the data hosted at the url provided." ] }, { "cell_type": "markdown", "id": "d5cbcbe6-62ca-4ccd-9162-d412e18ee09c", "metadata": {}, "source": [ "### {{a1_s1_nb4}}" ] }, { "cell_type": "code", "execution_count": 3, "id": "065310e4-a5e1-4997-9a44-ef5936f9ed31", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
\n", " | geometry | \n", "datetime | \n", "platform | \n", "s1:shape | \n", "proj:bbox | \n", "proj:shape | \n", "end_datetime | \n", "constellation | \n", "s1:resolution | \n", "proj:transform | \n", "... | \n", "sar:resolution_range | \n", "s1:product_timeliness | \n", "sar:resolution_azimuth | \n", "sar:pixel_spacing_range | \n", "sar:observation_direction | \n", "sar:pixel_spacing_azimuth | \n", "sar:looks_equivalent_number | \n", "s1:instrument_configuration_ID | \n", "sat:platform_international_designator | \n", "proj:code | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "POLYGON ((86.20642 27.84432, 86.21062 27.78391... | \n", "2022-05-21T12:14:09.786375Z | \n", "SENTINEL-1A | \n", "[28153, 21599] | \n", "[421860.0, 2916220.0, 703390.0, 3132210.0] | \n", "[21599, 28153] | \n", "2022-05-21 12:14:22.285304+00:00 | \n", "Sentinel-1 | \n", "high | \n", "[10.0, 0.0, 421860.0, 0.0, -10.0, 3132210.0, 0... | \n", "... | \n", "20 | \n", "Fast-24h | \n", "22 | \n", "10 | \n", "right | \n", "10 | \n", "4.4 | \n", "7 | \n", "2014-016A | \n", "EPSG:32645 | \n", "
1 | \n", "POLYGON ((89.98022 27.01218, 90.99374 27.17283... | \n", "2022-05-16T12:06:02.314833Z | \n", "SENTINEL-1A | \n", "[28422, 21971] | \n", "[618240.0, 2960440.0, 902460.0, 3180150.0] | \n", "[21971, 28422] | \n", "2022-05-16 12:06:14.814536+00:00 | \n", "Sentinel-1 | \n", "high | \n", "[10.0, 0.0, 618240.0, 0.0, -10.0, 3180150.0, 0... | \n", "... | \n", "20 | \n", "Fast-24h | \n", "22 | \n", "10 | \n", "right | \n", "10 | \n", "4.4 | \n", "7 | \n", "2014-016A | \n", "EPSG:32645 | \n", "
2 | \n", "POLYGON ((86.20632 27.84423, 86.21041 27.78364... | \n", "2022-05-09T12:14:08.980398Z | \n", "SENTINEL-1A | \n", "[28152, 21599] | \n", "[421850.0, 2916220.0, 703370.0, 3132210.0] | \n", "[21599, 28152] | \n", "2022-05-09 12:14:21.479341+00:00 | \n", "Sentinel-1 | \n", "high | \n", "[10.0, 0.0, 421850.0, 0.0, -10.0, 3132210.0, 0... | \n", "... | \n", "20 | \n", "Fast-24h | \n", "22 | \n", "10 | \n", "right | \n", "10 | \n", "4.4 | \n", "7 | \n", "2014-016A | \n", "EPSG:32645 | \n", "
3 | \n", "POLYGON ((89.98426 27.01255, 90.99525 27.17279... | \n", "2022-05-04T12:06:01.122366Z | \n", "SENTINEL-1A | \n", "[28428, 21971] | \n", "[619630.0, 2960660.0, 895970.0, 3178430.0] | \n", "[28428, 21971] | \n", "2022-05-04 12:06:13.622093+00:00 | \n", "Sentinel-1 | \n", "high | \n", "[10.0, 0.0, 618410.0, 0.0, -10.0, 3180150.0, 0... | \n", "... | \n", "20 | \n", "Fast-24h | \n", "22 | \n", "10 | \n", "right | \n", "10 | \n", "4.4 | \n", "7 | \n", "2014-016A | \n", "EPSG:32645 | \n", "
4 | \n", "POLYGON ((89.80391 27.02294, 89.82898 27.27428... | \n", "2022-04-30T00:03:27.201903Z | \n", "SENTINEL-1A | \n", "[27531, 21084] | \n", "[524470.0, 2980840.0, 799780.0, 3191680.0] | \n", "[21084, 27531] | \n", "2022-04-30 00:03:39.701264+00:00 | \n", "Sentinel-1 | \n", "high | \n", "[10.0, 0.0, 524470.0, 0.0, -10.0, 3191680.0, 0... | \n", "... | \n", "20 | \n", "Fast-24h | \n", "22 | \n", "10 | \n", "right | \n", "10 | \n", "4.4 | \n", "7 | \n", "2014-016A | \n", "EPSG:32645 | \n", "
5 rows × 36 columns
\n", "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", "┃ key ┃ value ┃\n", "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", "│ constellation │ Sentinel-1 │\n", "│ datetime │ 2022-05-21T12:14:09.786375Z │\n", "│ end_datetime │ 2022-05-21 12:14:22.285304+00:00 │\n", "│ platform │ SENTINEL-1A │\n", "│ proj:bbox │ [421860.0, 2916220.0, 703390.0, 3132210.0] │\n", "│ proj:code │ EPSG:32645 │\n", "│ proj:shape │ [21599, 28153] │\n", "│ proj:transform │ [10.0, 0.0, 421860.0, 0.0, -10.0, 3132210.0, 0.0, 0.0, 1.0] │\n", "│ s1:datatake_id │ 338944 │\n", "│ s1:instrument_configuration_ID │ 7 │\n", "│ s1:orbit_source │ RESORB │\n", "│ s1:processing_level │ 1 │\n", "│ s1:product_timeliness │ Fast-24h │\n", "│ s1:resolution │ high │\n", "│ s1:shape │ [28153, 21599] │\n", "│ s1:slice_number │ 6 │\n", "│ s1:total_slices │ 20 │\n", "│ sar:center_frequency │ 5.405 │\n", "│ sar:frequency_band │ C │\n", "│ sar:instrument_mode │ IW │\n", "│ sar:looks_azimuth │ 1 │\n", "│ sar:looks_equivalent_number │ 4.4 │\n", "│ sar:looks_range │ 5 │\n", "│ sar:observation_direction │ right │\n", "│ sar:pixel_spacing_azimuth │ 10 │\n", "│ sar:pixel_spacing_range │ 10 │\n", "│ sar:polarizations │ ['VV', 'VH'] │\n", "│ sar:product_type │ GRD │\n", "│ sar:resolution_azimuth │ 22 │\n", "│ sar:resolution_range │ 20 │\n", "│ sat:absolute_orbit │ 43309 │\n", "│ sat:orbit_state │ ascending │\n", "│ sat:platform_international_designator │ 2014-016A │\n", "│ sat:relative_orbit │ 12 │\n", "│ start_datetime │ 2022-05-21 12:13:57.287447+00:00 │\n", "└───────────────────────────────────────┴─────────────────────────────────────────────────────────────┘\n", "\n" ], "text/plain": [ "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n", "┃\u001b[1m \u001b[0m\u001b[1mkey \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mvalue \u001b[0m\u001b[1m \u001b[0m┃\n", "┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩\n", "│ constellation │ Sentinel-1 │\n", "│ datetime │ 2022-05-21T12:14:09.786375Z │\n", "│ end_datetime │ 2022-05-21 12:14:22.285304+00:00 │\n", "│ platform │ SENTINEL-1A │\n", "│ proj:bbox │ [421860.0, 2916220.0, 703390.0, 3132210.0] │\n", "│ proj:code │ EPSG:32645 │\n", "│ proj:shape │ [21599, 28153] │\n", "│ proj:transform │ [10.0, 0.0, 421860.0, 0.0, -10.0, 3132210.0, 0.0, 0.0, 1.0] │\n", "│ s1:datatake_id │ 338944 │\n", "│ s1:instrument_configuration_ID │ 7 │\n", "│ s1:orbit_source │ RESORB │\n", "│ s1:processing_level │ 1 │\n", "│ s1:product_timeliness │ Fast-24h │\n", "│ s1:resolution │ high │\n", "│ s1:shape │ [28153, 21599] │\n", "│ s1:slice_number │ 6 │\n", "│ s1:total_slices │ 20 │\n", "│ sar:center_frequency │ 5.405 │\n", "│ sar:frequency_band │ C │\n", "│ sar:instrument_mode │ IW │\n", "│ sar:looks_azimuth │ 1 │\n", "│ sar:looks_equivalent_number │ 4.4 │\n", "│ sar:looks_range │ 5 │\n", "│ sar:observation_direction │ right │\n", "│ sar:pixel_spacing_azimuth │ 10 │\n", "│ sar:pixel_spacing_range │ 10 │\n", "│ sar:polarizations │ ['VV', 'VH'] │\n", "│ sar:product_type │ GRD │\n", "│ sar:resolution_azimuth │ 22 │\n", "│ sar:resolution_range │ 20 │\n", "│ sat:absolute_orbit │ 43309 │\n", "│ sat:orbit_state │ ascending │\n", "│ sat:platform_international_designator │ 2014-016A │\n", "│ sat:relative_orbit │ 12 │\n", "│ start_datetime │ 2022-05-21 12:13:57.287447+00:00 │\n", "└───────────────────────────────────────┴─────────────────────────────────────────────────────────────┘\n" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table = rich.table.Table(\"key\", \"value\")\n", "for k, v in sorted(items[0].properties.items()):\n", " table.add_row(k, str(v))\n", "table" ] }, { "cell_type": "markdown", "id": "ad6bc1bf-5b49-4533-aea7-2862b812985c", "metadata": {}, "source": [ "We can also explore the object metadata outside of the table. Try typing `.assets`, `.links`, `.STAC_extensions` and `.properties` onto the term below. \n", "You can query the object programmatically for the same metadata stored in the table using dictionary syntax on the `properties` accessor." ] }, { "cell_type": "code", "execution_count": 10, "id": "ec3a354c-f6e5-46be-86ef-aca0273016e3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "
<xarray.DataArray 'stackstac-9bcc6847bcc8aaa157107857cb7c5801' (season: 4,\n", " band: 2,\n", " y: 1542, x: 888)> Size: 88MB\n", "array([[[[0.02535364, 0.03147986, 0.02786191, ..., 0.02108686,\n", " 0.02130041, 0.02315306],\n", " [0.0218459 , 0.02203541, 0.01941934, ..., 0.02368083,\n", " 0.02490576, 0.02375024],\n", " [0.02043244, 0.01921614, 0.01841479, ..., 0.02435656,\n", " 0.02297878, 0.02517332],\n", " ...,\n", " [0.07336626, 0.05900169, 0.05184992, ..., 0.06449757,\n", " 0.05422511, 0.04861424],\n", " [0.06321969, 0.06112303, 0.04925801, ..., 0.05013352,\n", " 0.04173466, 0.03783966],\n", " [0.05632453, 0.04643786, 0.04184489, ..., 0.0426184 ,\n", " 0.03918812, 0.03669885]],\n", "\n", " [[0.08964307, 0.11235596, 0.07842911, ..., 0.0773935 ,\n", " 0.06486455, 0.06993567],\n", " [0.07961441, 0.08544813, 0.07430832, ..., 0.08535827,\n", " 0.06914322, 0.07503611],\n", " [0.06167143, 0.07350944, 0.09076815, ..., 0.11603812,\n", " 0.09522323, 0.08346861],\n", "...\n", " [0.0467003 , 0.03025291, 0.02203605, ..., 0.02916168,\n", " 0.03355919, 0.03477128],\n", " [0.0422203 , 0.03006489, 0.02622437, ..., 0.02509039,\n", " 0.03020153, 0.02791584],\n", " [0.03369109, 0.02739136, 0.02804806, ..., 0.02638425,\n", " 0.03162629, 0.02560615]],\n", "\n", " [[0.07472315, 0.08895492, 0.09268859, ..., 0.07098423,\n", " 0.05950723, 0.0595387 ],\n", " [0.0703036 , 0.07719046, 0.08143807, ..., 0.07945435,\n", " 0.0719438 , 0.07451397],\n", " [0.06143826, 0.06699089, 0.07739989, ..., 0.08420351,\n", " 0.08209176, 0.08979529],\n", " ...,\n", " [0.1922634 , 0.120512 , 0.08602943, ..., 0.28120565,\n", " 0.2469148 , 0.1406847 ],\n", " [0.15826532, 0.12039333, 0.09145802, ..., 0.24468837,\n", " 0.1999128 , 0.1314866 ],\n", " [0.11499288, 0.09421477, 0.08536671, ..., 0.20733283,\n", " 0.18289436, 0.13072233]]]])\n", "Coordinates: (12/28)\n", " * band (band) <U2 16B 'vh' 'vv'\n", " * x (x) float64 7kB 6.194e+05 ... 6.28...\n", " * y (y) float64 12kB 3.102e+06 ... 3.0...\n", " * season (season) <U3 48B 'DJF' ... 'SON'\n", " sar:center_frequency float64 8B 5.405\n", " sar:looks_equivalent_number float64 8B 4.4\n", " ... ...\n", " sar:polarizations object 8B {'VH', 'VV'}\n", " sar:frequency_band <U1 4B 'C'\n", " title (band) <U41 328B 'VH: vertical tra...\n", " description (band) <U173 1kB 'Terrain-correcte...\n", " raster:bands object 8B {'nodata': -32768, 'data...\n", " epsg int64 8B 32645\n", "Attributes:\n", " spec: RasterSpec(epsg=32645, bounds=(619419.5314582244, 3089781...\n", " crs: epsg:32645\n", " transform: | 9.79, 0.00, 619419.53|\\n| 0.00,-7.70, 3101655.25|\\n| 0....\n", " resolution_xy: (9.788706070864338, 7.699930607059487)