Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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. Thank you in advance!

share|improve this question
2  
What is the error? –  Nathan W Apr 28 '13 at 5:05
    
Perhaps the error is really in the last line - OutTaster should be OutRaster? –  BradHards Apr 28 '13 at 5:14
    
sorry, it was OutRaster, but the error was "invalid syntax (line 6) " –  Irina Apr 28 '13 at 5:45
add comment

2 Answers

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)
share|improve this answer
    
Thank you for your help. Could you suggest the best way to define 'p' variable? –  Irina Apr 28 '13 at 5:50
1  
@Irina have a read of help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//… to see what ListRasters takes. –  Nathan W Apr 28 '13 at 6:41
    
The 'p' variable would have to be defined as a wild_card or raster_type when used in ListRasters; e.g. p = "TIFF" in a line previous to arcpy.ListRasters("*", p). The code sample in the online link @Nathan W indicated above will show you the possible parameters. –  ccn Apr 29 '13 at 5:56
add comment

I always put a "r" in front of any path so it is interpreted correctly. So turn

arcpy.workspace="C:/Users/Vaio/.../Monthly_Original.gdb"

into

arcpy.workspace = r"C:/Users/Vaio/.../Monthly_Original.gdb"

share|improve this answer
1  
I believe the OP is correct in their paths. There are three correct ways to express paths in Python: 1) "C:/temp" 2) "C:\\temp" or 3) r"C:\temp". –  Aaron Apr 29 '13 at 13:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.