| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": {}, |
| 6 |
"source": [ |
| 7 |
"# Dubois surface inversion tutorial\n" |
| 8 |
] |
| 9 |
}, |
| 10 |
{ |
| 11 |
"cell_type": "markdown", |
| 12 |
"metadata": {}, |
| 13 |
"source": [ |
| 14 |
"This notebook shows how to run the Dubois surface inversion on the UAVSAR multilooked GRD covariance product for `winnip_09002_12060_004_120714_L090_CX_02`, together with a matching incidence-angle raster.\n" |
| 15 |
] |
| 16 |
}, |
| 17 |
{ |
| 18 |
"cell_type": "markdown", |
| 19 |
"metadata": {}, |
| 20 |
"source": [ |
| 21 |
"## How the input product was prepared\n", |
| 22 |
"\n", |
| 23 |
"The example data used here were prepared in three steps:\n", |
| 24 |
"\n", |
| 25 |
"- Download the free UAVSAR multilooked GRD covariance product for `winnip_09002_12060_004_120714_L090_CX_02` from the [UAVSAR website](https://uavsar.jpl.nasa.gov/).\n", |
| 26 |
"- Import the covariance product into ESA SNAP and export it to NetCDF-BEAM format.\n", |
| 27 |
"- Download the matching incidence-angle file for the same scene and use the annotation file (`.ann`) to retrieve the original incidence-angle raster dimensions.\n", |
| 28 |
"\n", |
| 29 |
"In the notebook below we read the NetCDF-BEAM covariance product with `open_netcdf_beam`, load the incidence-angle raster, downsample it to match the multilooked grid, and then pass both inputs to `dubois_surface_inversion`.\n" |
| 30 |
] |
| 31 |
}, |
| 32 |
{ |
| 33 |
"cell_type": "markdown", |
| 34 |
"metadata": {}, |
| 35 |
"source": [ |
| 36 |
"## Import packages and set directories\n" |
| 37 |
] |
| 38 |
}, |
| 39 |
{ |
| 40 |
"cell_type": "code", |
| 41 |
"execution_count": null, |
| 42 |
"metadata": {}, |
| 43 |
"outputs": [], |
| 44 |
"source": [ |
| 45 |
"%load_ext autoreload\n", |
| 46 |
"%autoreload 2\n", |
| 47 |
"\n", |
| 48 |
"from pathlib import Path\n", |
| 49 |
"\n", |
| 50 |
"import numpy as np\n", |
| 51 |
"import xarray as xr\n", |
| 52 |
"from dask.diagnostics import ProgressBar\n", |
| 53 |
"\n", |
| 54 |
"from polsarpro.io import open_netcdf_beam\n", |
| 55 |
"from polsarpro.physical_inversion import dubois_surface_inversion\n", |
| 56 |
"\n", |
| 57 |
"# change these paths to match your local files\n", |
| 58 |
"product_id = \"winnip_09002_12060_004_120714_L090_CX_02\"\n", |
| 59 |
"\n", |
| 60 |
"input_uavsar_nc = Path(f\"/data/psp/test_files/{product_id}_ML5X5.nc\")\n", |
| 61 |
"incidence_angle_file = Path(f\"/data/psp/test_files/{product_id}.inc\")\n", |
| 62 |
"output_dir = Path(\"/data/psp/res\")\n", |
| 63 |
"output_dir.mkdir(parents=True, exist_ok=True)\n", |
| 64 |
"file_out = output_dir / 'dubois_surface_inversion_winnip_python.nc'\n", |
| 65 |
"\n", |
| 66 |
"# Dubois expects GHz; UAVSAR metadata stores the center wavelength in cm.\n", |
| 67 |
"with xr.open_dataset(input_uavsar_nc) as raw_ds:\n", |
| 68 |
" center_wavelength_cm = raw_ds[\"metadata\"].attrs[\"Annotation:CenterWavelength\"]\n", |
| 69 |
"\n", |
| 70 |
"freq_ghz = 29.9792458 / float(center_wavelength_cm)\n", |
| 71 |
"\n", |
| 72 |
"# Same parameter values used in the comparison notebook.\n", |
| 73 |
"thresh1_db = 0.0\n", |
| 74 |
"thresh2_db = 0.0\n", |
| 75 |
"calibration_coeff = 1.0\n", |
| 76 |
"\n", |
| 77 |
"print(f\"Derived radar frequency: {freq_ghz:.6f} GHz\")\n" |
| 78 |
] |
| 79 |
}, |
| 80 |
{ |
| 81 |
"cell_type": "markdown", |
| 82 |
"metadata": {}, |
| 83 |
"source": [ |
| 84 |
"## Load the input product\n" |
| 85 |
] |
| 86 |
}, |
| 87 |
{ |
| 88 |
"cell_type": "code", |
| 89 |
"execution_count": null, |
| 90 |
"metadata": {}, |
| 91 |
"outputs": [], |
| 92 |
"source": [ |
| 93 |
"C3 = open_netcdf_beam(input_uavsar_nc)\n" |
| 94 |
] |
| 95 |
}, |
| 96 |
{ |
| 97 |
"cell_type": "markdown", |
| 98 |
"metadata": {}, |
| 99 |
"source": [ |
| 100 |
"## Load and resample the incidence angle\n", |
| 101 |
"\n", |
| 102 |
"We first open the incidence-angle raster at its original dimensions, then downsample it so it matches the 5x5 geocoded image used by the C3 product.\n" |
| 103 |
] |
| 104 |
}, |
| 105 |
{ |
| 106 |
"cell_type": "code", |
| 107 |
"execution_count": null, |
| 108 |
"metadata": {}, |
| 109 |
"outputs": [], |
| 110 |
"source": [ |
| 111 |
"row_dim, col_dim = C3.dims\n", |
| 112 |
"\n", |
| 113 |
"# use lines from .ann file (infer from auxiliary file)\n", |
| 114 |
"# hgt.set_rows (pixels) = 3767 ; HGT Lines\n", |
| 115 |
"# hgt.set_cols (pixels) = 11636 ; HGT Samples\n", |
| 116 |
"nlines = 3767\n", |
| 117 |
"ncols = 11636\n", |
| 118 |
"\n", |
| 119 |
"# The incidence-angle file is a little-endian float32 stream in radians.\n", |
| 120 |
"# Read only the raster-sized prefix and reshape it to the UAVSAR grid.\n", |
| 121 |
"incidence_angle_values = np.fromfile(\n", |
| 122 |
" incidence_angle_file,\n", |
| 123 |
" dtype=\"<f4\",\n", |
| 124 |
" count=nlines * ncols,\n", |
| 125 |
")\n", |
| 126 |
"\n", |
| 127 |
"# Reduce to the same dimensions as multilooked 5x5 image.\n", |
| 128 |
"incidence_angle_values[incidence_angle_values == -10000] = np.nan\n", |
| 129 |
"incidence_angle = xr.DataArray(\n", |
| 130 |
" incidence_angle_values.reshape(nlines, ncols),\n", |
| 131 |
" dims=(row_dim, col_dim),\n", |
| 132 |
" name=\"incidence_angle\",\n", |
| 133 |
").coarsen(y=5, x=5, boundary=\"trim\").mean()\n" |
| 134 |
] |
| 135 |
}, |
| 136 |
{ |
| 137 |
"cell_type": "markdown", |
| 138 |
"metadata": {}, |
| 139 |
"source": [ |
| 140 |
"## Apply the inversion\n" |
| 141 |
] |
| 142 |
}, |
| 143 |
{ |
| 144 |
"cell_type": "code", |
| 145 |
"execution_count": null, |
| 146 |
"metadata": {}, |
| 147 |
"outputs": [], |
| 148 |
"source": [ |
| 149 |
"if file_out.exists():\n", |
| 150 |
" file_out.unlink()\n", |
| 151 |
"with ProgressBar():\n", |
| 152 |
" dubois_surface_inversion(\n", |
| 153 |
" C3,\n", |
| 154 |
" incidence_angle=incidence_angle,\n", |
| 155 |
" freq_ghz=freq_ghz,\n", |
| 156 |
" thresh1=thresh1_db,\n", |
| 157 |
" thresh2=thresh2_db,\n", |
| 158 |
" calibration_coeff=calibration_coeff,\n", |
| 159 |
" ).to_netcdf(file_out)\n" |
| 160 |
] |
| 161 |
}, |
| 162 |
{ |
| 163 |
"cell_type": "markdown", |
| 164 |
"metadata": {}, |
| 165 |
"source": [ |
| 166 |
"## Load the output file\n" |
| 167 |
] |
| 168 |
}, |
| 169 |
{ |
| 170 |
"cell_type": "code", |
| 171 |
"execution_count": null, |
| 172 |
"metadata": {}, |
| 173 |
"outputs": [], |
| 174 |
"source": [ |
| 175 |
"res = xr.open_dataset(file_out)" |
| 176 |
] |
| 177 |
}, |
| 178 |
{ |
| 179 |
"cell_type": "markdown", |
| 180 |
"metadata": {}, |
| 181 |
"source": [ |
| 182 |
"You can also plot the main Dubois outputs directly:\n" |
| 183 |
] |
| 184 |
}, |
| 185 |
{ |
| 186 |
"cell_type": "code", |
| 187 |
"execution_count": null, |
| 188 |
"metadata": {}, |
| 189 |
"outputs": [], |
| 190 |
"source": [ |
| 191 |
"res.dubois_ks.plot.imshow(cmap='viridis', vmax=0.5, interpolation='bilinear', figsize=(7,7))\n" |
| 192 |
] |
| 193 |
}, |
| 194 |
{ |
| 195 |
"cell_type": "code", |
| 196 |
"execution_count": null, |
| 197 |
"metadata": {}, |
| 198 |
"outputs": [], |
| 199 |
"source": [ |
| 200 |
"res.dubois_er.plot.imshow(cmap='viridis', vmax=20, interpolation='bilinear', figsize=(7,7))\n" |
| 201 |
] |
| 202 |
}, |
| 203 |
{ |
| 204 |
"cell_type": "code", |
| 205 |
"execution_count": null, |
| 206 |
"metadata": {}, |
| 207 |
"outputs": [], |
| 208 |
"source": [ |
| 209 |
"res.dubois_mv.plot.imshow(cmap='viridis', vmax=100, interpolation='bilinear', figsize=(7,7))\n" |
| 210 |
] |
| 211 |
}, |
| 212 |
{ |
| 213 |
"cell_type": "code", |
| 214 |
"execution_count": null, |
| 215 |
"metadata": {}, |
| 216 |
"outputs": [], |
| 217 |
"source": [ |
| 218 |
"res.dubois_mask_valid_in_out.plot.imshow(cmap='viridis', interpolation='bilinear', figsize=(7,7))\n" |
| 219 |
] |
| 220 |
} |
| 221 |
], |
| 222 |
"metadata": { |
| 223 |
"kernelspec": { |
| 224 |
"display_name": "Python 3", |
| 225 |
"language": "python", |
| 226 |
"name": "python3" |
| 227 |
}, |
| 228 |
"language_info": { |
| 229 |
"codemirror_mode": { |
| 230 |
"name": "ipython", |
| 231 |
"version": 3 |
| 232 |
}, |
| 233 |
"file_extension": ".py", |
| 234 |
"mimetype": "text/x-python", |
| 235 |
"name": "python", |
| 236 |
"nbconvert_exporter": "python", |
| 237 |
"pygments_lexer": "ipython3", |
| 238 |
"version": "3.12.13" |
| 239 |
} |
| 240 |
}, |
| 241 |
"nbformat": 4, |
| 242 |
"nbformat_minor": 2 |
| 243 |
} |