I am trying to figure out a way to have my model perform its functions on the rasters contained inside the sub-folders of the root folder. I can't use the default iteration function in model builder because it only looks for rasters in the root folder. This is what my model looks like currently.

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")

# Script arguments
Basins = arcpy.GetParameterAsText(0)
if Basins == '#' or not Basins:
    Basins = "Basins" # provide a default value if unspecified

# Local variables:
Basins_Project2 = "C:\\Users\\straussk\\Documents\\ArcGIS\\Default.gdb\\Basins_Project2"
UnmaskedData = "U:\\SWEModelConstruction\\UnmaskedData"
Raster = "U:\\SWEModelConstruction\\UnmaskedData\\Raster"
SetNull_Rast2 = "C:\\Users\\straussk\\Documents\\ArcGIS\\Default.gdb\\SetNull_Rast2"
Output_Name = "C:\\Users\\straussk\\Documents\\ArcGIS\\Default.gdb\\ZonalSt_Basins_3"
ZonalSt_Basins_3 = Output_Name
ZonalSt_Basins_3__6_ = ZonalSt_Basins_3
Raster_Output = ZonalSt_Basins_3__6_
Basin_Raster_Output = "U:\\SWEModelConstruction\\UnmaskedData\\%Name%.xls"
Name = "Raster"

# Process: Project
arcpy.Project_management(Basins, Basins_Project2, "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]", "", "PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]]", "NO_PRESERVE_SHAPE", "")

# Process: Iterate Rasters
arcpy.IterateRasters_mb(UnmaskedData, "", "", "NOT_RECURSIVE")

# Process: Set Null
arcpy.gp.SetNull_sa(Raster, Raster, SetNull_Rast2, "\"Value\" = 55537")

# Process: Zonal Statistics as Table
arcpy.gp.ZonalStatisticsAsTable_sa(Basins_Project2, "Drainage_A", SetNull_Rast2, Output_Name, "DATA", "ALL")

# Process: Calculate Field (7)
arcpy.CalculateField_management(Output_Name, "Mean", "[Mean] /1000", "VB", "")

# Process: Calculate Field (8)
arcpy.CalculateField_management(ZonalSt_Basins_3, "Std", "[Std] /1000", "VB", "")

# Process: Calculate Field (9)
arcpy.CalculateField_management(ZonalSt_Basins_3__6_, "Sum", "[Sum] /1000", "VB", "")

# Process: Table To Excel
arcpy.TableToExcel_conversion(Raster_Output, Basin_Raster_Output, "ALIAS", "CODE")

My rasters are placed in folders categorized by their year and date so simply inputting them into one root file is not possible. Is there a way to perform this function on a file system like the following U:\SWEModelConstruction\UnmaskedData\2010\February\15 where the raster files are located on the 15, not the year or month they are just sub-folders to categorize data.

  • 1
    Have you tried Walk? desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-data-access/… – phloem Feb 21 '17 at 19:32
  • Can walk be performed in conjunction with iteration or does it replace it? – user92047 Feb 21 '17 at 19:51
  • 2
    It replaces it. But now that I look at what you've got, why not just change "NOT_RECURSIVE" to "RECURSIVE" in your model? – phloem Feb 21 '17 at 20:10
  • Your right that does run everyone of my files Thank you. I have one more question, is there a way for the output of my file to still go to the sub folder? – user92047 Feb 21 '17 at 20:33
  • While there are no answers is an ideal time to revise what it is that you wish to ask in this question by using the edit button beneath it. – PolyGeo Feb 22 '17 at 21:21

Iterate Rasters in ModelBuilder should do what you need. Just set "RECURSIVE" to TRUE like @pholem suggested and it'll go through all the subfolders looking for those rasters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy