1

I am trying to combine two different raster files using a loop, with the following script.

import  arcpy
import os

thectr = 0
ws = arcpy.env.workspace = r'D:\BRB Snow Cover\Sample\TerAqua'
Terra_Rasters = arcpy.ListRasters("MOD*")
Aqua_Rasters = arcpy.ListRasters("MYD*")
output = "D:\\BRB Snow Cover\\Sample\\output""\\"+ str(thectr)+'.tif'
matches = 0
for item1 in Terra_Rasters:
    for item2 in Aqua_Rasters:
        print item1
        print item2
        if item1.endswith(item2[32:51]):
            print "Match"
            arcpy.gp.Con_sa(item1,item2,output,item1,"\"Value\" =8")
        else:
            print "No"
            matches = matches+1

I need to use a loop for the output files name, I faced the following error.

Runtime error Traceback (most recent call last): File "", line 7, in File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\geoprocessing_base.py", line 510, in return lambda *args: val(*gp_fixargs(args, True)) ExecuteError: ERROR 000872: Output raster: Dataset D:\BRB Snow Cover\Sample\output0.tif already exists and cannot be overwritten since the Overwrite existing datasets option is disabled.

2
  • No, the files are time series with some missing days, I need to use the name from one of input files Commented Aug 15, 2019 at 5:48
  • I want the output name be same as item1 or either item2. I put above code to my script. I got an error. Commented Aug 15, 2019 at 13:49

1 Answer 1

1

A few things here:

1) output will always be D:\BRB Snow Cover\Sample\output0.tif in your script. You are not changing it anywhere. If this is the expected behaviour (which I suspect is not) and you want to overwrite that file, simply write arcpy.env.overwriteOutput = True.

2) If you want to change the output name and increase it by 1 (which I assume is what you're after), modify your code so each time arcpy.gp.Con_sa() is executed, output is changed beforehand. It could look something like this:

import  arcpy
import os

thectr = 0
ws = arcpy.env.workspace = r'D:\BRB Snow Cover\Sample\TerAqua'
Terra_Rasters = arcpy.ListRasters("MOD*")
Aqua_Rasters = arcpy.ListRasters("MYD*")
matches = 0
for item1 in Terra_Rasters:
    for item2 in Aqua_Rasters:
        print item1
        print item2
        if item1.endswith(item2[32:51]):
            print "Match"
            output = "D:\\BRB Snow Cover\\Sample\\output""\\"+ str(thectr)+'.tif'
            arcpy.gp.Con_sa(item1,item2,output,item1,"\"Value\" =8")
            thectr += 1  # here you increase the variable by one each time Con is executed
        else:
            print "No"
            matches = matches+1

One of your comments says you want to match either item1 or item2 name in the output. In this case, simply change the output line to:

output = "D:\\BRB Snow Cover\\Sample\\output""\\"+ str(item1)+'.tif'

3) output string could be more readable. Consider changing it to something like:

output = r'D:\BRB Snow Cover\Sample\output{}.tif'.format(thectr)

4) You are doing a nested loop here. Assuming i is the number of elements in Terra_rasters and j is the number of elements in Aqua_rasters, you'll end up executing i * j loops. If this is not what you are trying to do, but instead you are trying to execute i loops (I assume both lists have the same number of elements) and want to access both rasters (Terra and Aqua) at a time, you can use the zip() function in only one loop:

for item1, item2 in zip(Terra_Rasters, Aqua_Rasters):
   print item1
   print item2
1
  • The output = "D:\\BRB Snow Cover\\Sample\\output""\\"+ str(item1)+'.tif' is exactly what I need. Commented Aug 16, 2019 at 2:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.