简体   繁体   中英

summation group of rasters have common file name

I have a folder contains groups of raster as:

  • 00z01JAN2010, 01z01JAN2010, 02z01JAN2010, ... 23z01JAN2010
  • 00z02JAN2010, 01z02JAN2010, 02z02JAN2010, ... 23z02JAN2010

These raster are for several months upto DEC. I want to sum each day in this folder. I have tried to split the file name at (z) and then list all the rasters which have a common similar second part and then do the summation using cell statistics. I used fnmatch to do this but it return empty list. Any suggestions!

import os, arcpy, fnmatch
import arcpy.sa
from arcpy import env

arcpy.CheckOutExtension("Spatial")  
arcpy.env.overwriteOutput = True  
finalRasterFold = "E:\Drive_GGGGGG\PhD_AinShams\PhD_Proposal_RainNetwork\Frequency_Files\Con_1_0"
outDayRasterFold = r"D:\PhD_work\Rasters_Day"

arcpy.env.workspace = finalRasterFold 
RainRasterList = arcpy.ListRasters('*')

for h in range(0,24):
    DayRainRaster = []
    j =[]
    for n in RainRasterList:
        fn1, fn2 = n.split('z')

        if fnmatch.filter(n[3:], fn2):
            j.append(n)

        j = arcpy.Raster()
        DayRainRaster.append(j)
    arcpy.gp.CellStatistics(DayRainRaster,outDayRasterFold + r"\DayRas" + str(h),"SUM","DATA")

You can use the defaultdict from the collections module to keep count of the number of occurrences for each day.

from collections import defaultdict

day_count = defaultdict(int)

for n in RainRasterList:
    fn1, fn2 = n.split('z')
    day_count[fn2] += 1

print(day_count)

@James.. I find a way to do it inspired by your answer.

 newlist = []
for n in RainRasterList:
    fn1, fn2 = n.split('z')
    temp = (fn2, n)
    newlist.append (s)
for k, v in newlist:
    day_count[k].append(v)
day_count.items()

rlist =[]   
for k, v in day_count.iteritems():
    rlist.append(v)

for i in range(len(rlist)):
    arcpy.gp.CellStatistics(rlist[i], outDayRasterFold + r"\D" + str(rlist[i][1]),"SUM","DATA")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM