I have many 2d arrays of the same dimension. Each pixel has a value of 0, 1, or 23. Each array has the values distributed in different locations. I want a max-mask that is the result of overlaying all arrays and taking the max value of each location. I also want a min-mask that does the opposite. Sorry if it's a stupid question... I tried to search but only got the methods to get a single max/min value over the entire array or on a certain axis. Thanks a lot in advance if you could help!
My arrays are large and I will give a simple example here -
a = ([[0, 1, 0, 0, 23],
[1, 0, 0, 0, 1],
[23, 23, 0, 1, 1],
[1, 1, 23, 0, 1]])
b = ([[23, 1, 0, 1, 1],
[1, 0, 0, 23, 1],
[0, 23, 0, 23, 1],
[1, 1, 0, 0, 23]])
# After some coding, max_mask and min_mask should be:
max_mask = ([[23, 1, 0, 1, 23],
[1, 0, 0, 23, 1],
[23, 23, 0, 23, 1],
[1, 1, 23, 0, 23]])
min_mask = ([[0, 1, 0, 0, 1],
[1, 0, 0, 0, 1],
[0, 23, 0, 1, 1],
[1, 1, 0, 0, 1]])
As I have too many arrays created from generic funtion and they are named like data1985, data1986... data2020, is there a way I can loop through all of them in an easier way?
# this is how I create them by reading images
for i in range(1985, 2021):
globals()[f"data{i}"], globals()[f"geo{i}"], globals()[f"proj{i}"] = read_tif(r"C:\Users\wqtcl\Desktop\REDD\images/" +str(i)+".tif")
globals()[f"data{i}"][np.isnan(globals()[f"data{i}"])]=23
# I want something like this (or easier!!)
# initialize array filled with zeros
mask = np.zeros([len(data1985), len(data1985[0])], dtype=int)
# populate array
for i in range(1985, 2021):
for j in range(len(data1985)):
for k in range(len(data1985[0])):
mask[j][k] = max(globals()[f"data{i}"][j][k])
# I got this error though...
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_18020/2600759678.py in <module>
6 for j in range(len(data1985)):
7 for k in range(len(data1985[0])):
----> 8 mask[j][k] = max(globals()[f"data{i}"][j][k])
9
10 print(mask)
TypeError: 'numpy.float32' object is not iterable