I am trying to download daily precipitation for each district in a country for a given year. I want to import CHIRPS data through GEE, aggregate average daily precipitation to the district-level, and then export it to .csv. Basically, I would like to export a file with three columns, being the district, the date and average precipitation in the district.
So far I am using the following code, but I am not sure it's correct, and the output I get also only has one value per district, not 365.
# Load CHIRPS precipitation data
chirps = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY").filterDate("2021-01-01", "2021-12-31").select('precipitation')
# Load Paraguay districts shapefile
districts_fc = ee.FeatureCollection("users/username/paraguay_districts")
# Define function to calculate daily average precipitation for each district
def daily_average(imageCollection):
# Calculate daily mean precipitation for each district
def reduce_image(district):
mean = imageCollection.reduce(ee.Reducer.mean()) \
.reduceRegion(
reducer=ee.Reducer.mean(),
geometry=district.geometry(),
scale=5500,
maxPixels=1e13
)
return district.set(mean)
# Map the reduce_image() function over the district FeatureCollection
dailyMean = districts_fc.map(reduce_image)
return dailyMean
# Call the daily_average() function to calculate daily mean precipitation for each district
dailyMean = daily_average(chirps)
# Export dailyMean to a CSV file
export_task = ee.batch.Export.table.toDrive(
collection=dailyMean,
description='paraguay_precipitation_2021',
fileFormat='CSV'
)
export_task.start()
I have tried different functions to obtain daily values, but I keep getting error messages (such as AttributeError: 'ImageCollection' object has no attribute 'reduceRegion'), and I cannot figure out where I am wrong. Also, there are many other posts just using one ROI point, and I seem stuck applying this to multiple polygons.
A similar question has been asked here: https://gis.stackexchange.com/questions/408555/daily-rainfall-data-extraction-from-chirps-database-for-polygons-using-google-ea, which I was trying to replicate in Google Colab, but I didn't get the expected result.
The shapefile is available here: https://drive.google.com/drive/folders/1pzoOo12m99P9diC-QAlaiZQB3EIH4AL6?usp=share_link
I'd appreciate any hints and support! Thanks a lot :-)