I would like to create a function where the cumulative precipitation (sum) of every n days is added to the ImageCollection. I'm using CHIRPS. Essentially, one should be able to do this for multiple days, so resulting in an imageCollection with for example 3 bands of the precipitation sum every 5, 10 and 20 days.
My approach right now is obviously not working, but I hope it displays where I'm going for.
var geometry =
/* color: #d63000 */
/* displayProperties: [
{
"type": "rectangle"
}
] */
ee.Geometry.Polygon(
[[[-131.06905651111887, 50.47632999698667],
[-131.06905651111887, 23.187753129057395],
[-54.340540886118866, 23.187753129057395],
[-54.340540886118866, 50.47632999698667]]], null, false);
// Define region of interest
var adm_data = ee.FeatureCollection("FAO/GAUL/2015/level0")
var country = 'United States of America'
var roi = adm_data.filter(ee.Filter.inList('ADM0_NAME', [country]))
var roi = geometry
Map.addLayer(roi, {}, 'roi', false)
var roi = roi // or roi_country when anaylsing one country
/// Set time
var startdate = '2020-01-01' // insert
var enddate = '2021-01-01' // insert
// Products
var nDays = function(imageCollection, n){
return function (image){
currentDate = ee.Date(image.get('system:time_start'));
nextDate = currentDate.advance(-n, 'day')
var Precipitation = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY")
.filterDate(startdate, enddate)
.filterBounds(roi)
.sum()
return image.addBands('every_'+n+'days')
}
}
// Add a band with the precipitation sum every n days. How?? Any help much appreciated!
var p = nDays(ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY"), 5)
print(p)
Link to the code: https://code.earthengine.google.com/51a533f812b337f82d5c1bc6f9b21fb6