Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions test/grid/grid/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,26 @@ def test_grid_ugrid_exodus_roundtrip(gridpath):
for filepath in test_files:
if os.path.exists(filepath):
os.remove(filepath)


def test_exodus_roundtrip_rll1deg_node_lonlat(gridpath, tmp_path):
"""Check Exodus round-trip coordinate stability for the RLL1deg grid."""
grid = ux.open_grid(gridpath("ugrid", "outRLL1deg", "outRLL1deg.ug"))

exodus_filepath = tmp_path / "outRLL1deg.exo"
grid.to_xarray("Exodus").to_netcdf(exodus_filepath)

reloaded_exodus = ux.open_grid(exodus_filepath)

np.testing.assert_allclose(
grid.node_lon.values,
reloaded_exodus.node_lon.values,
err_msg="Exodus longitude mismatch for RLL1deg",
rtol=ERROR_TOLERANCE,
)
np.testing.assert_allclose(
grid.node_lat.values,
reloaded_exodus.node_lat.values,
err_msg="Exodus latitude mismatch for RLL1deg",
rtol=ERROR_TOLERANCE,
)
16 changes: 8 additions & 8 deletions uxarray/grid/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def _xyz_to_lonlat_rad_no_norm(
def _xyz_to_lonlat_rad_scalar(x, y, z, normalize=True):
if normalize:
x, y, z = _normalize_xyz_scalar(x, y, z)
denom = abs(x * x + y * y + z * z)
x /= denom
y /= denom
z /= denom
denom = math.hypot(math.hypot(x, y), z)
x = x / denom
y = y / denom
z = z / denom

lon = np.arctan2(y, x)
lat = np.asin(z)
Expand Down Expand Up @@ -121,10 +121,10 @@ def _xyz_to_lonlat_rad(

if normalize:
x, y, z = _normalize_xyz(x, y, z)
denom = np.abs(x * x + y * y + z * z)
x /= denom
y /= denom
z /= denom
denom = np.hypot(np.hypot(x, y), z)
x = x / denom
y = y / denom
z = z / denom

lon = np.arctan2(y, x)
lat = np.arcsin(z)
Expand Down
Loading