-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from cspencerjones/wrapper
Added wrapper for xarrays/dask arrays
- Loading branch information
Showing
3 changed files
with
53 additions
and
4 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 |
---|---|---|
@@ -1 +1 @@ | ||
from .jmd95numba import rho, drhodt, drhods | ||
from .jmd95wrapper import rho, drhodt, drhods |
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,36 @@ | ||
|
||
import numpy as np | ||
import dask.array as dsa | ||
import dask | ||
import xarray as xr | ||
|
||
import fastjmd95.jmd95numba as jmd95numba | ||
|
||
def _any_dask_array(*args): | ||
return any([isinstance(a, dask.array.core.Array) for a in args]) | ||
|
||
def _any_xarray(*args): | ||
return any([isinstance(a, xr.DataArray) for a in args]) | ||
|
||
def maybe_wrap_arrays(func): | ||
def wrapper(*args): | ||
if _any_dask_array(*args): | ||
rho = dsa.map_blocks(func,*args) | ||
elif _any_xarray(*args): | ||
rho = xr.apply_ufunc(func,*args,output_dtypes=[float],dask='parallelized') | ||
else: | ||
rho = func(*args) | ||
return rho | ||
return wrapper | ||
|
||
@maybe_wrap_arrays | ||
def rho(s,t,p): | ||
return jmd95numba.rho(s,t,p) | ||
|
||
@maybe_wrap_arrays | ||
def drhodt(s,t,p): | ||
return jmd95numba.drhodt(s,t,p) | ||
|
||
@maybe_wrap_arrays | ||
def drhods(s,t,p): | ||
return jmd95numba.drhods(s,t,p) |
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