I am calculating the mean precipitation value over a given area from CHIRPS precipitation data.
My goal is to get a list containing a date and precipitation value for every Image of the ImageCollection
.
Using .getInfo()
on the reduced ImageCollection
I can get a dict representing the ImageCollection
and from there select the values on the client side.
import ee
from datetime import datetime
ee.Initialize()
dataset = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY').filter(ee.Filter.date('2016-01-01','2016-12-31'))
area = ee.Geometry.Polygon([[[29.045341997822412, -2.1339228457039368],
[29.07984593458999, -2.1339228457039368],
[29.07984593458999, -2.0966977113182073],
[29.045341997822412, -2.0966977113182073],
[29.045341997822412, -2.1339228457039368]]])
# Image reduction applied to each image.
def reduce_dataset_region(image):
# Calculate mean of precipitation on defined area.
local_precipitation_image = image.reduceRegion(
reducer=ee.Reducer.mean(),
geometry=area,
scale=20
)
return image.set('mean', local_precipitation_image)
# Apply region reduction to ImageCollection
reduced_dataset = dataset.map(reduce_dataset_region, True)
# Request server-side ImageCollection as dict
reduced_dataset_dict = reduced_dataset.getInfo()
# Create list to hold daily precipitation values.
precipitation_list = []
# Loop through features in image collection dict. One feature represents one image.
for feature in reduced_dataset_dict['features']:
# Get date and turn into datetime
# Get precipitation value and add with datetime to list.
precipitation_list.append((timestamp, feature['properties']['mean']['precipitation']))
print(precipitation_list)
This seems slow and not the right way to go, especially since I want to use data from multiple years.
How can I create a list containing the precipitation value for each Image and send only that, and not the whole ImageCollection
, to the client? What is the better alternative to .getInfo
here?