Hi all, I have multiple raster files (Daily data). All of them have a field "Area".
Now, I want to retrieve the sum of area of each daya (which I obviously can do using statistics).
But, I'm having to manually enter each day's data which is quite tedious for 20 years.
I want to save sum of area filed per day - for the entire year. The file names have got the information about day/month/year. Is there an easy way out I can add a filed that has name of the file, then join all these tables into one and export it and use for further analysis?
You can use a little Python.
raster_folder = "C:/data/rasters"
output_folder = ""
output_name = "AreaSumByDay"
# create the output table
table = arcpy.management.CreateTable(output_folder, output_name)
arcpy.management.AddField(table, "Day", "TEXT")
arcpy.management.AddField(table, "SumArea", "FLOAT")
# start inserting values
with arcpy.da.InsertCursor(table, ["Day", "SumArea"]) as cursor:
# loop over the rasters in the folder
arcpy.env.workspace = raster_folder
for raster in arcpy.ListRasters():
# get the day from the raster name
# for example, if your file names look like this: "bla_bla_date.tif"
# you could do it like this:
name = raster.split(".")[0]
day = name.split("_")[-1]
# get the sum of the area field
raster_obj = arcpy.Raster(raster_folder + "/" + raster)
sum_area = arcpy.RasterToNumPyArray(raster_obj).sum()
# write into the table
cursor.insertRow([day, sum_area])
Dear Johannes Linder, thank you so much for getting back.
I have tested this multiple ways, but seems like the table is generated, but empty.
I get the following system error, also the table is created with the name area_sum_of_day, yet looks empty (like the second screenshot).
My file names are in style: asi-s3125-20030928-v5.4.tif or asi-AMSR2-s3125-20130101-v5.4.tif.
I want to split it around 10th character "asi-s3125-" in the first case and use 7 characters from there for the filename. Can you please provide me what is going wrong, it would save lots of time as there's heaps of data, and I have been spending a lot of time understanding these already.
Any help would be highly appreciated and greatly obliged