Skip to content
Open
44 changes: 37 additions & 7 deletions test/grid/geometry/test_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ def _get_lonlat_rad_face_edge_nodes_testcase_helper_latlon_bounds_gca(face_nodes
return lonlat_coordinates


def _bounds_from_face_edges(face_edges_cart, face_edges_lonlat, **kwargs):
"""Call _construct_face_bounds using face-edge arrays as input,
similar to the old _construct_face_bounds interface from before solving issue #1896.
Old tests use this function; newer tests should prefer the new interface instead.

face_edges_cart : np.ndarray, shape (n_edges, 2, 3)
Cartesian (x, y, z) of each edge's (start, end) nodes.
face_edges_lonlat : np.ndarray, shape (n_edges, 2, 2)
(lon, lat) in radians of each edge's (start, end) nodes.
**kwargs
Passed to _construct_face_bounds (is_latlonface, is_GCA_list).
"""
# Edge i goes from node i to node i+1, so this helper passes
# node i = face_edges[i, 0] to _construct_face_bounds.
start_xyz = face_edges_cart[:, 0]
start_lonlat = face_edges_lonlat[:, 0]
# Column slices like start_xyz[:, 0] are strided (non-contiguous) views. Numba accepts these,
# but compiles a separate specialization of _construct_face_bounds for non-contiguous arrays.
# Making them contiguous ensures the tests exercise the same compiled code used in production.
return _construct_face_bounds(
np.arange(len(face_edges_cart)),
np.ascontiguousarray(start_xyz[:, 0]),
np.ascontiguousarray(start_xyz[:, 1]),
np.ascontiguousarray(start_xyz[:, 2]),
np.ascontiguousarray(start_lonlat[:, 0]),
np.ascontiguousarray(start_lonlat[:, 1]),
**kwargs,
)


def test_populate_bounds_normal_latlon_bounds_gca():
vertices_lonlat = [[10.0, 60.0], [10.0, 10.0], [50.0, 10.0], [50.0, 60.0]]
vertices_lonlat = np.array(vertices_lonlat)
Expand All @@ -95,7 +125,7 @@ def test_populate_bounds_normal_latlon_bounds_gca():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand Down Expand Up @@ -123,7 +153,7 @@ def test_populate_bounds_antimeridian_latlon_bounds_gca():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand All @@ -137,7 +167,7 @@ def test_populate_bounds_equator_latlon_bounds_gca():
face_edges_lonlat = np.array(
[[_xyz_to_lonlat_rad(*edge[0]), _xyz_to_lonlat_rad(*edge[1])] for edge in face_edges_cart])

bounds = _construct_face_bounds(face_edges_cart, face_edges_lonlat)
bounds = _bounds_from_face_edges(face_edges_cart, face_edges_lonlat)
expected_bounds = np.array([[-0.05235988, 0], [6.23082543, 0]])
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)

Expand All @@ -153,7 +183,7 @@ def test_populate_bounds_south_sphere_latlon_bounds_gca():
face_edges_lonlat = np.array(
[[_xyz_to_lonlat_rad(*edge[0]), _xyz_to_lonlat_rad(*edge[1])] for edge in face_edges_cart])

bounds = _construct_face_bounds(face_edges_cart, face_edges_lonlat)
bounds = _bounds_from_face_edges(face_edges_cart, face_edges_lonlat)
expected_bounds = np.array([[-1.51843645, -1.45388627], [3.14159265, 3.92699082]])
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)

Expand All @@ -169,7 +199,7 @@ def test_populate_bounds_near_pole_latlon_bounds_gca():
face_edges_lonlat = np.array(
[[_xyz_to_lonlat_rad(*edge[0]), _xyz_to_lonlat_rad(*edge[1])] for edge in face_edges_cart])

bounds = _construct_face_bounds(face_edges_cart, face_edges_lonlat)
bounds = _bounds_from_face_edges(face_edges_cart, face_edges_lonlat)
expected_bounds = np.array([[-1.20427718, -1.14935491], [0, 0.13568803]])
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)

Expand All @@ -185,7 +215,7 @@ def test_populate_bounds_near_pole2_latlon_bounds_gca():
face_edges_lonlat = np.array(
[[_xyz_to_lonlat_rad(*edge[0]), _xyz_to_lonlat_rad(*edge[1])] for edge in face_edges_cart])

