| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"id": "0056702d", |
| 6 |
"metadata": {}, |
| 7 |
"source": [ |
| 8 |
"# Getting started" |
| 9 |
] |
| 10 |
}, |
| 11 |
{ |
| 12 |
"cell_type": "markdown", |
| 13 |
"id": "053cf91e", |
| 14 |
"metadata": {}, |
| 15 |
"source": [ |
| 16 |
"This tutorial introduces some basic concepts required to use this new version of PolSARpro in python.\n", |
| 17 |
"\n", |
| 18 |
"It is important to note that the toolbox relies on Xarray. Here we only describe the basics required to run PolSARpro and refer the reader to https://docs.xarray.dev/en/stable/ for more details." |
| 19 |
] |
| 20 |
}, |
| 21 |
{ |
| 22 |
"cell_type": "markdown", |
| 23 |
"id": "1df4ba39", |
| 24 |
"metadata": {}, |
| 25 |
"source": [ |
| 26 |
"## Loading data" |
| 27 |
] |
| 28 |
}, |
| 29 |
{ |
| 30 |
"cell_type": "markdown", |
| 31 |
"id": "b2c7b602", |
| 32 |
"metadata": {}, |
| 33 |
"source": [ |
| 34 |
"First we need to import some data. The demo dataset (TODO: host the data somewhere) has been prepared using SNAP and exported in the NetCDF-BEAM. NetCDF is the format recommended by Xarray as they the same data model.\n", |
| 35 |
"\n", |
| 36 |
"Let's first open the data using the `open_netcdf_beam` function." |
| 37 |
] |
| 38 |
}, |
| 39 |
{ |
| 40 |
"cell_type": "code", |
| 41 |
"execution_count": null, |
| 42 |
"id": "7245ed40", |
| 43 |
"metadata": {}, |
| 44 |
"outputs": [], |
| 45 |
"source": [ |
| 46 |
"from polsarpro.io import open_netcdf_beam\n", |
| 47 |
"from pathlib import Path\n", |
| 48 |
"\n", |
| 49 |
"# change to your data path\n", |
| 50 |
"data_path = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n", |
| 51 |
"out_path = Path(\"/data/psp/res\")\n", |
| 52 |
"S = open_netcdf_beam(data_path)" |
| 53 |
] |
| 54 |
}, |
| 55 |
{ |
| 56 |
"cell_type": "markdown", |
| 57 |
"id": "2c3ed740", |
| 58 |
"metadata": {}, |
| 59 |
"source": [ |
| 60 |
"### Note\n", |
| 61 |
"\n", |
| 62 |
"Currently, this function is able to recognize and open the following data types: Sinclair matrix `S`, covariance matrices `C3`, `C4` and `C2` as well as coherency matrices `T3` and `T4`. Preparation of polarimetric data using SNAP is out of the scope of this tutorial and may be found here: https://step.esa.int/docs/tutorials/S1TBX%20Polarimetry%20with%20Radarsat-2%20Tutorial_v2.pdf \n", |
| 63 |
"\n", |
| 64 |
"Once the data has been prepared in SNAP, it should be exported in NetCDF-BEAM format from the `File > Export` menu to be opened in PolSARpro. " |
| 65 |
] |
| 66 |
}, |
| 67 |
{ |
| 68 |
"cell_type": "markdown", |
| 69 |
"id": "80d749ae", |
| 70 |
"metadata": {}, |
| 71 |
"source": [ |
| 72 |
"## Inspect data" |
| 73 |
] |
| 74 |
}, |
| 75 |
{ |
| 76 |
"cell_type": "markdown", |
| 77 |
"id": "8b703e35", |
| 78 |
"metadata": {}, |
| 79 |
"source": [ |
| 80 |
"The variable `S` has been recognized by the reader as a 2x2 Sinclair matrix. It has been imported in a data structure called `xarray.Dataset`. \n", |
| 81 |
"In a jupyter notebook it may be inspected as:" |
| 82 |
] |
| 83 |
}, |
| 84 |
{ |
| 85 |
"cell_type": "code", |
| 86 |
"execution_count": null, |
| 87 |
"id": "2089c525", |
| 88 |
"metadata": {}, |
| 89 |
"outputs": [], |
| 90 |
"source": [ |
| 91 |
"print(S)" |
| 92 |
] |
| 93 |
}, |
| 94 |
{ |
| 95 |
"cell_type": "markdown", |
| 96 |
"id": "c7a5ff66", |
| 97 |
"metadata": {}, |
| 98 |
"source": [ |
| 99 |
"Each polarimetric channel is represented by a data variable and may be accessed using the following dot notation:" |
| 100 |
] |
| 101 |
}, |
| 102 |
{ |
| 103 |
"cell_type": "code", |
| 104 |
"execution_count": null, |
| 105 |
"id": "067e8d24", |
| 106 |
"metadata": {}, |
| 107 |
"outputs": [], |
| 108 |
"source": [ |
| 109 |
"# Access to the hh polarization channel\n", |
| 110 |
"print(S.hh)" |
| 111 |
] |
| 112 |
}, |
| 113 |
{ |
| 114 |
"cell_type": "markdown", |
| 115 |
"id": "834456d5", |
| 116 |
"metadata": {}, |
| 117 |
"source": [ |
| 118 |
"## Convert S to T3" |
| 119 |
] |
| 120 |
}, |
| 121 |
{ |
| 122 |
"cell_type": "markdown", |
| 123 |
"id": "058017ee", |
| 124 |
"metadata": {}, |
| 125 |
"source": [ |
| 126 |
"Each variable is a dask array divided into smaller chunks, enabling parallel processing.\n", |
| 127 |
"Please note that the data is not loaded into memory at this point. This is called lazy loading and allows to execute processing only when the result is required.\n", |
| 128 |
"\n", |
| 129 |
"More information on lazy operations may be found here:https://docs.xarray.dev/en/latest/internals/internal-design.html#lazy-indexing-classes\n", |
| 130 |
"\n", |
| 131 |
"To convert the S matrix to other types, we may use utility functions:" |
| 132 |
] |
| 133 |
}, |
| 134 |
{ |
| 135 |
"cell_type": "code", |
| 136 |
"execution_count": null, |
| 137 |
"id": "ced6ed3a", |
| 138 |
"metadata": {}, |
| 139 |
"outputs": [], |
| 140 |
"source": [ |
| 141 |
"from polsarpro.util import S_to_T3\n", |
| 142 |
"\n", |
| 143 |
"T3 = S_to_T3(S)" |
| 144 |
] |
| 145 |
}, |
| 146 |
{ |
| 147 |
"cell_type": "markdown", |
| 148 |
"id": "560123e5", |
| 149 |
"metadata": {}, |
| 150 |
"source": [ |
| 151 |
"Let's now look at this new variable:" |
| 152 |
] |
| 153 |
}, |
| 154 |
{ |
| 155 |
"cell_type": "code", |
| 156 |
"execution_count": null, |
| 157 |
"id": "efd30af2", |
| 158 |
"metadata": {}, |
| 159 |
"outputs": [], |
| 160 |
"source": [ |
| 161 |
"print(T3)" |
| 162 |
] |
| 163 |
}, |
| 164 |
{ |
| 165 |
"cell_type": "markdown", |
| 166 |
"id": "d842b60b", |
| 167 |
"metadata": {}, |
| 168 |
"source": [ |
| 169 |
"The output has been automatically converted to a new polarimetric type `T3` representing the coherency matrix. Now the elements of the matrix are accessed as:" |
| 170 |
] |
| 171 |
}, |
| 172 |
{ |
| 173 |
"cell_type": "code", |
| 174 |
"execution_count": null, |
| 175 |
"id": "d743b0cd", |
| 176 |
"metadata": {}, |
| 177 |
"outputs": [], |
| 178 |
"source": [ |
| 179 |
"print(T3.m12)" |
| 180 |
] |
| 181 |
}, |
| 182 |
{ |
| 183 |
"cell_type": "markdown", |
| 184 |
"id": "a5268e0e", |
| 185 |
"metadata": {}, |
| 186 |
"source": [ |
| 187 |
"For storage optimization, PolSARpro takes advantage of the Hermitian structure of the matrix and stores only diagonal and upper elements." |
| 188 |
] |
| 189 |
}, |
| 190 |
{ |
| 191 |
"cell_type": "markdown", |
| 192 |
"id": "f2a36cc4", |
| 193 |
"metadata": {}, |
| 194 |
"source": [ |
| 195 |
"To ensure its algorithms are applied to valid polarimetric types, PolSARpro looks for a `poltype` \"attribute\" -- which is a kind of metadata attached to the dataset, and may be accessed as:" |
| 196 |
] |
| 197 |
}, |
| 198 |
{ |
| 199 |
"cell_type": "code", |
| 200 |
"execution_count": null, |
| 201 |
"id": "604b972d", |
| 202 |
"metadata": {}, |
| 203 |
"outputs": [], |
| 204 |
"source": [ |
| 205 |
"T3.poltype" |
| 206 |
] |
| 207 |
}, |
| 208 |
{ |
| 209 |
"cell_type": "markdown", |
| 210 |
"id": "ed33aea3", |
| 211 |
"metadata": {}, |
| 212 |
"source": [ |
| 213 |
"A human readable description may also be accessed as:" |
| 214 |
] |
| 215 |
}, |
| 216 |
{ |
| 217 |
"cell_type": "code", |
| 218 |
"execution_count": null, |
| 219 |
"id": "eb698709", |
| 220 |
"metadata": {}, |
| 221 |
"outputs": [], |
| 222 |
"source": [ |
| 223 |
"T3.description" |
| 224 |
] |
| 225 |
}, |
| 226 |
{ |
| 227 |
"cell_type": "markdown", |
| 228 |
"id": "7145a4db", |
| 229 |
"metadata": {}, |
| 230 |
"source": [ |
| 231 |
"## Compute the results" |
| 232 |
] |
| 233 |
}, |
| 234 |
{ |
| 235 |
"cell_type": "markdown", |
| 236 |
"id": "7ec4664d", |
| 237 |
"metadata": {}, |
| 238 |
"source": [ |
| 239 |
"To compute the actual result and load the data into memory, we may simply use:" |
| 240 |
] |
| 241 |
}, |
| 242 |
{ |
| 243 |
"cell_type": "code", |
| 244 |
"execution_count": null, |
| 245 |
"id": "976d239a", |
| 246 |
"metadata": {}, |
| 247 |
"outputs": [], |
| 248 |
"source": [ |
| 249 |
"T3 = T3.compute()\n", |
| 250 |
"print(T3)" |
| 251 |
] |
| 252 |
}, |
| 253 |
{ |
| 254 |
"cell_type": "markdown", |
| 255 |
"id": "05fe5ccd", |
| 256 |
"metadata": {}, |
| 257 |
"source": [ |
| 258 |
"## Crop, convert and save matrices\n", |
| 259 |
"\n", |
| 260 |
"Now, each variable is a numpy array stored in memory. \n", |
| 261 |
"\n", |
| 262 |
"Lazy loading allows to define complex processing pipelines without storing intermediate variables in memory. It is even possible to write the result to disk without storing the whole data in memory. For instance one may define:" |
| 263 |
] |
| 264 |
}, |
| 265 |
{ |
| 266 |
"cell_type": "code", |
| 267 |
"execution_count": null, |
| 268 |
"id": "a3b417f9", |
| 269 |
"metadata": {}, |
| 270 |
"outputs": [], |
| 271 |
"source": [ |
| 272 |
"from polsarpro.util import boxcar\n", |
| 273 |
"from polsarpro.io import polmat_to_netcdf\n", |
| 274 |
"\n", |
| 275 |
"# crop the data using the isel accessor, re-chunk to avoid errors when writing zarr\n", |
| 276 |
"S_crop = S.isel(y=slice(5000, 12000)).chunk(\"auto\")\n", |
| 277 |
"T3 = S_to_T3(S_crop)\n", |
| 278 |
"\n", |
| 279 |
"# save to a file with a custom function \n", |
| 280 |
"# that preserves chunks and handles complex valued matrices\n", |
| 281 |
"polmat_to_netcdf(boxcar(T3, 5, 5), out_path / \"T3_crop_box_5x5.nc\")" |
| 282 |
] |
| 283 |
}, |
| 284 |
{ |
| 285 |
"cell_type": "markdown", |
| 286 |
"id": "50a569e8", |
| 287 |
"metadata": {}, |
| 288 |
"source": [ |
| 289 |
"## Apply the H/A/Alpha decomposition" |
| 290 |
] |
| 291 |
}, |
| 292 |
{ |
| 293 |
"cell_type": "markdown", |
| 294 |
"id": "a9c1f15c", |
| 295 |
"metadata": {}, |
| 296 |
"source": [ |
| 297 |
"Now let's apply the H/A/Alpha decomposition to our S matrix and\n", |
| 298 |
"write the result to a NetCDF file:" |
| 299 |
] |
| 300 |
}, |
| 301 |
{ |
| 302 |
"cell_type": "code", |
| 303 |
"execution_count": null, |
| 304 |
"id": "62f28093", |
| 305 |
"metadata": {}, |
| 306 |
"outputs": [], |
| 307 |
"source": [ |
| 308 |
"from polsarpro.decompositions import h_a_alpha\n", |
| 309 |
"from dask.diagnostics import ProgressBar\n", |
| 310 |
"\n", |
| 311 |
"# optional progress bar to monitor the process\n", |
| 312 |
"with ProgressBar():\n", |
| 313 |
" h_a_alpha(S_crop, boxcar_size=[5, 5]).to_netcdf(out_path / \"h_a_alpha.nc\")" |
| 314 |
] |
| 315 |
}, |
| 316 |
{ |
| 317 |
"cell_type": "code", |
| 318 |
"execution_count": null, |
| 319 |
"id": "da5f1d6e", |
| 320 |
"metadata": {}, |
| 321 |
"outputs": [], |
| 322 |
"source": [ |
| 323 |
"import xarray as xr\n", |
| 324 |
"# Let's open the file we have written\n", |
| 325 |
"res = xr.open_dataset(out_path / \"h_a_alpha.nc\")\n", |
| 326 |
"print(res)" |
| 327 |
] |
| 328 |
}, |
| 329 |
{ |
| 330 |
"cell_type": "markdown", |
| 331 |
"id": "cf357145", |
| 332 |
"metadata": {}, |
| 333 |
"source": [ |
| 334 |
"As we can see, the new dataset has a specific `poltype` and stores the output parameters into labeled variables. As previously they can be accessed via the dot notation e.g. `res.alpha`.\n", |
| 335 |
"We can now plot the outputs either using matplotlib or xarray plotting capabilities:" |
| 336 |
] |
| 337 |
}, |
| 338 |
{ |
| 339 |
"cell_type": "markdown", |
| 340 |
"id": "064da891", |
| 341 |
"metadata": {}, |
| 342 |
"source": [ |
| 343 |
"## Visualize the results" |
| 344 |
] |
| 345 |
}, |
| 346 |
{ |
| 347 |
"cell_type": "code", |
| 348 |
"execution_count": null, |
| 349 |
"id": "1b33770f", |
| 350 |
"metadata": {}, |
| 351 |
"outputs": [], |
| 352 |
"source": [ |
| 353 |
"res.alpha.plot.imshow()" |
| 354 |
] |
| 355 |
}, |
| 356 |
{ |
| 357 |
"cell_type": "code", |
| 358 |
"execution_count": null, |
| 359 |
"id": "2a820f14", |
| 360 |
"metadata": {}, |
| 361 |
"outputs": [], |
| 362 |
"source": [ |
| 363 |
"res.entropy.plot.imshow()" |
| 364 |
] |
| 365 |
}, |
| 366 |
{ |
| 367 |
"cell_type": "code", |
| 368 |
"execution_count": null, |
| 369 |
"id": "4f872fba", |
| 370 |
"metadata": {}, |
| 371 |
"outputs": [], |
| 372 |
"source": [ |
| 373 |
"res.anisotropy.plot.imshow()" |
| 374 |
] |
| 375 |
}, |
| 376 |
{ |
| 377 |
"cell_type": "markdown", |
| 378 |
"id": "da4efbd7", |
| 379 |
"metadata": {}, |
| 380 |
"source": [ |
| 381 |
"Additionally, we can plot points in the H/Alpha plane and save the result as an image:" |
| 382 |
] |
| 383 |
}, |
| 384 |
{ |
| 385 |
"cell_type": "code", |
| 386 |
"execution_count": null, |
| 387 |
"id": "5d0f5089", |
| 388 |
"metadata": {}, |
| 389 |
"outputs": [], |
| 390 |
"source": [ |
| 391 |
"from polsarpro.util import plot_h_alpha_plane\n", |
| 392 |
"ax, fig = plot_h_alpha_plane(res, bins=400, min_pts=5)\n", |
| 393 |
"fig.savefig(out_path / \"h_a_alpha_plane.png\")" |
| 394 |
] |
| 395 |
} |
| 396 |
], |
| 397 |
"metadata": { |
| 398 |
"kernelspec": { |
| 399 |
"display_name": "psp", |
| 400 |
"language": "python", |
| 401 |
"name": "python3" |
| 402 |
}, |
| 403 |
"language_info": { |
| 404 |
"codemirror_mode": { |
| 405 |
"name": "ipython", |
| 406 |
"version": 3 |
| 407 |
}, |
| 408 |
"file_extension": ".py", |
| 409 |
"mimetype": "text/x-python", |
| 410 |
"name": "python", |
| 411 |
"nbconvert_exporter": "python", |
| 412 |
"pygments_lexer": "ipython3", |
| 413 |
"version": "3.12.11" |
| 414 |
} |
| 415 |
}, |
| 416 |
"nbformat": 4, |
| 417 |
"nbformat_minor": 5 |
| 418 |
} |