1

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_RAIN_2001.tiff)

enter image description here

Script:

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 = arcpy.ListRasters("TIF")
 print rasterList
 for Rasters in rasterList:
 rasMean = CellStatistics(Rasters,"MEAN", "DATA")
 file_name_only = os.path.splitext(Rasters)[0]
 tifname = file_name_only[-4:]
 rasMean.save(os.path.join(outraster,'Mean_{0}.tif'.format(tifname)))
 print rasMean

Printing statement: its printing only folder name,

D:\Test
[]
D:\Test2001円A
[]
D:\Test2002円A
[]
D:\Test2003円A
[]
D:\Test2004円A
[]
D:\Test2005円A
[]
D:\Test2006円A
[]
D:\Test2007円A
[]
D:\Test2008円A
[]
D:\Test2009円A
[]
asked Mar 25, 2017 at 9:44
10
  • As you will see in the code snippet recommendations it is best to remove any try/except statements while testing. Commented Mar 25, 2017 at 10:02
  • yes, i removed try/except statements and rerun the code, now its producing error Commented Mar 25, 2017 at 10:11
  • 1
    It would be helpful if you could include the full error message, and the output from any print statements from running this code. Commented Mar 25, 2017 at 10:19
  • i did little change the code, now its not showing any error but generating only one output name of "mean" in test folder. even output of raster not goes to subfolder to save it in each folder. Commented Mar 25, 2017 at 10:24
  • 2
    Where's the output of those two print statements? I would print rasterList too. Commented Mar 25, 2017 at 11:47

1 Answer 1

1

I can see several things wrong with this script. Firstly you keep overwriting your output with this line:

rasMean.save(os.path.join(outraster,"mean.tif"))

The reasons for that is outraster never changes as it comes from the environment settings which you set to d:\test. outraster needs to change with each level that you are looping over (a concatenation of dirpath and dirnames).

I think it would be a good idea if you experiment with the walk() function and understand what it is generating as this is the source of your problem.

Secondly the tool CellStatistics takes as input a list of rasters, but your current code is building the list and calling the tool within your "for file in filenames" loop which is horribly inefficient. You need to build the list then feed that into the tool so all you need to do is remove the indentation from the 3 lines after the append call so they fall outside the loop.

answered Mar 26, 2017 at 20:34
2
  • thank you. can i get some idea how to reconstruct the code . Commented Mar 29, 2017 at 6:16
  • please help me to reconstruct the code Commented Mar 29, 2017 at 12:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.