2

This is probably easiest explained with a picture,

enter image description here

I'm trying to iteratively perform a calculation using specific rasters from each two consecutive folders in a folder. For example from above picture, Iterate through the Date Folders in the Year Folder, and select the B4.TIF from date 1 and date 2, then process using these two tifs.

I can parse and slice filenames for the output files and folders, however just cannot figure out how to selectively iterate?

Have tried to parse filename and slice/calculate value for an inline variable based on each date to use as a wildcard but I'm just getting rasters from the last folder in the list.

I know using python would be easier, but I don't have the skills hence the post!

  • 1
    I doubt it is possible using Modelbuilder. Do you have enough python skill to post a code attempt and you will get the help you need to make it work? See for example this post: stackoverflow.com/questions/5817209/… – BERA Mar 27 '18 at 8:03
2

I doubt it is possible using Model Builder. So this is one Python way to do it which you are not asking for. I can delete the answer if you want. Change root folder and out folder and it should work. You can execute it in the Python window. I don't have Spatial Analyst so I have not tested it fully.

import arcpy, os
from arcpy.sa import *
from collections import defaultdict
from itertools import tee, izip

root_folder = r'C:\Test\Root_folder'
out_folder = r'C:\Test\outfolder'
file_ending_to_select = 'B4.TIF'

def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

d = defaultdict(list)

for subdir, dirs, files in os.walk(root_folder):
    for file in files:
        if file.endswith(file_ending_to_select):
            d[subdir.split('\\')[-2]].append(os.path.join(subdir,file))

print 'Defaultdict is: ', d

for year, rasters in d.iteritems():
    for y1, y2 in pairwise(rasters):
        #For this line to work the file name need to look like in your screenshot:
        foldername = '{0}-{1}'.format(os.path.basename(y1).split('_')[-5], os.path.basename(y2).split('_')[-5])
        newfolder = os.path.join(out_folder, foldername)
        os.makedirs(newfolder)
        outMinus = Minus(y1, y2)
        outMinus.save(os.path.join(newfolder,'{0}.tif'.format(foldername)))
  • that's programming for me, as frustrating as it is promising. I'm copying the scipt into the Python window in Arc, hitting enter and nothing happens! I will do some reading and see if I'm missing anything. – Grasshopper Mar 27 '18 at 23:41
  • @Grasshopper: Have you changed root_folder and out_folder? I have changed the script to look for files ending with B4.TIF instead of B4.tif and added a print statement. What gets printed out? – BERA Mar 28 '18 at 4:56
  • 1
    That worked, wow. I think I was not entering the folder directories correctly. Ran very quickly too. I might be inspired to figure out what this script is doing. Thanks! One other thing, is it possible to have the Result.tif named the same as the output folder? – Grasshopper Mar 28 '18 at 7:02
  • @Grasshopper yes try again. Try to research os.walk, collections.defaultdict, and how to manipulate strings using format, split, and indexing (like [-5]). – BERA Mar 28 '18 at 7:25

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.