Skip to content

Commit

Permalink
updated grid, doc strings, api, test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronzedwick committed Jan 7, 2025
1 parent ae151aa commit aaf49ab
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 29 deletions.
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Indexing
:toctree: generated/

Grid.isel
Grid.inverse_indices

Dimensions
~~~~~~~~~~
Expand Down
6 changes: 5 additions & 1 deletion test/test_subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_uxda_isel_with_coords():
assert len(sub.coords['lon_face']) == 3


def test_inverse_face_indices():
def test_inverse_indices():
grid = ux.open_grid(GRID_PATHS[0])

# Test nearest neighbor subsetting
Expand All @@ -145,3 +145,7 @@ def test_inverse_face_indices():
# Ensure code raises exceptions when the element is edges or nodes
assert pytest.raises(Exception, grid.subset.bounding_circle, center_coord, r=10, element="edge centers", inverse_indices=True)
assert pytest.raises(Exception, grid.subset.bounding_circle, center_coord, r=10, element="node centers", inverse_indices=True)

# Test isel directly
subset = grid.isel(n_face=[1], inverse_indices=True)
assert subset.inverse_indices is not None
10 changes: 6 additions & 4 deletions uxarray/cross_sections/dataarray_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def constant_latitude(
lat : float
The latitude at which to extract the cross-section, in degrees.
Must be between -90.0 and 90.0
inverse_indices : bool, optional
If True, stores the original grid indices
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
Returns
-------
Expand Down Expand Up @@ -77,8 +78,9 @@ def constant_longitude(
lon : float
The latitude at which to extract the cross-section, in degrees.
Must be between -180.0 and 180.0
inverse_indices : bool, optional
If True, stores the original grid indices
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
Returns
-------
Expand Down
10 changes: 6 additions & 4 deletions uxarray/cross_sections/grid_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def constant_latitude(
Must be between -90.0 and 90.0
return_face_indices : bool, optional
If True, also returns the indices of the faces that intersect with the line of constant latitude.
inverse_indices : bool, optional
If True, stores the original grid indices
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
Returns
-------
Expand Down Expand Up @@ -94,8 +95,9 @@ def constant_longitude(
Must be between -90.0 and 90.0
return_face_indices : bool, optional
If True, also returns the indices of the faces that intersect with the line of constant longitude.
inverse_indices : bool, optional
If True, stores the original grid indices
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
Returns
-------
Expand Down
16 changes: 10 additions & 6 deletions uxarray/grid/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ class Grid:
source_dims_dict : dict, default={}
Mapping of dimensions from the source dataset to their UGRID equivalent (i.e. {nCell : n_face})
is_subset : bool, default=False
Flag to mark if the grid is a subset or not
inverse_indices: xr.Dataset, defaul=None
A dataset of indices that correspond to the original grid, if the grid being constructed is a subset
Examples
----------
Expand Down Expand Up @@ -198,7 +204,7 @@ def __init__(
self.is_subset = is_subset

if inverse_indices is not None:
self.inverse_indices = inverse_indices
self._inverse_indices = inverse_indices

# cached parameters for GeoDataFrame conversions
self._gdf_cached_parameters = {
Expand Down Expand Up @@ -1532,11 +1538,6 @@ def inverse_indices(self):
"Grid is not a subset, therefore no inverse face indices exist"
)

@inverse_indices.setter
def inverse_indices(self, value):
assert isinstance(value, xr.Dataset)
self._inverse_indices = value

def chunk(self, n_node="auto", n_edge="auto", n_face="auto"):
"""Converts all arrays to dask arrays with given chunks across grid
dimensions in-place.
Expand Down Expand Up @@ -2244,6 +2245,9 @@ def isel(
exclusive and clipped indexing is in the works.
Parameters
inverse_indices : Union[List[str], Set[str], bool], default=False
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
**dims_kwargs: kwargs
Dimension to index, one of ['n_node', 'n_edge', 'n_face']
Expand Down
5 changes: 3 additions & 2 deletions uxarray/grid/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def _slice_node_indices(
grid,
indices,
inclusive=True,
inverse_indices: Union[List[str], Set[str], bool] = False,
):
"""Slices (indexes) an unstructured grid given a list/array of node
indices, returning a new Grid composed of elements that contain the nodes
Expand Down Expand Up @@ -45,7 +44,6 @@ def _slice_edge_indices(
grid,
indices,
inclusive=True,
inverse_indices: Union[List[str], Set[str], bool] = False,
):
"""Slices (indexes) an unstructured grid given a list/array of edge
indices, returning a new Grid composed of elements that contain the edges
Expand Down Expand Up @@ -91,6 +89,9 @@ def _slice_face_indices(
inclusive: bool
Whether to perform inclusive (i.e. elements must contain at least one desired feature from a slice) as opposed
to exclusive (i.e elements be made up all desired features from a slice)
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
"""
if inclusive is False:
raise ValueError("Exclusive slicing is not yet supported.")
Expand Down
15 changes: 9 additions & 6 deletions uxarray/subset/dataarray_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ def bounding_box(
face centers, or edge centers lie within the bounds.
element: str
Element for use with `coords` comparison, one of `nodes`, `face centers`, or `edge centers`
inverse_indices : bool
Flag to indicate whether to store the original grids face indices for later use
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
"""
grid = self.uxda.uxgrid.subset.bounding_box(
lon_bounds, lat_bounds, element, method, inverse_indices=inverse_indices
Expand All @@ -82,8 +83,9 @@ def bounding_circle(
Radius of bounding circle (in degrees)
element: str
Element for use with `coords` comparison, one of `nodes`, `face centers`, or `edge centers`
inverse_indices : bool
Flag to indicate whether to store the original grids face indices for later use
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
"""
grid = self.uxda.uxgrid.subset.bounding_circle(
center_coord, r, element, inverse_indices=inverse_indices, **kwargs
Expand All @@ -109,8 +111,9 @@ def nearest_neighbor(
Number of neighbors to query
element: str
Element for use with `coords` comparison, one of `nodes`, `face centers`, or `edge centers`
inverse_indices : bool
Flag to indicate whether to store the original grids face indices for later use
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
"""

grid = self.uxda.uxgrid.subset.nearest_neighbor(
Expand Down
15 changes: 9 additions & 6 deletions uxarray/subset/grid_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ def bounding_box(
face centers, or edge centers lie within the bounds.
element: str
Element for use with `coords` comparison, one of `nodes`, `face centers`, or `edge centers`
inverse_indices : bool
Flag to indicate whether to store the original grids face indices for later use
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
"""

if method == "coords":
Expand Down Expand Up @@ -132,8 +133,9 @@ def bounding_circle(
Radius of bounding circle (in degrees)
element: str
Element for use with `coords` comparison, one of `nodes`, `face centers`, or `edge centers`
inverse_indices : bool
Flag to indicate whether to store the original grids face indices for later use
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
"""

coords = np.asarray(center_coord)
Expand Down Expand Up @@ -168,8 +170,9 @@ def nearest_neighbor(
Number of neighbors to query
element: str
Element for use with `coords` comparison, one of `nodes`, `face centers`, or `edge centers`
inverse_indices : bool
Flag to indicate whether to store the original grids face indices for later use
inverse_indices : Union[List[str], Set[str], bool], optional
Indicates whether to store the original grids indices. Passing `True` stores the original face centers,
other reverse indices can be stored by passing any or all of the following: (["face centers", "edge centers", "nodes"], True)
"""

coords = np.asarray(center_coord)
Expand Down

0 comments on commit aaf49ab

Please sign in to comment.