AnsweredAssumed Answered

Perform Raster Calculation from Multiple Sub-folder Using ArcPy

Question asked by india123 on Oct 7, 2016
Latest reply on Oct 11, 2016 by Dan_Patterson
Branched to a new discussion

I have five subfolders with Rasters name with the same parameter. I am trying to figure out a way to Loop average of all  raster and save output raster in outfolder . For instance 

Subfolder1   Subfolder2   Subfolder3   Subfolder4    Subfolder5   Outfolder
r001_NPP.tif r001_npp.tif r001_NPP.tif r_001_NPP.tif r001_NPP.tif r001_MEAN_NPP.tif

I have tried to write code for this, Below i post my code what i have so far

import arcpy, os, glob
arcpy.CheckOutExtension("Spatial")

ws1 = glob.glob(r'D:\subfolder1\*.tif')
ws2 = glob.glob(r'D:\subfolder2\*.tif')
ws3 = glob.glob(r'D:\subfolder3\*.tif')
ws4 = glob.glob(r'D:\subfolder4\*.tif')
ws5 = glob.glob(r'D:\subfolder5\*.tif')
outws = r'D:\outfolder'

for r in ws1, ws2, ws3, ws4:
    basename = os.path.basename(r).split("_")[0]
    r1 = arcpy.sa.Raster(os.path.join(ws1, basename + "r_NPP.tif"))
    r2 = arcpy.sa.Raster(os.path.join(ws2, basename + "r_NPP.tif"))
    r3 = arcpy.sa.Raster(os.path.join(ws3, basename + "r_NPP.tif"))
    r4 = arcpy.sa.Raster(os.path.join(ws4, basename + "r_NPP.tif"))
    r5 = arcpy.sa.Raster(os.path.join(ws5, basename + "r_NPP.tif"))
   

    result = (r1 + r2 + r3 + r4 + r5) / 5
    outname = basename + "r_Mean_NPP.tif"

    result.save(os.path.join(outws, outname))

Outcomes