Skip to content

Commit

Permalink
test geolocation utility: Added tests for find closest index in grid
Browse files Browse the repository at this point in the history
  • Loading branch information
JuLieAlgebra committed Mar 18, 2024
1 parent 6d1c5f3 commit ec3683d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
8 changes: 3 additions & 5 deletions icedyno/preprocess/geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
18 changes: 18 additions & 0 deletions tests/test_geolocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ec3683d

Please sign in to comment.