Posted By: Anonymous
My dataset has 3 dimensions in the order
(time, y, x) and I use
apply_ufunc to apply a computation along the
time dimension. This rearranges the order of the dimensions as
(y, x, time). I need to restructure the xarray so its in the
(time, y, x) order as the original dataset. How would I go along doing this?
Here is a visual description of what’s happening:
Then I apply my function:
dcube = xr.apply_ufunc( bc.clip_and_normalize_percentile, dcube, input_core_dims=[["time"]], output_core_dims=[["time"]], dask = 'allowed', vectorize=True )
time is moved to the last dimension:
How do I rearrange this so that its in the order of the original array? Are there parameters that prevent apply_ufunc from moving the dims?
The docs say that
Core dimensions are automatically moved to the last axes of input
variables before applying func, which facilitates using NumPy style
so it’s unlikely that there’s a way (or any parameters) to prevent that.
What I’ve been doing is simply call
.transpose afterwards to restore the initial order.
In your example, that would look like:
dcube = dcube.transpose("time", ...)
time to be the first dimension and shifting all other ones behind using