Skip to content

Commit 7a02f7e

Browse files
authored
Merge branch 'main' into gradient
2 parents d00e61b + 5700944 commit 7a02f7e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

test/test_grid.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,3 +885,22 @@ def test_from_topology():
885885
face_node_connectivity=face_node_connectivity,
886886
fill_value=-1,
887887
)
888+
889+
890+
def test_sphere_radius_mpas_ocean():
891+
"""Test sphere radius functionality with MPAS ocean mesh."""
892+
# Test with MPAS ocean mesh file
893+
mpas_ocean_file = current_path / "meshfiles" / "mpas" / "QU" / "oQU480.231010.nc"
894+
grid = ux.open_grid(mpas_ocean_file)
895+
896+
# Check that MPAS sphere radius is preserved (Earth's radius)
897+
assert np.isclose(grid.sphere_radius, 6371229.0, rtol=1e-10)
898+
899+
# Test setting a new radius
900+
new_radius = 1000.0
901+
grid.sphere_radius = new_radius
902+
assert np.isclose(grid.sphere_radius, new_radius, rtol=1e-10)
903+
904+
# Test invalid radius
905+
with pytest.raises(ValueError, match="Sphere radius must be positive"):
906+
grid.sphere_radius = -1.0

uxarray/grid/grid.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ class Grid:
113113
For constructing a grid from non-UGRID datasets or other types of supported data, see our ``ux.open_grid`` method or
114114
specific class methods (``Grid.from_dataset``, ``Grid.from_face_verticies``, etc.)
115115
116+
Note on Sphere Radius:
117+
All internal calculations use a unit sphere (radius=1.0). The physical sphere radius
118+
from the source grid is preserved in the ``sphere_radius`` property for scaling results.
119+
116120
117121
Parameters
118122
----------
@@ -2030,6 +2034,39 @@ def normalize_cartesian_coordinates(self):
20302034
self._ds[f"{prefix}_y"] = dy / norm
20312035
self._ds[f"{prefix}_z"] = dz / norm
20322036

2037+
@property
2038+
def sphere_radius(self) -> float:
2039+
"""Physical sphere radius from the source grid (e.g., Earth's radius for MPAS ocean grids).
2040+
2041+
Internally, all calculations use a unit sphere. This property stores the original
2042+
radius for scaling results back to physical units.
2043+
2044+
Returns
2045+
-------
2046+
sphere_radius : float
2047+
The physical sphere radius. Defaults to 1.0 if not set.
2048+
"""
2049+
return self._ds.attrs.get("sphere_radius", 1.0)
2050+
2051+
@sphere_radius.setter
2052+
def sphere_radius(self, radius: float) -> None:
2053+
"""Set the sphere radius for the grid.
2054+
2055+
Parameters
2056+
----------
2057+
radius : float
2058+
The sphere radius to set. Must be positive.
2059+
2060+
Raises
2061+
------
2062+
ValueError
2063+
If radius is not positive.
2064+
"""
2065+
if radius <= 0:
2066+
raise ValueError(f"Sphere radius must be positive, got {radius}")
2067+
2068+
self._ds.attrs["sphere_radius"] = radius
2069+
20332070
def to_xarray(self, grid_format: Optional[str] = "ugrid"):
20342071
"""Returns an ``xarray.Dataset`` with the variables stored under the
20352072
``Grid`` encoded in a specific grid format.

0 commit comments

Comments
 (0)