I am currently using xarray to make probability maps. I want to use a statistical assessment like a “counting” exercise. Meaning, for all data points in NEU count how many time
np.nanpercentile by default works on a flattened array, however, in this case, the goal is to reduce only the first dimension generating a 2D array containing the result at each gridpoint. To achieve this, the axis
argument of nanpercentile
can be used:
np.nanpercentile(NEU.rr, 1, axis=0)
This however will remove the labeled dimensions and coordinates. It is to preserve the dims and coords that apply_ufunc
has to be used, it does not vectorize the functions for you.
xr.apply_ufunc(
lambda x: np.nanpercentile(x, 1, axis=-1), NEU.rr, input_core_dims=[["time"]]
)
Note how now the axis is -1
and we are using input_core_dims
which tells apply_ufunc
this dimension will be reduced and also moves it to the last position (hence the -1
). For a more detailed explanation on apply_ufunc
, this other answer may help.