arcpy.env.workspace = "folder for 1992" listOfRastersByWeek = arcpy.ListRasters() # repeat this inside a loop for all 12 months week1 = arcpy.RasterToNumPyArray(listOfRastersByWeek[0]) week2 = arcpy.RasterToNumPyArray(listOfRastersByWeek[1]) week3 = arcpy.RasterToNumPyArray(listOfRastersByWeek[2]) week4 = arcpy.RasterToNumPyArray(listOfRastersByWeek[3]) month1 = (week1 + week2 + week3 + week4) / 4 raster_month1 = arcpy.NumPyArraryToRaster(month1) raster_month1.save("unique name of the month")
# we are using the python calendar module to help us out with the weeks import calendar # define your current workspace as the arcpy listing function use this information arcpy.env.workspace = r'd:\archive\1992' # let's generate a dictionary with the names for the months month_dict = dict((v,k) for k,v in enumerate(calendar.month_name)) # within the workspace let's get all the rasters # the assumption here is that the naming of the individual rasters actually orders # them by weeks starting at the beginning of the year all the way to the end # (this might or might not be true in your case) listOfRastersByWeek = arcpy.ListRasters() # now this getting a little funky and might be too much # how do you distribute the 52 weeks over the year, or how many weeks are in each month? year = [] weekCounter = 0 for index in range(1,13): # now month contains an arrangement of calendar days in weekly chunks month = calendar.monthcalendar(1992, index) # this array tells us how many days in a week for the specified month daysinweek = [len[1 for day in weeks if day] for weeks in month] # if a weekly chuck has more than 4 days we'll keep it and count it as a full week weeksInMonth = sum(1 for daycount in daysinweek if daycount > 3) # since the assumption of 4 days is somewhat random, let's doublecheck the number # of weeks weekCounter = weekCounter + weeksInMonth if (weekCounter > 52): year.append(52 - (weekCounter - weeksInMonth)) else: year.append(weeksInMonth) # at this point the year array has the number of weeks per month # with the understanding of how many weeks per month let's compute the average startIndex = 0 for index in range(12): # initialize the monthly raster with the first week monthRaster = arcpy.Raster(listOfRastersByWeek[startIndex]) for rasterIndex in range(1,year[index]): if startIndex < 52: # add each week into the monthly raster monthRaster = monthRaster + arcpy.Raster(listOfRastersByWeek[startIndex]) # push the index to the next week startIndex = startIndex + 1 # average the added weekly values monthRaster = monthRaster / year[index] # save the raster with the name of the month monthRaster.save(month_dict[index + 1])