| 1 |
"""Test of the interpolation of geographical tiepoints for the VII products. |
| 2 |
|
| 3 |
It follows the description provided in document "EPS-SG VII Level 1B Product Format Specification V4A". |
| 4 |
This version is compatible for vii (METimage) test data version V2 (Jan 2022). It is not back compatible |
| 5 |
with V1. |
| 6 |
|
| 7 |
""" |
| 8 |
|
| 9 |
import unittest |
| 10 |
import numpy as np |
| 11 |
import xarray as xr |
| 12 |
import pytest |
| 13 |
from geotiepoints.viiinterpolator import tie_points_interpolation, tie_points_geo_interpolation |
| 14 |
|
| 15 |
|
| 16 |
TEST_N_SCANS = 2 |
| 17 |
TEST_TIE_POINTS_FACTOR = 2 |
| 18 |
TEST_SCAN_ALT_TIE_POINTS = 3 |
| 19 |
TEST_VALID_ALT_TIE_POINTS = TEST_SCAN_ALT_TIE_POINTS * TEST_N_SCANS |
| 20 |
TEST_INVALID_ALT_TIE_POINTS = TEST_SCAN_ALT_TIE_POINTS * TEST_N_SCANS + 1 |
| 21 |
TEST_ACT_TIE_POINTS = 4 |
| 22 |
|
| 23 |
# Results of latitude/longitude interpolation with simple interpolation on coordinates |
| 24 |
TEST_LON_1 = np.array( |
| 25 |
[[-12., -11.5, -11., -10.5, -10.0, -9.5], |
| 26 |
[-10., -9.5, -9., -8.5, -8.0, -7.5], |
| 27 |
[-8., -7.5, -7., -6.5, -6.0, -5.5], |
| 28 |
[-6., -5.5, -5., -4.5, -4.0, -3.5], |
| 29 |
[0., 0.5, 1., 1.5, 2.0, 2.5], |
| 30 |
[2., 2.5, 3., 3.5, 4.0, 4.5], |
| 31 |
[4., 4.5, 5., 5.5, 6.0, 6.5], |
| 32 |
[6., 6.5, 7., 7.5, 8.0, 8.5]] |
| 33 |
) |
| 34 |
TEST_LAT_1 = np.array( |
| 35 |
[[0., 0.5, 1., 1.5, 2., 2.5], |
| 36 |
[2., 2.5, 3., 3.5, 4., 4.5], |
| 37 |
[4., 4.5, 5., 5.5, 6., 6.5], |
| 38 |
[6., 6.5, 7., 7.5, 8., 8.5], |
| 39 |
[12., 12.5, 13., 13.5, 14.0, 14.5], |
| 40 |
[14., 14.5, 15., 15.5, 16., 16.5], |
| 41 |
[16., 16.5, 17., 17.5, 18., 18.5], |
| 42 |
[18., 18.5, 19., 19.5, 20., 20.5]] |
| 43 |
) |
| 44 |
|
| 45 |
# Results of latitude/longitude interpolation on cartesian coordinates (latitude above 60 degrees) |
| 46 |
TEST_LON_2 = np.array( |
| 47 |
[[-12., -11.50003808, -11., -10.50011426, -10., -9.50019052], |
| 48 |
[-10.00243991, -9.5032411, -9.00366173, -8.50454031, -8.00488578, -7.5058423], |
| 49 |
[-8., -7.50034342, -7., -6.50042016, -6., -5.50049716], |
| 50 |
[-6.00734362, -5.50845783, -5.00857895, -4.50977302, -4.00981958, -3.51109426], |
| 51 |
[0., 0.49903263, 1., 1.49895241, 2., 2.49887151], |
| 52 |
[1.98257947, 2.48080192, 2.98127841, 3.47941324, 3.97996512, 4.47801105], |
| 53 |
[4., 4.49870746, 5., 5.49862418, 6., 6.49853998], |
| 54 |
[5.97729789, 6.47516183, 6.97594186, 7.47371256, 7.97456943, 8.47224525]] |
| 55 |
) |
| 56 |
|
| 57 |
TEST_LAT_2 = np.array( |
| 58 |
[[0., 0.49998096, 1., 1.49994287, 2., 2.49990475], |
| 59 |
[1.99878116, 2.49838091, 2.99817081, 3.49773189, 3.99755935, 4.49708148], |
| 60 |
[4., 4.4998283, 5., 5.49978993, 6., 6.49975143], |
| 61 |
[5.99633155, 6.4957749, 6.99571447, 7.49511791, 7.99509473, 8.49445789], |
| 62 |
[12., 12.49951634, 13., 13.49947623, 14., 14.499435779], |
| 63 |
[13.99129786, 14.4904098, 14.99064796, 15.48971613, 15.98999196, 16.48901572], |
| 64 |
[16., 16.49935377, 17., 17.49931213, 18., 18.49927003], |
| 65 |
[17.98865968, 18.48759253, 18.98798235, 19.48686863, 19.98729684, 20.48613573]] |
| 66 |
) |
| 67 |
|
| 68 |
# Results of latitude/longitude interpolation on cartesian coordinates (longitude with a 360 degrees step) |
| 69 |
TEST_LON_3 = np.array( |
| 70 |
[[-12., -11.50444038, -11., -10.50459822, -10., -9.50476197], |
| 71 |
[-10.07492627, -9.58101155, -9.07759836, -8.5839056, - 8.0803761, -7.58691614], |
| 72 |
[-8., -7.50510905, -7., -6.5052934, -6., -5.50548573], |
| 73 |
[-6.0862821, -5.59332416, -5.08942935, -4.59674283, -4.09272043, -3.60032066], |
| 74 |
[0., 0.49315061, 1., 1.49287934, 2., 2.49259217], |
| 75 |
[1.88371709, 2.3739768, 2.87898193, 3.36879304, 3.87395153, 4.36327924], |
| 76 |
[4., 4.49196335, 5., 5.49161771, 6., 6.49124808], |
| 77 |
[5.86287282, 6.35111105, 6.85674573, 7.3443667, 7.85016382, 8.33710998]] |
| 78 |
) |
| 79 |
|
| 80 |
TEST_LAT_3 = np.array( |
| 81 |
[[45., 45.49777998, 46., 46.49770107, 47., 47.4976192], |
| 82 |
[46.96258417, 47.4595462, 47.9612508, 48.4581022, 48.95986481, 49.45660021], |
| 83 |
[49., 49.49744569, 50., 50.49735352, 51., 51.49725738], |
| 84 |
[50.95691833, 51.45340364, 51.95534841, 52.45169855, 52.95370691, 53.55477452], |
| 85 |
[57., 57.50277937, 58., 58.50267347, 59., 59.50256981], |
| 86 |
[59.04185095, 59.54357898, 60.04020844, 60.54185139, 61.03859839, 61.54015723], |
| 87 |
[61., 61.5023687, 62., 62.502271, 63., 63.50217506], |
| 88 |
[63.03546804, 63.53686131, 64.03394419, 64.53525587, 65.03244567, 65.53367647]] |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
class TestViiInterpolator(unittest.TestCase): |
| 93 |
"""Test the vii_utils module.""" |
| 94 |
|
| 95 |
def setUp(self): |
| 96 |
"""Set up the test.""" |
| 97 |
# Create the arrays for the interpolation test |
| 98 |
# The first has a valid number of n_tie_alt points (multiple of SCAN_ALT_TIE_POINTS) |
| 99 |
self.valid_data_for_interpolation = xr.DataArray( |
| 100 |
np.arange( |
| 101 |
TEST_VALID_ALT_TIE_POINTS * TEST_ACT_TIE_POINTS, |
| 102 |
dtype=np.float64, |
| 103 |
).reshape(TEST_VALID_ALT_TIE_POINTS, TEST_ACT_TIE_POINTS), |
| 104 |
dims=('num_tie_points_alt', 'num_tie_points_act'), |
| 105 |
) |
| 106 |
# The second has an invalid number of n_tie_alt points (not multiple of SCAN_ALT_TIE_POINTS) |
| 107 |
self.invalid_data_for_interpolation = xr.DataArray( |
| 108 |
np.arange( |
| 109 |
TEST_INVALID_ALT_TIE_POINTS * TEST_ACT_TIE_POINTS, |
| 110 |
dtype=np.float64, |
| 111 |
).reshape(TEST_INVALID_ALT_TIE_POINTS, TEST_ACT_TIE_POINTS), |
| 112 |
dims=('num_tie_points_alt', 'num_tie_points_act'), |
| 113 |
) |
| 114 |
# Then two arrays containing valid longitude and latitude data |
| 115 |
self.longitude = xr.DataArray( |
| 116 |
np.linspace( |
| 117 |
-12, |
| 118 |
11, |
| 119 |
num=TEST_VALID_ALT_TIE_POINTS * TEST_ACT_TIE_POINTS, |
| 120 |
dtype=np.float64, |
| 121 |
).reshape(TEST_VALID_ALT_TIE_POINTS, TEST_ACT_TIE_POINTS), |
| 122 |
dims=('num_tie_points_alt', 'num_tie_points_act'), |
| 123 |
) |
| 124 |
self.latitude = xr.DataArray( |
| 125 |
np.linspace( |
| 126 |
0, |
| 127 |
23, |
| 128 |
num=TEST_VALID_ALT_TIE_POINTS * TEST_ACT_TIE_POINTS, |
| 129 |
dtype=np.float64, |
| 130 |
).reshape(TEST_VALID_ALT_TIE_POINTS, TEST_ACT_TIE_POINTS), |
| 131 |
dims=('num_tie_points_alt', 'num_tie_points_act'), |
| 132 |
) |
| 133 |
# Then one containing latitude data above 60 degrees |
| 134 |
self.latitude_over60 = xr.DataArray( |
| 135 |
np.linspace( |
| 136 |
45, |
| 137 |
68, |
| 138 |
num=TEST_VALID_ALT_TIE_POINTS * TEST_ACT_TIE_POINTS, |
| 139 |
dtype=np.float64, |
| 140 |
).reshape(TEST_VALID_ALT_TIE_POINTS, TEST_ACT_TIE_POINTS), |
| 141 |
dims=('num_tie_points_alt', 'num_tie_points_act'), |
| 142 |
) |
| 143 |
# Then one containing longitude data with a 360 degrees step |
| 144 |
self.longitude_over360 = xr.DataArray( |
| 145 |
np.linspace( |
| 146 |
-12, |
| 147 |
11, |
| 148 |
num=TEST_VALID_ALT_TIE_POINTS * TEST_ACT_TIE_POINTS, |
| 149 |
dtype=np.float64, |
| 150 |
).reshape(TEST_VALID_ALT_TIE_POINTS, TEST_ACT_TIE_POINTS) % 360., |
| 151 |
dims=('num_tie_points_alt', 'num_tie_points_act'), |
| 152 |
) |
| 153 |
|
| 154 |
def tearDown(self): |
| 155 |
"""Tear down the test.""" |
| 156 |
# Nothing to do |
| 157 |
pass |
| 158 |
|
| 159 |
def test_tie_points_interpolation(self): |
| 160 |
"""# Test the interpolation routine with valid and invalid input.""" |
| 161 |
# Test the interpolation routine with valid input |
| 162 |
result_valid = tie_points_interpolation( |
| 163 |
[self.valid_data_for_interpolation], |
| 164 |
TEST_SCAN_ALT_TIE_POINTS, |
| 165 |
TEST_TIE_POINTS_FACTOR |
| 166 |
)[0] |
| 167 |
|
| 168 |
act_points_interp = (TEST_ACT_TIE_POINTS - 1) * TEST_TIE_POINTS_FACTOR |
| 169 |
num_scans = TEST_VALID_ALT_TIE_POINTS // TEST_SCAN_ALT_TIE_POINTS |
| 170 |
scan_alt_points_interp = (TEST_SCAN_ALT_TIE_POINTS - 1) * TEST_TIE_POINTS_FACTOR |
| 171 |
|
| 172 |
# Across the track |
| 173 |
delta_axis_0 = [0., 0.5, 1., 1.5, 2., 2.5] |
| 174 |
self.assertTrue(np.allclose(result_valid[0, :], delta_axis_0)) |
| 175 |
# Along track |
| 176 |
delta_axis_1 = [0., 2., 4., 6., 12., 14., 16., 18] |
| 177 |
self.assertTrue(np.allclose(result_valid[:, 0], delta_axis_1)) |
| 178 |
|
| 179 |
# Test the interpolation routine with invalid input |
| 180 |
pytest.raises(ValueError, tie_points_interpolation, |
| 181 |
[self.invalid_data_for_interpolation], |
| 182 |
TEST_SCAN_ALT_TIE_POINTS, |
| 183 |
TEST_TIE_POINTS_FACTOR) |
| 184 |
|
| 185 |
def test_tie_points_geo_interpolation(self): |
| 186 |
"""# Test the coordinates interpolation routine with valid and invalid input.""" |
| 187 |
# Test the interpolation routine with valid input |
| 188 |
lon, lat = tie_points_geo_interpolation( |
| 189 |
self.longitude, |
| 190 |
self.latitude, |
| 191 |
TEST_SCAN_ALT_TIE_POINTS, |
| 192 |
TEST_TIE_POINTS_FACTOR |
| 193 |
) |
| 194 |
self.assertTrue(np.allclose(lon, TEST_LON_1)) |
| 195 |
self.assertTrue(np.allclose(lat, TEST_LAT_1)) |
| 196 |
|
| 197 |
lon, lat = tie_points_geo_interpolation( |
| 198 |
self.longitude_over360, |
| 199 |
self.latitude, |
| 200 |
TEST_SCAN_ALT_TIE_POINTS, |
| 201 |
TEST_TIE_POINTS_FACTOR |
| 202 |
) |
| 203 |
self.assertTrue(np.allclose(lon, TEST_LON_2)) |
| 204 |
self.assertTrue(np.allclose(lat, TEST_LAT_2)) |
| 205 |
|
| 206 |
lon, lat = tie_points_geo_interpolation( |
| 207 |
self.longitude, |
| 208 |
self.latitude_over60, |
| 209 |
TEST_SCAN_ALT_TIE_POINTS, |
| 210 |
TEST_TIE_POINTS_FACTOR |
| 211 |
) |
| 212 |
self.assertTrue(np.allclose(lon, TEST_LON_3)) |
| 213 |
self.assertTrue(np.allclose(lat, TEST_LAT_3)) |
| 214 |
|
| 215 |
# Test the interpolation routine with invalid input (different dimensions of the two arrays) |
| 216 |
with self.assertRaises(ValueError): |
| 217 |
tie_points_geo_interpolation( |
| 218 |
self.longitude, |
| 219 |
self.invalid_data_for_interpolation, |
| 220 |
TEST_SCAN_ALT_TIE_POINTS, |
| 221 |
TEST_TIE_POINTS_FACTOR |
| 222 |
) |