Select to view content in your preferred language

Struggling with seemingly simple RasterList and sum script: please HELP!

1028
4
04-09-2011 05:36 PM
AlisterFenix
Deactivated User
Hello,

I???ve gotten quite rusty on my Python over the years and have  now been stuck on a seemingly easy little script for far too long, and have officially reached the reaching out point.  Hopefully some python guru???s out there will be able to see what I???m doing wrong and point me in the right direction. 
Quick background: I???ve created a complex model whose output is a number of grids (will be in the order of over a thousand).  I now simply need to sum all of the grids together as my final output before I can complete the analysis.  Obviously, when you have over a thousand grids the option of manually adding names within raster calculator (or anywhere else for that matter) becomes obsolete.  So, here???s the script for listing the output rasters and summing them:

-----------------------------------------------------------

#Script to list each output raster and sum them
import arcgisscripting

#create the geoprocessor object
gp = arcgisscripting.create(9.3)

#check out any necessary licenses
gp.CheckOutExtension("Spatial")

#set up the local variables...
gp.workspace = "C:\\test\\results"
outputFolder = "C:\\test\\final"
try:
    rasterList = gp.ListRasters("","GRID")
    print rasterList
    raster = rasterList.next()
    print raster
    somaExp = " "
    while raster:
        soma = somaExp + raster + "+"
        print somaExp
        raster = rasterList.next()
        gpsa = outputFolder + "\\"
        gp.SingleOutputMapAlgebra_sa(soma[:-1], gpsa)

#In the rare case that this script does not work...
except:
    print "Nice try, but it didn't work."  

---------------------------------------------------------------

The ListRasters is working (although it is putting a ???u??? in front of the names), but I can???t get any other clues about why it keeps crashing.
I???m running ArcGIS 9.3 and python 2.5.1 on Windows XP.

Thanks in advance for any help!!!

Analisa
Tags (2)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus
did you rule out cell statistics with the sum option?
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?id=6283&pid=6281&topicname=Cell_Statistics
You may be running into problems if the grid list is too long, in which case, you may need to do it incrementally
0 Kudos
AlisterFenix
Deactivated User
Hello Dan,

I have tried the Cell Statistics with the same result.  One thing that's interesting to me is that I can't get the SomaExp line to print in order to determine if that line is working and I'm uncertain how to figure out just where exactly this code is breaking. Also, I am testing this code on a small sub-sample (only 8 grids that are under 2mb in total size).  Please see revised code (thanks to guidance from Rick Momsen):

#Script to list each output raster and sum them
import arcgisscripting, time

#set up
gp = arcgisscripting.create(9.3)
gp.CheckOutExtension("Spatial")
gp.workspace = "C:\\test\\results"
outputFolder = "C:\\test\\final"

try:
    rasterList = gp.ListRasters("","GRID")
    somaExp = " "
    for raster in rasterList:
        somaExp = somaExp + raster + ";"
        #print somaExp
        gpsa = outputFolder + "\\"
        gp.CellStatistics_sa(somaExp[:-1], gpsa, "SUM")

except:
    print "Nice try, but it didn't work."
    time.sleep(15)  #give's you time to see the error

Finally, I found reference in the forums to version issues of python and arcGIS, and wonder if I may be having issues importing arcscripting.  I'm uncertain how to check if the import is working or not.

Thanks again for any advice or thoughts.  ALL are appreciated and helping me to get caught up on Python.
0 Kudos
DanPatterson_Retired
MVP Emeritus
haven't used it in a while but is there a need to build the soma line?  I got the impression from the help file for 9.3 that you just needed to pass on a list of grids which you already have in your rasterList variable...could be wrong, but it would only seem logical unless the ListRasters doesn't provide a list formatted properly for use in subsequent SA tools.
0 Kudos
ChrisSnyder
Honored Contributor
This code should work:

rasterList = gp.ListRasters("","GRID")
somaExp = "" #note that your code had somaExp = " " (and extra space)
for raster in rasterList:
   somaExp = somaExp + raster + " + " #Note the SOMA language requires single spaces around the operations, so your "+" would be invalid 
gpsa = outputFolder + "\\output" #Note in your code you were only naming the folder
gp.SingleOutputMapAlgebra_sa(soma[:-3], gpsa) #Note the [:-3] index
0 Kudos