I have a 4D dataarray and I'm interpolating it only along the vertical axis.
from scipy.interpolate import interp1d
#data array dims
da[time,plev,la,lon]
#array with vertical levels
lev = da.plev
#new temperatures ->dummy values
tem = np.arange(10,100,5)
#begin loop for interpolation
for time in range(da.time.size):
for lat in range(da.lat.size):
for lon in range(da.lon.size):
f = interp1d(da[time,:,lat,lon],lev,fill_value='extrapolate')
holder[time,:,lat,lon] = f(tem)
The code works, but takes a WHILE to run. I'm still learning about apply_ufunc and Dask and I saw few examples here which I think helps cut the run time by a lot (at least compared to the for loops).
I tried to run something similar
# return a tuple of DataArrays
res = xr.apply_ufunc(interp1d, hus, lev,
input_core_dims=[['plev'], ['plev']],
output_core_dims=[[]],
vectorize=True)
But when I try to use the interpolation function:
holder = res(tem)
I get an error saying: DataArray object not callable.
UPDATE
I tried the following code where I put the interpolator inside the function. I know it's working, cause I printed some results before the return statement. But the issue is with the return statement.
def interp(x, y):
# Wrapper around scipy linregress to use in apply_ufunc
f = interp1d(x,y,fill_value='extrapolate')
new = f(tem)
return (new)
holder_new = xr.apply_ufunc(interp, check, p,
input_core_dims=[['plev'], ['plev']],
output_core_dims=[[]],
vectorize=True)
The error message:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<timed exec> in <module>
~/anaconda3/lib/python3.7/site-packages/xarray/core/computation.py in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, meta, dask_gufunc_kwargs, *args)
1108 join=join,
1109 exclude_dims=exclude_dims,
-> 1110 keep_attrs=keep_attrs,
1111 )
1112 # feed Variables directly through apply_variable_ufunc
~/anaconda3/lib/python3.7/site-packages/xarray/core/computation.py in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, *args)
260
261 data_vars = [getattr(a, "variable", a) for a in args]
--> 262 result_var = func(*data_vars)
263
264 if signature.num_outputs > 1:
~/anaconda3/lib/python3.7/site-packages/xarray/core/computation.py in apply_variable_ufunc(func, signature, exclude_dims, dask, output_dtypes, vectorize, keep_attrs, dask_gufunc_kwargs, *args)
698 )
699
--> 700 result_data = func(*input_data)
701
702 if signature.num_outputs == 1:
~/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py in __call__(self, *args, **kwargs)
2106 vargs.extend([kwargs[_n] for _n in names])
2107
-> 2108 return self._vectorize_call(func=func, args=vargs)
2109
2110 def _get_ufunc_and_otypes(self, func, args):
~/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py in _vectorize_call(self, func, args)
2180 """Vectorized call to `func` over positional `args`."""
2181 if self.signature is not None:
-> 2182 res = self._vectorize_call_with_signature(func, args)
2183 elif not args:
2184 res = func()
~/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py in _vectorize_call_with_signature(self, func, args)
2244
2245 for output, result in zip(outputs, results):
-> 2246 output[index] = result
2247
2248 if outputs is None:
ValueError: setting an array element with a sequence.