[GIS] What might cause invalid syntax from ArcPy with Raster Calculator and Multiple Rasters

arcgis-10.1arcgis-desktoparcpypythonraster

I am having trouble for writing script in Python (ArcGIS 10.1). The task is generally divide each raster by 100. I have many of these raster files…

So what I've done is the following:

from arcpy.sa import *
  arcpy.workspace="C:/Users/Vaio/.../Monthly_Original.gdb"
  rasterlist=arcpy.ListRasters(p)
  for k in range():
     OutRaster=(Raster(rasterlist[k])/100
     OutRaster.Name='p_2000_*.*'
     OutTaster.Save(OutRaster.Name)

I have an Error in line 6. I am very new to any sort of programming.

Best Answer

Insert 'import arcpy' at the top of your code (NameError: name 'arcpy' is not defined)

No indentation needed after the import statements

Check out the ArcGIS Spatial Analyst extension license using 'arcpy.CheckOutExtension("Spatial")' (RuntimeError: ERROR 000824: The tool is not licensed.)

Assuming that 'arcpy.workspace="C:/Users/Vaio/.../Monthly_Original.gdb"' is not the actual directory path run in the code, but merely abbreviated for posting here - you still need to state arcpy.env.workspace

'ListRasters(p)' needs to have the variable 'p' previously defined (NameError: name 'p' is not defined)

'range()' expects atleast one argument - or try something like for 'k in rasterlist:' (TypeError: range expected at least 1 arguments, got 0)

This is the reason for the invalid syntax error: 'OutRaster=(Raster(rasterlist[k])/100' has two opening parentheses but only one closing parenthesis

Do not use '.Name' for assigning the output name (AttributeError: 'Raster' object has no attribute 'Name') Note: (lower case) '.name' is Read Only

What are the '*' in 'p_2000_*.*' supposed to mean? If you want variables then you may need a counter variable defined, or extract the original file name from the item used in the for loop with rasterlist to replace the first '*' (perhaps the second '*' could be replaced with the text for the desired output raster file extension). The '*' characters used in the output name will cause more errors. (RuntimeError: ERROR 010093: Output raster format UNKNOWN is unsupported.)

The very last line needs to be changed from 'OutTaster' to 'OutRaster' (NameError: name 'OutTaster' is not defined)

The '.save' cannot be capitalized (AttributeError: 'Raster' object has no attribute 'Save')

EDIT: Now that I finally have access to the software I was able to revise your code - you may need to modify to suit your actual data (see comments and online help):

import arcpy
from arcpy.sa import *
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Set the workspace environment
arcpy.env.workspace="C:/Users/Vaio/FULLPATHNAME/Monthly_Original.gdb"
# Create a list of raster files
rasterlist = arcpy.ListRasters()
# Loop through the actual list of rasters generated by ListRasters()
for k in rasterlist:
    print k # Optional visual check of file name
    # Divide each existing raster by 100
    OutRaster = Raster(k) / 100
    # Specify the output name variable - modify as needed
    # This example adds prefix 'p_' to original file name with extension included
    OutRaster_Name = 'p_' + k
    # Save the output raster
    OutRaster.save(OutRaster_Name)