Skip to content

Commit 71b858f

Browse files
authored
preserve attributes and name (#1375)
1 parent 195dc3c commit 71b858f

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

test/grid/test_grid.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,3 +824,9 @@ def test_sphere_radius_mpas_ocean(gridpath):
824824
# Test invalid radius
825825
with pytest.raises(ValueError, match="Sphere radius must be positive"):
826826
grid.sphere_radius = -1.0
827+
828+
829+
def test_set_lon_range_attrs(gridpath):
830+
grid = ux.open_grid(gridpath("mpas", "QU", "oQU480.231010.nc"))
831+
assert "standard_name" in grid.node_lon.attrs
832+
assert grid.node_lon.name == "node_lon"

uxarray/grid/coordinates.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,14 @@ def _construct_edge_centroids(node_x, node_y, node_z, edge_node_conn):
692692

693693
def _set_desired_longitude_range(uxgrid):
694694
"""Sets the longitude range to [-180, 180] for all longitude variables."""
695-
696-
for lon_name in ["node_lon", "edge_lon", "face_lon"]:
697-
if lon_name in uxgrid._ds:
698-
if uxgrid._ds[lon_name].max() > 180:
699-
uxgrid._ds[lon_name] = (uxgrid._ds[lon_name] + 180) % 360 - 180
695+
with xr.set_options(keep_attrs=True):
696+
for lon_name in ["node_lon", "edge_lon", "face_lon"]:
697+
if lon_name in uxgrid._ds:
698+
if uxgrid._ds[lon_name].max() > 180:
699+
da = uxgrid._ds[lon_name]
700+
wrapped = (uxgrid._ds[lon_name] + 180) % 360 - 180
701+
wrapped.name = da.name
702+
uxgrid._ds[lon_name] = wrapped
700703

701704

702705
def prepare_points(points, normalize):

0 commit comments

Comments
 (0)