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
Solved! Go to Solution.
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
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)
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:]
Hope this explains the process.
Kind regards, Xander
xander_bakker, Thank you very much for brief clarification.
You're welcome