Skip to content

Commit 5706b35

Browse files
authored
Correctly check dimensions with HEALPix (#1278)
* update n_node property and add extra check to dimension renaming * update n_node dimension * add test case
1 parent d27bbfc commit 5706b35

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

test/test_healpix.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55
import os
66
import xarray as xr
7+
import pandas as pd
78
from pathlib import Path
89

910

@@ -38,6 +39,17 @@ def test_boundaries(resolution_level):
3839
# check for the correct number of boundary nodes
3940
assert (uxgrid.n_node == uxgrid.n_face + 2)
4041

42+
@pytest.mark.parametrize("pixels_only", [True, False])
43+
def test_time_dimension_roundtrip(pixels_only):
44+
ds = xr.open_dataset(ds_path)
45+
dummy_time = pd.to_datetime(["2025-01-01T00:00:00"])
46+
ds_time = ds.expand_dims(time=dummy_time)
47+
uxds = ux.UxDataset.from_healpix(ds_time, pixels_only=pixels_only)
48+
49+
# Ensure time dimension is preserved and that the conversion worked
50+
assert "time" in uxds.dims
51+
assert uxds.sizes["time"] == 1
52+
4153
def test_dataset():
4254
uxds = ux.UxDataset.from_healpix(ds_path)
4355

uxarray/core/utils.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,17 @@ def _map_dims_to_ugrid(
4444
# drop dimensions not present in the original dataset
4545
_source_dims_dict.pop(key)
4646

47-
# only check edge dimension if it is present (to avoid overhead of computing connectivity)
48-
if "n_edge" in grid._ds.dims:
49-
n_edge = grid._ds.sizes["n_edge"]
50-
else:
51-
n_edge = None
52-
53-
for dim in set(ds.dims) ^ _source_dims_dict.keys():
54-
# obtain dimensions that were not parsed source_dims_dict and attempt to match to a grid element
55-
if ds.sizes[dim] == grid.n_face:
56-
_source_dims_dict[dim] = "n_face"
57-
elif ds.sizes[dim] == grid.n_node:
58-
_source_dims_dict[dim] = "n_node"
59-
elif n_edge is not None:
60-
if ds.sizes[dim] == n_edge:
61-
_source_dims_dict[dim] = "n_edge"
47+
# build a reverse map
48+
size_to_name = {
49+
grid._ds.sizes[name]: name
50+
for name in ("n_face", "n_node", "n_edge")
51+
if name in grid._ds.dims
52+
}
53+
54+
for dim in set(ds.dims) - _source_dims_dict.keys():
55+
name = size_to_name.get(ds.sizes[dim])
56+
if name:
57+
_source_dims_dict[dim] = name
6258

6359
# rename dimensions to follow the UGRID conventions
6460
ds = ds.swap_dims(_source_dims_dict)

uxarray/grid/grid.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,8 @@ def attrs(self) -> dict:
817817
@property
818818
def n_node(self) -> int:
819819
"""Total number of nodes."""
820+
if "face_node_connectivity" not in self._ds:
821+
_ = self.face_node_connectivity
820822
return self._ds.sizes["n_node"]
821823

822824
@property

0 commit comments

Comments
 (0)