2014-05-20 75 views
-1

我最近计算出了如何在栅格文件上做一些NumPy计算,但是现在我徒劳地尝试迭代运行我在208个栅格上创建的进程,所有进程都在1个文件夹中。python进程for循环的问题

我认为这是一个非常简单的任务,但在查阅了一些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() 

回答

3

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

def cumulativecalculation(): 
+0

如果OP的问题是由添加颜色或一些地方解决了,这是更好地标志offtopic->简单的打字错误 –

+0

谢谢。对不起,我猜这个问题太简单了。 – Vergentorix