Search code examples
javascripttime-seriesreducegoogle-earth-engine

Reduce daily data to monthly using Google Earth Engine


I am looking at precipitation data (both GPM and CHIRPS) for different provinces in Indonesia using Google Earth Engine. GPM is sub-daily (every 30 minutes) and CHIRPS is daily. I am only interested in getting the monthly values. Unlike here and here I am not interested in getting the multi-annual monthly values, but simply the average of each month and make a time series.

Here I found a way to create a list of values containing the mean of each month.

Edit: Thanks to Nicholas Clinton's answer I managed to get it to work:

var fc = ee.FeatureCollection('ft:1J2EbxO3zzCLggEYc57Q4mzItFFaaPCAHqe1CBA4u') // Containing multiple polygons
    .filter(ee.Filter.eq('name', 'bangka')); // Here I select my ROI

Map.addLayer(fc, {}, 'area');
Map.centerObject(fc, 7);

var aggregate_array = fc.aggregate_array('name');
print('Name of area: ', aggregate_array, 'Selected data in FeatureCollection:', fc);

var month_mean = ee.List.sequence(0, 16*12).map(function(n) { // .sequence: number of years from starting year to present
  var start = ee.Date('2002-01-01').advance(n, 'month'); // Starting date
  var end = start.advance(1, 'month'); // Step by each iteration

  return ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY")
        .filterDate(start, end)
        .mean()
        .set('system:time_start', start.millis());
});
print(month_mean); 

var collection = ee.ImageCollection(month_mean);

print(collection);

// Plotting

var area_name = fc.aggregate_array('name').getInfo();
var title = 'CHIRPS [mm/hr] for ' + area_name;

var TimeSeries = ui.Chart.image.seriesByRegion({
    imageCollection: collection,
    regions: fc,
    reducer: ee.Reducer.mean(),
    scale: 5000,
    xProperty: 'system:time_start',
    seriesProperty: 'label'
  }).setChartType('ScatterChart')
    .setOptions({
      title: title,
      vAxis: {title: '[mm/hr]'},
      lineWidth: 1,
      pointSize: 1,
    });

print('TimeSeries of selected area:', TimeSeries);

Solution

  • Have not tested, but should be something like this (or set some other date property):

    return ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY")
          .filterDate(start, end)
          .sum()
          .set('system:time_start', start.millis());