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")
Solved! Go to Solution.
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
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)
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
I tried your suggestion and it works. That was really helpful, thank you very much.