python进程的for循环出现问题

2024-04-26 00:55:57 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

Tags: ofthetoimportas光栅npfilename

热门问题