Debian Codegraph @

tests/test_io.py

raw
1 import pytest
2 import numpy as np
3 import xarray as xr
4 from pathlib import Path
5
6 from polsarpro.io import open_netcdf_beam, polmat_to_netcdf, _parse_slc_bands
7
8
9 @pytest.fixture
10 def tmp_netcdf(tmp_path: Path):
11 """Helper to write a temporary NetCDF file."""
12
13 def _create_ds(variables: dict, geocoded=False):
14 y = np.arange(2)
15 x = np.arange(3)
16 dims = ("lat", "lon") if geocoded else ("y", "x")
17 coords = {"y": y, "x": x} if dims == ("y", "x") else {"lat": y, "lon": x}
18 ds = xr.Dataset(
19 {name: (dims, np.ones((len(y), len(x)))) for name in variables},
20 coords=coords,
21 )
22 # add metadata attribute
23 ds["metadata"] = xr.DataArray(
24 attrs={"Abstracted_Metadata:is_terrain_corrected": int(geocoded)}
25 )
26 file_path = tmp_path / "test.nc"
27 ds.to_netcdf(file_path)
28 return file_path
29
30 return _create_ds
31
32
33 def test_s_matrix(tmp_netcdf):
34 # Create S matrix dataset
35 S_vars = [
36 f"{x}_{p1}{p2}" for p1 in ("H", "V") for p2 in ("H", "V") for x in ("i", "q")
37 ]
38 file_path = tmp_netcdf(S_vars, geocoded=False)
39
40 ds_out = open_netcdf_beam(file_path)
41
42 assert set(ds_out.data_vars) == {"hh", "hv", "vh", "vv"}
43 assert ds_out.attrs["poltype"] == "S"
44 assert "description" in ds_out.attrs
45 assert "y" in ds_out.coords and "x" in ds_out.coords
46
47 def test_s_matrix_biomass(tmp_netcdf):
48 # Create S matrix dataset
49 tag = "S1"
50 S_vars = [
51 f"{x}_{tag}_{p1}{p2}" for p1 in ("H", "V") for p2 in ("H", "V") for x in ("i", "q")
52 ]
53 file_path = tmp_netcdf(S_vars, geocoded=False)
54
55 ds_out = open_netcdf_beam(file_path)
56
57 assert set(ds_out.data_vars) == {"hh", "hv", "vh", "vv"}
58 assert ds_out.attrs["poltype"] == "S"
59 assert "description" in ds_out.attrs
60 assert "y" in ds_out.coords and "x" in ds_out.coords
61
62
63 def test_c3_matrix(tmp_netcdf):
64 C3_vars = [
65 "C11",
66 "C22",
67 "C33",
68 "C12_real",
69 "C12_imag",
70 "C13_real",
71 "C13_imag",
72 "C23_real",
73 "C23_imag",
74 ]
75 file_path = tmp_netcdf(C3_vars, geocoded=True)
76
77 ds_out = open_netcdf_beam(file_path)
78
79 assert set(ds_out.data_vars) == {"m11", "m22", "m33", "m12", "m13", "m23"}
80 assert ds_out.attrs["poltype"] == "C3"
81 assert "description" in ds_out.attrs
82 # Should not add y/x for geocoded datasets
83 assert "y" not in ds_out.coords
84
85
86 def test_t3_matrix(tmp_netcdf):
87 T3_vars = [
88 "T11",
89 "T22",
90 "T33",
91 "T12_real",
92 "T12_imag",
93 "T13_real",
94 "T13_imag",
95 "T23_real",
96 "T23_imag",
97 ]
98 file_path = tmp_netcdf(T3_vars, geocoded=True)
99
100 ds_out = open_netcdf_beam(file_path)
101
102 assert set(ds_out.data_vars) == {"m11", "m22", "m33", "m12", "m13", "m23"}
103 assert ds_out.attrs["poltype"] == "T3"
104 assert "description" in ds_out.attrs
105
106
107 def test_invalid_vars(tmp_netcdf):
108 file_path = tmp_netcdf(["random_var"], geocoded=False)
109
110 with pytest.raises(ValueError, match="Polarimetric type not recognized"):
111 open_netcdf_beam(file_path)
112
113
114 @pytest.mark.parametrize(
115 "synthetic_poldata",
116 ["S", "C2", "C3", "C4", "T3", "T4"],
117 indirect=True,
118 )
119 def test_polmat_to_netcdf(synthetic_poldata, tmp_path):
120
121 input_data = synthetic_poldata
122 for _, ds in input_data.items():
123 poltype = ds.poltype
124 out_file = tmp_path / f"test_{poltype}.nc"
125 polmat_to_netcdf(ds, out_file)
126
127 ds_out = xr.open_dataset(out_file)
128
129 if poltype == "S":
130 expected_vars = {
131 "i_HH",
132 "q_HH",
133 "i_HV",
134 "q_HV",
135 "i_VH",
136 "q_VH",
137 "i_VV",
138 "q_VV",
139 }
140 elif poltype == "C2":
141 expected_vars = {"C11", "C12_real", "C12_imag", "C22"}
142 elif poltype == "T3":
143 expected_vars = {
144 "T11",
145 "T12_real",
146 "T12_imag",
147 "T13_real",
148 "T13_imag",
149 "T22",
150 "T23_real",
151 "T23_imag",
152 "T33",
153 }
154 elif poltype == "C3":
155 expected_vars = {
156 "C11",
157 "C12_real",
158 "C12_imag",
159 "C13_real",
160 "C13_imag",
161 "C22",
162 "C23_real",
163 "C23_imag",
164 "C33",
165 }
166 elif poltype == "T4":
167 expected_vars = {
168 "T11",
169 "T12_real",
170 "T12_imag",
171 "T13_real",
172 "T13_imag",
173 "T14_real",
174 "T14_imag",
175 "T22",
176 "T23_real",
177 "T23_imag",
178 "T24_real",
179 "T24_imag",
180 "T33",
181 "T34_real",
182 "T34_imag",
183 "T44",
184 }
185 elif poltype == "C4":
186 expected_vars = {
187 "C11",
188 "C12_real",
189 "C12_imag",
190 "C13_real",
191 "C13_imag",
192 "C14_real",
193 "C14_imag",
194 "C22",
195 "C23_real",
196 "C23_imag",
197 "C24_real",
198 "C24_imag",
199 "C33",
200 "C34_real",
201 "C34_imag",
202 "C44",
203 }
204 assert set(ds_out.data_vars.keys()) == expected_vars
205 var = "hh" if "hh" in ds.data_vars else "m11"
206 shp = ds[var].shape
207 for var in expected_vars:
208 assert ds_out[var].shape == shp
209 assert not np.isnan(ds_out[var].values).any()
210
211
212 def test_parse_slc_bands():
213 tag = "S3"
214 var_names = {
215 f"i_{tag}_HH",
216 f"q_{tag}_HH",
217 f"i_{tag}_HV",
218 f"q_{tag}_HV",
219 f"i_{tag}_VH",
220 f"q_{tag}_VH",
221 f"i_{tag}_VV",
222 f"q_{tag}_VV",
223 }
224 pol_list = ("H", "V")
225 var_names_no_tags = {
226 f"{x}_{p1}{p2}" for p1 in pol_list for p2 in pol_list for x in ("i", "q")
227 }
228 var_names_miss = {
229 f"i_{tag}_HH",
230 f"q_{tag}_HH",
231 f"i_{tag}_HV",
232 f"q_{tag}_VH",
233 f"i_{tag}_VV",
234 }
235 var_names_bad_prefix = {
236 f"i_{tag}_HH",
237 f"q_{tag}_HH",
238 f"{tag}_HV",
239 f"q_{tag}_HV",
240 f"i_{tag}_VH",
241 f"q_{tag}_VH",
242 f"i_{tag}_VV",
243 f"q_{tag}_VV",
244 }
245 var_names_bad_pol = {
246 f"i_{tag}_HH",
247 f"q_{tag}_HH",
248 f"i_{tag}_HV",
249 f"q_{tag}_HV",
250 f"i_{tag}_GG",
251 f"q_{tag}_VH",
252 f"i_{tag}_VV",
253 f"q_{tag}_VV",
254 }
255 tagx = "Sx"
256 var_names_bad_tag = {
257 f"i_{tagx}_HH",
258 f"q_{tagx}_HH",
259 f"i_{tagx}_HV",
260 f"q_{tagx}_HV",
261 f"i_{tagx}_VH",
262 f"q_{tagx}_VH",
263 f"i_{tagx}_VV",
264 f"q_{tagx}_VV",
265 }
266 assert _parse_slc_bands(var_names=var_names) == tag
267 assert _parse_slc_bands(var_names=var_names_no_tags) == ""
268 assert _parse_slc_bands(var_names=var_names_miss) is None
269 assert _parse_slc_bands(var_names=var_names_bad_prefix) is None
270 assert _parse_slc_bands(var_names=var_names_bad_pol) is None
271 assert _parse_slc_bands(var_names=var_names_bad_tag) is None