Debian Codegraph @

notebooks/refined-lee-tutorial.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Polarimetric Refined Lee speckle filter\n",
8 "\n",
9 "This tutorial shows how to:\n",
10 "- Open the ALOS dataset and convert the S matrix to T3 coherency matrix\n",
11 "- Pre-process the data by applying a 4-look decimation in azimuth\n",
12 "- Apply the Refined Lee speckle filter to the 3x3 coherency matrix\n",
13 "- Write the results to a file\n",
14 "- Visualize the results as an RGB image\n"
15 ]
16 },
17 {
18 "cell_type": "code",
19 "execution_count": null,
20 "metadata": {},
21 "outputs": [],
22 "source": [
23 "%load_ext autoreload\n",
24 "%autoreload 2\n",
25 "\n",
26 "import os\n",
27 "\n",
28 "import xarray as xr\n",
29 "\n",
30 "from pathlib import Path\n",
31 "from polsarpro.io import open_netcdf_beam, polmat_to_netcdf\n",
32 "from polsarpro.speckle_filters import refined_lee\n",
33 "from polsarpro.util import S_to_T3, multilook, pauli_rgb\n",
34 "\n",
35 "# optional import for progress bar\n",
36 "from dask.diagnostics import ProgressBar\n",
37 "\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 "\n",
43 "# output files from C\n",
44 "output_test_dir = Path(\"/data/psp/res/\")"
45 ]
46 },
47 {
48 "cell_type": "markdown",
49 "metadata": {},
50 "source": [
51 "## Load ALOS data"
52 ]
53 },
54 {
55 "cell_type": "code",
56 "execution_count": null,
57 "metadata": {},
58 "outputs": [],
59 "source": [
60 "S = open_netcdf_beam(input_alos_data)"
61 ]
62 },
63 {
64 "cell_type": "markdown",
65 "metadata": {},
66 "source": [
67 "## Apply the filter to T3 matrices"
68 ]
69 },
70 {
71 "cell_type": "code",
72 "execution_count": null,
73 "metadata": {},
74 "outputs": [],
75 "source": [
76 "if os.path.exists(output_test_dir / \"T3_refined_lee.nc\"):\n",
77 " os.remove(output_test_dir / \"T3_refined_lee.nc\")\n",
78 "\n",
79 "with ProgressBar():\n",
80 " # convert S to T3\n",
81 " T3 = S_to_T3(S)\n",
82 " # apply 4-look multilook along lines\n",
83 " T3 = multilook(T3, dim_az=4, dim_rg=1)\n",
84 " # apply the filter\n",
85 " T3_flt = refined_lee(T3, window_size=7, num_looks=4)\n",
86 " # use custom function that writes complex matrices and preserves chunks\n",
87 " polmat_to_netcdf(T3_flt, output_test_dir / f\"T3_refined_lee.nc\")\n",
88 " "
89 ]
90 },
91 {
92 "cell_type": "markdown",
93 "metadata": {},
94 "source": [
95 "## Open the file and visualize the results as an RBG image"
96 ]
97 },
98 {
99 "cell_type": "code",
100 "execution_count": null,
101 "metadata": {},
102 "outputs": [],
103 "source": [
104 "# open the file\n",
105 "T3_flt = open_netcdf_beam(output_test_dir / f\"T3_refined_lee.nc\")\n",
106 "\n",
107 "pauli_rgb(T3_flt).plot.imshow(figsize=(5, 7))"
108 ]
109 }
110 ],
111 "metadata": {
112 "kernelspec": {
113 "display_name": "psp",
114 "language": "python",
115 "name": "python3"
116 },
117 "language_info": {
118 "codemirror_mode": {
119 "name": "ipython",
120 "version": 3
121 },
122 "file_extension": ".py",
123 "mimetype": "text/x-python",
124 "name": "python",
125 "nbconvert_exporter": "python",
126 "pygments_lexer": "ipython3",
127 "version": "3.12.12"
128 }
129 },
130 "nbformat": 4,
131 "nbformat_minor": 2
132 }