| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"id": "b91f3198", |
| 6 |
"metadata": {}, |
| 7 |
"source": [ |
| 8 |
"# Process a BIOMASS L1 product on MAAP using the STAC API\n", |
| 9 |
"\n", |
| 10 |
"\n", |
| 11 |
"**Important note:** To use this notebook, an account on ESA BIOMASS MAAP Product Algorithm Laboratory is required.\n", |
| 12 |
"\n", |
| 13 |
"This tutorial is for users who are already familiar with STAC API and want to process data locally without explicit download or directly in the cloud. \n", |
| 14 |
"It is shown how to:\n", |
| 15 |
"- Look for a product using the STAC API\n", |
| 16 |
"- Use credentials to get an access token\n", |
| 17 |
"- Open BIOMASS raster data lazily with rioxarray (rasterio+xarray) using file urls\n", |
| 18 |
"- Make a valid PolSARpro dataset from these raster arrays\n", |
| 19 |
"- Apply a polarimetric decomposition and save the result locally\n", |
| 20 |
"\n", |
| 21 |
"### Prerequisites:\n", |
| 22 |
"- Access to the collaborative environment https://portal.maap.eo.esa.int/biomass/initiative-membership/\n", |
| 23 |
"- Credentials as described here https://portal.maap.eo.esa.int/ini/kb/books/enhanced-data-access/page/using-the-long-lasting-token-to-access-and-download-data \n", |
| 24 |
"- Pystac-client installed (`conda install pystac-client`)\n", |
| 25 |
"\n", |
| 26 |
"This notebook may be run locally or in the MAAP coding environment. Installation of PyPolSARpro on the MAAP environment is described here: https://gist.github.com/odhondt/f9ce88acca5f91e8f3563b32a342addc" |
| 27 |
] |
| 28 |
}, |
| 29 |
{ |
| 30 |
"cell_type": "code", |
| 31 |
"execution_count": null, |
| 32 |
"id": "da5de933-73c7-467d-93e4-9e658623bcb7", |
| 33 |
"metadata": {}, |
| 34 |
"outputs": [], |
| 35 |
"source": [ |
| 36 |
"import requests\n", |
| 37 |
"from pystac_client import Client\n", |
| 38 |
"from pathlib import Path\n", |
| 39 |
"\n", |
| 40 |
"URL_LANDING_PAGE = \"https://catalog.maap.eo.esa.int/catalogue/\"\n", |
| 41 |
"api = Client.open(URL_LANDING_PAGE) # Connection to the catalog" |
| 42 |
] |
| 43 |
}, |
| 44 |
{ |
| 45 |
"cell_type": "markdown", |
| 46 |
"id": "98df8f54-4a3e-4414-9efc-710be8c537c1", |
| 47 |
"metadata": {}, |
| 48 |
"source": [ |
| 49 |
"## Search a product by ID using the STAC API\n", |
| 50 |
"This step may be replaced by any valid STAC API query.\n", |
| 51 |
"In the following, for the sake of demonstration, we only process the first product in the list." |
| 52 |
] |
| 53 |
}, |
| 54 |
{ |
| 55 |
"cell_type": "code", |
| 56 |
"execution_count": null, |
| 57 |
"id": "d11d4168-0f3c-4f19-826b-c1196fe4fc18", |
| 58 |
"metadata": { |
| 59 |
"editable": true, |
| 60 |
"slideshow": { |
| 61 |
"slide_type": "" |
| 62 |
}, |
| 63 |
"tags": [] |
| 64 |
}, |
| 65 |
"outputs": [], |
| 66 |
"source": [ |
| 67 |
"ids = [\"BIO_S2_SCS__1S_20251216T034800_20251216T034815_T_G01_M01_C02_T017_F289_01_DJQGAN\"]\n", |
| 68 |
"\n", |
| 69 |
"results = api.search(\n", |
| 70 |
" method = 'GET', \n", |
| 71 |
" collections = 'BiomassLevel1a',\n", |
| 72 |
" ids = ids\n", |
| 73 |
")" |
| 74 |
] |
| 75 |
}, |
| 76 |
{ |
| 77 |
"cell_type": "code", |
| 78 |
"execution_count": null, |
| 79 |
"id": "e3fdf943-f1a6-4ba0-893c-a924938cc806", |
| 80 |
"metadata": {}, |
| 81 |
"outputs": [], |
| 82 |
"source": [ |
| 83 |
"items = results.item_collection()\n", |
| 84 |
"# uncomment the following line to inspect item\n", |
| 85 |
"# items[0]" |
| 86 |
] |
| 87 |
}, |
| 88 |
{ |
| 89 |
"cell_type": "code", |
| 90 |
"execution_count": null, |
| 91 |
"id": "3bc134b0-2b1d-43de-9764-98072b99c649", |
| 92 |
"metadata": { |
| 93 |
"scrolled": true |
| 94 |
}, |
| 95 |
"outputs": [], |
| 96 |
"source": [ |
| 97 |
"# Extract urls of amplitude and phase rasters\n", |
| 98 |
"url_abs = items[0].assets[\"enclosure_i_abs_tiff\"].href\n", |
| 99 |
"url_phase = items[0].assets[\"enclosure_i_phase_tiff\"].href" |
| 100 |
] |
| 101 |
}, |
| 102 |
{ |
| 103 |
"cell_type": "markdown", |
| 104 |
"id": "970be18e-e043-4064-887b-74f94ca13526", |
| 105 |
"metadata": {}, |
| 106 |
"source": [ |
| 107 |
"## Request a token for product access\n", |
| 108 |
"The code in the cell below was slightly adapted from: https://portal.maap.eo.esa.int/ini/kb/books/enhanced-data-access/page/using-the-long-lasting-token-to-access-and-download-data" |
| 109 |
] |
| 110 |
}, |
| 111 |
{ |
| 112 |
"cell_type": "code", |
| 113 |
"execution_count": null, |
| 114 |
"id": "c1146acb-8034-45c8-9b08-6bfb25885169", |
| 115 |
"metadata": {}, |
| 116 |
"outputs": [], |
| 117 |
"source": [ |
| 118 |
"def load_credentials(file_path):\n", |
| 119 |
" \"\"\"Read key-value pairs from a credentials file into a dictionary.\"\"\"\n", |
| 120 |
" creds = {}\n", |
| 121 |
" if not file_path.exists():\n", |
| 122 |
" raise FileNotFoundError(f\"Credentials file not found: {file_path}\")\n", |
| 123 |
" with open(file_path, \"r\") as f:\n", |
| 124 |
" for line in f:\n", |
| 125 |
" line = line.strip()\n", |
| 126 |
" if not line or line.startswith(\"#\"):\n", |
| 127 |
" continue\n", |
| 128 |
" if \"=\" not in line:\n", |
| 129 |
" continue\n", |
| 130 |
" key, value = line.split(\"=\", 1)\n", |
| 131 |
" creds[key.strip()] = value.strip()\n", |
| 132 |
" return creds\n", |
| 133 |
"\n", |
| 134 |
"\n", |
| 135 |
"def get_token(credential_file_path):\n", |
| 136 |
" \"\"\"Use OFFLINE_TOKEN to fetch a short-lived access token.\"\"\"\n", |
| 137 |
" creds = load_credentials(Path(credential_file_path))\n", |
| 138 |
"\n", |
| 139 |
" OFFLINE_TOKEN = creds.get(\"OFFLINE_TOKEN\")\n", |
| 140 |
" CLIENT_ID = creds.get(\"CLIENT_ID\")\n", |
| 141 |
" CLIENT_SECRET = creds.get(\"CLIENT_SECRET\")\n", |
| 142 |
"\n", |
| 143 |
" if not all([OFFLINE_TOKEN, CLIENT_ID, CLIENT_SECRET]):\n", |
| 144 |
" raise ValueError(\"Missing OFFLINE_TOKEN, CLIENT_ID, or CLIENT_SECRET in credentials file\")\n", |
| 145 |
"\n", |
| 146 |
" url = \"https://iam.maap.eo.esa.int/realms/esa-maap/protocol/openid-connect/token\"\n", |
| 147 |
" data = {\n", |
| 148 |
" \"client_id\": CLIENT_ID,\n", |
| 149 |
" \"client_secret\": CLIENT_SECRET,\n", |
| 150 |
" \"grant_type\": \"refresh_token\",\n", |
| 151 |
" \"refresh_token\": OFFLINE_TOKEN,\n", |
| 152 |
" \"scope\": \"offline_access openid\"\n", |
| 153 |
" }\n", |
| 154 |
"\n", |
| 155 |
" response = requests.post(url, data=data)\n", |
| 156 |
" response.raise_for_status()\n", |
| 157 |
"\n", |
| 158 |
" response_json = response.json()\n", |
| 159 |
" access_token = response_json.get('access_token')\n", |
| 160 |
"\n", |
| 161 |
" if not access_token:\n", |
| 162 |
" raise RuntimeError(\"Failed to retrieve access token from IAM response\")\n", |
| 163 |
"\n", |
| 164 |
" return access_token" |
| 165 |
] |
| 166 |
}, |
| 167 |
{ |
| 168 |
"cell_type": "markdown", |
| 169 |
"id": "c0ca04f1-80f0-4e37-a2b7-b1d9504c93f2", |
| 170 |
"metadata": {}, |
| 171 |
"source": [ |
| 172 |
"## Access product raster and process on-the-fly\n", |
| 173 |
"Requisite: create a credential file as described here https://portal.maap.eo.esa.int/ini/kb/books/enhanced-data-access/page/using-the-long-lasting-token-to-access-and-download-data " |
| 174 |
] |
| 175 |
}, |
| 176 |
{ |
| 177 |
"cell_type": "code", |
| 178 |
"execution_count": null, |
| 179 |
"id": "8588d626-f930-483c-b3a1-53dd336baca7", |
| 180 |
"metadata": {}, |
| 181 |
"outputs": [], |
| 182 |
"source": [ |
| 183 |
"# Authentication\n", |
| 184 |
"token = get_token(\"/data/credentials.txt\")" |
| 185 |
] |
| 186 |
}, |
| 187 |
{ |
| 188 |
"cell_type": "code", |
| 189 |
"execution_count": null, |
| 190 |
"id": "d2f31191-f893-4ecc-8a97-a4786add562c", |
| 191 |
"metadata": {}, |
| 192 |
"outputs": [], |
| 193 |
"source": [ |
| 194 |
"import rioxarray as riox\n", |
| 195 |
"import rasterio as rio\n", |
| 196 |
"\n", |
| 197 |
"# Datasets lazy loading\n", |
| 198 |
"with rio.Env(\n", |
| 199 |
" GDAL_HTTP_HEADERS=f\"Authorization: Bearer {token}\",\n", |
| 200 |
" GDAL_DISABLE_READDIR_ON_OPEN=\"EMPTY_DIR\",\n", |
| 201 |
" GDAL_NUM_THREADS=\"ALL_CPUS\",\n", |
| 202 |
"):\n", |
| 203 |
" da_abs = riox.open_rasterio(url_abs, chunks={})\n", |
| 204 |
" da_phase = riox.open_rasterio(url_phase, chunks={})" |
| 205 |
] |
| 206 |
}, |
| 207 |
{ |
| 208 |
"cell_type": "code", |
| 209 |
"execution_count": null, |
| 210 |
"id": "da19c647-cd43-4130-86b9-b5de0315b7a8", |
| 211 |
"metadata": {}, |
| 212 |
"outputs": [], |
| 213 |
"source": [ |
| 214 |
"import numpy as np\n", |
| 215 |
"import xarray as xr\n", |
| 216 |
"\n", |
| 217 |
"# Reconstruct SLC images\n", |
| 218 |
"slc = da_abs * np.exp(1j * da_phase)\n", |
| 219 |
"dims = (\"y\", \"x\")\n", |
| 220 |
"# Extract polSAR channels and create a PolSARpro S matrix\n", |
| 221 |
"S = xr.Dataset(\n", |
| 222 |
" {\n", |
| 223 |
" \"hh\": (dims, slc[0].data),\n", |
| 224 |
" \"hv\": (dims, slc[1].data),\n", |
| 225 |
" \"vh\": (dims, slc[2].data),\n", |
| 226 |
" \"vv\": (dims, slc[3].data),\n", |
| 227 |
" },\n", |
| 228 |
" coords={\"y\": np.arange(slc.shape[1]), \"x\": np.arange(slc.shape[2])},\n", |
| 229 |
" attrs={\"poltype\": \"S\", \"description\": \"Scattering matrix\"},\n", |
| 230 |
").chunk(\"auto\")" |
| 231 |
] |
| 232 |
}, |
| 233 |
{ |
| 234 |
"cell_type": "markdown", |
| 235 |
"id": "746658f2", |
| 236 |
"metadata": {}, |
| 237 |
"source": [ |
| 238 |
"## Process with (Py)PolSARpro" |
| 239 |
] |
| 240 |
}, |
| 241 |
{ |
| 242 |
"cell_type": "code", |
| 243 |
"execution_count": null, |
| 244 |
"id": "252cf18e-8460-4079-82d8-090cadb9aa6f", |
| 245 |
"metadata": {}, |
| 246 |
"outputs": [], |
| 247 |
"source": [ |
| 248 |
"from polsarpro.decompositions import freeman\n", |
| 249 |
"from dask.diagnostics import ProgressBar\n", |
| 250 |
"import os\n", |
| 251 |
"\n", |
| 252 |
"out_file = \"/data/psp/res/freeman_biomass.nc\"\n", |
| 253 |
"if os.path.exists(out_file):\n", |
| 254 |
" os.remove(out_file)\n", |
| 255 |
"\n", |
| 256 |
"with ProgressBar():\n", |
| 257 |
" freeman(S, [7, 7]).to_netcdf(out_file)" |
| 258 |
] |
| 259 |
}, |
| 260 |
{ |
| 261 |
"cell_type": "markdown", |
| 262 |
"id": "c1ebcc22", |
| 263 |
"metadata": {}, |
| 264 |
"source": [ |
| 265 |
"## Visualize the results" |
| 266 |
] |
| 267 |
}, |
| 268 |
{ |
| 269 |
"cell_type": "code", |
| 270 |
"execution_count": null, |
| 271 |
"id": "b61591c1-fdba-4bc2-82b5-a78676a28ddf", |
| 272 |
"metadata": {}, |
| 273 |
"outputs": [], |
| 274 |
"source": [ |
| 275 |
"out_file = \"/data/psp/res/freeman_biomass.nc\"\n", |
| 276 |
"out = xr.open_dataset(out_file)" |
| 277 |
] |
| 278 |
}, |
| 279 |
{ |
| 280 |
"cell_type": "code", |
| 281 |
"execution_count": null, |
| 282 |
"id": "ce17a80e-e3d3-4dc2-9ccd-400a4d4fcdd7", |
| 283 |
"metadata": {}, |
| 284 |
"outputs": [], |
| 285 |
"source": [ |
| 286 |
"out.odd.plot.imshow(vmax=1)" |
| 287 |
] |
| 288 |
}, |
| 289 |
{ |
| 290 |
"cell_type": "code", |
| 291 |
"execution_count": null, |
| 292 |
"id": "3314957c", |
| 293 |
"metadata": {}, |
| 294 |
"outputs": [], |
| 295 |
"source": [ |
| 296 |
"out.double.plot.imshow(vmax=1)" |
| 297 |
] |
| 298 |
}, |
| 299 |
{ |
| 300 |
"cell_type": "code", |
| 301 |
"execution_count": null, |
| 302 |
"id": "6bd89b57", |
| 303 |
"metadata": {}, |
| 304 |
"outputs": [], |
| 305 |
"source": [ |
| 306 |
"out.volume.plot.imshow(vmax=1)" |
| 307 |
] |
| 308 |
} |
| 309 |
], |
| 310 |
"metadata": { |
| 311 |
"kernelspec": { |
| 312 |
"display_name": "psp", |
| 313 |
"language": "python", |
| 314 |
"name": "python3" |
| 315 |
}, |
| 316 |
"language_info": { |
| 317 |
"codemirror_mode": { |
| 318 |
"name": "ipython", |
| 319 |
"version": 3 |
| 320 |
}, |
| 321 |
"file_extension": ".py", |
| 322 |
"mimetype": "text/x-python", |
| 323 |
"name": "python", |
| 324 |
"nbconvert_exporter": "python", |
| 325 |
"pygments_lexer": "ipython3", |
| 326 |
"version": "3.12.12" |
| 327 |
} |
| 328 |
}, |
| 329 |
"nbformat": 4, |
| 330 |
"nbformat_minor": 5 |
| 331 |
} |