1

I want to replace the even pixel values of a raster from 1 and rest with 0. Since, I have 100+ raster files therefore, I am calling filenames in loop.

I copied the python snippet after applying con operation on a single file that worked fine. However, it fails in the script and shows error as,

NameError: name 'fin' is not defined Failed to execute (RasterCalculator).

Here, fin is the variable name.

import arcpy

Path = "Path/to/folder/"

for year in range(2010,2016):
    for day in range(1,369,8):

        fin = Path + 'Raster'+ str(year) + "_D" + str(day).zfill(3) + ".tif"    
        out = Path + 'Processed_Raster'+ str(year) + "_D" + str(day).zfill(3) + ".tif"
        arcpy.gp.RasterCalculator_sa("""Con(Mod(fin,2) == 0,1,0)""", out)

How to resolve this error?

  • Print fin to the screen and see what it looks like, then confirm that that file exists in that location. – GBG May 10 '17 at 14:33
  • fin prints the correct name. – Mario May 10 '17 at 15:41
1

I think your problem might be the expression is a string to when the raster calc runs it can't find the "fin" variable. I rewrote the expression to use the arcpy.sa module for the Con and Mod functions. I would also build a list of tif files in a directory (assuming all your tifs are contained in it and not more) so that you don't miss any and don't try to use a raster that may not exist in the directory.

try this:

import arcpy, os

Path = r"Path\to\folder"

finFiles = [file for file in os.listdir(Path) if file.endswith('.tif')]

for file in finFiles:
    fin = os.path.join(Path,file)
    out = os.path.join(Path,(file.replace('Raster','Processed_Raster')))
    arcpy.gp.RasterCalculator_sa("Con(Mod('{0}',2) == 0,1,0)".format(fin), out)
  • I tried as you said, but it gives error ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 1) – Mario May 10 '17 at 16:26
  • 1
    Try it now with the changes, the syntax error was in the expression. – PMK May 10 '17 at 17:42
  • It worked. I only replaced my line with arcpy.gp.RasterCalculator_sa("Con(Mod('{0}',2) == 0,1,0)".format(fin), out). Many Thanks! – Mario May 10 '17 at 17:54
0

Raster calculator is executing your string as its own code, and the variable fin does not exist in that code. Rather than passing in the variable, pass in the actual string the code needs. I believe changing your last line to this would work:

arcpy.gp.RasterCalculator_sa("""Con(Mod(%s,2) == 0,1,0)""" % fin, out)
  • it gives error as AttributeError: Object: Tool or environment <RasterCalculator_s> not found – Mario May 10 '17 at 15:41
  • That looks like the last 'a' in RasterCalculator_sa got dropped somehow, I'm not sure how that happened. – Craig T May 10 '17 at 19:31
  • I didn't notice it, However, the error is still there, ExecuteError: ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 1) Though I could solve it, from another answer on this question. – Mario May 11 '17 at 4:01

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.