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

DRAFT: .where() returning Xarray objects #1002

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
40 changes: 32 additions & 8 deletions uxarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ def __init__(self, *args, uxgrid: Grid = None, **kwargs):

super().__init__(*args, **kwargs)

# TODO:
def __array_wrap__(self, obj, context=None):
return UxDataArray(obj, uxgrid=self.uxgrid)

# TODO:
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
results = super().__array_ufunc__(ufunc, method, *inputs, **kwargs)
if isinstance(results, xr.DataArray):
return UxDataArray(results, uxgrid=self.uxgrid)
return results

# declare various accessors
plot = UncachedAccessor(UxDataArrayPlotAccessor)
subset = UncachedAccessor(DataArraySubsetAccessor)
Expand Down Expand Up @@ -109,17 +120,23 @@ def _copy(self, **kwargs):

return copied

# def _replace(self, *args, **kwargs):
# """Override to make the result a complete instance of
# ``uxarray.UxDataArray``."""
# da = super()._replace(*args, **kwargs)
#
# if isinstance(da, UxDataArray):
# da.uxgrid = self.uxgrid
# else:
# da = UxDataArray(da, uxgrid=self.uxgrid)
#
# return da

def _replace(self, *args, **kwargs):
"""Override to make the result a complete instance of
``uxarray.UxDataArray``."""
da = super()._replace(*args, **kwargs)

if isinstance(da, UxDataArray):
da.uxgrid = self.uxgrid
else:
da = UxDataArray(da, uxgrid=self.uxgrid)

return da
result = super()._replace(*args, **kwargs)
return UxDataArray(result, uxgrid=self.uxgrid)

@property
def uxgrid(self):
Expand All @@ -138,6 +155,11 @@ def uxgrid(self):
def uxgrid(self, ugrid_obj):
self._uxgrid = ugrid_obj

# TODO:
def copy(self, *args, **kwargs):
result = super().copy(*args, **kwargs)
return UxDataArray(result, uxgrid=self.uxgrid)

def to_geodataframe(
self,
periodic_elements: Optional[str] = "exclude",
Expand Down Expand Up @@ -1121,3 +1143,5 @@ def _slice_from_grid(self, sliced_grid):
dims=self.dims,
attrs=self.attrs,
)

# def where(self):
Loading