Skip to content
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

hierarchical operations on cell ids: parents #62

Draft
wants to merge 4 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 @@ -104,3 +104,23 @@ def cell_centers(self):
"longitude": (self.index._dim, lon_data),
}
)

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

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

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

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

return self.coord.copy(data=data).assign_attrs(**params).rename("parents")
9 changes: 9 additions & 0 deletions xdggs/h3.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import numpy as np
import xarray as xr
from h3ronpy.arrow import change_resolution
from h3ronpy.arrow.vector import cells_to_coordinates, coordinates_to_cells
from xarray.indexes import PandasIndex

Expand Down Expand Up @@ -45,6 +46,14 @@ def cell_ids2geographic(
def geographic2cell_ids(self, lon, lat):
return coordinates_to_cells(lat, lon, self.resolution, radians=False)

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
13 changes: 13 additions & 0 deletions xdggs/healpix.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ def cell_ids2geographic(self, cell_ids):
def geographic2cell_ids(self, lon, lat):
return healpy.ang2pix(self.nside, lon, lat, lonlat=True, nest=self.nest)

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

offset = self.resolution - resolution
x, y, f = healpy.pix2xyf(self.nside, cell_ids, nest=self.nest)
x_ = x >> offset
y_ = y >> offset

return healpy.xyf2pix(2**resolution, 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 @@ -72,6 +72,9 @@ def _replace(self, new_pd_index: PandasIndex):
def cell_centers(self) -> tuple[np.ndarray, np.ndarray]:
return self._grid.cell_ids2geographic(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