Debian Codegraph @

notebooks/pwf-tutorial.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Polarimetric Whitening Filter (PWF)\n",
8 "\n",
9 "This notebook shows how to apply the PWF on the S matrix."
10 ]
11 },
12 {
13 "cell_type": "code",
14 "execution_count": null,
15 "metadata": {},
16 "outputs": [],
17 "source": [
18 "%load_ext autoreload\n",
19 "%autoreload 2\n",
20 "\n",
21 "import os\n",
22 "\n",
23 "import numpy as np\n",
24 "from pathlib import Path\n",
25 "import xarray as xr\n",
26 "from polsarpro.io import open_netcdf_beam\n",
27 "from polsarpro.speckle_filters import pwf\n",
28 "\n",
29 "# optional import for progress bar\n",
30 "from dask.diagnostics import ProgressBar\n",
31 "\n",
32 "# change to your data paths\n",
33 "# original dataset\n",
34 "input_alos_data = Path(\"/data/psp/test_files/SAN_FRANCISCO_ALOS1_slc.nc\")\n",
35 "\n",
36 "# output files from C\n",
37 "output_dir = Path(\"/data/psp/res/\")"
38 ]
39 },
40 {
41 "cell_type": "markdown",
42 "metadata": {},
43 "source": [
44 "## Load ALOS data and C outputs"
45 ]
46 },
47 {
48 "cell_type": "code",
49 "execution_count": null,
50 "metadata": {},
51 "outputs": [],
52 "source": [
53 "S = open_netcdf_beam(input_alos_data)"
54 ]
55 },
56 {
57 "cell_type": "markdown",
58 "metadata": {},
59 "source": [
60 "## Apply the filter\n",
61 "**Important note:** \n",
62 "this version differs from the C implementation since it allows to smooth the covariance matrix using the `test_window_size` prior to multiplying it with the inverse covariance estimated using the `train_window size` parameter. Using values `[1, 1]` for the test window is equivalent to the original C PolSARpro implementation."
63 ]
64 },
65 {
66 "cell_type": "code",
67 "execution_count": null,
68 "metadata": {},
69 "outputs": [],
70 "source": [
71 "file_out = output_dir / \"pwf.nc\"\n",
72 "# netcdf writer cannot overwrite\n",
73 "if os.path.isfile(file_out):\n",
74 " os.remove(file_out)\n",
75 "\n",
76 "with ProgressBar():\n",
77 " pwf(S, train_window_size=[15, 15], test_window_size=[3, 3]).to_netcdf(file_out)"
78 ]
79 },
80 {
81 "cell_type": "markdown",
82 "metadata": {},
83 "source": [
84 "## Open the output file"
85 ]
86 },
87 {
88 "cell_type": "code",
89 "execution_count": null,
90 "metadata": {},
91 "outputs": [],
92 "source": [
93 "res = xr.open_dataset(file_out)"
94 ]
95 },
96 {
97 "cell_type": "markdown",
98 "metadata": {},
99 "source": [
100 "## Display the result"
101 ]
102 },
103 {
104 "cell_type": "code",
105 "execution_count": null,
106 "metadata": {},
107 "outputs": [],
108 "source": [
109 "# clip the intensity to handle the high dynamic range\n",
110 "res.pwf.plot.imshow(figsize=(7,9), vmax=10)"
111 ]
112 }
113 ],
114 "metadata": {
115 "kernelspec": {
116 "display_name": "psp",
117 "language": "python",
118 "name": "python3"
119 },
120 "language_info": {
121 "codemirror_mode": {
122 "name": "ipython",
123 "version": 3
124 },
125 "file_extension": ".py",
126 "mimetype": "text/x-python",
127 "name": "python",
128 "nbconvert_exporter": "python",
129 "pygments_lexer": "ipython3",
130 "version": "3.12.12"
131 }
132 },
133 "nbformat": 4,
134 "nbformat_minor": 2
135 }