elncl3

Stand alone script fails using spatial analyst tools

Discussion created by elncl3 on Mar 5, 2012
Latest reply on Jun 21, 2013 by mjgleaso
Hi,

I'm trying to create a stand alone script to automate watershed analysis. So far it works up until it gets to the spatial analyst
tools but then fails. Can anyone show me where I have gone wrong please? Here is my code:

import arcpy
import os
import time
import sys
from arcpy import env
from arcpy.sa import *

env.workspace = "H:/PhD/PythonLessons/Scripts/testIt"
catchmentName = "CatchmentName"
catchmentRaster = catchmentName + ".tif"
catchmentRasterProj = catchmentName + "proj.tif"
catchment100 = catchmentName + "100.tif"
catchment100Fill = catchmentName + "100Fill.tif"
catchment100Flow = catchmentName + "100Flow.tif"
catchment100Acc = catchmentName + "100Acc.tif"
catchment100PP = catchmentName + "100PP.tif"
catchment100WS = catchmentName + "100WS.tif"
folder = "H:/PhD/PythonLessons/Scripts/testIt"
folderAdd = "H:/PhD/PythonLessons/Scripts/testIt/"
outlet = "outlet.shp"
projection = "C:/Program Files (x86)/ArcGIS/Desktop10.0/Coordinate Systems/Projected Coordinate Systems/National Grids/Europe/British National Grid.prj"
# identify guage
#create pourpoint shapefile
# identify catchment outline
# select appropriate testIt

# have a folder with all of the little ascii testIt for a catchment. This folder will be called *catchment name*
print "checking extensions"

try:
 arcpy.CheckExtension("Spatial") == "Available"
 arcpy.CheckOutExtension("Spatial")
 print "done"
except:
 print "piddle"
 

print "converting the ascii files to raster files"

asciiList = os.listdir(folder)    

for x in range(len(asciiList)-1, -1, -1):
 if not asciiList[x].endswith(".asc"):
   asciiList.pop(x)


for file in asciiList:  
 print folderAdd + file
 print folderAdd + file[:-4]
 
 try:
  arcpy.ASCIIToRaster_conversion(folderAdd+ file, folderAdd+ file[:-4] + ".tif", "INTEGER")
 except:
  print "Processing " + file + "FAILED"
  
 time.sleep(3)
 
tifList = os.listdir(folder)

print tifList

for x in range(len(tifList) -1, -1, -1):
    if not tifList[x].endswith(".tif"):
        tifList.pop(x)

print tifList

tifString = ""
for tif in tifList:
 tifString += tif + ";"
 
tifString = tifString[:-1]

print tifString

print "mosaic to new raster"

try:
 arcpy.MosaicToNewRaster_management(tifString, folderAdd, catchmentRaster, "#", "16_BIT_SIGNED", "#", "1" , "MEAN")
except:
 print "Error with mosaic to new raster"
 
time.sleep(3)


print "Giving it the right coordinates"
try:
 arcpy.DefineProjection_management(folderAdd + catchmentRaster, projection)
 arcpy.ProjectRaster_management(folderAdd + catchmentRaster, folderAdd + catchmentRasterProj, projection, "BILINEAR", "#", "#","#",projection)
except: 
 print "Error giving it the right coordinates"

time.sleep(3)
 
print "Changing resolution"

try:
 outAggreg = Aggregate(catchmentRasterProj, 10, "MEDIAN", "TRUNCATE", "DATA")
 outAggreg.save(folderAdd + catchment100)
except:
 print "Error changing resolution"
 
time.sleep(3)
 
print "Filling sinks"

try:
 outFill = Fill(catchment100)
 outFill.save(folderAdd + catchment100Fill)
except:
 print "error filling sinks"
 
print "Calculating fow direction"

time.sleep(3)

try:
 outFlowDirection = FlowDirection(catchment100Fill, "NORMAL")
 outFlowDirection.save(folderAdd + catchment100Flow)
except:
 print "Error calculating flow direction"
 
 
print "Calculating flow accumulation"

time.sleep(3)

try:
 outFlowAccumulation = FlowAccumulation(catchment100Flow)
 outFlowAccumulation.save(folderAdd + catchment100Acc)
except:
 print "Error calculating flow accumulation"

print "Creating pour point snap"

time.sleep(3)

try:
 outSnapPour = SnapPourPoint(outlet, catchment100Acc, 5, "FID")
 outSnapPour.save(folderAdd + catchment100PP)
except:
 print "Error snapping pour point"
 
time.sleep(3)

print "Creating watershed"

try:
 outWatershed = Watershed(catchment100Flow, catchment100PP)
 outWatershed.save(folderAdd + catchment100WS)
except:
 print "Error creating watershed"
 
time.sleep(3)
 
print "Creating files for SHETRAN prepare"

try:
 arcpy.RasterToASCII_conversion(catchment100, folderAdd + catchmentName + "100DEM.txt")
 arcpy.RasterToASCII_conversion(catchment100WS, folderAdd + catchmentName + "100Mask.txt")
except:
 print "error creating files for SHETRAN prepare"
 
time.sleep(3)
 
print "Done!"

raw_input("close to exit")



Thanks very much

Outcomes