|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from dominosee.grid import ( |
| 5 | + deg_to_equatorial_distance, |
| 6 | + equatorial_distance_to_deg, |
| 7 | + neighbour_distance, |
| 8 | + geo_distance, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +def test_deg_equatorial_roundtrip_values(): |
| 13 | + for deg in [0.5, 1.0, 5.0, 10.0, 30.0]: |
| 14 | + d = deg_to_equatorial_distance(deg) |
| 15 | + back = equatorial_distance_to_deg(d) |
| 16 | + assert pytest.approx(back, rel=1e-6, abs=1e-8) == deg |
| 17 | + |
| 18 | + |
| 19 | +def test_known_value_5deg_equator(): |
| 20 | + # 5 degrees at equator is circumference * 5/360 |
| 21 | + d = deg_to_equatorial_distance(5.0) |
| 22 | + # Use expected from formula to avoid hardcoding magic numbers |
| 23 | + R = 6371.0 |
| 24 | + expected = 2 * np.pi * R * (5.0 / 360.0) |
| 25 | + assert pytest.approx(d, rel=1e-6, abs=1e-6) == expected |
| 26 | + # And inverse |
| 27 | + back = equatorial_distance_to_deg(d) |
| 28 | + assert pytest.approx(back, rel=1e-9, abs=1e-9) == 5.0 |
| 29 | + |
| 30 | + |
| 31 | +def test_invalid_inputs_raise(): |
| 32 | + with pytest.raises(ValueError): |
| 33 | + deg_to_equatorial_distance(0) |
| 34 | + with pytest.raises(ValueError): |
| 35 | + deg_to_equatorial_distance(-1) |
| 36 | + with pytest.raises(ValueError): |
| 37 | + equatorial_distance_to_deg(0) |
| 38 | + with pytest.raises(ValueError): |
| 39 | + equatorial_distance_to_deg(-10) |
| 40 | + |
| 41 | + |
| 42 | +def test_neighbour_distance_on_equator_line(): |
| 43 | + # Three points on equator along longitude: (0,0), (1,0), (2,0) |
| 44 | + grid = { |
| 45 | + 'lon': np.array([0.0, 1.0, 2.0]), |
| 46 | + 'lat': np.array([0.0, 0.0, 0.0]), |
| 47 | + } |
| 48 | + nd = neighbour_distance(grid) |
| 49 | + assert nd.shape == (3,) |
| 50 | + expected = deg_to_equatorial_distance(1.0) |
| 51 | + assert pytest.approx(nd[0], rel=1e-6) == expected |
| 52 | + assert pytest.approx(nd[1], rel=1e-6) == expected |
| 53 | + assert pytest.approx(nd[2], rel=1e-6) == expected |
| 54 | + |
| 55 | + |
| 56 | +def test_neighbour_distance_on_unit_rectangle(): |
| 57 | + # Four points forming a 1x1 degree rectangle near equator |
| 58 | + # (0,0), (0,1), (1,0), (1,1) |
| 59 | + grid = { |
| 60 | + 'lon': np.array([0.0, 0.0, 1.0, 1.0]), |
| 61 | + 'lat': np.array([0.0, 1.0, 0.0, 1.0]), |
| 62 | + } |
| 63 | + nd = neighbour_distance(grid) |
| 64 | + assert nd.shape == (4,) |
| 65 | + # Expected nearest distances per point |
| 66 | + # (0,0): min to (0,1) or (1,0) at equator |
| 67 | + e00 = min(geo_distance(0.0, 0.0, 0.0, 1.0), geo_distance(0.0, 0.0, 1.0, 0.0)) |
| 68 | + # (0,1): min to (0,0) (lat diff) or (1,1) (lon diff at 1° lat) |
| 69 | + e01 = min(geo_distance(0.0, 1.0, 0.0, 0.0), geo_distance(0.0, 1.0, 1.0, 1.0)) |
| 70 | + # (1,0): min to (0,0) or (1,1) |
| 71 | + e10 = min(geo_distance(1.0, 0.0, 0.0, 0.0), geo_distance(1.0, 0.0, 1.0, 1.0)) |
| 72 | + # (1,1): min to (1,0) (lat diff) or (0,1) (lon diff at 1° lat) |
| 73 | + e11 = min(geo_distance(1.0, 1.0, 1.0, 0.0), geo_distance(1.0, 1.0, 0.0, 1.0)) |
| 74 | + expected = np.array([e00, e01, e10, e11]) |
| 75 | + assert np.allclose(nd, expected, rtol=1e-6) |
0 commit comments