Skip to content

Commit

Permalink
Dataset is the exception case, not DataArray
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyCMWF committed Aug 25, 2023
1 parent 47b6b6c commit 539ef64
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
3 changes: 3 additions & 0 deletions ci/combined-environment-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ dependencies:
- sphinx
- sphinx-autoapi
- xarray
- pip:
- git+https://github.com/ecmwf/multiurl

18 changes: 9 additions & 9 deletions earthkit/climate/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,15 +259,15 @@ def _reduce_dataarray(

else:
# If how is string, fetch function from dictionary:
if isinstance(how, str):
if how in dir(dataarray):
red_array = dataarray.__getattribute__(how)(**kwargs)
else:
if isinstance(how, str) and how in dir(dataarray):
red_array = dataarray.__getattribute__(how)(**kwargs)
else:
if isinstance(how, str):
how_label = deepcopy(how)
how = get_how(how)
assert isinstance(how, T.Callable), f"how method not recognised: {how}"
assert isinstance(how, T.Callable), f"how method not recognised: {how}"

red_array = dataarray.reduce(how, **kwargs)
red_array = dataarray.reduce(how, **kwargs)

if how_label:
red_array = red_array.rename(f"{red_array.name}_{how_label}")
Expand Down Expand Up @@ -313,12 +313,12 @@ def reduce(
A data array with reduce dimensions removed.
"""
if isinstance(dataarray, xr.DataArray):
return _reduce_dataarray(dataarray, **kwargs)
else:
if isinstance(dataarray, (xr.Dataset)):
return xr.Dataset(
[_reduce_dataarray(dataarray[var], **kwargs) for var in dataarray.data_vars]
)
else:
return _reduce_dataarray(dataarray, **kwargs)


def rolling_reduce(
Expand Down
8 changes: 4 additions & 4 deletions earthkit/climate/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,7 @@ def reduce(
Each slice of layer corresponds to a feature in layer.
"""
# kwargs.update({"how": how})
if isinstance(dataarray, xr.DataArray):
return _reduce_dataarray(dataarray, geodataframe, **kwargs)
else:
if isinstance(dataarray, xr.Dataset):
if kwargs.get("return_as", "pandas") in ["xarray"]:
return xr.Dataset(
[
Expand All @@ -285,6 +282,9 @@ def reduce(
for var in dataarray.data_vars:
out = _reduce_dataarray(dataarray[var], geodataframe, **kwargs)
return out
else:
return _reduce_dataarray(dataarray, geodataframe, **kwargs)



def _reduce_dataarray(
Expand Down

0 comments on commit 539ef64

Please sign in to comment.