Debian Codegraph @

notebooks/tsvm-tutorial.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# TSVM decomposition"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "This notebook shows how to apply the TSVM 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 tsvm\n",
35 "\n",
36 "# optional import for progress bar\n",
37 "from dask.diagnostics import ProgressBar\n",
38 "\n",
39 "# change to your data paths\n",
40 "# original dataset\n",
41 "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
42 "output_dir = Path(\"/data/psp/res\")\n"
43 ]
44 },
45 {
46 "cell_type": "markdown",
47 "metadata": {},
48 "source": [
49 "## Load data\n",
50 "\n",
51 "We load the SNAP NetCDF-BEAM dataset using the `open_netcdf_beam` function. \n",
52 "To obtain such a dataset, please refer to the \"Getting Started\" tutorial or the `quickstart-tutorial.ipynb` notebook."
53 ]
54 },
55 {
56 "cell_type": "code",
57 "execution_count": null,
58 "metadata": {},
59 "outputs": [],
60 "source": [
61 "# uncomment to test on S matrix made with SNAP\n",
62 "S = open_netcdf_beam(input_alos_data)\n",
63 "\n",
64 "# Variables to compute. A list can be found in the API reference documentation.\n",
65 "# For the sake of illustration we chose the default parameter and one of the per-eigenvalue outputs\n",
66 "flags = (\"alpha_phi_tau_psi\", \"alpha\")"
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_tsvm.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 " tsvm(S, boxcar_size=[7, 7], flags=flags).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 "import matplotlib.pyplot as plt\n",
133 "for var in res.data_vars:\n",
134 " plt.figure(figsize=(5, 5))\n",
135 " res[var].plot.imshow()"
136 ]
137 }
138 ],
139 "metadata": {
140 "kernelspec": {
141 "display_name": "psp",
142 "language": "python",
143 "name": "python3"
144 },
145 "language_info": {
146 "codemirror_mode": {
147 "name": "ipython",
148 "version": 3
149 },
150 "file_extension": ".py",
151 "mimetype": "text/x-python",
152 "name": "python",
153 "nbconvert_exporter": "python",
154 "pygments_lexer": "ipython3",
155 "version": "3.12.12"
156 }
157 },
158 "nbformat": 4,
159 "nbformat_minor": 2
160 }