bob_says48

How to loop a function with a different input file ?

Discussion created by bob_says48 on Feb 25, 2013
Latest reply on Feb 25, 2013 by rafaelr
Hi All,

I am very new to python scripting. I have a script in which outputs a statistics when I execute it in Python Idle. My question is how do I incorporate a code in which will allow me to loop the function since I have 71 files that i need to calculate and i  would like to automate the process ?

Hope someone can help me.

here are my codes below:


def calc_hx(dem,h):
    import arcpy, sys, string, os, traceback
    from arcpy import env
    from arcpy.sa import *

    env.overwriteOutput = True

    elevDict = {}
    areaTotal = 0
    elevMin = 9999
    elevMax  = -9999
    h1 = 1-(h*.01)

    try:
        print "Calculating H" + str(h)
        arcpy.CalculateStatistics_management(dem)
        arcpy.BuildRasterAttributeTable_management(dem)
 rows = arcpy.SearchCursor(dem)
 for row in rows:
            elev = row.Value
            #elevStr = str(elev)
            area = row.Count
            elevDict[elev] = area
            areaTotal += area
            if elev < elevMin:
                elevMin = elev
            if elev > elevMax:
                elevMax = elev
 del rows
 del row

 areaCmTotal = 0
 elevHx = 0
 elevKeyList = elevDict.keys()
 elevKeyList.sort()
 for elevKey in elevKeyList:
     areaCmTotal += elevDict[elevKey]
     if areaCmTotal >= int( areaTotal * h1 ) and elevHx == 0: 
                elevHx = elevKey
  #elevRange = elevMax - elevMin
                #meltonsIndex = elevRange / (math.sqrt(areaTotal))
        result = [elevHx,elevMax,elevMin]
        return result

    except arcpy.ExecuteError:
        # Get the tool error messages 
        msgs = arcpy.GetMessages(2) 

        # Return tool error messages for use with a script tool 
        arcpy.AddError(msgs) 

        # Print tool error messages for use in Python/PythonWin 
        print msgs

    except:
        # Get the traceback object
        tb = sys.exc_info()[2]
        tbinfo = traceback.format_tb(tb)[0]

        # Concatenate information together concerning the error into a message string
        pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
        msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"

        # Return python error messages for use in script tool or Python Window
        arcpy.AddError(pymsg)
        arcpy.AddError(msgs)

        # Print Python error messages for use in Python / Python Window
        print pymsg + "\n"
        print msgs
[/CODE]

Outcomes