Search code examples
pythonfor-loopnumpygisarcpy

Trouble with for loop for python process


I recently figured out how to do some NumPy calculations on raster files, but am now trying in vain to iteratively run the process I came up with on 208 rasters, all of which are in 1 folder.

I think this is a pretty straightforward task, but after consulting a number of stackexchange threads and tutorials, I'm still not figuring it out. (I'm very new to python.)

I've included the code below. The error I'm getting:

invalid syntax on "def cumulativecalculation()"

Thanks in advance for the help!

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()

Solution

  • You need to add a colon after the ().

    def cumulativecalculation():