I've been trying to find the best way to calculate the rank of a value from a 3d numpy array. This array is created from 35 years worth of rainfall data rasters. I'm treating the last raster in the stack as the "base" raster for comparison of the ranking in this case.
The numpy array's shape would be something like (36, 500, 500). For each column I need to get all the values from the stack and then rank where the most recent (last) year falls.
So far I have:
for row in range(rows): for col in range(cols): # get an array of all the values of the raster # stack at current row and col iteration pixel_stack = stor_array[:, row, col] # Get the rank index of the last value in the pixel_stack rank_index = int(stats.rankdata(pixel_stack, 'min')[-1]) # add the rank index at current # row and col iteration to and output array out_array[row, col] = rank_index
This method works but seems slow. Is there a better (quicker/cleaner) way to iterate over the columns of the array?
For instance, is there anything that would allow me to use scipy function in a similar way to this numpy one?
out_arr = np.mean(stor_arr, axis=0)