总结栅格组有共同的文件名

时间:2017-08-29 12:02:23

标签: python python-2.7

我有一个包含栅格组的文件夹:

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

这些光栅可以持续几个月到DEC。我想在这个文件夹中总结每一天。我试图在(z)处拆分文件名,然后列出所有具有相似第二部分的栅格,然后使用单元格统计进行求和。我使用fnmatch来做这个,但它返回空列表。任何建议!

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")

2 个答案:

答案 0 :(得分:0)

您可以使用defaultdict模块中的collections来计算每天的出现次数。

from collections import defaultdict

day_count = defaultdict(int)

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

print(day_count)

答案 1 :(得分:0)

@James ..我找到了一种灵感来自你答案的方法。

 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")