|
24 | 24 | import numpy as np |
25 | 25 | import pytest |
26 | 26 | import xarray as xr |
| 27 | +from pykdtree.kdtree import KDTree |
27 | 28 | from pytest_lazy_fixtures import lf |
28 | 29 |
|
29 | 30 | from pyresample.future.geometry import AreaDefinition, SwathDefinition |
30 | 31 | from pyresample.future.resamplers import KDTreeNearestXarrayResampler |
| 32 | +from pyresample.future.resamplers._transform_utils import lonlat2xyz |
| 33 | +from pyresample.future.resamplers.nearest import query_no_distance |
31 | 34 | from pyresample.test.utils import assert_maximum_dask_computes, assert_warnings_contain, catch_warnings |
32 | 35 | from pyresample.utils.errors import PerformanceWarning |
33 | 36 |
|
@@ -300,3 +303,71 @@ def test_inconsistent_input_shapes(self, src_geom, match, call_precompute, |
300 | 303 | resampler.precompute(mask=data_2d_float32_xarray_dask.notnull()) |
301 | 304 | else: |
302 | 305 | resampler.resample(data_2d_float32_xarray_dask) |
| 306 | + |
| 307 | + |
| 308 | +class TestQueryNoDistance: |
| 309 | + """Tests for direct KDTree query index remapping.""" |
| 310 | + |
| 311 | + def test_unselected_and_oob_are_minus_one(self): |
| 312 | + voi = np.array([[True, False], [True, False]]) |
| 313 | + tlons = np.array([[0.0, 0.0], [10.0, 0.0]], dtype=np.float64) |
| 314 | + tlats = np.zeros_like(tlons) |
| 315 | + |
| 316 | + src_lons = np.array([0.0], dtype=np.float64) |
| 317 | + src_lats = np.array([0.0], dtype=np.float64) |
| 318 | + src_xyz = lonlat2xyz(src_lons, src_lats).astype(np.float64, copy=False) |
| 319 | + kdtree = KDTree(src_xyz) |
| 320 | + |
| 321 | + res = query_no_distance( |
| 322 | + tlons, |
| 323 | + tlats, |
| 324 | + voi, |
| 325 | + neighbours=1, |
| 326 | + epsilon=0.0, |
| 327 | + radius=1.0, # meters; only exact match is within this ROI |
| 328 | + kdtree=kdtree, |
| 329 | + ) |
| 330 | + |
| 331 | + np.testing.assert_array_equal(res[..., 0], np.array([[0, -1], [-1, -1]])) |
| 332 | + |
| 333 | + def test_forwards_filtered_source_mask(self): |
| 334 | + voi = np.array([[True]]) |
| 335 | + |
| 336 | + src_lons = np.array([[0.0, 0.0001], [0.0002, 0.0003]], dtype=np.float64) |
| 337 | + src_lats = np.zeros_like(src_lons) |
| 338 | + valid_input_index = np.array([[True, True], [True, False]]) |
| 339 | + |
| 340 | + src_xyz = lonlat2xyz(src_lons, src_lats).astype(np.float64, copy=False) |
| 341 | + kdtree = KDTree(src_xyz[valid_input_index.ravel()]) |
| 342 | + |
| 343 | + target_lons = np.array([[0.0]], dtype=np.float64) |
| 344 | + target_lats = np.array([[0.0]], dtype=np.float64) |
| 345 | + |
| 346 | + res_unmasked = query_no_distance( |
| 347 | + target_lons, |
| 348 | + target_lats, |
| 349 | + voi, |
| 350 | + neighbours=1, |
| 351 | + epsilon=0.0, |
| 352 | + radius=1000.0, |
| 353 | + kdtree=kdtree, |
| 354 | + ) |
| 355 | + |
| 356 | + # Mask out the nearest source point (after valid_input_index filtering). |
| 357 | + source_mask = np.array([[True, False], [False, True]]) |
| 358 | + res_masked = query_no_distance( |
| 359 | + target_lons, |
| 360 | + target_lats, |
| 361 | + voi, |
| 362 | + mask=source_mask, |
| 363 | + valid_input_index=valid_input_index, |
| 364 | + neighbours=1, |
| 365 | + epsilon=0.0, |
| 366 | + radius=1000.0, |
| 367 | + kdtree=kdtree, |
| 368 | + ) |
| 369 | + |
| 370 | + assert res_unmasked.shape == (1, 1, 1) |
| 371 | + assert res_masked.shape == (1, 1, 1) |
| 372 | + assert res_unmasked[0, 0, 0] == 0 |
| 373 | + assert res_masked[0, 0, 0] == 1 |
0 commit comments