Loop through multiple folder and Calculate mean of raster of Same folder

3646
23
Jump to solution
03-24-2017 12:18 PM
ShouvikJha
Occasional Contributor III

Hi All, 

I am writing a script to loop through multiple folders and calculate mean of the raster of each folder's data sets and save it in same folder. Below script i am working, but its not producing the mean rasters for each year folder, after running the script its generating only name of "mean" raster in main folder, and overlapping it .

Below images showing the name of the raster in one folder, another folder the raster name is same, only year value is changed in different year.
here how to take the year value in name of mean raster (E.g. for 2001 folder, output mean raster name would be Mean_Temp_2001.tif, E.g. for 2002 folder, output mean raster name would be Mean_Temp_2002.tif)

Below i have attached data sets also (Test_data folder)v which i am working on. 

Thanks. 

import arcpy, os
from arcpy import env
arcpy.CheckOutExtension("Spatial") 
arcpy.env.overwriteOutput = True
env.workspace = r"D:\Test"
outraster = env.workspace


walk = arcpy.da.Walk(env.workspace, topdown=True, datatype="RasterDataset")
for dirpath, dirnames, filenames in walk:
    print dirpath
    rasterList = []
    for file in filenames:
        raster = os.path.join(dirpath, file)
        rasterList.append(raster)
        rasMean = arcpy.sa.CellStatistics(rasterList,"MEAN")
        rasMean.save(os.path.join(outraster,"mean.tif"))
        print rasMean‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
23 Replies
ShouvikJha
Occasional Contributor III

xander_bakker‌, I am very grateful to get such offer from you. 

Can you kindly explain the step involved line no 16 to 21.

Thanks

Shouvik Jha  

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi shouvik jha , sure, no problem.

Let's first look at lines 16 - 18:

            if filename.upper().endswith('.TIF'):
                if filename.upper().startswith('MEAN') == False:
                    rasters.append(raster)

  • The first line will check if the name of the raster ends with ".TIF" to avoid including other rasters from a different format in the list of raster that will be used to calculate the mean. 
  • The second line is used to check is the current raster name starts with 'MEAN'. Since you include the line "arcpy.env.overwriteOutput = True" in your code, I assumed that you may want to run the calculation again. A second run would include your previous MEAN raster in case you don't exclude it with this second line.
  • The third line will simply add the raster to the list that will be used for the MEAN calculation.

When we look at lines 21-24:

        if len(rasters) != 0:
            print " - calculate mean..."
            file_name_only = os.path.splitext(rasters[0])[0]
            tifname = file_name_only[-4:]
  • The first line will check the number os raster found in the current folder. The first (outside folder) will have no rasters and should ne be processed.
  • The second line print a simple message
  • The third line will take the first raster from the list "rasters[0]" and use that raster path name to split the path name from the extension (it will eliminate the extension from the path name
  • The fourth line is the same you had to extract the year from the name of the raster (list 4 characters)

Hope this explains the process.

Kind regards, Xander

ShouvikJha
Occasional Contributor III

xander_bakker‌, Thank you very much for brief clarification.  

0 Kudos
XanderBakker
Esri Esteemed Contributor

You're welcome

0 Kudos