通常,我们需要将多个栅格求平均,例如,将一年中每个月的NDVI值加起来除以12,就会等到月均NDVI,该过程虽然在栅格计算器中可以实现,但是当时间序列较长时就比较费事,此时,python代码是不二的选择。
下图所示为栅格数据相加的原理图,也就是对应的栅格相加,生成新的栅格数据。求均值是需要再除以栅格个数。

在独立脚本中编写如下代码:

python求均值代码友情赠送:
1import arcpy 2from arcpy.sa import * 3arcpy.CheckOutExtension("spatial") 4arcpy.gp.overwriteOutput=1 5 6#custom 7arcpy.env.workspace="G:\\Phenology of 30 Years\\GIMMS 3g\\15Length\\1Length\\" 8#custom 9outpath="G:\\Phenology of 30 Years\\GIMMS 3g\\15Length\\2mean_len\\" 10#custom 11outfilename="mean";n=30; 12 13Sum=0 14 15files=arcpy.ListRasters() 16 17for file in files: 18 Sum=Sum+Raster(file) 19(Sum/n).save(outpath+outfilename) 20 21print("Done,please close")