Skip to content

Commit b620b81

Browse files
committed
add pixels only parameter to dataset and adjust area
1 parent a13ddca commit b620b81

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

test/test_healpix.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
import os
55
from pathlib import Path
6+
import numpy as np
67

78

89
current_path = Path(os.path.dirname(os.path.realpath(__file__)))
@@ -19,6 +20,10 @@ def test_to_ugrid(resolution_level):
1920

2021
assert uxgrid.n_face == expected_n_face
2122

23+
n_side = uxgrid._ds.attrs["n_side"]
24+
expected_area = np.ones(uxgrid.n_face) * np.pi / 3 * n_side**2
25+
assert np.array_equal(uxgrid.face_areas.values, expected_area)
26+
2227
@pytest.mark.parametrize("resolution_level", [0, 1, 2, 3])
2328
def test_boundaries(resolution_level):
2429
uxgrid = ux.Grid.from_healpix(resolution_level)
@@ -42,6 +47,9 @@ def test_dataset():
4247
assert uxds.uxgrid.source_grid_spec == "HEALPix"
4348
assert "n_face" in uxds.dims
4449

50+
uxds = ux.UxDataset.from_healpix(ds_path, pixels_only=False)
51+
assert "face_node_connectivity" in uxds.uxgrid._ds
52+
4553

4654

4755
def test_number_of_boundary_nodes():

uxarray/core/dataset.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,9 @@ def from_xarray(cls, ds: xr.Dataset, uxgrid: Grid = None, ugrid_dims: dict = Non
282282
return cls(ds, uxgrid=uxgrid)
283283

284284
@classmethod
285-
def from_healpix(cls, ds: Union[str, os.PathLike, xr.Dataset], **kwargs):
285+
def from_healpix(
286+
cls, ds: Union[str, os.PathLike, xr.Dataset], pixels_only: bool = True, **kwargs
287+
):
286288
"""
287289
Loads a dataset represented in the HEALPix format into a ``ux.UxDataSet``, paired
288290
with a ``Grid`` containing information about the HEALPix definition.
@@ -291,6 +293,8 @@ def from_healpix(cls, ds: Union[str, os.PathLike, xr.Dataset], **kwargs):
291293
----------
292294
ds: str, os.PathLike, xr.Dataset
293295
Reference to a HEALPix Dataset
296+
pixels_only : bool
297+
Whether to only compute pixels (`face_lon`, `face_lat`) or to also construct boundaries (`face_node_connectivity`, `node_lon`, `node_lat`)
294298
295299
Returns
296300
-------
@@ -308,7 +312,7 @@ def from_healpix(cls, ds: Union[str, os.PathLike, xr.Dataset], **kwargs):
308312
zoom = np.emath.logn(4, (ds.sizes["cell"] / 12)).astype(int)
309313

310314
# Attach a HEALPix Grid
311-
uxgrid = Grid.from_healpix(zoom)
315+
uxgrid = Grid.from_healpix(zoom, pixels_only=pixels_only)
312316

313317
return cls.from_xarray(ds, uxgrid, {"cell": "n_face"})
314318

uxarray/grid/grid.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,8 +1902,11 @@ def compute_face_areas(
19021902
array([0.00211174, 0.00211221, 0.00210723, ..., 0.00210723, 0.00211221,
19031903
0.00211174])
19041904
"""
1905-
# if self._face_areas is None: # this allows for using the cached result,
1906-
# but is not the expected behavior behavior as we are in need to recompute if this function is called with different quadrature_rule or order
1905+
if self.source_grid_spec == "HEALPix":
1906+
# Derive HEALPix Area
1907+
n_side = self._ds.attrs["n_side"]
1908+
face_area = np.pi / 3 * n_side**2
1909+
return np.ones(self.n_face) * face_area, None
19071910

19081911
self.normalize_cartesian_coordinates()
19091912
x = self.node_x.values

0 commit comments

Comments
 (0)