-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_end2end.py
446 lines (387 loc) · 16.1 KB
/
test_end2end.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# -*- coding: utf-8 -*-
# Copyright (c) 2020 pygac-fdr developers
#
# This file is part of pygac-fdr.
#
# pygac-fdr is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# pygac-fdr is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# pygac-fdr. If not, see <http://www.gnu.org/licenses/>.
"""End-to-end tests for pygac-fdr.
Download test data set, run the entire chain of processing steps (read GAC files, write data
to netCDF, enhance metadata) and compare results against reference data. They should be identical.
Usage:
$ python test_end2end.py
Or
$ pytest -vs test_end2end.py
The test behaviour can be controlled using the configuration file test_end2end.yaml .
Do not use pytest in combination with plotting as this might eat up all your memory. For some
reason pytest caches all generated figures until the end of the test suite.
"""
import gzip
import logging
import shutil
import subprocess
import unittest
from pathlib import Path
import numpy as np
import xarray as xr
import yaml
from cfchecker.cfchecks import CFChecker
from dateutil.parser import isoparse
from xarray.core.formatting import diff_attrs_repr
from xarray.core.utils import dict_equiv
from pygac_fdr.metadata import QualityFlags
from pygac_fdr.utils import LOGGER_NAME, logging_on
LOG = logging.getLogger(LOGGER_NAME)
class EndToEndTestBase(unittest.TestCase):
tag = None
with_metadata = False
def _assert_time_attrs_close(self, attrs_a, attrs_b):
time_attrs = ["start_time", "end_time"]
for attr in time_attrs:
with self.subTest(attribute=attr):
time_a = isoparse(attrs_a.pop(attr))
time_b = isoparse(attrs_b.pop(attr))
self.assertLess(abs((time_b - time_a).total_seconds()), 2)
def _assert_numerical_attrs_close(self, attrs_a, attrs_b):
numerical_attrs = [
"geospatial_lon_min",
"geospatial_lon_max",
"geospatial_lat_min",
"geospatial_lat_max",
]
for attr in numerical_attrs:
with self.subTest(attribute=attr):
val_a = attrs_a.pop(attr)
val_b = attrs_b.pop(attr)
np.testing.assert_allclose(val_a, val_b, rtol=self.rtol, atol=self.atol)
def assert_global_attrs_close(self, attrs_a, attrs_b):
attrs_a = attrs_a.copy()
attrs_b = attrs_b.copy()
self._assert_time_attrs_close(attrs_a, attrs_b)
self._assert_numerical_attrs_close(attrs_a, attrs_b)
assert dict_equiv(attrs_a, attrs_b), diff_attrs_repr(
attrs_a, attrs_b, "identical"
)
def assert_variable_attrs_equal(self, ds_a, ds_b):
# Does not test whether ds_a and ds_b have the same set of variables
for var_name in ds_a.variables.keys():
attrs_a = ds_a[var_name].attrs
attrs_b = ds_b[var_name].attrs
assert dict_equiv(attrs_a, attrs_b), diff_attrs_repr(
attrs_a, attrs_b, "identical"
)
@classmethod
def setUpClass(cls):
logging_on(logging.DEBUG, for_all=True)
# Read config file
with open(Path(__file__).parent / "test_end2end.yaml") as fh:
config = yaml.safe_load(fh)
cls.test_data_dir = config["test_data_dir"]
cls.cleanup = config["cleanup"]
cls.resume = config["resume"]
cls.fast = config["fast"]
cls.rtol = float(config["rtol"])
cls.atol = float(config["atol"])
cls.plot = config["plot"]
cls.trigger_failure = config["trigger_failure"]
# Set class attributes
cls.cfg_file = Path(__file__).absolute().parents[2] / "etc" / "pygac-fdr.yaml"
cls.tle_dir = Path(cls.test_data_dir) / "tle"
cls.input_dir = Path(cls.test_data_dir) / "input" / cls.tag
cls.output_dir = Path(cls.test_data_dir) / "output" / cls.tag
cls.output_dir_ref = Path(cls.test_data_dir) / "output_ref" / cls.tag
# Discover input files and reference output files
cls.gac_files_gz, cls.nc_files_ref = cls._discover_files()
# Process input files
dbfile = Path(cls.output_dir) / "test.sqlite3"
cls.nc_files = cls._run(
cls.gac_files_gz, dbfile=dbfile if cls.with_metadata else None
)
@classmethod
def _discover_files(cls):
gac_files_gz = sorted(Path(cls.input_dir).glob("*.gz"))
nc_files_ref = sorted(Path(cls.output_dir_ref).glob("*.nc"))
if cls.fast:
gac_files_gz = [gac_files_gz[-1]]
nc_files_ref = [nc_files_ref[-1]]
return gac_files_gz, nc_files_ref
@classmethod
def _unzip_gac_files(cls, filenames_gz, output_dir):
filenames = []
for filename_gz in filenames_gz:
filename = Path(output_dir) / filename_gz.stem
LOG.info("Decompressing {}".format(filename_gz))
with gzip.open(filename_gz, "rb") as f_in, open(filename, "wb") as f_out:
shutil.copyfileobj(f_in, f_out)
filenames.append(filename)
return filenames
@classmethod
def _run(cls, gac_files_gz, dbfile=None):
"""Read GAC files and write calibrated scenes to netCDF.
If database file is given, also collect & update metadata.
"""
if cls.resume:
# Resume testing with existing output files
LOG.info("Resuming test with existing output files")
return sorted(Path(cls.output_dir).glob("*.nc"))
# Prepare output directory
Path(cls.output_dir).mkdir(parents=True, exist_ok=True)
cls._cleanup_output_dir()
# Unzip GAC files
gac_files = cls._unzip_gac_files(gac_files_gz, cls.output_dir)
# Read GAC files and write netCDF files
run = [
"pygac-fdr-run",
"--cfg",
cls.cfg_file,
"--output-dir",
cls.output_dir,
"--tle-dir",
cls.tle_dir,
"--verbose",
"--log-all",
] + gac_files
subprocess.run(run, check=True)
nc_files = sorted(Path(cls.output_dir).glob("*.nc"))
if dbfile:
# Collect & complement metadata
collect = [
"pygac-fdr-mda-collect",
"--dbfile",
dbfile,
"--if-exists",
"replace",
"--verbose",
] + nc_files
subprocess.run(collect, check=True)
# Update metadata
update = ["pygac-fdr-mda-update", "--dbfile", dbfile]
subprocess.run(update, check=True)
return nc_files
@classmethod
def _cleanup_output_dir(cls):
if cls.cleanup:
for item in Path(cls.output_dir).iterdir():
if item.is_file():
item.unlink()
@classmethod
def tearDownClass(cls):
cls._cleanup_output_dir()
def _tst_regression(self, nc_files, nc_files_ref):
"""Test entire netCDF contents against reference file."""
dynamic_attrs = [
"date_created",
"history",
"version_satpy",
"version_pygac",
"version_pygac_fdr",
]
for nc_file, nc_file_ref in zip(nc_files, nc_files_ref):
LOG.info("Performing regression test with {}".format(nc_file))
with self.subTest(nc_file=nc_file), xr.open_dataset(
nc_file, chunks=1024
) as ds, xr.open_dataset(nc_file_ref, chunks=1024) as ds_ref:
# Remove dynamic attributes
for attr in dynamic_attrs:
ds.attrs.pop(attr)
ds_ref.attrs.pop(attr)
# If testing just one file, there is no overlap
if self.fast:
ds = ds.drop_vars(
["overlap_free_start", "overlap_free_end"], errors="ignore"
)
ds_ref = ds_ref.drop_vars(
["overlap_free_start", "overlap_free_end"], errors="ignore"
)
# Trigger test failure
if self.trigger_failure:
ds["latitude"] = ds["latitude"] * 2
ds.attrs["geospatial_lat_min"] = 9999.0
# Compare datasets
try:
xr.testing.assert_allclose(
ds, ds_ref, atol=self.atol, rtol=self.rtol
)
except AssertionError:
if self.plot:
self._plot_diffs(ds_ref=ds_ref, ds_tst=ds, file_tst=nc_file)
raise
self.assert_global_attrs_close(ds.attrs, ds_ref.attrs)
self.assert_variable_attrs_equal(ds, ds_ref)
def _plot_diffs(self, ds_ref, ds_tst, file_tst):
"""Plot differences and save figure to output directory."""
import matplotlib.pyplot as plt
cmp_vars = {
"reflectance_channel_1": {},
"reflectance_channel_2": {},
"brightness_temperature_channel_3": {},
"reflectance_channel_3a": {},
"brightness_temperature_channel_3b": {},
"brightness_temperature_channel_4": {},
"brightness_temperature_channel_5": {},
"longitude": {"vmin": -180, "vmax": 180},
"latitude": {"vmin": -90, "vmax": 90},
"solar_zenith_angle": {},
"solar_azimuth_angle": {},
"sensor_zenith_angle": {},
"sensor_azimuth_angle": {},
"sun_sensor_azimuth_difference_angle": {},
}
for varname, prop in cmp_vars.items():
try:
diff = ds_tst[varname] - ds_ref[varname]
except KeyError:
continue
vmin = min(ds_tst[varname].min(), ds_ref[varname].min())
vmax = max(ds_tst[varname].max(), ds_ref[varname].max())
min_diff = diff.min()
max_diff = diff.max()
abs_max_diff = max(abs(min_diff), abs(max_diff))
fig, (ax_ref, ax_tst, ax_diff) = plt.subplots(
nrows=3, figsize=(20, 8.5), sharex=True
)
ds_ref[varname].transpose().plot.imshow(
ax=ax_ref, vmin=prop.get("vmin", vmin), vmax=prop.get("vmax", vmax)
)
ds_tst[varname].transpose().plot.imshow(
ax=ax_tst, vmin=prop.get("vmin", vmin), vmax=prop.get("vmax", vmax)
)
diff.transpose().plot.imshow(
ax=ax_diff,
vmin=-0.8 * abs_max_diff,
vmax=0.8 * abs_max_diff,
cmap="RdBu_r",
)
ax_ref.set_title("Reference", fontsize=10)
ax_tst.set_title("Test", fontsize=10)
ax_diff.set_title("Test - Reference", fontsize=10)
ax_ref.set_xlabel(None)
ax_tst.set_xlabel(None)
plt.suptitle(Path(file_tst).name)
ofile = "{}_{}.png".format(Path(file_tst).name, varname)
plt.savefig(Path(self.output_dir) / ofile, bbox_inches="tight")
plt.close("all")
class EndToEndTestNormal(EndToEndTestBase):
"""End-to-end test with normal data (no corruption).
Also compare metadata against results from CLARA-A3 feedback loop 1.
"""
tag = "normal"
with_metadata = True
mda_exp = {
"NSS.GHRR.NA.D81089.S0054.E0246.B0912021.GC": {
"midnight_line": np.nan,
"overlap_free_end": 12995,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S0242.E0427.B0912122.GC": {
"midnight_line": np.nan,
"overlap_free_end": 12157,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S0423.E0609.B0912223.GC": {
"midnight_line": np.nan,
"overlap_free_end": 12603,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S0753.E0947.B0912425.WI": {
"midnight_line": np.nan,
"overlap_free_end": 13053,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S0943.E1058.B0912525.WI": {
"midnight_line": np.nan,
"overlap_free_end": 9043,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S1102.E1136.B0912626.WI": {
"midnight_line": np.nan,
"overlap_free_end": 3408,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S1131.E1205.B0912626.WI": {
"midnight_line": np.nan,
"overlap_free_end": 3232,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S1221.E1315.B0912627.WI": {
"midnight_line": np.nan,
"overlap_free_end": 6194,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S1329.E1459.B0912728.WI": {
"midnight_line": np.nan,
"overlap_free_end": 10770,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S1459.E1631.B0912829.GC": {
"midnight_line": np.nan,
"overlap_free_end": 10489,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S1626.E1812.B0912930.GC": {
"midnight_line": np.nan,
"overlap_free_end": 12057,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S1807.E1951.B0913031.GC": {
"midnight_line": np.nan,
"overlap_free_end": 12011,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S1947.E2131.B0913132.GC": {
"midnight_line": np.nan,
"overlap_free_end": 11955,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S2126.E2256.B0913233.WI": {
"midnight_line": np.nan,
"overlap_free_end": 10057,
"global_quality_flag": QualityFlags.OK,
},
"NSS.GHRR.NA.D81089.S2251.E0035.B0913334.WI": {
"midnight_line": 8260,
"overlap_free_end": 12473,
# This is the last file here, therefore overlap_free_end == along_track - 1.
# In CLARA there is overlap with the next file, which is why they have 12101.
"global_quality_flag": QualityFlags.OK,
},
} # from CLARA-A3 Feedback Loop 1
def test_regression(self):
self._tst_regression(self.nc_files, self.nc_files_ref)
def test_mda_clara(self):
"""Test metadata against CLARA-A3 reference."""
for nc_file in self.nc_files:
with xr.open_dataset(nc_file) as ds:
gac_file = ds.attrs["gac_filename"]
mda_exp = self.mda_exp[gac_file].copy()
for var_name, exp in mda_exp.items():
np.testing.assert_equal(ds[var_name].values, exp)
def test_cf_compliance(self):
"""Test CF compliance of generated files using CF checker."""
checker = CFChecker()
for nc_file in self.nc_files:
LOG.info("Checking CF compliance of {}".format(nc_file))
res = checker.checker(str(nc_file))
global_err = any([res["global"][cat] for cat in ("ERROR", "FATAL")])
var_err = any(
[(v["ERROR"] or v["FATAL"]) for v in res["variables"].values()]
)
err = global_err or var_err
self.assertFalse(err, msg="{} is not CF compliant".format(nc_file))
class EndToEndTestCorrupt(EndToEndTestBase):
"""End-to-end test with data that have common defects."""
tag = "corrupt"
with_metadata = False
def test_regression(self):
self._tst_regression(self.nc_files, self.nc_files_ref)
if __name__ == "__main__":
unittest.main(verbosity=2)