| 1 |
"""Tests for MODIS interpolators.""" |
| 2 |
import warnings |
| 3 |
|
| 4 |
import numpy as np |
| 5 |
from pyproj import Geod |
| 6 |
import h5py |
| 7 |
import os |
| 8 |
import dask |
| 9 |
import dask.array as da |
| 10 |
import xarray as xr |
| 11 |
import pytest |
| 12 |
from .utils import CustomScheduler |
| 13 |
from geotiepoints.modisinterpolator import (modis_1km_to_250m, |
| 14 |
modis_1km_to_500m, |
| 15 |
modis_5km_to_1km, |
| 16 |
modis_5km_to_500m, |
| 17 |
modis_5km_to_250m) |
| 18 |
FILENAME_DATA = os.path.join( |
| 19 |
os.path.dirname(__file__), '../../testdata/modis_test_data.h5') |
| 20 |
|
| 21 |
|
| 22 |
def _to_dask(arr): |
| 23 |
return da.from_array(arr, chunks=4096) |
| 24 |
|
| 25 |
|
| 26 |
def _to_da(arr): |
| 27 |
return xr.DataArray(_to_dask(arr), dims=['y', 'x']) |
| 28 |
|
| 29 |
|
| 30 |
def _load_h5_geo_vars(*var_names): |
| 31 |
h5f = h5py.File(FILENAME_DATA, 'r') |
| 32 |
return tuple(h5f[var_name] for var_name in var_names) |
| 33 |
|
| 34 |
|
| 35 |
def load_1km_lonlat_as_numpy(): |
| 36 |
lon1, lat1 = _load_h5_geo_vars('lon_1km', 'lat_1km') |
| 37 |
return lon1[:], lat1[:] |
| 38 |
|
| 39 |
|
| 40 |
def load_1km_lonlat_as_dask(): |
| 41 |
lon1, lat1 = _load_h5_geo_vars('lon_1km', 'lat_1km') |
| 42 |
return _to_dask(lon1), _to_dask(lat1) |
| 43 |
|
| 44 |
|
| 45 |
def load_1km_lonlat_as_xarray_dask(): |
| 46 |
lon1, lat1 = _load_h5_geo_vars('lon_1km', 'lat_1km') |
| 47 |
return _to_da(lon1), _to_da(lat1) |
| 48 |
|
| 49 |
|
| 50 |
def load_1km_lonlat_satz_as_xarray_dask(): |
| 51 |
lon1, lat1, satz1 = _load_h5_geo_vars('lon_1km', 'lat_1km', 'satz_1km') |
| 52 |
return _to_da(lon1), _to_da(lat1), _to_da(satz1) |
| 53 |
|
| 54 |
|
| 55 |
def load_5km_lonlat_satz1_as_xarray_dask(): |
| 56 |
lon1, lat1, satz1 = _load_h5_geo_vars('lon_1km', 'lat_1km', 'satz_1km') |
| 57 |
lon5 = lon1[2::5, 2::5] |
| 58 |
lat5 = lat1[2::5, 2::5] |
| 59 |
satz5 = satz1[2::5, 2::5] |
| 60 |
return _to_da(lon5), _to_da(lat5), _to_da(satz5) |
| 61 |
|
| 62 |
|
| 63 |
def load_l2_5km_lonlat_satz1_as_xarray_dask(): |
| 64 |
lon1, lat1, satz1 = _load_h5_geo_vars('lon_1km', 'lat_1km', 'satz_1km') |
| 65 |
lon5 = lon1[2::5, 2:-5:5] |
| 66 |
lat5 = lat1[2::5, 2:-5:5] |
| 67 |
satz5 = satz1[2::5, 2:-5:5] |
| 68 |
return _to_da(lon5), _to_da(lat5), _to_da(satz5) |
| 69 |
|
| 70 |
|
| 71 |
def load_500m_lonlat_expected_as_xarray_dask(): |
| 72 |
h5f = h5py.File(FILENAME_DATA, 'r') |
| 73 |
lon500 = _to_da(h5f['lon_500m']) |
| 74 |
lat500 = _to_da(h5f['lat_500m']) |
| 75 |
return lon500, lat500 |
| 76 |
|
| 77 |
|
| 78 |
def load_250m_lonlat_expected_as_xarray_dask(): |
| 79 |
h5f = h5py.File(FILENAME_DATA, 'r') |
| 80 |
lon250 = _to_da(h5f['lon_250m']) |
| 81 |
lat250 = _to_da(h5f['lat_250m']) |
| 82 |
return lon250, lat250 |
| 83 |
|
| 84 |
|
| 85 |
def assert_geodetic_distance( |
| 86 |
lons_actual: np.ndarray, |
| 87 |
lats_actual: np.ndarray, |
| 88 |
lons_desired: np.ndarray, |
| 89 |
lats_desired: np.ndarray, |
| 90 |
max_distance_diff: float, |
| 91 |
) -> None: |
| 92 |
"""Check that the geodetic distance between two sets of coordinates is smaller than a threshold. |
| 93 |
|
| 94 |
Args: |
| 95 |
lons_actual: Longitude array produced by interpolation being tested. |
| 96 |
lats_actual: Latitude array produced by interpolation being tested. |
| 97 |
lons_desired: Longitude array of expected/truth coordinates. |
| 98 |
lats_desired: Latitude array of expected/truth coordinates. |
| 99 |
max_distance_diff: Limit of allowed distance difference in meters. |
| 100 |
|
| 101 |
""" |
| 102 |
g = Geod(ellps="WGS84") |
| 103 |
_, _, dist = g.inv(lons_actual, lats_actual, lons_desired, lats_desired) |
| 104 |
np.testing.assert_array_less( |
| 105 |
dist, max_distance_diff, |
| 106 |
err_msg=f"Coordinates are greater than {max_distance_diff} geodetic " |
| 107 |
"meters from the expected coordinates.") |
| 108 |
|
| 109 |
|
| •110 |
@pytest.mark.skipif(not os.path.isfile(FILENAME_DATA), reason='data file not available') |
| 111 |
@pytest.mark.parametrize( |
| 112 |
("input_func", "exp_func", "interp_func", "dist_max", "exp_5km_warning"), |
| 113 |
[ |
| 114 |
(load_1km_lonlat_satz_as_xarray_dask, load_500m_lonlat_expected_as_xarray_dask, modis_1km_to_500m, 5, False), |
| 115 |
(load_1km_lonlat_satz_as_xarray_dask, load_250m_lonlat_expected_as_xarray_dask, modis_1km_to_250m, 8.30, False), |
| 116 |
(load_5km_lonlat_satz1_as_xarray_dask, load_1km_lonlat_as_xarray_dask, modis_5km_to_1km, 25, False), |
| 117 |
(load_l2_5km_lonlat_satz1_as_xarray_dask, load_1km_lonlat_as_xarray_dask, modis_5km_to_1km, 110, False), |
| 118 |
(load_5km_lonlat_satz1_as_xarray_dask, load_500m_lonlat_expected_as_xarray_dask, modis_5km_to_500m, |
| 119 |
19500, True), |
| 120 |
(load_5km_lonlat_satz1_as_xarray_dask, load_250m_lonlat_expected_as_xarray_dask, modis_5km_to_250m, |
| 121 |
25800, True), |
| 122 |
] |
| 123 |
) |
| 124 |
def test_sat_angle_based_interp(input_func, exp_func, interp_func, dist_max, exp_5km_warning): |
| 125 |
lon1, lat1, satz1 = input_func() |
| 126 |
lons_exp, lats_exp = exp_func() |
| 127 |
|
| 128 |
# when working with dask arrays, we shouldn't compute anything |
| 129 |
with dask.config.set(scheduler=CustomScheduler(0)), warnings.catch_warnings(record=True) as warns: |
| 130 |
lons, lats = interp_func(lon1, lat1, satz1) |
| 131 |
has_5km_warning = any("may result in poor quality" in str(w.message) for w in warns) |
| 132 |
if exp_5km_warning: |
| 133 |
assert has_5km_warning |
| 134 |
else: |
| 135 |
assert not has_5km_warning |
| 136 |
|
| 137 |
if hasattr(lons, "compute"): |
| 138 |
lons, lats = da.compute(lons, lats) |
| 139 |
assert_geodetic_distance(lons, lats, lons_exp, lats_exp, dist_max) |
| 140 |
assert not np.any(np.isnan(lons)) |
| 141 |
assert not np.any(np.isnan(lats)) |
| 142 |
|
| 143 |
|
| •144 |
@pytest.mark.skipif(not os.path.isfile(FILENAME_DATA), reason='data file not available') |
| 145 |
def test_sat_angle_based_interp_nan_handling(): |
| 146 |
# See GH #19 |
| 147 |
lon1, lat1, satz1 = load_1km_lonlat_satz_as_xarray_dask() |
| 148 |
satz1 = _to_da(abs(np.linspace(-65.4, 65.4, 1354, dtype=np.float32)).repeat(20).reshape(-1, 20).T) |
| 149 |
lons, lats = modis_1km_to_500m(lon1, lat1, satz1) |
| 150 |
assert not np.any(np.isnan(lons.compute())) |
| 151 |
assert not np.any(np.isnan(lats.compute())) |
| 152 |
|
| 153 |
|
| •154 |
@pytest.mark.skipif(not os.path.isfile(FILENAME_DATA), reason='data file not available') |
| 155 |
def test_poles_datum(): |
| 156 |
orig_lon, lat1, satz1 = load_1km_lonlat_satz_as_xarray_dask() |
| 157 |
lon1 = orig_lon + 180 |
| 158 |
lon1 = xr.where(lon1 > 180, lon1 - 360, lon1) |
| 159 |
|
| 160 |
lat5 = lat1[2::5, 2::5] |
| 161 |
lon5 = lon1[2::5, 2::5] |
| 162 |
satz5 = satz1[2::5, 2::5] |
| 163 |
lons, lats = modis_5km_to_1km(lon5, lat5, satz5) |
| 164 |
|
| 165 |
lons = lons + 180 |
| 166 |
lons = xr.where(lons > 180, lons - 360, lons) |
| 167 |
assert_geodetic_distance(lons, lats, orig_lon, lat1, 25.0) |