| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# Dubois surface inversion: Python vs C GUI on Winnip C3 data\n", |
| 8 |
"\n", |
| 9 |
"This notebook loads the precomputed C GUI outputs from `/data/psp/winnip_cpsp/C3`, runs the Python Dubois surface inversion on the same C3 input, and compares the two results numerically." |
| 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 |
"from pathlib import Path\n", |
| 22 |
"\n", |
| 23 |
"import numpy as np\n", |
| 24 |
"import xarray as xr\n", |
| 25 |
"from dask.diagnostics import ProgressBar\n", |
| 26 |
"\n", |
| 27 |
"from polsarpro.dev.io import read_C3_psp, read_psp_bin\n", |
| 28 |
"from polsarpro.dev.metrics import summarize_metrics, visualize_errors\n", |
| 29 |
"from polsarpro.physical_inversion import dubois_surface_inversion\n", |
| 30 |
"\n", |
| 31 |
"# Same Dubois parameters as the original notebook.\n", |
| 32 |
"freq_ghz = 1.2575\n", |
| 33 |
"thresh1_db = 0\n", |
| 34 |
"thresh2_db = 0\n", |
| 35 |
"calibration_coeff = 1.0\n", |
| 36 |
"\n", |
| 37 |
"input_test_dir = Path(\"/data/psp/winnip_cpsp/C3\")\n", |
| 38 |
"incidence_angle_file = Path(\"/data/psp/winnip_cpsp/incidence_angle_ml5x5_rad.bin\")\n", |
| 39 |
"file_out = Path(\"/data/psp/res/test_dubois_surface_inversion_winnip_python.nc\")\n", |
| 40 |
"file_out.parent.mkdir(parents=True, exist_ok=True)\n", |
| 41 |
"\n", |
| 42 |
"print(f\"Using Dubois frequency: {freq_ghz:.6f} GHz\")\n" |
| 43 |
] |
| 44 |
}, |
| 45 |
{ |
| 46 |
"cell_type": "markdown", |
| 47 |
"metadata": {}, |
| 48 |
"source": [ |
| 49 |
"## Load C3 input and C GUI reference outputs" |
| 50 |
] |
| 51 |
}, |
| 52 |
{ |
| 53 |
"cell_type": "code", |
| 54 |
"execution_count": null, |
| 55 |
"metadata": {}, |
| 56 |
"outputs": [], |
| 57 |
"source": [ |
| 58 |
"C3 = read_C3_psp(input_test_dir)\n", |
| 59 |
"row_dim, col_dim = C3.dims\n", |
| 60 |
"\n", |
| 61 |
"incidence_angle_values = np.fromfile(\n", |
| 62 |
" incidence_angle_file,\n", |
| 63 |
" dtype=\"<f4\",\n", |
| 64 |
" count=C3.sizes[row_dim] * C3.sizes[col_dim],\n", |
| 65 |
")\n", |
| 66 |
"incidence_angle_values[incidence_angle_values == -10000] = np.nan\n", |
| 67 |
"incidence_angle = xr.DataArray(\n", |
| 68 |
" incidence_angle_values.reshape(C3.sizes[row_dim], C3.sizes[col_dim]),\n", |
| 69 |
" dims=(row_dim, col_dim),\n", |
| 70 |
" coords={row_dim: C3.coords[row_dim], col_dim: C3.coords[col_dim]},\n", |
| 71 |
" name=\"incidence_angle\",\n", |
| 72 |
")\n", |
| 73 |
"\n", |
| 74 |
"out_c = {}\n", |
| 75 |
"for name in (\n", |
| 76 |
" \"dubois_er\",\n", |
| 77 |
" \"dubois_mv\",\n", |
| 78 |
" \"dubois_ks\",\n", |
| 79 |
" \"dubois_mask_out\",\n", |
| 80 |
" \"dubois_mask_in\",\n", |
| 81 |
" \"dubois_mask_valid_in_out\",\n", |
| 82 |
"):\n", |
| 83 |
" out_c[name] = read_psp_bin(input_test_dir / f\"{name}.bin\")\n", |
| 84 |
"\n", |
| 85 |
"print(f\"Loaded C3 input with shape {C3.sizes[row_dim]} x {C3.sizes[col_dim]}\")\n" |
| 86 |
] |
| 87 |
}, |
| 88 |
{ |
| 89 |
"cell_type": "markdown", |
| 90 |
"metadata": {}, |
| 91 |
"source": [ |
| 92 |
"## Incidence Angle\n", |
| 93 |
"\n", |
| 94 |
"Plot the multilooked incidence angle raster used by both the Python and C GUI runs.\n" |
| 95 |
] |
| 96 |
}, |
| 97 |
{ |
| 98 |
"cell_type": "code", |
| 99 |
"execution_count": null, |
| 100 |
"metadata": {}, |
| 101 |
"outputs": [], |
| 102 |
"source": [ |
| 103 |
"import matplotlib.pyplot as plt\n", |
| 104 |
"\n", |
| 105 |
"incidence_angle_fig = incidence_angle.transpose(*reversed(incidence_angle.dims))\n", |
| 106 |
"\n", |
| 107 |
"plt.figure(figsize=(10, 5))\n", |
| 108 |
"plt.imshow(incidence_angle_fig, cmap=\"viridis\", interpolation=\"none\")\n", |
| 109 |
"plt.title(\"Incidence angle (transposed for display)\")\n", |
| 110 |
"plt.axis(\"off\")\n", |
| 111 |
"plt.colorbar(fraction=0.046, pad=0.04, location=\"bottom\")\n", |
| 112 |
"plt.tight_layout()\n" |
| 113 |
] |
| 114 |
}, |
| 115 |
{ |
| 116 |
"cell_type": "markdown", |
| 117 |
"metadata": {}, |
| 118 |
"source": [ |
| 119 |
"## Run the Python inversion" |
| 120 |
] |
| 121 |
}, |
| 122 |
{ |
| 123 |
"cell_type": "code", |
| 124 |
"execution_count": null, |
| 125 |
"metadata": {}, |
| 126 |
"outputs": [], |
| 127 |
"source": [ |
| 128 |
"if file_out.exists():\n", |
| 129 |
" file_out.unlink()\n", |
| 130 |
"\n", |
| 131 |
"with ProgressBar():\n", |
| 132 |
" out_py = dubois_surface_inversion(\n", |
| 133 |
" C3,\n", |
| 134 |
" incidence_angle=incidence_angle,\n", |
| 135 |
" freq_ghz=freq_ghz,\n", |
| 136 |
" thresh1=thresh1_db,\n", |
| 137 |
" thresh2=thresh2_db,\n", |
| 138 |
" calibration_coeff=calibration_coeff,\n", |
| 139 |
" )\n", |
| 140 |
" out_py.to_netcdf(file_out)\n" |
| 141 |
] |
| 142 |
}, |
| 143 |
{ |
| 144 |
"cell_type": "markdown", |
| 145 |
"metadata": {}, |
| 146 |
"source": [ |
| 147 |
"## Compare Python and C GUI outputs" |
| 148 |
] |
| 149 |
}, |
| 150 |
{ |
| 151 |
"cell_type": "code", |
| 152 |
"execution_count": null, |
| 153 |
"metadata": {}, |
| 154 |
"outputs": [], |
| 155 |
"source": [ |
| 156 |
"df = summarize_metrics(out_py, out_c, short_titles=False, verbose=False)\n", |
| 157 |
"df" |
| 158 |
] |
| 159 |
}, |
| 160 |
{ |
| 161 |
"cell_type": "code", |
| 162 |
"execution_count": null, |
| 163 |
"metadata": {}, |
| 164 |
"outputs": [], |
| 165 |
"source": [ |
| 166 |
"# Use transposed views for plotting so x and y are swapped in the figures.\n", |
| 167 |
"out_py_fig = out_py.transpose(\"x\", \"y\")\n", |
| 168 |
"out_c_fig = {name: arr.T for name, arr in out_c.items()}\n", |
| 169 |
"\n", |
| 170 |
"visualize_errors(out_py=out_py_fig, out_c=out_c_fig, sub_az=1, sub_rg=1)\n" |
| 171 |
] |
| 172 |
} |
| 173 |
], |
| 174 |
"metadata": { |
| 175 |
"kernelspec": { |
| 176 |
"display_name": "Python 3", |
| 177 |
"language": "python", |
| 178 |
"name": "python3" |
| 179 |
}, |
| 180 |
"language_info": { |
| 181 |
"codemirror_mode": { |
| 182 |
"name": "ipython", |
| 183 |
"version": 3 |
| 184 |
}, |
| 185 |
"file_extension": ".py", |
| 186 |
"mimetype": "text/x-python", |
| 187 |
"name": "python", |
| 188 |
"nbconvert_exporter": "python", |
| 189 |
"pygments_lexer": "ipython3", |
| 190 |
"version": "3.12.13" |
| 191 |
} |
| 192 |
}, |
| 193 |
"nbformat": 4, |
| 194 |
"nbformat_minor": 2 |
| 195 |
} |