Skip to content

hierarchical operations on cell ids: parents #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
20 changes: 20 additions & 0 deletions xdggs/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,26 @@ def cell_boundaries(self):
boundaries, coords={self._name: self.cell_ids}, dims=self.cell_ids.dims
)

def parents(self, level: int) -> xr.DataArray:
"""determine the parent cell ids of the cells

Parameters
----------
level : int
The parent resolution level. Must be smaller than the current resolution.

Returns
-------
parents : DataArray
The parent cell ids, one for each input cell.
"""
data = self.index.parents(level)

params = self.grid_info.to_dict()
params["resolution"] = level

return self.coord.copy(data=data).assign_attrs(**params).rename("parents")

def explore(self, *, cmap="viridis", center=None, alpha=None):
"""interactively explore the data using `lonboard`

Expand Down
8 changes: 8 additions & 0 deletions xdggs/h3.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ def cell_boundaries(self, cell_ids, backend="shapely"):
raise ValueError("invalid backend: {backend!r}")
return backend_func(wkb)

def parents(self, cell_ids, resolution):
if resolution >= self.resolution:
raise ValueError(
f"resolution is not a parent: {resolution} >= {self.resolution}"
)

return change_resolution(cell_ids, resolution)


@register_dggs("h3")
class H3Index(DGGSIndex):
Expand Down
12 changes: 12 additions & 0 deletions xdggs/healpix.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ def cell_boundaries(self, cell_ids: Any, backend="shapely") -> np.ndarray:

return backend_func(vertices)

def parents(self, cell_ids, level):
if level >= self.level:
raise ValueError(f"given level is not a parent: {level} >= {self.level}")

# TODO: reimplement using `cdshealpix`
offset = self.level - level
x, y, f = healpy.pix2xyf(self.nside, cell_ids, nest=self.nest)
x_ = x >> offset
y_ = y >> offset

return healpy.xyf2pix(self.nside, x_, y_, f, nest=self.nest)


@register_dggs("healpix")
class HealpixIndex(DGGSIndex):
Expand Down
3 changes: 3 additions & 0 deletions xdggs/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def cell_centers(self) -> tuple[np.ndarray, np.ndarray]:
def cell_boundaries(self) -> np.ndarray:
return self.grid_info.cell_boundaries(self._pd_index.index.values)

def parents(self, resolution: int) -> np.ndarray:
return self._grid.parents(self._pd_index.index.values, resolution=resolution)

@property
def grid_info(self) -> DGGSInfo:
return self._grid
Loading