From ec3683dac5ed7e545e87f417531d7b65cd867dd4 Mon Sep 17 00:00:00 2001 From: Julieanna Bacon Date: Sun, 17 Mar 2024 23:16:51 -0400 Subject: [PATCH] test geolocation utility: Added tests for find closest index in grid --- icedyno/preprocess/geolocation.py | 8 +++----- tests/test_geolocation.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/icedyno/preprocess/geolocation.py b/icedyno/preprocess/geolocation.py index cecdca3..0e5ba63 100644 --- a/icedyno/preprocess/geolocation.py +++ b/icedyno/preprocess/geolocation.py @@ -128,10 +128,10 @@ def polar_lonlat_to_xy( return x, y -def find_closest_index_in_grid(target: float, coordinates: np.array) -> int: +def find_closest_index_in_grid(target: float) -> int: """ - For IMS 1km data, given a target coordinate in projected coordinates (x or y) and the list of coordinates, - return the index of the closest number in the coordinates. Assumes a 1km grid. + For IMS 1km data, given a target coordinate in projected coordinates (x or y), + return the index of the closest number in the IMS coordinates. Assumes a 1km grid. """ start = -12287500.0 @@ -141,6 +141,4 @@ def find_closest_index_in_grid(target: float, coordinates: np.array) -> int: # Calculate the index of the closest number index = int((target - start) // grid_resolution) - # If the solution is correct, the target and the found value should never be more than the grid_resolution apart. - # assert abs(coordinates[index] - target) < grid_resolution return index diff --git a/tests/test_geolocation.py b/tests/test_geolocation.py index 1630843..c2c9ce5 100644 --- a/tests/test_geolocation.py +++ b/tests/test_geolocation.py @@ -17,3 +17,21 @@ def test_xy_to_latlon_inversion(): reverted_lat_lon = geolocation.polar_xy_to_lonlat(*xy_converted_coordinates) assert np.allclose((lon, lat), reverted_lat_lon) + + +def test_find_closest_index_in_grid(): + # Grid parameters for IMS data + start = -12287500.0 + stop = 12287500.0 + grid_resolution = 1000.0 + coordinates = np.arange(start=start, stop=stop + 1000.0, step=grid_resolution) + + test_val = -1400.0 + index = geolocation.find_closest_index_in_grid(test_val) + assert np.isclose(coordinates[index], -1500.0) + + test_vals = np.random.uniform(start, stop, 10) + for test_val in test_vals: + index = geolocation.find_closest_index_in_grid(test_val) + # If the solution is correct, the target and the found value should never be more than the grid_resolution apart. + assert abs(coordinates[index] - test_val) < grid_resolution