Hi everyone,
I am trying to use apply_ufunc for a ds (lat, lon, depth, time). I have done it in the past with simple functions but now I am struggling with scipy.signal.argrelextrema. I want to find the local maxima/minima over depth for the entire dataset.
argrelextrema gives a tuple of ndarrays even for 1D data, so for one water column profile I would have:
data = 283 + 5 * np.random.randn(5, 3, 4, 10)
var = xr.DataArray(data, dims=['time', 'lat', 'lon', 'depth'])
maxidx = argrelextrema(var.isel(time=0, lat=0, lon=0).values, np.greater)[0]
I am trying to extend this for the entire dataset to avoid looping through time, lat and lot (Iâve done the looping but itâs taking too long).
I am not sure if itâs even feasible with applu_ufunc given that the length of maxidx would be different for all points. Here is my try:
maxidx = xr.apply_ufunc(argrelextrema, var, kwargs=dict(comparator=np.greater, axis=-1),
input_core_dims=[["depth"]], dask='parallelized', output_core_dims=[[]],
dask_gufunc_kwargs={'allow_rechunk': True})
but I am getting the error:
{ValueError}ValueError(âapplied function returned data with an unexpected number of dimensions. Received 0 dimension(s) but expected 3 dimâŚ, 3, 5, 2, 5, 4, 6, 5, 7, 2, 4, 7, 2, 5, 8, 2, 7, 3, 5, 8,\n 1, 3, 5, 7, 5, 8, 2, 4, 8, 2, 6, 3, 5, 7])), dtype=object)â)
I donât know how to specify that I want the 0th position in the tuple (so the ndarray) and how to assing that to a new dimension that takes the length of the largest???
Any thoughts/ideas? Thanks!