"Failed to open raster dataset" in zonal statistics

Discussion created by rpatarasuk on Jul 11, 2012
Latest reply on Jul 13, 2012 by rpatarasuk

I am trying to use zonal statistics to calculate the average amount of rainfall for each county. Here is my code:

# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *

# Check out any necessary licenses

# Input data source
arcpy.env.workspace = "S:/Work/Risa/Trials_Errors/InputFiles02"
arcpy.env.overwriteOutput = True

ZoneShapefile = "S:/Work/Risa/USA Boundary_AlbersNAD83/PINEMAP_Counties_USCensus.shp"

# Output File
OutputFolder = "S:/Work/Risa/Trials_Errors/ZonalStats"

# Loop through a list of files in the workspace
RasterFiles = arcpy.ListRasters()

# Local variables:
for filename in RasterFiles:
    print("Processing: " + filename)
    inRaster = arcpy.env.workspace + "/" + filename
    fileroot = filename
    outRaster = OutputFolder + "/" + "Avg_" + fileroot + ".tif"
    # Process: Zonal Statistics
    arcpy.gp.ZonalStatistics_sa(ZoneShapefile, "FID", inRaster, outRaster, "MEAN", "DATA")

print " "
print ":o) End Processing :)"

The problem is that when I run it, it creates this error:

Processing: ppt_1970_01.tif

Traceback (most recent call last):
File "S:/Work/Risa/Python codes/zonalStats_test.py", line 36, in <module>
arcpy.gp.ZonalStatistics_sa(ZoneShapefile, "FID", inRaster, outRaster, "MEAN", "DATA")
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 474, in <lambda>
return lambda *args: val(*gp_fixargs(args))
ExecuteError: ERROR 999999: Error executing function.
Failed to open raster dataset
Failed to execute (ZonalStatistics).

What is weird is that every time when I run the program, it creates a raster file in the input folder, which I didn't specify. e.g. "t_t126". If I hit run again, will create another file and so on. These files don't give me the average value by the county either. It gave me a weird number. If the program runs properly, it should create "Avg_ppt_1970_01.tif" in the output folder.

However, if I run one file at a time (exported from Model Builder), it works fine. Here's the code that works.

# Import arcpy module
import arcpy

# Check out any necessary licenses

# Local variables:
PINEMAP_Counties_USCensus = "S:/Work/Risa/USA Boundary_AlbersNAD83/PINEMAP_Counties_USCensus.shp"
arcpy.env.overwriteOutput = True
ppt_1970_01_tif = "S:/Work/Risa/Trials_Errors/InputFiles02/ppt_1970_01.tif"
ZonalSt_shp2 = "S:/Work/Risa/Trials_Errors/ZonalStats/ZonalSt_shp.tif"

# Process: Zonal Statistics
arcpy.gp.ZonalStatistics_sa(PINEMAP_Counties_USCensus, "FID", ppt_1970_01_tif, ZonalSt_shp2, "MEAN", "DATA")

print "Done!"

So, I don't get it why it says "Failed to open raster dataset".

Here are the sample files (see the attachments)

Any thoughts and help would be pretty much appreciated.

Thank you.