Select to view content in your preferred language

How to save every iteration's output from a double for loop in arcpy

2433
4
Jump to solution
11-22-2020 01:28 PM
CamiSunley
Emerging Contributor

Hello, everyone. I have two sets of similar kind of rasters (1st set: idwlist, 2nd set: equallist) and I want to multiply each raster from the 1st group (‘eachidw’) with every raster from the 2nd group (‘eachequal’). In other words, I want to create a code that calculates and stores all the possible products (multiplications) between the rasters of the two groups.

 The problem with the arcpy code below is that the output of every iteration is not saved but overwritten, so I would like to ask if there is any way to make the code save each multiplication output. Thanks a lot !

from arcpy import env

from arcpy.sa import *

import os

arcpy.env.workspace = "C:/MSc_thesis/XrApor"

idwlist  = arcpy.ListRasters("*", "TIF")

equallist = arcpy.ListRasters("*", "GRID")

for eachidw in idwlist:

     for eachequal in equallist:

          outTimes = Times(eachidw,eachequal)          

.         outTimes.save("C:/MSc_thesis/products")

 

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor
for i, eachidw in enumerate(idwlist):
     for j, eachequal in enumerate(equallist):
          outTimes = Times(eachidw,eachequal)
          nme = "C:/MSc_thesis/products/" + "raster_{}_{}".format(i, j)
.         outTimes.save(nme)

in its simplest form.  Now if you need the actual names and eachidw and eachequal are short names and not full paths, you can replace, i, j in the format section


... sort of retired...

View solution in original post

4 Replies
DavidPike
MVP Frequent Contributor

Logically you would want to save the output file as a combination of the names of the rasters.

I'd grab the names of the rasters before you multiple them, and create a path from that combination.

ensure you import the os module (import os)

 

from arcpy import env

from arcpy.sa import *

import os

arcpy.env.workspace = "C:/MSc_thesis/XrApor"

idwlist  = arcpy.ListRasters("*", "TIF")

equallist = arcpy.ListRasters("*", "GRID")

for eachidw in idwlist:

    for eachequal in equallist:

        out_name = eachidw.name + "_" + eachequal.name

        out_path = os.path.join(r"C:/MSc_thesis/products", out_name)

        outTimes = Times(eachidw,eachequal)          

        outTimes.save(out_path)

 

 

CamiSunley
Emerging Contributor
I really appreciate your response. Since I am a beginner in python I am not sure if I replace correctly the elements eachidw.name and eachequal.name you use in the code. Should I use the full path of the first rasters respectively or sth else ? Which is the proper format ? Thanks.
0 Kudos
DanPatterson
MVP Esteemed Contributor
for i, eachidw in enumerate(idwlist):
     for j, eachequal in enumerate(equallist):
          outTimes = Times(eachidw,eachequal)
          nme = "C:/MSc_thesis/products/" + "raster_{}_{}".format(i, j)
.         outTimes.save(nme)

in its simplest form.  Now if you need the actual names and eachidw and eachequal are short names and not full paths, you can replace, i, j in the format section


... sort of retired...
CamiSunley
Emerging Contributor

I tried your suggestion and it works. That was really helpful, thank you very much.

0 Kudos