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

Preserve Coordinates when Indexing UxDataArray #1003

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions test/test_subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,21 @@ def test_grid_bounding_box_subset():

grid_subset_antimeridian = grid.subset.bounding_box(
bbox_antimeridian[0], bbox_antimeridian[1], element=element)




def test_uxda_isel():
uxds = ux.open_dataset(GRID_PATHS[0], DATA_PATHS[0])

sub = uxds['bottomDepth'].isel(n_face=[1, 2, 3])

assert len(sub) == 3

def test_uxda_isel_with_coords():
uxds = ux.open_dataset(GRID_PATHS[0], DATA_PATHS[0])
uxds = uxds.assign_coords({"lon_face": uxds.uxgrid.face_lon})
sub = uxds['bottomDepth'].isel(n_face=[1, 2, 3])

# check if coordinates are preserved
assert "lon_face" in sub.coords
3 changes: 1 addition & 2 deletions uxarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,12 +1106,11 @@ def _slice_from_grid(self, sliced_grid):
raise ValueError(
"Data variable must be either node, edge, or face centered."
)

return UxDataArray(
uxgrid=sliced_grid,
data=d_var,
name=self.name,
coords=self.coords,
coords=d_var.coords,
dims=self.dims,
attrs=self.attrs,
)
Expand Down
1 change: 0 additions & 1 deletion uxarray/remap/inverse_distance_weighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ def _inverse_distance_weighted_remap_uxda(
uxda_remap = uxarray.core.dataarray.UxDataArray(
data=destination_data,
name=source_uxda.name,
coords=source_uxda.coords,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philipc2 Here's my solution to preserve any non-spatial coordinates (like time or ensemble_member) while dropping the spatial one:

Copy source_uxda.coords to a new xarray.Coordinates object. Then delete the key associated with the spatial dimension. Then use that new object as the coords argument to uxarray.core.dataarray.UxDataArray.

    destination_coords = source_uxda.coords
    del(destination_coords[destination_dim])

    # construct data array for remapping variable
    uxda_remap = uxarray.core.dataarray.UxDataArray(
        data=destination_data,
        name=source_uxda.name,
        coords=destination_coords,

Does this make sense?

dims=destination_dims,
uxgrid=destination_grid,
)
Expand Down
1 change: 0 additions & 1 deletion uxarray/remap/nearest_neighbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def _nearest_neighbor_uxda(
uxda_remap = uxarray.core.dataarray.UxDataArray(
data=destination_data,
name=source_uxda.name,
coords=source_uxda.coords,
dims=destination_dims,
uxgrid=destination_grid,
)
Expand Down
Loading