Debian Codegraph @

notebooks/dev/test-yamaguchi4-real-data.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "%load_ext autoreload\n",
10 "%autoreload 2\n",
11 "\n",
12 "import os\n",
13 "\n",
14 "from polsarpro.dev.devtools import parse_psp_parameter_string\n",
15 "import numpy as np\n",
16 "from pathlib import Path\n",
17 "import xarray as xr\n",
18 "from polsarpro.io import open_netcdf_beam\n",
19 "from polsarpro.dev.io import read_psp_bin\n",
20 "from polsarpro.decompositions import yamaguchi4\n",
21 "from polsarpro.dev.metrics import summarize_metrics, visualize_errors\n",
22 "import shutil\n",
23 "\n",
24 "# optional import for progress bar\n",
25 "from dask.diagnostics import ProgressBar\n",
26 "\n",
27 "# change to your local C-PolSARpro install dir\n",
28 "c_psp_dir = \"/home/c_psp/Soft/bin/\"\n",
29 "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_process_sngl/\"\n",
30 "os.environ[\"PATH\"]+=os.pathsep+f\"{c_psp_dir}/data_convert/\"\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 "# input files from C (use the same as for h-a-alpha)\n",
37 "input_test_dir = Path(\"/data/psp/SAN_FRANCISCO_ALOS1/\")\n",
38 "\n",
39 "# output files from C\n",
40 "output_test_dir = Path(\"/data/psp/res/yamaguchi4_c/\")\n",
41 "if not os.path.isdir(output_test_dir):\n",
42 " os.mkdir(output_test_dir)\n",
43 "\n",
44 "# uncomment to test the different modes\n",
45 "# mode = \"y4o\"\n",
46 "# mode = \"y4r\"\n",
47 "mode = \"s4r\""
48 ]
49 },
50 {
51 "cell_type": "markdown",
52 "metadata": {},
53 "source": [
54 "## Run the C-version on some test data"
55 ]
56 },
57 {
58 "cell_type": "code",
59 "execution_count": null,
60 "metadata": {},
61 "outputs": [],
62 "source": [
63 "input_str = f\"\"\"id: {input_test_dir}\n",
64 "od: {output_test_dir}\n",
65 "iodf: S2\n",
66 "mod: {mode.upper()}\n",
67 "nwr: 7\n",
68 "nwc: 7\n",
69 "ofr: 0\n",
70 "ofc: 0\n",
71 "fnr: 18432\n",
72 "fnc: 1248\n",
73 "errf: /tmp\"\"\"\n",
74 "\n",
75 "result = parse_psp_parameter_string(input_str)\n",
76 "os.system(f\"yamaguchi_4components_decomposition.exe {result}\")\n",
77 "\n",
78 "# CPSP does not create another config file in output dir\n",
79 "shutil.copy(input_test_dir / \"config.txt\", output_test_dir)"
80 ]
81 },
82 {
83 "cell_type": "markdown",
84 "metadata": {},
85 "source": [
86 "## Load ALOS data and C outputs"
87 ]
88 },
89 {
90 "cell_type": "code",
91 "execution_count": null,
92 "metadata": {},
93 "outputs": [],
94 "source": [
95 "S = open_netcdf_beam(input_alos_data)"
96 ]
97 },
98 {
99 "cell_type": "markdown",
100 "metadata": {},
101 "source": [
102 "## Apply the decomposition"
103 ]
104 },
105 {
106 "cell_type": "code",
107 "execution_count": null,
108 "metadata": {},
109 "outputs": [],
110 "source": [
111 "file_out = f\"/data/psp/res/test_yamaguchi4_{mode}.nc\"\n",
112 "# netcdf writer cannot overwrite\n",
113 "if os.path.isfile(file_out):\n",
114 " os.remove(file_out)\n",
115 "\n",
116 "with ProgressBar():\n",
117 " yamaguchi4(S, boxcar_size=[7, 7], mode=mode).to_netcdf(file_out)\n",
118 "print(f\"Wrote to {file_out}\")\n"
119 ]
120 },
121 {
122 "cell_type": "markdown",
123 "metadata": {},
124 "source": [
125 "# Numerical evaluation"
126 ]
127 },
128 {
129 "cell_type": "code",
130 "execution_count": null,
131 "metadata": {},
132 "outputs": [],
133 "source": [
134 "import matplotlib.pyplot as plt\n",
135 "\n",
136 "# open python output (comment if using compute)\n",
137 "out_py = xr.open_dataset(f\"/data/psp/res/test_yamaguchi4_{mode}.nc\")\n",
138 "# open C-PolSARPro outputs\n",
139 "out_c = {}\n",
140 "out_names = (\n",
141 " (\"odd\", f\"Yamaguchi4_{mode.upper()}_Odd\"),\n",
142 " (\"double\", f\"Yamaguchi4_{mode.upper()}_Dbl\"),\n",
143 " (\"volume\", f\"Yamaguchi4_{mode.upper()}_Vol\"),\n",
144 " (\"helix\", f\"Yamaguchi4_{mode.upper()}_Hlx\"),\n",
145 ")\n",
146 "for name in out_names:\n",
147 " print(f\"Loading {name[1]}\")\n",
148 " file_name = output_test_dir / f\"{name[1]}.bin\"\n",
149 " out_c[name[0]] = read_psp_bin(file_name)\n",
150 "\n",
151 "df = summarize_metrics(out_py, out_c, short_titles=False, verbose=False)\n",
152 "df"
153 ]
154 },
155 {
156 "cell_type": "code",
157 "execution_count": null,
158 "metadata": {},
159 "outputs": [],
160 "source": [
161 "visualize_errors(out_py=out_py, out_c=out_c)"
162 ]
163 }
164 ],
165 "metadata": {
166 "kernelspec": {
167 "display_name": "psp",
168 "language": "python",
169 "name": "python3"
170 },
171 "language_info": {
172 "codemirror_mode": {
173 "name": "ipython",
174 "version": 3
175 },
176 "file_extension": ".py",
177 "mimetype": "text/x-python",
178 "name": "python",
179 "nbconvert_exporter": "python",
180 "pygments_lexer": "ipython3",
181 "version": "3.12.12"
182 }
183 },
184 "nbformat": 4,
185 "nbformat_minor": 2
186 }