Debian Codegraph @

geotiepoints/tests/test_geointerpolator.py

raw
1 """Tests for GeoInterpolator."""
2
3 import unittest
4
5 import numpy as np
6 import pytest
7 from pyresample.geometry import SwathDefinition
8
9 from geotiepoints.geointerpolator import GeoInterpolator, GeoGridInterpolator, GeoSplineInterpolator
10
11 TIES_EXP1 = np.array([[6384905.78040055, 6381081.08333225, 6371519.34066148,
12 6328950.00792935, 6253610.69157758, 6145946.19489936,
13 6124413.29556372],
14 [6377591.95940176, 6370997., 6354509.6014956,
15 6305151.62592155, 6223234.99818839, 6109277.14889072,
16 6086485.57903118],
17 [6359307.40690478, 6345786.79166939, 6311985.2535809,
18 6245655.67090206, 6147295.76471541, 6017604.5338691,
19 5991666.28769983],
20 [6351993.58590599, 6335702.70833714, 6294975.51441502,
21 6221857.28889426, 6116920.07132621, 5980935.48786045,
22 5953738.5711673],
23 [6338032.26190294, 6320348.4990906, 6276139.09205974,
24 6199670.56624433, 6091551.90273768, 5952590.38414781,
25 5924798.08042984],
26 [6290665.5946295, 6270385.16249031, 6219684.08214232,
27 6137100.75832981, 6023313.2794414, 5879194.72399075,
28 5850371.01290062],
29 [6172248.92644589, 6145476.82098957, 6078546.55734877,
30 5980676.23854351, 5852716.72120069, 5695705.57359808,
31 5664303.34407756],
32 [6124882.25917245, 6095513.48438928, 6022091.54743135,
33 5918106.430629, 5784478.09790441, 5622309.91344102,
34 5589876.27654834]])
35
36 TIES_EXP2 = np.array([[6372937.31273379, 6370997., 6366146.21816553,
37 6351605.98629588, 6327412.61244969, 6293626.50067273,
38 6286869.27831734],
39 [6353136.46335726, 6345786.79166939, 6327412.61244969,
40 6299445.69529922, 6261968.60390423, 6215087.60607344,
41 6205711.40650728]])
42
43 TIES_EXP4 = np.array([[6381081.08333225, 6381639.66045187, 6372470.10269454,
44 6353590.21586788, 6325042.05851245],
45 [6370997., 6366146.21816553, 6351605.98629588,
46 6327412.61244969, 6293626.50067273],
47 [6345786.79166939, 6327412.61244969, 6299445.69529922,
48 6261968.60390423, 6215087.60607344],
49 [6335702.70833714, 6311919.17016336, 6278581.57890056,
50 6235791.00048604, 6183672.04823372]])
51
52 TIES_EXP5 = np.array([[6381081.08333225, 6371519.34066148, 6328950.00792935,
53 6253610.69157758, 6145946.19489936],
54 [6370997., 6354509.6014956, 6305151.62592155,
55 6223234.99818839, 6109277.14889072],
56 [6345786.79166939, 6311985.2535809, 6245655.67090206,
57 6147295.76471541, 6017604.5338691],
58 [6270385.16249031, 6219684.08214232, 6137100.75832981,
59 6023313.2794414, 5879194.72399075],
60 [6145476.82098957, 6078546.55734877, 5980676.23854351,
61 5852716.72120069, 5695705.57359808],
62 [6095513.48438928, 6022091.54743135, 5918106.430629,
63 5784478.09790441, 5622309.91344102]])
64
65 TIES_EXP6 = np.array([[6381081.08333225, 6371519.34066148, 6328950.00792935,
66 6253610.69157758, 6145946.19489936],
67 [6370997., 6354509.6014956, 6305151.62592155,
68 6223234.99818839, 6109277.14889072],
69 [6345786.79166939, 6311985.2535809, 6245655.67090206,
70 6147295.76471541, 6017604.5338691],
71 [6335702.70833714, 6294975.51441502, 6221857.28889426,
72 6116920.07132621, 5980935.48786045],
73 [6320348.4990906, 6276139.09205974, 6199670.56624433,
74 6091551.90273768, 5952590.38414781],
75 [6270385.16249031, 6219684.08214232, 6137100.75832981,
76 6023313.2794414, 5879194.72399075],
77 [6145476.82098957, 6078546.55734877, 5980676.23854351,
78 5852716.72120069, 5695705.57359808],
79 [6095513.48438928, 6022091.54743135, 5918106.430629,
80 5784478.09790441, 5622309.91344102]])
81
82 TIES_EXP7 = np.array([[6372937.31273379, 6370997., 6366146.21816553,
83 6351605.98629588, 6327412.61244969, 6293626.50067273,
84 6286869.27831734],
85 [6353136.46335726, 6345786.79166939, 6327412.61244969,
86 6299445.69529922, 6261968.60390423, 6215087.60607344,
87 6205711.40650728]])
88
89
90 class TestGeoInterpolator(unittest.TestCase):
91 """Class for unit testing the ancillary interpolation functions."""
92
93 def test_fillborders(self):
94 """Test filling borders."""
95 lons = np.arange(20).reshape((4, 5), order="F")
96 lats = np.arange(20).reshape((4, 5), order="C")
97 lines = np.array([2, 7, 12, 17]) / 5.0
98 cols = np.array([2, 7, 12, 17, 22])
99 hlines = np.arange(20) / 5.0
100 hcols = np.arange(24)
101 satint = GeoInterpolator(
102 (lons, lats), (lines, cols), (hlines, hcols), chunk_size=10)
103 satint.fill_borders('x', 'y')
104
105 np.testing.assert_allclose(satint.tie_data[0], TIES_EXP1)
106 np.testing.assert_allclose(satint.row_indices, np.array(
107 [0, 2, 7, 9, 10, 12, 17, 19]) / 5.0)
108 self.assertTrue(
109 np.allclose(satint.col_indices, np.array([0, 2, 7, 12, 17, 22, 23])))
110
111 def test_extrapolate_cols(self):
112 """Test extrapolating columns."""
113 lons = np.arange(10).reshape((2, 5), order="F")
114 lats = np.arange(10).reshape((2, 5), order="C")
115 lines = np.array([2, 7])
116 cols = np.array([2, 7, 12, 17, 22])
117 hlines = np.arange(10)
118 hcols = np.arange(24)
119 satint = GeoInterpolator((lons, lats), (lines, cols), (hlines, hcols))
120
121 self.assertTrue(np.allclose(satint._extrapolate_cols(satint.tie_data[0]),
122 TIES_EXP2))
123
124 def test_fill_col_borders(self):
125 """Test filling the column borders."""
126 lons = np.arange(10).reshape((2, 5), order="F")
127 lats = np.arange(10).reshape((2, 5), order="C")
128 lines = np.array([2, 7])
129 cols = np.array([2, 7, 12, 17, 22])
130 hlines = np.arange(10)
131 hcols = np.arange(24)
132 satint = GeoInterpolator((lons, lats), (lines, cols), (hlines, hcols))
133 satint._fill_col_borders()
134 np.testing.assert_allclose(satint.tie_data[0], TIES_EXP7)
135 np.testing.assert_allclose(satint.col_indices,
136 np.array([0, 2, 7, 12, 17, 22, 23]))
137
138 def test_extrapolate_rows(self):
139 """Test extrapolation of rows."""
140 lons = np.arange(10).reshape((2, 5), order="F")
141 lats = np.arange(10).reshape((2, 5), order="C")
142 lines = np.array([2, 7])
143 cols = np.array([2, 7, 12, 17, 22])
144 hlines = np.arange(10)
145 hcols = np.arange(24)
146 satint = GeoInterpolator((lons, lats), (lines, cols), (hlines, hcols))
147 np.testing.assert_allclose(satint._extrapolate_rows(satint.tie_data[0],
148 hlines, -0.4, 9.4),
149 TIES_EXP4)
150
151 def test_fill_row_borders(self):
152 """Test filling the row borders."""
153 lons = np.arange(20).reshape((4, 5), order="F")
154 lats = np.arange(20).reshape((4, 5), order="C")
155 lines = np.array([2, 7, 12, 17]) / 5.0
156 cols = np.array([2, 7, 12, 17, 22])
157 hlines = np.arange(20) / 5.0
158 hcols = np.arange(24)
159 satint = GeoInterpolator((lons, lats), (lines, cols), (hlines, hcols))
160 satint._fill_row_borders()
161 np.testing.assert_allclose(satint.tie_data[0],
162 TIES_EXP5)
163 np.testing.assert_allclose(satint.row_indices,
164 np.array([0, 2, 7, 12, 17, 19]) / 5.0)
165 satint = GeoInterpolator((lons, lats), (lines, cols),
166 (hlines, hcols), chunk_size=10)
167 satint._fill_row_borders()
168 np.testing.assert_allclose(satint.tie_data[0],
169 TIES_EXP6)
170 np.testing.assert_allclose(satint.row_indices,
171 np.array([0, 2, 7, 9, 10, 12, 17, 19]) / 5.0)
172
173
174 TIE_LONS = np.array([[1, 2, 3, 4],
175 [1, 2, 3, 4],
176 [1, 2, 3, 4],
177 [1, 2, 3, 4],
178 [1, 2, 3, 4]])
179
180 TIE_LATS = np.array([[1, 1, 1, 1],
181 [2, 2, 2, 2],
182 [3, 3, 3, 3],
183 [4, 4, 4, 4],
184 [5, 5, 5, 5]])
185
186
187 class TestGeoGridInterpolator:
188 """Test the GeoGridInterpolator."""
189
190 @pytest.mark.parametrize("args", ((TIE_LONS, TIE_LATS),
191 [SwathDefinition(TIE_LONS, TIE_LATS)]
192 ))
193 def test_geogrid_interpolation(self, args):
194 """Test that the interpolator works with both explicit tie-point arrays and swath definition objects."""
195 x_points = np.array([0, 1, 3, 7])
196 y_points = np.array([0, 1, 3, 7, 15])
197
198 interpolator = GeoGridInterpolator((y_points, x_points), *args)
199
200 fine_x_points = np.arange(8)
201 fine_y_points = np.arange(16)
202
203 lons, lats = interpolator.interpolate((fine_y_points, fine_x_points))
204
205 lons_expected = np.array([1., 2., 2.5, 3., 3.25, 3.5, 3.75, 4.])
206 lats_expected = np.array([1., 2., 2.5, 3., 3.25, 3.5, 3.75, 4., 4.125,
207 4.25, 4.375, 4.5, 4.625, 4.75, 4.875, 5.])
208
209 np.testing.assert_allclose(lons[0, :], lons_expected, rtol=5e-5)
210 np.testing.assert_allclose(lats[:, 0], lats_expected, rtol=5e-5)
211
212 def test_geogrid_interpolation_counts_its_arguments(self):
213 """Test that an arbitrary number of argument is not allowed in the interpolator."""
214 with pytest.raises(ValueError):
215 _ = GeoGridInterpolator((None, None), None, None, None)
216
217 def test_geogrid_interpolation_to_shape(self):
218 """Test that the interpolator works with both explicit tie-point arrays and swath definition objects."""
219 x_points = np.array([0, 1, 3, 7])
220 y_points = np.array([0, 1, 3, 7, 15])
221
222 interpolator = GeoGridInterpolator((y_points, x_points), TIE_LONS, TIE_LATS)
223
224 lons, lats = interpolator.interpolate_to_shape((16, 8))
225
226 lons_expected = np.array([1., 2., 2.5, 3., 3.25, 3.5, 3.75, 4.])
227 lats_expected = np.array([1., 2., 2.5, 3., 3.25, 3.5, 3.75, 4., 4.125,
228 4.25, 4.375, 4.5, 4.625, 4.75, 4.875, 5.])
229
230 np.testing.assert_allclose(lons[0, :], lons_expected, rtol=5e-5)
231 np.testing.assert_allclose(lats[:, 0], lats_expected, rtol=5e-5)
232
233 def test_geogrid_interpolation_preserves_dtype(self):
234 """Test that the interpolator works with both explicit tie-point arrays and swath definition objects."""
235 x_points = np.array([0, 1, 3, 7])
236 y_points = np.array([0, 1, 3, 7, 15])
237
238 interpolator = GeoGridInterpolator((y_points, x_points),
239 TIE_LONS.astype(np.float32), TIE_LATS.astype(np.float32))
240
241 lons, lats = interpolator.interpolate_to_shape((16, 8))
242
243 assert lons.dtype == np.float32
244 assert lats.dtype == np.float32
245
246 def test_chunked_geogrid_interpolation(self):
247 """Test that the interpolator works with both explicit tie-point arrays and swath definition objects."""
248 dask = pytest.importorskip("dask")
249
250 x_points = np.array([0, 1, 3, 7])
251 y_points = np.array([0, 1, 3, 7, 15])
252
253 interpolator = GeoGridInterpolator((y_points, x_points),
254 TIE_LONS.astype(np.float32), TIE_LATS.astype(np.float32))
255
256 lons, lats = interpolator.interpolate_to_shape((16, 8), chunks=4)
257
258 assert lons.chunks == ((4, 4, 4, 4), (4, 4))
259 assert lats.chunks == ((4, 4, 4, 4), (4, 4))
260
261 with dask.config.set({"array.chunk-size": 64}):
262
263 lons, lats = interpolator.interpolate_to_shape((16, 8), chunks="auto")
264 assert lons.chunks == ((4, 4, 4, 4), (4, 4))
265 assert lats.chunks == ((4, 4, 4, 4), (4, 4))
266
267 def test_geogrid_interpolation_can_extrapolate(self):
268 """Test that the interpolator can also extrapolate given the right parameters."""
269 x_points = np.array([0, 1, 3, 7])
270 y_points = np.array([0, 1, 3, 7, 15])
271
272 interpolator = GeoGridInterpolator((y_points, x_points), TIE_LONS, TIE_LATS,
273 bounds_error=False, fill_value=None)
274
275 lons, lats = interpolator.interpolate_to_shape((16, 16), method="cubic")
276
277 assert lons.shape == (16, 16)
278
279
280 class TestGeoSplineInterpolator:
281 """Test the GeoGridInterpolator."""
282
283 @pytest.mark.parametrize("args", ((TIE_LONS, TIE_LATS),
284 [SwathDefinition(TIE_LONS, TIE_LATS)]
285 ))
286 def test_geospline_interpolation(self, args):
287 """Test that the interpolator works with both explicit tie-point arrays and swath definition objects."""
288 x_points = np.array([0, 1, 3, 7])
289 y_points = np.array([0, 1, 3, 7, 15])
290
291 interpolator = GeoSplineInterpolator((y_points, x_points), *args, kx=1, ky=1)
292
293 fine_x_points = np.arange(8)
294 fine_y_points = np.arange(16)
295
296 lons, lats = interpolator.interpolate((fine_y_points, fine_x_points))
297
298 lons_expected = np.array([1., 2., 2.5, 3., 3.25, 3.5, 3.75, 4.])
299 lats_expected = np.array([1., 2., 2.5, 3., 3.25, 3.5, 3.75, 4., 4.125,
300 4.25, 4.375, 4.5, 4.625, 4.75, 4.875, 5.])
301
302 np.testing.assert_allclose(lons[0, :], lons_expected, rtol=5e-5)
303 np.testing.assert_allclose(lats[:, 0], lats_expected, rtol=5e-5)
304
305 def test_geospline_interpolation_to_shape(self):
306 """Test that the interpolator works with both explicit tie-point arrays and swath definition objects."""
307 x_points = np.array([0, 1, 3, 7])
308 y_points = np.array([0, 1, 3, 7, 15])
309
310 interpolator = GeoSplineInterpolator((y_points, x_points), TIE_LONS, TIE_LATS, kx=1, ky=1)
311
312 lons, lats = interpolator.interpolate_to_shape((16, 8))
313
314 lons_expected = np.array([1., 2., 2.5, 3., 3.25, 3.5, 3.75, 4.])
315 lats_expected = np.array([1., 2., 2.5, 3., 3.25, 3.5, 3.75, 4., 4.125,
316 4.25, 4.375, 4.5, 4.625, 4.75, 4.875, 5.])
317
318 np.testing.assert_allclose(lons[0, :], lons_expected, rtol=5e-5)
319 np.testing.assert_allclose(lats[:, 0], lats_expected, rtol=5e-5)
320
321 def test_geospline_interpolation_preserves_dtype(self):
322 """Test that the interpolator works with both explicit tie-point arrays and swath definition objects."""
323 x_points = np.array([0, 1, 3, 7])
324 y_points = np.array([0, 1, 3, 7, 15])
325
326 interpolator = GeoGridInterpolator((y_points, x_points),
327 TIE_LONS.astype(np.float32), TIE_LATS.astype(np.float32))
328
329 lons, lats = interpolator.interpolate_to_shape((16, 8))
330
331 assert lons.dtype == np.float32
332 assert lats.dtype == np.float32
333
334 def test_chunked_geospline_interpolation(self):
335 """Test that the interpolator works with both explicit tie-point arrays and swath definition objects."""
336 dask = pytest.importorskip("dask")
337
338 x_points = np.array([0, 1, 3, 7])
339 y_points = np.array([0, 1, 3, 7, 15])
340
341 interpolator = GeoGridInterpolator((y_points, x_points),
342 TIE_LONS.astype(np.float32), TIE_LATS.astype(np.float32))
343
344 lons, lats = interpolator.interpolate_to_shape((16, 8), chunks=4)
345
346 assert lons.chunks == ((4, 4, 4, 4), (4, 4))
347 assert lats.chunks == ((4, 4, 4, 4), (4, 4))
348
349 with dask.config.set({"array.chunk-size": 64}):
350
351 lons, lats = interpolator.interpolate_to_shape((16, 8), chunks="auto")
352 assert lons.chunks == ((4, 4, 4, 4), (4, 4))
353 assert lats.chunks == ((4, 4, 4, 4), (4, 4))