AnsweredAssumed Answered

Loop through multiple folder and composite-band of raster of Same folder

Question asked by HANIMANI1 on Sep 7, 2018
Latest reply on Sep 8, 2018 by HANIMANI1

Hi 

I am trying to make a loop function.  loop function picks up the rasters file from every subfolder, composite(layer stack#) the raster file and keeps it in that subfolders. I am getting an error. please help anyone. Under ppp folder have two subfolder P1 and P2 both folders contain the raster file. Thanks in advance.

import arcpy ,os ,sys
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
env.workspace = "G:/ANFIS_DATA_2/PRECEPITATION/aug/POINT/ppp"

ws = r"G:/ANFIS_DATA_2/PRECEPITATION/aug/POINT/ppp"
walk = arcpy.da.Walk(ws, topdown=True, datatype="RasterDataset")
for dirpath, dirnames, filenames in walk:
        print "Processing folder:", dirpath

        rasters=[]
        for filename in filenames:
            raster= os.path.join(dirpath,filename)
            print raster
            if filename.endswith(".tif"):
                    rasters.append(raster)
print "-rasters found:",len(rasters)

if len(rasters) != 0:
            print " layerstack"
            file_name_only = os.path.splitext(rasters[0]) [0]
            tifname = file_name_only[-9:]
            print tifname
            ras_name = os.path.join(dirpath,'L_{0}.tif'.format(tifname))
            layr =arcpy.CompositeBands_management(rasters,ras_name)


else :
    print" -skipping folder..."

print "Finished..."

Outcomes