bounds = _construct_face_bounds(face_edges_cart, face_edges_lonlat)
bounds = _bounds_from_face_edges(face_edges_cart, face_edges_lonlat)
expected_bounds = np.array([[-1.20427718, -1.14935491], [6.147497, 4.960524e-16]])
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)

Expand All @@ -209,7 +239,7 @@ def test_populate_bounds_long_face_latlon_bounds_gca():
face_edges_lonlat = np.array(
[[_xyz_to_lonlat_rad(*edge[0]), _xyz_to_lonlat_rad(*edge[1])] for edge in face_edges_cart])

bounds = _construct_face_bounds(face_edges_cart, face_edges_lonlat)
bounds = _bounds_from_face_edges(face_edges_cart, face_edges_lonlat)

# The expected bounds should not contain the south pole [0,-0.5*np.pi]
assert bounds[1][0] != 0.0
Expand Down
64 changes: 47 additions & 17 deletions test/grid/geometry/test_bounds_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,36 @@ def _get_lonlat_rad_face_edge_nodes_testcase_helper_latlon_bounds_latlonface(fac
return lonlat_coordinates


def _bounds_from_face_edges(face_edges_cart, face_edges_lonlat, **kwargs):
"""Call _construct_face_bounds using face-edge arrays as input,
similar to the old _construct_face_bounds interface from before solving issue #1896.
Old tests use this function; newer tests should prefer the new interface instead.

face_edges_cart : np.ndarray, shape (n_edges, 2, 3)
Cartesian (x, y, z) of each edge's (start, end) nodes.
face_edges_lonlat : np.ndarray, shape (n_edges, 2, 2)
(lon, lat) in radians of each edge's (start, end) nodes.
**kwargs
Passed to _construct_face_bounds (is_latlonface, is_GCA_list).
"""
# Edge i goes from node i to node i+1, so this helper passes
# node i = face_edges[i, 0] to _construct_face_bounds.
start_xyz = face_edges_cart[:, 0]
start_lonlat = face_edges_lonlat[:, 0]
# Column slices like start_xyz[:, 0] are strided (non-contiguous) views. Numba accepts these,
# but compiles a separate specialization of _construct_face_bounds for non-contiguous arrays.
# Making them contiguous ensures the tests exercise the same compiled code used in production.
return _construct_face_bounds(
np.arange(len(face_edges_cart)),
np.ascontiguousarray(start_xyz[:, 0]),
np.ascontiguousarray(start_xyz[:, 1]),
np.ascontiguousarray(start_xyz[:, 2]),
np.ascontiguousarray(start_lonlat[:, 0]),
np.ascontiguousarray(start_lonlat[:, 1]),
**kwargs,
)


def test_populate_bounds_antimeridian_latlon_bounds_gca():
vertices_lonlat = [[350, 60.0], [350, 10.0], [50.0, 10.0], [50.0, 60.0]]
vertices_lonlat = np.array(vertices_lonlat)
Expand All @@ -189,7 +219,7 @@ def test_populate_bounds_antimeridian_latlon_bounds_gca():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand All @@ -204,7 +234,7 @@ def test_populate_bounds_near_pole2_latlon_bounds_gca():
face_edges_lonlat = np.array(
[[_xyz_to_lonlat_rad(*edge[0]), _xyz_to_lonlat_rad(*edge[1])] for edge in face_edges_cart])

bounds = _construct_face_bounds(face_edges_cart, face_edges_lonlat)
bounds = _bounds_from_face_edges(face_edges_cart, face_edges_lonlat)
expected_bounds = np.array([[-1.20427718, -1.14935491], [6.147497, 4.960524e-16]])
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)

