-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
simplify the conversion functions using
call_on_dataset
(#110)
* vendor a short helper function * only attempt to convert to DataArray if the result is a Dataset * also try attaching a empty dict * refactor the conversion functions to use call_on_dataset * catch the ImportError
- Loading branch information
Showing
3 changed files
with
133 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import xarray as xr | ||
|
||
try: | ||
from xarray import call_on_dataset | ||
except ImportError: | ||
|
||
def call_on_dataset(func, obj, name, *args, **kwargs): | ||
if isinstance(obj, xr.DataArray): | ||
ds = obj.to_dataset(name=name) | ||
else: | ||
ds = obj | ||
|
||
result = func(ds, *args, **kwargs) | ||
|
||
if isinstance(obj, xr.DataArray) and isinstance(result, xr.Dataset): | ||
result = result.get(name).rename(obj.name) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters