Perform Raster Calculation from Multiple Sub-folder Using ArcPy

3180
23
Jump to solution
10-07-2016 05:14 AM
ShouvikJha
Occasional Contributor III

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))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
23 Replies
DanPatterson_Retired
MVP Emeritus

If the rasters aren't huge convert to points, then Ordinary Least Squares (OLS)—Help | ArcGIS for Desktop I think the report allows for a graph

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi shouvik jha ,

When you ask a new question, could you please create a new question? In case it has a relation with a previous question, include the link to that question. I branched the question to a new thread: Scatter plot of two rasters . However, you replied to a different level, so the last comments have not been passed to the new thread. You should not mix questions in the same thread. In Dan would resolved this question, and you would mark it correct, people looking at this thread due to the title will be completely confused. 

Please move your posts to the new thread.

Sorry Dan Patterson  for the inconvenience.

ShouvikJha
Occasional Contributor III

xander_bakker‌, Thank you for the suggestion. I am extremely sorry. Next time surely i will follow your suggestion. Sorry for the inconvenience. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

No problem Xander, I will look for it

0 Kudos