Debian Codegraph @

notebooks/cameron-tutorial.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Cameron decomposition"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "This notebook shows how to apply the Cameron 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 cameron\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)"
63 ]
64 },
65 {
66 "cell_type": "markdown",
67 "metadata": {},
68 "source": [
69 "## Apply the decomposition\n",
70 "\n",
71 "Let's apply the decomposition and write the result to a NetCDF file.\n",
72 "Optionally we can use a progress bar to monitor the progress of the computation."
73 ]
74 },
75 {
76 "cell_type": "code",
77 "execution_count": null,
78 "metadata": {},
79 "outputs": [],
80 "source": [
81 "# change to the name of your liking\n",
82 "file_out = output_dir / \"psp_freeman.nc\"\n",
83 "\n",
84 "# netcdf writer cannot overwrite\n",
85 "if os.path.isfile(file_out):\n",
86 " os.remove(file_out)\n",
87 "\n",
88 "with ProgressBar():\n",
89 " cameron(S).to_netcdf(file_out)"
90 ]
91 },
92 {
93 "cell_type": "markdown",
94 "metadata": {},
95 "source": [
96 "## Display outputs"
97 ]
98 },
99 {
100 "cell_type": "markdown",
101 "metadata": {},
102 "source": [
103 "We open the previously saved dataset:"
104 ]
105 },
106 {
107 "cell_type": "code",
108 "execution_count": null,
109 "metadata": {},
110 "outputs": [],
111 "source": [
112 "res = xr.open_dataset(file_out)"
113 ]
114 },
115 {
116 "cell_type": "markdown",
117 "metadata": {},
118 "source": [
119 "Then we may plot the outputs:"
120 ]
121 },
122 {
123 "cell_type": "code",
124 "execution_count": null,
125 "metadata": {},
126 "outputs": [],
127 "source": [
128 "# the image is cropped for better visualization\n",
129 "res.cameron[8000:12000,200:1100].plot.imshow(cmap=\"Dark2\")"
130 ]
131 }
132 ],
133 "metadata": {
134 "kernelspec": {
135 "display_name": "psp",
136 "language": "python",
137 "name": "python3"
138 },
139 "language_info": {
140 "codemirror_mode": {
141 "name": "ipython",
142 "version": 3
143 },
144 "file_extension": ".py",
145 "mimetype": "text/x-python",
146 "name": "python",
147 "nbconvert_exporter": "python",
148 "pygments_lexer": "ipython3",
149 "version": "3.12.12"
150 }
151 },
152 "nbformat": 4,
153 "nbformat_minor": 2
154 }