If you have an array with x, y and z dimensions, how can I create an array of x, y dimensions that holds a count of how many z values at each location (x,y) are noData?
I would prefer to use numpy and python for this task, but could also use arcpy (I am working with raster data) or R.
Best Answer
Assuming that you allready read your raster data into a numpy array called
raster_stack
and stacked all rasters along the z-axis.This will result in a 2-dimensional array containing the count of
no_data
values for each x,y location. It will also be as fast as you can get with Python since all the looping is handled bynumpy
in fast C functions, instead of slow Python loops.How does it work:
raster_stack == no_data
creates abool
array with the same dimensions as yourraster_stack
which containsTrue
for allno_data
observations.In numpy you can treat True/False like 1/0, meaning that
True + True
will equal2
.np.sum
sums up all True observations along the z-axis (axis=0
) and returns the result as a flattened 2D-array.To support my performance claim, let's compare this with the numpy method in @JamesSLC answer.
.
.
.
Using numpys internal functions without the masked array is roughly 3 times faster. It should be noted however that the vast majority of executing the real task will be spent reading the data into numpy arrays in the first place.