Getting the Mean value out of the Max values of a raster data set

3417
15
Jump to solution
11-27-2018 03:41 AM
anTonialcaraz
Occasional Contributor II

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

Tags (3)
0 Kudos
15 Replies
anTonialcaraz
Occasional Contributor II

That works beautifully. Really appreciate it.

0 Kudos
XanderBakker
Esri Esteemed Contributor

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))
anTonialcaraz
Occasional Contributor II

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.

0 Kudos
DanPatterson_Retired
MVP Emeritus

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
DanPatterson_Retired
MVP Emeritus

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
anTonialcaraz
Occasional Contributor II

That works perfect Dan. All the slightly different approaches above give same result. Really appreciate your help once again.

0 Kudos