AnsweredAssumed Answered

Aggregating hourly rasters into daily rasters

Question asked by adiazjr on Oct 5, 2018
Latest reply on Oct 6, 2018 by Dan_Patterson

I have hundreds of three hourly soil moisture rasters that we need to aggregate into daily rasters. I am using a python script running in PyCharm using the ArcGIS Pro Python interpreters. My script runs the first day aggregation in under 10 seconds and after that everthing starts to slow down even adding each of the three hourly raster takes up to 15 seconds, whereas the first total day process takes less than a single raster process. You will notice I am using a list comprehension to list my rasters because using the ListRasters option caused all the temp files to be created in the same location as the soil moisture rasters. My script is as follows:

 

import arcpy
from arcpy.sa import *
from datetime import datetime
import os

 

arcpy.CheckOutExtension("Spatial")

 

location = r"E:\New Folder\\"
arcpy.env.scratchWorkspace = r"E:\SMAP\Scratch\\"
arcpy.env.overwriteOutput = True

 

rasters = [f for f in os.listdir(location) if f.endswith(".tif")]

 

current_month = datetime.strptime("20150331", "%Y%m%d")

x = 0
total = 0

 

for r in rasters:

    print(r)
    month = r[0:8]
    month = datetime.strptime(month, "%Y%m%d")

 

    current_raster = Raster(location + r)
    print("RASTER MADE")

 

    if current_month.day == month.day:
        
        smap = Con(current_raster > -9999, current_raster)
        print("CON MADE")
        total = total + smap
        print("CON ADDED")
        x += 1

 

    else:
        print(x)
        arcpy.env.mask = location + "SMERGE.shp"

 

        total = total / x
        print("TOTAL OBTAINED")
        x = 1
        print("S" + str(current_month.year) + str(current_month.strftime("%m")))
        total.save(r"E:\SMAP\outdaily\\" + "S4" + str(current_month.year) + str(current_month.strftime("%m")) +                          str(current_month.strftime("%d")) + ".tif")
        print("TOTAL SAVED")
        total = 0
        total = smap

 


    current_month = month
    y += 1

 

print(x)
arcpy.env.mask = location + "SMERGE.shp"
total = total / x
print("S" + str(current_month.year) + str(current_month.strftime("%m")))
total.save(r"E:\SMAP\outdaily\\" + "S4" + str(current_month.year) + str(current_month.strftime("%m")) + str(current_month.strftime("%d")) + ".tif")

 

arcpy.CheckInExtension("Spatial")

 

Any Ideas will be appreciated.

Outcomes