| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "code", |
| 5 |
"execution_count": null, |
| 6 |
"id": "da5de933-73c7-467d-93e4-9e658623bcb7", |
| 7 |
"metadata": {}, |
| 8 |
"outputs": [], |
| 9 |
"source": [ |
| 10 |
"import requests\n", |
| 11 |
"from pystac_client import Client\n", |
| 12 |
"from pathlib import Path\n", |
| 13 |
"\n", |
| 14 |
"URL_LANDING_PAGE = \"https://catalog.maap.eo.esa.int/catalogue/\"\n", |
| 15 |
"api = Client.open(URL_LANDING_PAGE) # Connection to the catalog" |
| 16 |
] |
| 17 |
}, |
| 18 |
{ |
| 19 |
"cell_type": "markdown", |
| 20 |
"id": "98df8f54-4a3e-4414-9efc-710be8c537c1", |
| 21 |
"metadata": {}, |
| 22 |
"source": [ |
| 23 |
"# Search product by ID using the STAC API" |
| 24 |
] |
| 25 |
}, |
| 26 |
{ |
| 27 |
"cell_type": "code", |
| 28 |
"execution_count": null, |
| 29 |
"id": "d11d4168-0f3c-4f19-826b-c1196fe4fc18", |
| 30 |
"metadata": { |
| 31 |
"editable": true, |
| 32 |
"slideshow": { |
| 33 |
"slide_type": "" |
| 34 |
}, |
| 35 |
"tags": [] |
| 36 |
}, |
| 37 |
"outputs": [], |
| 38 |
"source": [ |
| 39 |
"ids = [\"BIO_S2_SCS__1S_20251216T034800_20251216T034815_T_G01_M01_C02_T017_F289_01_DJQGAN\"]\n", |
| 40 |
"\n", |
| 41 |
"results = api.search(\n", |
| 42 |
" method = 'GET', \n", |
| 43 |
" collections = 'BiomassLevel1a',\n", |
| 44 |
" ids = ids\n", |
| 45 |
")" |
| 46 |
] |
| 47 |
}, |
| 48 |
{ |
| 49 |
"cell_type": "code", |
| 50 |
"execution_count": null, |
| 51 |
"id": "e3fdf943-f1a6-4ba0-893c-a924938cc806", |
| 52 |
"metadata": {}, |
| 53 |
"outputs": [], |
| 54 |
"source": [ |
| 55 |
"items = results.item_collection()\n", |
| 56 |
"items[0]" |
| 57 |
] |
| 58 |
}, |
| 59 |
{ |
| 60 |
"cell_type": "code", |
| 61 |
"execution_count": null, |
| 62 |
"id": "3bc134b0-2b1d-43de-9764-98072b99c649", |
| 63 |
"metadata": { |
| 64 |
"scrolled": true |
| 65 |
}, |
| 66 |
"outputs": [], |
| 67 |
"source": [ |
| 68 |
"url_abs = items[0].assets[\"enclosure_i_abs_tiff\"].href\n", |
| 69 |
"url_phase = items[0].assets[\"enclosure_i_phase_tiff\"].href" |
| 70 |
] |
| 71 |
}, |
| 72 |
{ |
| 73 |
"cell_type": "markdown", |
| 74 |
"id": "970be18e-e043-4064-887b-74f94ca13526", |
| 75 |
"metadata": {}, |
| 76 |
"source": [ |
| 77 |
"# Request a token for product access" |
| 78 |
] |
| 79 |
}, |
| 80 |
{ |
| 81 |
"cell_type": "code", |
| 82 |
"execution_count": null, |
| 83 |
"id": "c1146acb-8034-45c8-9b08-6bfb25885169", |
| 84 |
"metadata": {}, |
| 85 |
"outputs": [], |
| 86 |
"source": [ |
| 87 |
"def load_credentials(file_path):\n", |
| 88 |
" \"\"\"Read key-value pairs from a credentials file into a dictionary.\"\"\"\n", |
| 89 |
" creds = {}\n", |
| 90 |
" if not file_path.exists():\n", |
| 91 |
" raise FileNotFoundError(f\"Credentials file not found: {file_path}\")\n", |
| 92 |
" with open(file_path, \"r\") as f:\n", |
| 93 |
" for line in f:\n", |
| 94 |
" line = line.strip()\n", |
| 95 |
" if not line or line.startswith(\"#\"):\n", |
| 96 |
" continue\n", |
| 97 |
" if \"=\" not in line:\n", |
| 98 |
" continue\n", |
| 99 |
" key, value = line.split(\"=\", 1)\n", |
| 100 |
" creds[key.strip()] = value.strip()\n", |
| 101 |
" return creds\n", |
| 102 |
"\n", |
| 103 |
"\n", |
| 104 |
"def get_token(credential_file_path):\n", |
| 105 |
" \"\"\"Use OFFLINE_TOKEN to fetch a short-lived access token.\"\"\"\n", |
| 106 |
" creds = load_credentials(Path(credential_file_path))\n", |
| 107 |
"\n", |
| 108 |
" OFFLINE_TOKEN = creds.get(\"OFFLINE_TOKEN\")\n", |
| 109 |
" CLIENT_ID = creds.get(\"CLIENT_ID\")\n", |
| 110 |
" CLIENT_SECRET = creds.get(\"CLIENT_SECRET\")\n", |
| 111 |
"\n", |
| 112 |
" if not all([OFFLINE_TOKEN, CLIENT_ID, CLIENT_SECRET]):\n", |
| 113 |
" raise ValueError(\"Missing OFFLINE_TOKEN, CLIENT_ID, or CLIENT_SECRET in credentials file\")\n", |
| 114 |
"\n", |
| 115 |
" url = \"https://iam.maap.eo.esa.int/realms/esa-maap/protocol/openid-connect/token\"\n", |
| 116 |
" data = {\n", |
| 117 |
" \"client_id\": CLIENT_ID,\n", |
| 118 |
" \"client_secret\": CLIENT_SECRET,\n", |
| 119 |
" \"grant_type\": \"refresh_token\",\n", |
| 120 |
" \"refresh_token\": OFFLINE_TOKEN,\n", |
| 121 |
" \"scope\": \"offline_access openid\"\n", |
| 122 |
" }\n", |
| 123 |
"\n", |
| 124 |
" response = requests.post(url, data=data)\n", |
| 125 |
" response.raise_for_status()\n", |
| 126 |
"\n", |
| 127 |
" response_json = response.json()\n", |
| 128 |
" access_token = response_json.get('access_token')\n", |
| 129 |
"\n", |
| 130 |
" if not access_token:\n", |
| 131 |
" raise RuntimeError(\"Failed to retrieve access token from IAM response\")\n", |
| 132 |
"\n", |
| 133 |
" return access_token" |
| 134 |
] |
| 135 |
}, |
| 136 |
{ |
| 137 |
"cell_type": "markdown", |
| 138 |
"id": "c0ca04f1-80f0-4e37-a2b7-b1d9504c93f2", |
| 139 |
"metadata": {}, |
| 140 |
"source": [ |
| 141 |
"# Access product raster and process on the fly" |
| 142 |
] |
| 143 |
}, |
| 144 |
{ |
| 145 |
"cell_type": "code", |
| 146 |
"execution_count": null, |
| 147 |
"id": "8588d626-f930-483c-b3a1-53dd336baca7", |
| 148 |
"metadata": {}, |
| 149 |
"outputs": [], |
| 150 |
"source": [ |
| 151 |
"# Authentication\n", |
| 152 |
"# token = get_token(\"./credentials.txt\")\n", |
| 153 |
"token = get_token(\"/data/credentials.txt\")" |
| 154 |
] |
| 155 |
}, |
| 156 |
{ |
| 157 |
"cell_type": "code", |
| 158 |
"execution_count": null, |
| 159 |
"id": "d2f31191-f893-4ecc-8a97-a4786add562c", |
| 160 |
"metadata": {}, |
| 161 |
"outputs": [], |
| 162 |
"source": [ |
| 163 |
"import rioxarray as riox\n", |
| 164 |
"import rasterio as rio\n", |
| 165 |
"\n", |
| 166 |
"# Datasets lazy loading\n", |
| 167 |
"with rio.Env(\n", |
| 168 |
" GDAL_HTTP_HEADERS=f\"Authorization: Bearer {token}\",\n", |
| 169 |
" GDAL_DISABLE_READDIR_ON_OPEN=\"EMPTY_DIR\",\n", |
| 170 |
" GDAL_NUM_THREADS=\"ALL_CPUS\",\n", |
| 171 |
"):\n", |
| 172 |
" da_abs = riox.open_rasterio(url_abs, chunks={})\n", |
| 173 |
" da_phase = riox.open_rasterio(url_phase, chunks={})" |
| 174 |
] |
| 175 |
}, |
| 176 |
{ |
| 177 |
"cell_type": "code", |
| 178 |
"execution_count": null, |
| 179 |
"id": "da19c647-cd43-4130-86b9-b5de0315b7a8", |
| 180 |
"metadata": {}, |
| 181 |
"outputs": [], |
| 182 |
"source": [ |
| 183 |
"import numpy as np\n", |
| 184 |
"import xarray as xr\n", |
| 185 |
"\n", |
| 186 |
"# Reconstruct SLC images\n", |
| 187 |
"slc = da_abs * np.exp(1j * da_phase)\n", |
| 188 |
"dims = (\"y\", \"x\")\n", |
| 189 |
"# Extract polSAR channels and create a PolSARpro S matrix\n", |
| 190 |
"S = xr.Dataset(\n", |
| 191 |
" {\n", |
| 192 |
" \"hh\": (dims, slc[0].data),\n", |
| 193 |
" \"hv\": (dims, slc[1].data),\n", |
| 194 |
" \"vh\": (dims, slc[2].data),\n", |
| 195 |
" \"vv\": (dims, slc[3].data),\n", |
| 196 |
" },\n", |
| 197 |
" coords={\"y\": np.arange(slc.shape[1]), \"x\": np.arange(slc.shape[2])},\n", |
| 198 |
" attrs={\"poltype\": \"S\", \"description\": \"Scattering matrix\"},\n", |
| 199 |
").chunk(\"auto\")" |
| 200 |
] |
| 201 |
}, |
| 202 |
{ |
| 203 |
"cell_type": "code", |
| 204 |
"execution_count": null, |
| 205 |
"id": "252cf18e-8460-4079-82d8-090cadb9aa6f", |
| 206 |
"metadata": {}, |
| 207 |
"outputs": [], |
| 208 |
"source": [ |
| 209 |
"from polsarpro.decompositions import freeman\n", |
| 210 |
"from dask.diagnostics import ProgressBar\n", |
| 211 |
"import os\n", |
| 212 |
"\n", |
| 213 |
"out_file = \"/data/psp/res/freeman_biomass.nc\"\n", |
| 214 |
"if os.path.exists(out_file):\n", |
| 215 |
" os.remove(out_file)\n", |
| 216 |
"\n", |
| 217 |
"# HTTP warnings may be ignored\n", |
| 218 |
"with ProgressBar():\n", |
| 219 |
" freeman(S, [7, 7]).to_netcdf(out_file)" |
| 220 |
] |
| 221 |
}, |
| 222 |
{ |
| 223 |
"cell_type": "code", |
| 224 |
"execution_count": null, |
| 225 |
"id": "b61591c1-fdba-4bc2-82b5-a78676a28ddf", |
| 226 |
"metadata": {}, |
| 227 |
"outputs": [], |
| 228 |
"source": [ |
| 229 |
"out_file = \"/data/psp/res/freeman_biomass.nc\"\n", |
| 230 |
"out = xr.open_dataset(out_file)" |
| 231 |
] |
| 232 |
}, |
| 233 |
{ |
| 234 |
"cell_type": "code", |
| 235 |
"execution_count": null, |
| 236 |
"id": "ce17a80e-e3d3-4dc2-9ccd-400a4d4fcdd7", |
| 237 |
"metadata": {}, |
| 238 |
"outputs": [], |
| 239 |
"source": [ |
| 240 |
"out.volume.plot.imshow(vmax=1)" |
| 241 |
] |
| 242 |
} |
| 243 |
], |
| 244 |
"metadata": { |
| 245 |
"kernelspec": { |
| 246 |
"display_name": "psp", |
| 247 |
"language": "python", |
| 248 |
"name": "python3" |
| 249 |
}, |
| 250 |
"language_info": { |
| 251 |
"codemirror_mode": { |
| 252 |
"name": "ipython", |
| 253 |
"version": 3 |
| 254 |
}, |
| 255 |
"file_extension": ".py", |
| 256 |
"mimetype": "text/x-python", |
| 257 |
"name": "python", |
| 258 |
"nbconvert_exporter": "python", |
| 259 |
"pygments_lexer": "ipython3", |
| 260 |
"version": "3.12.12" |
| 261 |
} |
| 262 |
}, |
| 263 |
"nbformat": 4, |
| 264 |
"nbformat_minor": 5 |
| 265 |
} |