Hi,
As the title says, I'm trying to calculate in python the mean value out of all maximum values in a list of rasters (696) in a GDB.
First thing would be to calculate all max values but I'm not sure how to "hold" all those values to then calculate the mean.
Any help will be greatly appreciate it.
Running ArcGis 10.5.1
Solved! Go to Solution.
That works beautifully. Really appreciate it.
Could you try this and share what it prints (it's basically the same code as Jake shared earlier)?
import arcpy
import os
# settings
ws = r"D:\000_TEST_MAGNITUDE_summary\MAX_VAL_calculation\Vel_rasters.gdb"
arcpy.env.workspace = ws
# List rasters
raster_names = arcpy.ListRasters()
# loop through rasters
max_values = []
for raster_name in raster_names:
raster_path = os.path.join(ws, raster_name)
vel_max = arcpy.GetRasterProperties_management(raster_path, "MAXIMUM")
print(" - {}: {}".format(raster_name, vel_max))
max_values.append(float(vel_max.getOutput(0)))
mean_val = float(sum([max_values])) / len(max_values)
print("Mean value: {}".format(mean_val))
Thank you Xander. It seems there is a minor issue in line 19.
I also tried with float(len(max_values) but getting the same error. Other than that, I'm sure is perfect. Very nice the way it prints the max values for each raster. Both Jake's and Dan's are working and giving the same result. Many thanks.
Can't let a numpy opportunity go by...
import arcpy
arcpy.env.workspace = r"C:\Data\rasters"
import numpy as np
r_max = np.finfo(np.float).min
for raster in arcpy.ListRasters("*"):
ras = arcpy.RasterToNumPyArray(raster)
r_max = max(r_max, np.nanmax(ras))
print(r_max)
255
Mean of max's? no problem
import arcpy
arcpy.env.workspace = r"C:\Data\rasters"
import numpy as np
r_max_mean = []
for raster in arcpy.ListRasters("*"):
ras = arcpy.RasterToNumPyArray(raster)
r_max_mean.append( np.nanmax(ras) )
print("max_list\n{}\nmean_max {}".format(r_max_mean, np.nanmean(r_max_mean)))
max_list
[255, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
mean_max 103.875
That works perfect Dan. All the slightly different approaches above give same result. Really appreciate your help once again.