Debian Codegraph @

testdata/create_modis_test_data.py

raw
1 #!/usr/bin/env python
2 """Generate MODIS interpolation test data from real input data.
3
4 This script is used to generate the "testdata/modis_test_data.h5" file that is
5 used to validate the various modis interpolation algorithms in
6 python-geotiepoints. The test data file consists of 1km "truth" longitude and
7 latitude arrays from an input MOD03 HDF4 file and interpolated longitude and
8 latitude arrays at 500m and 250m resolution. The interpolation is done using
9 the CVIIRS based algorithm in ``geotiepoints/modisinterpolator.py``.
10 The CVIIRS algorithm was used as opposed to the "simple" or other interpolation
11 methods due to the smoother interpolation between pixels (no linear "steps").
12
13 MOD03 files geolocation data is terrain corrected. This means that the
14 interpolation methods currently in python-geotiepoints can't produce an
15 exact matching result for a round trip test of 1km (truth) ->
16 5km (every 5th pixel) -> 1km (interpolation result).
17 The input MOD03 test data was chosen due to its lack of varying terrain
18 (almost entirely ocean view) to minimize error/differences between the
19 1km truth and 1km interpolation results.
20
21 To limit size of the test data file and the reduce the execution time of tests
22 the test data is limited to the last 2 scans (20 rows of 1km data) of the
23 provided input data.
24
25 """
26 import os
27 import sys
28 from datetime import datetime
29
30 import h5py
31 import numpy as np
32 from pyhdf.SD import SD, SDC
33 import xarray as xr
34 import dask.array as da
35
36 from geotiepoints.modisinterpolator import modis_1km_to_500m, modis_1km_to_250m
37
38
39 def main():
40 import argparse
41 parser = argparse.ArgumentParser()
42 parser.add_argument("-i", "--input", required=True,
43 help="Input MOD03 geolocation HDF4 filename to read 1km lon/lat data from.")
44 parser.add_argument("-o", "--output", default="modis_test_data.h5",
45 help="Output test data HDF5 filename being created")
46 args = parser.parse_args()
47
48 num_1km_rows = 20
49 lons_1km, lats_1km, satz_1km = _get_1km_lon_lat_satz_from_mod03(args.input)
50 lons_1km = lons_1km[-num_1km_rows:]
51 lats_1km = lats_1km[-num_1km_rows:]
52 satz_1km = satz_1km[-num_1km_rows:]
53 lons_1km = xr.DataArray(da.from_array(lons_1km), dims=("y", "x"))
54 lats_1km = xr.DataArray(da.from_array(lats_1km), dims=("y", "x"))
55 satz_1km = xr.DataArray(da.from_array(satz_1km), dims=("y", "x"))
56
57 with h5py.File(args.output, "w") as output_h:
58 lons_500m, lats_500m = modis_1km_to_500m(lons_1km, lats_1km, satz_1km)
59 lons_500m = lons_500m.astype(np.float32, copy=False)
60 lats_500m = lats_500m.astype(np.float32, copy=False)
61
62 lons_250m, lats_250m = modis_1km_to_250m(lons_1km, lats_1km, satz_1km)
63 lons_250m = lons_250m.astype(np.float32, copy=False)
64 lats_250m = lats_250m.astype(np.float32, copy=False)
65
66 output_h.create_dataset("lon_1km", data=lons_1km, compression="gzip", compression_opts=9, shuffle=True)
67 output_h.create_dataset("lat_1km", data=lats_1km, compression="gzip", compression_opts=9, shuffle=True)
68 output_h.create_dataset("satz_1km", data=satz_1km, compression="gzip", compression_opts=9, shuffle=True)
69 output_h.create_dataset("lon_500m", data=lons_500m, compression="gzip", compression_opts=9, shuffle=True)
70 output_h.create_dataset('lat_500m', data=lats_500m, compression="gzip", compression_opts=9, shuffle=True)
71 output_h.create_dataset("lon_250m", data=lons_250m, compression="gzip", compression_opts=9, shuffle=True)
72 output_h.create_dataset("lat_250m", data=lats_250m, compression="gzip", compression_opts=9, shuffle=True)
73 output_h.attrs["1km_data_origin"] = os.path.basename(args.input)
74 output_h.attrs["description"] = (
75 "MODIS interpolation test data for the python-geotiepoints package. "
76 "The 1 km data is taken directly from a MOD03 file. The 250m and "
77 "500m is generated using the cviirs-based algorithm in "
78 "`geotiepoints/modisinterpolator.py`. For more information see "
79 "the generation script in `testdata/create_modis_test_data.py` in "
80 "the python-geotiepoints git repository."
81 )
82 output_h.attrs["creation_date"] = datetime.utcnow().strftime("%Y-%m-%d")
83
84
85 def _get_1km_lon_lat_satz_from_mod03(hdf4_filename: str) -> tuple:
86 h = SD(hdf4_filename, mode=SDC.READ)
87 lon_var = h.select("Longitude")
88 lat_var = h.select("Latitude")
89 sat_zen_var = h.select("SensorZenith")
90
91 # ensure 32-bit float
92 lon_data = lon_var[:].astype(np.float32, copy=False)
93 lat_data = lat_var[:].astype(np.float32, copy=False)
94 sat_zen_attrs = sat_zen_var.attributes()
95 scale_factor = sat_zen_attrs.get("scale_factor", 1.0)
96 add_offset = sat_zen_attrs.get("add_offset", 0.0)
97 sat_zen_data = (sat_zen_var[:] * scale_factor + add_offset).astype(np.float32, copy=False)
98
99 return lon_data, lat_data, sat_zen_data
100
101
102 if __name__ == "__main__":
103 sys.exit(main())