ArcGIS python calculates the average value of multiple raster data in a long time series

Usually, we need to average multiple rasters. For example, if we add up the NDVI value of each month of the year and divide by 12, we will wait until the monthly average NDVI. Although this process can be implemented in a raster calculator, When the time series is long, it is more troublesome. At this time, python code is the best choice.

The following figure shows the principle of adding raster data, that is, adding corresponding rasters to generate new raster data. The average value needs to be divided by the number of grids.

Write the following code in a stand-alone script:

Python mean value code as a gift:

import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("spatial")
arcpy.gp.overwriteOutput=1
 
#custom
arcpy.env.workspace="G:\\Phenology of 30 Years\\GIMMS 3g\\15Length\\1Length\\"
#custom
outpath="G:\\Phenology of 30 Years\\GIMMS 3g\\15Length\\2mean_len\\"
#custom
outfilename="mean";n=30;
 
Sum=0
 
files=arcpy.ListRasters()
 
for file in files:
    Sum=Sum+Raster(file)
(Sum/n).save(outpath+outfilename)
 
print("Done,please close")

Guess you like

Origin blog.csdn.net/lucky51222/article/details/109412616