Skip to content

Commit

Permalink
Merge pull request #146 from csiro-coasts/feature/select-indexes
Browse files Browse the repository at this point in the history
Add `Convention.select_indexes()` method
  • Loading branch information
mx-moth authored Jul 10, 2024
2 parents 16266c3 + b363826 commit 02917cf
Show file tree
Hide file tree
Showing 26 changed files with 612 additions and 285 deletions.
2 changes: 1 addition & 1 deletion docs/api/conventions/ugrid.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ Topology
Masking
=======

.. autofunction:: mask_from_face_indices
.. autofunction:: mask_from_face_indexes
.. autofunction:: buffer_faces
4 changes: 2 additions & 2 deletions docs/developing/conventions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ and returns a value indicating whether this convention implementation can unders
:pyobject: Grass.check_dataset

:meth:`.DimensionConvention.unpack_index` and :meth:`.DimensionConvention.pack_index`
transform between native index types and a grid kind and indices.
transform between native index types and a grid kind and indexes.
The native representation must be representable as JSON for GeoJSON export support.
The simplest representation is a tuple of (grid_kind, indices):
The simplest representation is a tuple of (grid_kind, indexes):

.. literalinclude:: ./grass.py
:pyobject: Grass.unpack_index
Expand Down
4 changes: 2 additions & 2 deletions docs/developing/grass.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def check_dataset(cls, dataset: xarray.Dataset) -> int | None:
def unpack_index(self, index: GrassIndex) -> tuple[GrassGridKind, Sequence[int]]:
return index[0], list(index[1])

def pack_index(self, grid_kind: GrassGridKind, indices: Sequence[int]) -> GrassIndex:
return (grid_kind, list(indices))
def pack_index(self, grid_kind: GrassGridKind, indexes: Sequence[int]) -> GrassIndex:
return (grid_kind, list(indexes))

@cached_property
def grid_dimensions(self) -> dict[GrassGridKind, Sequence[Hashable]]:
Expand Down
16 changes: 16 additions & 0 deletions docs/releases/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,19 @@ Next release (in development)
(:pr:`142`).
* Add :attr:`.Convention.depth_coordinates` and :meth:`.Convention.get_depth_coordinate_for_data_array()`. Deprecate functions :meth:`.Convention.get_depth_name()`, :meth:`.Convention.get_all_depth_names()`, and :meth:`Convention.get_time_name()`. Remove deprecated functions ``Convention.get_depths()`` and ``Convention.get_times()`` (:pr:`143`).
* Swap to using `pyproject.toml` for all project metadata (:pr:`145`).
* Add new methods
:meth:`.Convention.selector_for_indexes()`,
:meth:`.Convention.select_indexes()`, and
:meth:`.Convention.select_points()`.
These allow for more efficient extraction of multiple points at the same time.
The return type of :meth:`.Convention.selector_for_index()` has been changed
from a `dict` to an :class:`xarray.Dataset`,
but this new value is also designed to be passed directly to :meth:`Dataset.isel() <xarray.Dataset.isel>`.
:meth:`.Convention.select_index()` and :meth:`.Convention.select_indexes()`
have a new `drop_geometry` flag which defaults to True.
Previously these methods would act as if `drop_geometry` was False,
but this led to convention-dependent results as to which geometry variables were returned.
The fragmented geometry variables from different conventions often did not contain enough data to be useful.
By dropping geometry the results are more consistent across all conventions
and do not contain potentially fragmented geometry information.
(:issue:`106`, :pr:`146`).
2 changes: 1 addition & 1 deletion src/emsarray/cli/commands/extract_points.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def handle(self, options: argparse.Namespace) -> None:
point_dimension=options.point_dimension,
missing_points=options.missing_points)
except point_extraction.NonIntersectingPoints as err:
rows = dataframe.iloc[err.indices]
rows = dataframe.iloc[err.indexes]
raise CommandException(
f"Error extracting points: the points in the following rows "
f"did not intersect the dataset geometry:\n"
Expand Down
12 changes: 6 additions & 6 deletions src/emsarray/compat/shapely.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ def query(
geom: BaseGeometry,
) -> numpy.ndarray:
if shapely_version >= v2:
indices = self.index.query(geom)
indexes = self.index.query(geom)
else:
indices = self.index._query(geom)
return cast(numpy.ndarray, self.items.take(indices))
indexes = self.index._query(geom)
return cast(numpy.ndarray, self.items.take(indexes))

def nearest(
self,
geom: BaseGeometry,
) -> numpy.ndarray:
if shapely_version >= v2:
indices = self.index.nearest(geom)
indexes = self.index.nearest(geom)
else:
indices = self.index._nearest(geom)
return cast(numpy.ndarray, self.items.take(indices))
indexes = self.index._nearest(geom)
return cast(numpy.ndarray, self.items.take(indexes))
Loading

0 comments on commit 02917cf

Please sign in to comment.