Debian Codegraph @

notebooks/dev/test-dubois-surface-inversion-real-data-uavsar.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Dubois surface inversion evaluation on UAVSAR C3 data\n",
8 "\n",
9 "In this notebook we run the Dubois surface inversion on the UAVSAR C3 product using the provided incidence-angle stream. The radar frequency is derived from the NetCDF metadata."
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 "import shutil\n",
23 "from pathlib import Path\n",
24 "\n",
25 "import numpy as np\n",
26 "import xarray as xr\n",
27 "from dask.diagnostics import ProgressBar\n",
28 "\n",
29 "from polsarpro.dev.devtools import parse_psp_parameter_string\n",
30 "from polsarpro.dev.io import read_psp_bin\n",
31 "from polsarpro.dev.metrics import summarize_metrics, visualize_errors\n",
32 "from polsarpro.io import open_netcdf_beam\n",
33 "from polsarpro.physical_inversion import dubois_surface_inversion\n",
34 "\n",
35 "# change to your local C-PolSARpro install dir\n",
36 "c_psp_dir = \"/home/c_psp/Soft/bin/\"\n",
37 "os.environ[\"PATH\"] += os.pathsep + f\"{c_psp_dir}/data_process_sngl/\"\n",
38 "os.environ[\"PATH\"] += os.pathsep + f\"{c_psp_dir}/data_convert/\"\n",
39 "\n",
40 "product_id = \"winnip_09002_12060_004_120714_L090_CX_02\"\n",
41 "\n",
42 "input_uavsar_nc = Path(f\"/data/psp/test_files/{product_id}_ML5X5.nc\")\n",
43 "input_test_dir = Path(\"/data/psp/winnip_cpsp/C3\")\n",
44 "output_test_dir = Path(\"/data/psp/res/dubois_c3/\")\n",
45 "output_test_dir.mkdir(parents=True, exist_ok=True)\n",
46 "\n",
47 "incidence_angle_file = Path(f\"/data/psp/test_files/{product_id}.inc\")\n",
48 "incidence_angle_c_file = Path(\"/data/psp/winnip_cpsp/incidence_angle_ml5x5_rad.bin\")\n",
49 "file_out = Path(\"/data/psp/res/test_dubois_surface_inversion_uavsar.nc\")\n",
50 "\n",
51 "with xr.open_dataset(input_uavsar_nc) as raw_ds:\n",
52 " center_wavelength_cm = raw_ds[\"metadata\"].attrs[\"Annotation:CenterWavelength\"]\n",
53 "\n",
54 "# Dubois expects GHz; UAVSAR metadata stores the center wavelength in cm.\n",
55 "freq_ghz = 29.9792458 / float(center_wavelength_cm)\n",
56 "\n",
57 "# thresh1_db = -5.0\n",
58 "thresh1_db = 0\n",
59 "thresh2_db = 0\n",
60 "calibration_coeff = 1.0\n",
61 "calibration_flag = 1\n",
62 "angle_unit = 1 # C-PolSARpro: 0 = degrees, 1 = radians\n",
63 "\n",
64 "print(f\"Derived radar frequency: {freq_ghz:.6f} GHz\")\n"
65 ]
66 },
67 {
68 "cell_type": "markdown",
69 "metadata": {},
70 "source": [
71 "## Load UAVSAR data and incidence angle raster"
72 ]
73 },
74 {
75 "cell_type": "code",
76 "execution_count": null,
77 "metadata": {},
78 "outputs": [],
79 "source": [
80 "C3 = open_netcdf_beam(input_uavsar_nc)\n",
81 "row_dim, col_dim = C3.dims\n",
82 "\n",
83 "# use lines from .ann file (infer from auxiliary file)\n",
84 "# hgt.set_rows (pixels) = 3767 ; HGT Lines\n",
85 "# hgt.set_cols (pixels) = 11636 ; HGT Samples\n",
86 "nlines = 3767\n",
87 "ncols = 11636\n",
88 "\n",
89 "# The incidence-angle file is a little-endian float32 stream in radians.\n",
90 "# Read only the raster-sized prefix and reshape it to the UAVSAR grid.\n",
91 "incidence_angle_values = np.fromfile(\n",
92 " incidence_angle_file,\n",
93 " dtype=\"<f4\",\n",
94 " count=nlines * ncols,\n",
95 ")\n",
96 "\n",
97 "# reduce to the same dimensions as multilooked 5x5 image\n",
98 "incidence_angle_values[incidence_angle_values == -10000] = np.nan\n",
99 "incidence_angle = xr.DataArray(\n",
100 " incidence_angle_values.reshape(nlines, ncols),\n",
101 " dims=(row_dim, col_dim),\n",
102 " name=\"incidence_angle\",\n",
103 ").coarsen(y=5, x=5, boundary=\"trim\").mean()\n",
104 "\n",
105 "# Write the multilooked incidence angle stream for the C run.\n",
106 "if incidence_angle_c_file.exists():\n",
107 " incidence_angle_c_file.unlink()\n",
108 "incidence_angle.astype(np.float32).values.tofile(incidence_angle_c_file)"
109 ]
110 },
111 {
112 "cell_type": "markdown",
113 "metadata": {},
114 "source": [
115 "## Run the C version on the C3 input"
116 ]
117 },
118 {
119 "cell_type": "code",
120 "execution_count": null,
121 "metadata": {},
122 "outputs": [],
123 "source": [
124 "fnr, fnc = C3.sizes[row_dim], C3.sizes[col_dim]\n",
125 "\n",
126 "input_str = f\"\"\"id: {input_test_dir}\n",
127 "od: {output_test_dir}\n",
128 "iodf: C3\n",
129 "ang: {incidence_angle_c_file}\n",
130 "ofr: 0\n",
131 "ofc: 0\n",
132 "fnr: {fnr}\n",
133 "fnc: {fnc}\n",
134 "fr: {freq_ghz}\n",
135 "un: {angle_unit}\n",
136 "caf: {calibration_flag}\n",
137 "cac: {calibration_coeff}\n",
138 "th1: {thresh1_db}\n",
139 "th2: {thresh2_db}\n",
140 "errf: /tmp\"\"\"\n",
141 "\n",
142 "result = parse_psp_parameter_string(input_str)\n",
143 "os.system(f\"surface_inversion_dubois.exe {result}\")\n",
144 "\n",
145 "# CPSP does not create another config file in output dir\n",
146 "shutil.copy(input_test_dir / \"config.txt\", output_test_dir)\n"
147 ]
148 },
149 {
150 "cell_type": "markdown",
151 "metadata": {},
152 "source": [
153 "## Apply the surface inversion"
154 ]
155 },
156 {
157 "cell_type": "code",
158 "execution_count": null,
159 "metadata": {},
160 "outputs": [],
161 "source": [
162 "if file_out.exists():\n",
163 " file_out.unlink()\n",
164 "\n",
165 "with ProgressBar():\n",
166 " out_py = dubois_surface_inversion(\n",
167 " C3,\n",
168 " incidence_angle=incidence_angle,\n",
169 " freq_ghz=freq_ghz,\n",
170 " thresh1=thresh1_db,\n",
171 " thresh2=thresh2_db,\n",
172 " calibration_coeff=calibration_coeff,\n",
173 " ).to_netcdf(file_out)\n"
174 ]
175 },
176 {
177 "cell_type": "markdown",
178 "metadata": {},
179 "source": [
180 "## Inspect results"
181 ]
182 },
183 {
184 "cell_type": "code",
185 "execution_count": null,
186 "metadata": {},
187 "outputs": [],
188 "source": [
189 "out_py = xr.open_dataset(file_out)\n",
190 "out_py"
191 ]
192 },
193 {
194 "cell_type": "markdown",
195 "id": "0004916e",
196 "metadata": {},
197 "source": [
198 "## Numerical evaluation"
199 ]
200 },
201 {
202 "cell_type": "code",
203 "execution_count": null,
204 "id": "af669f5b",
205 "metadata": {},
206 "outputs": [],
207 "source": [
208 "# open python output (comment if using compute)\n",
209 "out_py = xr.open_dataset(file_out)\n",
210 "# open C-PolSARPro outputs\n",
211 "out_c = {}\n",
212 "out_names = (\n",
213 " \"dubois_er\",\n",
214 " \"dubois_mv\",\n",
215 " \"dubois_ks\",\n",
216 " \"dubois_mask_out\",\n",
217 " \"dubois_mask_in\",\n",
218 " \"dubois_mask_valid_in_out\",\n",
219 ")\n",
220 "for name in out_names:\n",
221 " file_name = output_test_dir / f\"{name}.bin\"\n",
222 " out_c[name] = read_psp_bin(file_name).T\n",
223 "\n",
224 "out_py = out_py.transpose(\"x\", \"y\")\n",
225 "\n",
226 "df = summarize_metrics(out_py, out_c, short_titles=False, verbose=False)\n",
227 "df\n"
228 ]
229 },
230 {
231 "cell_type": "code",
232 "execution_count": null,
233 "id": "f4c73149",
234 "metadata": {},
235 "outputs": [],
236 "source": [
237 "visualize_errors(out_py=out_py, out_c=out_c, sub_az=1, sub_rg=1)"
238 ]
239 },
240 {
241 "cell_type": "code",
242 "execution_count": null,
243 "metadata": {},
244 "outputs": [],
245 "source": [
246 "from polsarpro.util import pauli_rgb\n",
247 "import matplotlib.pyplot as plt\n",
248 "\n",
249 "rgb = pauli_rgb(C3).transpose(\"x\", \"y\", \"band\")\n",
250 "plt.figure(figsize=(15,10))\n",
251 "plt.subplot(1,2,1)\n",
252 "plt.imshow(rgb, interpolation=\"none\")\n",
253 "plt.subplot(1,2,2)\n",
254 "plt.imshow(out_py.dubois_mask_valid_in_out, interpolation=\"none\")\n",
255 "plt.tight_layout()"
256 ]
257 }
258 ],
259 "metadata": {
260 "kernelspec": {
261 "display_name": "Python 3",
262 "language": "python",
263 "name": "python3"
264 },
265 "language_info": {
266 "codemirror_mode": {
267 "name": "ipython",
268 "version": 3
269 },
270 "file_extension": ".py",
271 "mimetype": "text/x-python",
272 "name": "python",
273 "nbconvert_exporter": "python",
274 "pygments_lexer": "ipython3",
275 "version": "3.12.13"
276 }
277 },
278 "nbformat": 4,
279 "nbformat_minor": 2
280 }