| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# H/A/Alpha decomposition" |
| 8 |
] |
| 9 |
}, |
| 10 |
{ |
| 11 |
"cell_type": "markdown", |
| 12 |
"metadata": {}, |
| 13 |
"source": [ |
| 14 |
"This notebook shows how to apply the H/A/Alpha decomposition to a PolSARpro NetCDF dataset." |
| 15 |
] |
| 16 |
}, |
| 17 |
{ |
| 18 |
"cell_type": "markdown", |
| 19 |
"metadata": {}, |
| 20 |
"source": [ |
| 21 |
"## Import packages and set directories" |
| 22 |
] |
| 23 |
}, |
| 24 |
{ |
| 25 |
"cell_type": "code", |
| 26 |
"execution_count": null, |
| 27 |
"metadata": {}, |
| 28 |
"outputs": [], |
| 29 |
"source": [ |
| 30 |
"import os \n", |
| 31 |
"from pathlib import Path\n", |
| 32 |
"import xarray as xr\n", |
| 33 |
"from polsarpro.io import open_netcdf_beam\n", |
| 34 |
"from polsarpro.decompositions import h_a_alpha\n", |
| 35 |
"from polsarpro.util import plot_h_alpha_plane\n", |
| 36 |
"\n", |
| 37 |
"# optional import for progress bar\n", |
| 38 |
"from dask.diagnostics import ProgressBar\n", |
| 39 |
"\n", |
| 40 |
"# change to your data paths\n", |
| 41 |
"# original dataset\n", |
| 42 |
"input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n", |
| 43 |
"output_dir = Path(\"/data/psp/res\")\n" |
| 44 |
] |
| 45 |
}, |
| 46 |
{ |
| 47 |
"cell_type": "markdown", |
| 48 |
"metadata": {}, |
| 49 |
"source": [ |
| 50 |
"## Load data\n", |
| 51 |
"\n", |
| 52 |
"We load the SNAP NetCDF-BEAM dataset using the `open_netcdf_beam` function. \n", |
| 53 |
"To obtain such a dataset, please refer to the \"Getting Started\" tutorial or the `quickstart-tutorial.ipynb` notebook." |
| 54 |
] |
| 55 |
}, |
| 56 |
{ |
| 57 |
"cell_type": "code", |
| 58 |
"execution_count": null, |
| 59 |
"metadata": {}, |
| 60 |
"outputs": [], |
| 61 |
"source": [ |
| 62 |
"# uncomment to test on S matrix made with SNAP\n", |
| 63 |
"S = open_netcdf_beam(input_alos_data)\n", |
| 64 |
"\n", |
| 65 |
"# Variables to compute. A list can be found in the API reference documentation.\n", |
| 66 |
"out_names = (\"entropy\", \"anisotropy\", \"alpha\", \"alphas\")" |
| 67 |
] |
| 68 |
}, |
| 69 |
{ |
| 70 |
"cell_type": "markdown", |
| 71 |
"metadata": {}, |
| 72 |
"source": [ |
| 73 |
"## Apply the decomposition\n", |
| 74 |
"\n", |
| 75 |
"Let's apply the decomposition and write the result to a NetCDF file.\n", |
| 76 |
"Optionally we can use a progress bar to monitor the progress of the computation." |
| 77 |
] |
| 78 |
}, |
| 79 |
{ |
| 80 |
"cell_type": "code", |
| 81 |
"execution_count": null, |
| 82 |
"metadata": {}, |
| 83 |
"outputs": [], |
| 84 |
"source": [ |
| 85 |
"# change to the name of your liking\n", |
| 86 |
"file_out = output_dir / \"psp_h_a_alpha.nc\"\n", |
| 87 |
"\n", |
| 88 |
"# netcdf writer cannot overwrite\n", |
| 89 |
"if os.path.isfile(file_out):\n", |
| 90 |
" os.remove(file_out)\n", |
| 91 |
"\n", |
| 92 |
"with ProgressBar():\n", |
| 93 |
" h_a_alpha(S, boxcar_size=[7, 7], flags=out_names).to_netcdf(file_out)" |
| 94 |
] |
| 95 |
}, |
| 96 |
{ |
| 97 |
"cell_type": "markdown", |
| 98 |
"metadata": {}, |
| 99 |
"source": [ |
| 100 |
"## Display outputs" |
| 101 |
] |
| 102 |
}, |
| 103 |
{ |
| 104 |
"cell_type": "markdown", |
| 105 |
"metadata": {}, |
| 106 |
"source": [ |
| 107 |
"We open the previously saved dataset:" |
| 108 |
] |
| 109 |
}, |
| 110 |
{ |
| 111 |
"cell_type": "code", |
| 112 |
"execution_count": null, |
| 113 |
"metadata": {}, |
| 114 |
"outputs": [], |
| 115 |
"source": [ |
| 116 |
"res = xr.open_dataset(file_out)" |
| 117 |
] |
| 118 |
}, |
| 119 |
{ |
| 120 |
"cell_type": "markdown", |
| 121 |
"metadata": {}, |
| 122 |
"source": [ |
| 123 |
"Then we may plot individual parameters:" |
| 124 |
] |
| 125 |
}, |
| 126 |
{ |
| 127 |
"cell_type": "code", |
| 128 |
"execution_count": null, |
| 129 |
"metadata": {}, |
| 130 |
"outputs": [], |
| 131 |
"source": [ |
| 132 |
"res.entropy.plot.imshow()" |
| 133 |
] |
| 134 |
}, |
| 135 |
{ |
| 136 |
"cell_type": "code", |
| 137 |
"execution_count": null, |
| 138 |
"metadata": {}, |
| 139 |
"outputs": [], |
| 140 |
"source": [ |
| 141 |
"res.anisotropy.plot.imshow()" |
| 142 |
] |
| 143 |
}, |
| 144 |
{ |
| 145 |
"cell_type": "code", |
| 146 |
"execution_count": null, |
| 147 |
"metadata": {}, |
| 148 |
"outputs": [], |
| 149 |
"source": [ |
| 150 |
"res.alpha.plot.imshow()" |
| 151 |
] |
| 152 |
}, |
| 153 |
{ |
| 154 |
"cell_type": "markdown", |
| 155 |
"metadata": {}, |
| 156 |
"source": [ |
| 157 |
"Let's plot the `alpha_i` angles corresponding to the `lambda_i` eigenvalues.\n", |
| 158 |
"We may treat them as an RGB image by normalizing the data range between 0 and 1:" |
| 159 |
] |
| 160 |
}, |
| 161 |
{ |
| 162 |
"cell_type": "code", |
| 163 |
"execution_count": null, |
| 164 |
"metadata": {}, |
| 165 |
"outputs": [], |
| 166 |
"source": [ |
| 167 |
"(res.alphas/90).plot.imshow()" |
| 168 |
] |
| 169 |
}, |
| 170 |
{ |
| 171 |
"cell_type": "markdown", |
| 172 |
"metadata": {}, |
| 173 |
"source": [ |
| 174 |
"or we may plot only one of them using numpy-like indexing:" |
| 175 |
] |
| 176 |
}, |
| 177 |
{ |
| 178 |
"cell_type": "code", |
| 179 |
"execution_count": null, |
| 180 |
"metadata": {}, |
| 181 |
"outputs": [], |
| 182 |
"source": [ |
| 183 |
"res.alphas[..., 0].plot.imshow()" |
| 184 |
] |
| 185 |
}, |
| 186 |
{ |
| 187 |
"cell_type": "markdown", |
| 188 |
"metadata": {}, |
| 189 |
"source": [ |
| 190 |
"Finally we can plot the data points in the H/Alpha plane and save the result to an image:" |
| 191 |
] |
| 192 |
}, |
| 193 |
{ |
| 194 |
"cell_type": "code", |
| 195 |
"execution_count": null, |
| 196 |
"metadata": {}, |
| 197 |
"outputs": [], |
| 198 |
"source": [ |
| 199 |
"ax, fig = plot_h_alpha_plane(res, bins=500, min_pts=2)\n", |
| 200 |
"fig.savefig(output_dir / \"h_a_alpha_plane.png\")" |
| 201 |
] |
| 202 |
} |
| 203 |
], |
| 204 |
"metadata": { |
| 205 |
"kernelspec": { |
| 206 |
"display_name": "psp", |
| 207 |
"language": "python", |
| 208 |
"name": "python3" |
| 209 |
}, |
| 210 |
"language_info": { |
| 211 |
"codemirror_mode": { |
| 212 |
"name": "ipython", |
| 213 |
"version": 3 |
| 214 |
}, |
| 215 |
"file_extension": ".py", |
| 216 |
"mimetype": "text/x-python", |
| 217 |
"name": "python", |
| 218 |
"nbconvert_exporter": "python", |
| 219 |
"pygments_lexer": "ipython3", |
| 220 |
"version": "3.12.11" |
| 221 |
} |
| 222 |
}, |
| 223 |
"nbformat": 4, |
| 224 |
"nbformat_minor": 2 |
| 225 |
} |