Expand Down Expand Up @@ -232,7 +262,7 @@ def test_populate_bounds_node_on_pole_latlon_bounds_gca():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand All @@ -259,7 +289,7 @@ def test_populate_bounds_edge_over_pole_latlon_bounds_gca():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand All @@ -286,7 +316,7 @@ def test_populate_bounds_pole_inside_latlon_bounds_gca():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat)
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand All @@ -311,8 +341,8 @@ def test_populate_bounds_node_on_pole_latlon_bounds_gca_list():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_GCA_list=[True, False, True, False])
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_GCA_list=[True, False, True, False])
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand All @@ -336,8 +366,8 @@ def test_populate_bounds_node_on_pole_latlon_bounds_latlonface():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_latlonface=True)
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_latlonface=True)
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand All @@ -362,8 +392,8 @@ def test_populate_bounds_edge_over_pole_latlon_bounds_gca_list():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_GCA_list=[True, False, True, False])
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_GCA_list=[True, False, True, False])
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand All @@ -388,8 +418,8 @@ def test_populate_bounds_edge_over_pole_latlon_bounds_latlonface():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_latlonface=True)
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_latlonface=True)
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand All @@ -413,8 +443,8 @@ def test_populate_bounds_pole_inside_latlon_bounds_gca_list():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_GCA_list=[True, True, True, True])
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_GCA_list=[True, True, True, True])
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)


Expand All @@ -438,8 +468,8 @@ def test_populate_bounds_pole_inside_latlon_bounds_latlonface():
grid.edge_node_connectivity.values, grid.node_lon.values,
grid.node_lat.values)
expected_bounds = np.array([[lat_min, lat_max], [lon_min, lon_max]])
bounds = _construct_face_bounds(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_latlonface=True)
bounds = _bounds_from_face_edges(face_edges_connectivity_cartesian, face_edges_connectivity_lonlat,
is_latlonface=True)
np.testing.assert_allclose(bounds, expected_bounds, atol=ERROR_TOLERANCE)

from uxarray.grid.bounds import _populate_face_bounds
Expand Down
10 changes: 4 additions & 6 deletions test/grid/geometry/test_geometry_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from uxarray.constants import ERROR_TOLERANCE, INT_FILL_VALUE
from uxarray.grid.coordinates import _lonlat_rad_to_xyz, _normalize_xyz, _xyz_to_lonlat_rad
from uxarray.grid.arcs import extreme_gca_z
from uxarray.grid.bounds import _get_latlonbox_width, insert_pt_in_latlonbox
from uxarray.grid.bounds import _get_lon_width, insert_pt_in_latlonbox
from uxarray.grid.geometry import _pole_point_inside_polygon_cartesian


Expand Down Expand Up @@ -230,13 +230,11 @@ def test_extreme_gca_latitude_min():
assert np.isclose(min_latitude, expected_min_latitude, atol=ERROR_TOLERANCE)


def test_get_latlonbox_width():
gca_latlon = np.array([[0.0, 0.0], [0.0, 3.0]])
width = _get_latlonbox_width(gca_latlon)
def test_get_lon_width():
width = _get_lon_width(0.0, 3.0)
assert width == 3.0

gca_latlon = np.array([[0.0, 0.0], [2 * np.pi - 1.0, 1.0]])
width = _get_latlonbox_width(gca_latlon)
width = _get_lon_width(2 * np.pi - 1.0, 1.0)
assert width == 2.0


Expand Down
2 changes: 1 addition & 1 deletion test/grid/geometry/test_point_in_face.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import uxarray as ux
from uxarray.constants import INT_FILL_VALUE
from uxarray.grid.coordinates import _lonlat_rad_to_xyz
from uxarray.grid.utils import _get_cartesian_face_edge_nodes_array, _get_cartesian_face_edge_nodes
from uxarray.grid.utils import _get_cartesian_face_edge_nodes_array
from uxarray.grid.point_in_face import _point_in_face_from_grid


Expand Down
4 changes: 3 additions & 1 deletion uxarray/grid/arcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ def extreme_gca_latitude(gca_cart, gca_lonlat, extreme_type):
raise ValueError("Invalid extreme_type. Expected 'max' or 'min'.")
# (numba complains about f-strings, so don't put `extreme_type` value in message.)

# Extract the two points
# Extract the two points; upcast to float64 to ensure high-precision math.
n1 = gca_cart[0]
n2 = gca_cart[1]
n1 = np.float64(n1[0]), np.float64(n1[1]), np.float64(n1[2])
n2 = np.float64(n2[0]), np.float64(n2[1]), np.float64(n2[2])

# Compute dot product
dot_n1_n2 = _numba_dot3(n1, n2)
Expand Down
Loading
Loading