Select to view content in your preferred language

problem with 2 iterators(one nested)

1335
6
04-20-2018 02:29 AM
panosfountou
Emerging Contributor

Hello , 
Iam a student from Greece and I want to create a tool in modelbuilder that uses diff toolbox and takes the 2 inputs that diff toolbox needs from 2 folders. I tried iterator raster but i cant use 2 in one modelbuilder so i have to nest one and save it as model and then insert this model into my final model.So i have one iterator raster and one model .The outputs of these 2 (iterator raster and model ) go as inputs to my diff toolbox . The problem is that the model iterates through all my rasters in the folder and stops in the last one and that it the input that goes to diff toolbox.My job is to , for example: i have 32 images  in one folder and 32 in another and i need to add as inputs to diff toolbox the 1st of every folder and then the 2nd and the 3rd etc but even when my one iterator is working fine, the nested iterator goes through all my rasters ,stops in the last one and adds as input to diff the last image of the folder so i have the 1st image of the folder1 with the last image of the folder2 and then the 2nd of the folder1 with the last of the folder2 and so goes on!

any suggestions on how to solve this or can anyone create something like this and send a picture?
thanks and sorry for my english if you dont understand something!

Panagiotis

0 Kudos
6 Replies
AbdullahAnter
Honored Contributor

32 raster in not huge number.Use batch of Diff tool rather than model builder

0 Kudos
XanderBakker
Esri Esteemed Contributor

Are these examples of the raster filename that you are using?

  • MYD09GQ.A2004202.h20v05.006.2015
  • MYDTBGA.A2004202.h20v05.006.2015

If, so you should be able to use one raster name and deduce the name of the corresponding raster in the other folder. 

I would prefer using Python to do this. Is it a requirement to use Model Builder? 

panosfountou
Emerging Contributor

well first of all thanks for answer this .Second, I said 32 raster for example...all my rasters are about 500. 
Xander Bakker I dont know how to do this on python or either how to use it after .It would be very helpfull if you could do this of course if its easy for you and you have time otherwise i guess i have to do it with modelbuilder.!

0 Kudos
XanderBakker
Esri Esteemed Contributor

The complexity of the script depends highly on how your rasters are named. Let's assume a simple well structured case. Rasters in both folder share part of the name but have different prefixes: "MYD09GQ" and "MYDTBGA". In that case you would list the rasters of one of the folders and deduce name of the corresponding rasters in the other folder, perform the difference calculation (Minus) and store the result. 

An example of what this (untested) code could look like:

def main():
    import arcpy
    import os

    # settings
    folder1 = r'C:\Path\To\Folders\With\Rasters1'
    folder2 = r'C:\Path\To\Folders\With\Rasters2'
    out_folder = r'C:\Path\To\Folders\With\Result'
    prefix1 = 'MYD09GQ'
    prefix2 = 'MYDTBGA'
    prefix_result = 'DIF'

    # list rasters in folders 1
    arcpy.env.workspace = folder1
    lst1 = arcpy.ListRasters()

    # check out SA extension
    arcpy.CheckOutExtension("SPATIAL")

    # loop through list of rasters
    for ras_name1 in lst1:
        # determine path name of raster 1
        ras1 = os.path.join(folder1, ras_name1)
        # deduce path and name of raster 2
        ras_name2 = ras_name1.replace(prefix1, prefix2)
        ras2 = os.path.join(folder2, ras_name2)
        # calculate the difference
        dif = arcpy.sa.Minus(ras1, ras2) # or ras2 - ras1
        # determine output name and path
        out_name = ras_name1.replace(prefix1, prefix_result)
        out_path_name = os.path.join(out_folder, out_name)
        # store the result
        dif.saveas(out_path_name)

    # return SA extension
    arcpy.CheckInExtension("SPATIAL")

if __name__ == '__main__':
    main()
panosfountou
Emerging Contributor

I think its good (didnt test it) but I cant find how this is gonna help me using diff tool and having correct results?

0 Kudos
XanderBakker
Esri Esteemed Contributor

It uses the Minus tool in Python to get the difference between the two rasters (for each pair of rasters assuming that the naming is as I described before). After reading more carefully your question I realize that you want to use the Diff—Help | ArcGIS Desktop tool. 

In that case just change line 28 into:

dif = arcpy.sa.Diff(ras1, ras2)