rafaelr

ArcPy Script always using same inputparams when run multiple times?

Discussion created by rafaelr on Aug 2, 2011
Latest reply on Aug 3, 2011 by rafaelr
I made this arcpy script to calculate area stats by class for a raster.
now, i´m by no means a programmer and i know the code i wrote shows it (and there´s probably a much simpler way to achieve what i want...) :)
but still, if i run this outside of arcmap without the parameter stuff it works just fine.
i added the code to a script tool and there it also works fine the first time. but when i run it again, it just uses the input raster from the first run and i always get the same results no matter what i choose for the input.


import os, arcpy, numpy
import numpy.ma as ma
from arcpy import env
from arcpy.sa import *

# Input Raster Folder
input = arcpy.GetParameterAsText(0)
dataPath = os.path.split(input)[0]#r'x:\Immissionen\Szenario 2005\NOx\'

# Output Folder
output = arcpy.GetParameterAsText(3) #r'x:\Immissionen\Szenario 2005\NOx\out'
outPath = os.path.split(output)[0] #r'x:\Immissionen\Szenario 2005\NOx\out'
outFile = os.path.split(output)[1]

# Set environment settings
arcpy.env.overwriteOutput = 1
arcpy.CheckOutExtension('Spatial')
arcpy.env.scratchWorkspace = outPath
arcpy.env.workspace = dataPath


# Raster Variables
inRaster = os.path.split(input)[1] #"cut_jmw_no2_kor.tif"
NoDataValue = float(arcpy.GetParameterAsText(1)) #-3.40282346639e+038
reclassField = "Value"

# Get Raster CellSize
RasterCellSize = str(arcpy.GetRasterProperties_management(inRaster, "CELLSIZEX"))

# Get Class Breaks as String
inClassbreaks = arcpy.GetParameterAsText(2) #"30,40"

# Create NumPy Array Raster, mask with NoData Value 
myArray = arcpy.RasterToNumPyArray(inRaster)
myArray = ma.masked_equal(myArray, NoDataValue)
myArray = myArray.compressed()

# Get Min, Max Values of Raster and add/subtract 1
myArrayMin = myArray.min()- 1
myArrayMax = myArray.max()+ 1
print myArrayMin, myArrayMax
print "myArray" + str(myArray)



# Split Classbreaks and convert them to float, add min and max Values from Raster Array and sort them
Classbreaks = map(float, inClassbreaks.split(","))
print Classbreaks
Classbreaks.append(myArrayMin)
Classbreaks.append(myArrayMax)
Classbreaks.sort()
print Classbreaks

# Build Classes for Remapping, using min, Classbreaks and max Values
i = 0
k = 0
Classes = []

for CB in Classbreaks[:-1]:
    addClass = [Classbreaks[k], Classbreaks[k+1], i]
    k = k+1
    i = i+1
    Classes.append(addClass)
    print "classes" + str(Classes)

  
remap = RemapValue(Classes) 
  

# Execute Reclassify
outReclassify = Reclassify(inRaster, reclassField, remap, "NODATA")


# Convert Raster to Numpy Array for Stats Calculations
statArray = arcpy.RasterToNumPyArray(outReclassify)
statArray = ma.masked_equal(statArray, 255)
print "statArray" + str(statArray)
statArray = statArray.compressed()
print "comp"+ str(statArray)

# flatten Array so bincount will work 
statArray = statArray.flatten()

print "unique" + str(numpy.unique(statArray))
numCells = numpy.bincount(statArray)
print numpy.bincount(statArray)
nameClass = numpy.unique(statArray)

# Specify Cellsize in m
cellSize = float(RasterCellSize)
#print cellSize
cellSizeKm = float(cellSize / 1000)
print cellSizeKm
cellAreaSqkm = cellSizeKm ** 2
print cellAreaSqkm

# Write Stats to CSV file
stats = open(outPath + "/" + outFile, "a")

stats.write("Statistik:, " + str(dataPath) + str(inRaster) + "\n")
stats.write("Size (Number of Cells):, " + str(statArray.size) + "\n")
stats.write("Minimum:, " + str(myArray.min()) + "\n")
stats.write("Maximum:, " + str(myArray.max()) + "\n")
stats.write("Mean:, " + str(myArray.mean()) + "\n")
stats.write("Class,NumCells,Area[sqkm]," + "\n")

j=0
for area in numCells:
    sumArea = area * cellAreaSqkm
    print str(nameClass[j]) + " Anz:" + str(area) + " flaeche: " + str(sumArea)
    #stats.write("Class: " + str(nameClass[j]) + " NumberOfCells: " + str(area) + " Area: " + str(sumArea) + "\n")
    stats.write(str(nameClass[j]) + "," + str(area) + "," + str(sumArea) + "\n")
    j = j+1
    


stats.close


Outcomes