How do I ask a “for loop” to operate on groups of three files folder (e.x. files A, B, C), then files B, C, D, and then files C, D, E, and so on?

I have a folder full of rasters (.tif): RasterA.tif, RasterB.tif, RasterC.tif, RasterD.tif…and so on. I’ve got a python script that can run the batch conditional test I want. However, it only runs through one raster at a time, and I’d like it to run through multiple.

I think it’s easier to show via code. Please see below.

# Omitted the top part of the script for readability. 
# I have all the imports (arcpy and os), set the environment, set overwrite, set cell size
# Please let me know if I need to add this top part back in to the question and I will

rasters = arcpy.ListRasters("*", "ALL")
for raster in rasters:
    inRas = Raster(raster) #pulls the first raster (ex. Raster A)
    inRas2 = Raster(raster) 
    # Right now I know this means inRas and inRas2 are the same thing. 
    # What I'd like to do is have inRas2 be the next raster "in line" (ex. Raster B) 
    inRas3 = Raster(raster) 
    # Same deal as above, would like it to be Raster C

    # The CON test itself 
    output = arcpy.sa.Con(inRas >= 25, 1, 0) + arcpy.sa.Con(inRas2 >= 20, 1, 0) + arcpy.sa.Con(inRas3 >= 15, 1, 0)

    # My output raster needs to be identified by the first file in the group
    # This part runs fine, can ignore in context of question 
    RasName, RasExt = os.path.splitext(raster)
    output.save(outWorkspace + RasName + ".tif")

The fixed script is below. Everything ran without problem.

# Get a list of Rasters 
rasters = arcpy.ListRasters("*", "ALL")
# set it so can pull group of rasters with indexing  
n = len(rasters)
# run the con test on batch, in groups of 3
for i in range(2,n):
   rasterA = Raster(rasters[i-2])
   rasterB = Raster(rasters[i-1])
   rasterC = Raster(rasters[i])
   output = arcpy.sa.Con(rasterA >= 25, 1, 0) + arcpy.sa.Con(rasterB >= 20, 1, 0) + arcpy.sa.Con(rasterC >= 15, 1, 0)
   RasName, RasExt = os.path.splitext(rasters[i-2])
   output.save(outWorkspace + RasName + ".tif")
   # I have it print just to make sure naming files goes right
   print RasName
   print RasExt
   print "Done"
  • [rasters[i:(i+3)] for i in range(0, len(rasters) - 2)] – Mike T Sep 3 '14 at 2:12
  • FelixP and PolyGeo suggested a similar fix, which worked. Thank you! – Savi Sep 3 '14 at 14:28
n= len(rasters)

for i in range(2,n):
| improve this answer | |
  • This worked! Thank you PolyGeo and FelixP. I will add the fixed script to my original question. – Savi Sep 3 '14 at 14:23
  • If you work a lot with SA use from arcpy.sa import *. Code will look nicer, e.g. output=Con(raster>=25,1,0). Your code can be simplified output=(rasterA>=25)+(raster>=20)+(raster>=15) – FelixIP Sep 3 '14 at 22:09

I do most of my programming in C++, but have some knowledge of python and arcpy. Have you tried this?

for raster in rasters:
    if raster < rasters-2
        inRas = Raster(raster) #pulls the first raster (ex. Raster A)
        inRas2 = Raster(raster+1) 
        # Right now I know this means inRas and inRas2 are the same thing. 
        # What I'd like to do is have inRas2 be the next raster "in line" (ex. Raster B) 
        inRas3 = Raster(raster+2) 
| improve this answer | |
  • Thank you for the suggestion. Unfortunately it didn't seem to work. I just get the error: if raster < rasters-2: TypeError: unsupported operand type(s) for -: 'list' and 'int' – Savi Sep 2 '14 at 21:26
  • Ok, I see. What you need to do when you assign inRas2 and inRas3 is assign them an index 1 or 2 ahead of the current raster. Apparently I'm a little rusty with my python syntax. The if statement is just to stop the loop from assigning rasters outside of the list index. You can remove the if statement and try to run it, but the code will crash when it gets to the third to last raster in the folder – khafen Sep 2 '14 at 21:51
  • The fix suggested below worked, so we're all good. Indexed, just as you said. – Savi Sep 3 '14 at 14:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.