python进程的for循环问题

时间:2014-05-20 17:53:28

标签: python for-loop numpy gis arcpy

我最近想出了如何对光栅文件进行一些NumPy计算,但是我现在徒劳地尝试迭代运行我提出的208个栅格的过程,所有这些都在1个文件夹中。

我认为这是一个相当简单的任务,但在咨询了一些stackexchange线程和教程之后,我仍然没有弄清楚它。 (我对python很新。)

我已经包含了以下代码。我得到的错误:

invalid syntax on "def cumulativecalculation()"

提前感谢您的帮助!

import os 
import sys 
import arcpy 
import numpy as np 
import glob


arcpy.env.overwriteOutput=True

def cumulativecalculation()

    #Set geoprocessing variables
    inRaster = filename
    des = arcpy.Describe(inRaster)
    sr = des.SpatialReference
    ext = des.Extent
    ll = arcpy.Point(ext.XMin,ext.YMin)

    #Convert GeoTIFF to numpy array
    a = arcpy.RasterToNumPyArray(inRaster)

    #Flatten for calculations
    a.flatten()

    #Find unique values, and record their indices to a separate object
    a_unq, a_inv = np.unique(a, return_inverse=True)

    #Count occurences of array indices
    a_cnt = np.bincount(a_inv)

    #Cumulatively sum the unique values multiplied by the number of
    #occurences, arrange sums as initial array
    b = np.cumsum(a_unq * a_cnt)[a_inv]

    #Divide all values by 10 (reverses earlier multiplication done to
    #facilitate accurate translation of ASCII scientific notation
    #values < 1 to array)
    b /= 10

    #Rescale values between 1 and 100
    maxval = np.amax(b)
    b /= maxval
    b *= 100

    #Restore flattened array to shape of initial array
    c = b.reshape(a.shape)

    #Convert the array back to raster format
    outRaster = arcpy.NumPyArrayToRaster(c,ll,des.meanCellWidth,des.meanCellHeight)

    #Set output projection to match input
    arcpy.DefineProjection_management(outRaster, sr)

    #Setting the OutName
    OutName = "filename" + "_cumulative" + ".tif"

    #Save the raster as a TIFF
    outRaster.save("E:\\NSF Project\\Salamander_Data\\New_Cumulative_Rasters\\OutName")

src = "E:\\NSF Project\\Salamander_Data\\NoDataToZero\\HadleyGCM\\*.tif"

for filename in glob.glob(src):
    cumulativecalculation(filename)

sys.exit()

1 个答案:

答案 0 :(得分:3)

您需要在()之后添加冒号。

def cumulativecalculation():