[GIS] Iterate through rasters in subfolders using Model Builder

arcgis-desktopmodelbuilderrasterraster-calculator

I have a directory folder of precipitation data; within this folder are subfolders for years 1901-2014. In each of these subfolders are 12 .asc grid files; each file represents a month of the year (i.e. Jan through Dec).

I am trying to create a model that will iterate through each of these subfolders and perform a calculation on the 12 grid files (for example: calculate total annual precipitation). I may want to perform other calculations, but for now would like to just have the model working.

I have tried the Iterate Workspace function with the Raster Calculator, and the Iterate Rasters function with the Raster Calculator, but could not get either to work how I wanted.

I also tried Iterate Rasters with Collect Values, and Cell Statistics – but that is also not working.

Below are the models I have tried:RastCalcCalcStats

Best Answer

When faced with a problem like this I use os.walk() which returns all files and then see if the files match what I want by extension, here's an example for shapefiles:

import sys, os, arcpy
InFolder = sys.argv[1]

for (path, dirs, files) in os.walk(InFolder):
    for ThisFile in files:
        fName,fExt = os.path.splitext(ThisFile)
        if fExt.upper() == ".SHP":
            fc = path + "\\" + ThisFile

Change fExt.upper() == ".SHP": to fExt.upper() == ".ASC": and then operate as normal with fc being the full path to the dataset.

The other option is to put all the rasters in manually... or semi manually:

Open a command prompt in your base folder and type:

DIR *.ASC /B/S

Which gives a list of all the ASCII files in all subfolders... if there's too many to select then try piping to a text file:

DIR *.ASC /B/S > ASC_File_List.txt
START NOTEPAD ASC_File_List.txt

Then select all, copy, and paste into the tool.

Related Question