Search code examples
javascriptreducegoogle-earth-engine

Summarize Pixel Value in Every Image of an Image Collection in Google Earth Engine


I am a newbie in GEE. I want to calculate a sum / mean of a pixel value (rainfall data from CHIRPS satellite) in one region of every image data in an ImageCollection. Basically i want to calculate the mean daily rainfall in my study area.

Here is my code so far:

//define study area
var aoi = ee.FeatureCollection(juana)

//load & filtering ImageCollection
var precipitation = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY')
.filterDate('2021-01-01', '2021-12-31')
.select('precipitation');

//clipped ImageCollection to Study Area
var clipped = precipitation.map(function(precipitation){
return precipitation.clip(aoi);
});

//plot in map
Map.addLayer(clipped)

Here is the result in map: enter image description here

Every pixel has a daily rainfall data like this: enter image description here

But what i need is i want to sum/averaging those pixel in the study area and make a daily graphic. What should i do?


Solution

  • You use ee.Image().reduceRegion() to calculate aggregate statistics for an area of an image. In your case, you want a time-series of the stats, so you want to do this for every image in you collection. To do that, you use ee.ImageCollection.map(). You have relevant sections in the docs here and here. Something like this:

    var dailyStats = precipitation.map(function (image) {
      var reduced = image.reduceRegion({
        reducer: ee.Reducer.sum()
          .combine(ee.Reducer.mean(), null, true), 
        geometry: aoi.geometry(), 
        scale: 5000, 
        maxPixels: 1e13
      })
      return ee.Feature(null, reduced)
        .set('system:time_start', image.get('system:time_start'))
    })
    
    var meanChart = ui.Chart.feature.byFeature({
      features: dailyStats,
      xProperty: 'system:time_start', 
      yProperties: 'precipitation_mean'
    })
    
    var sumChart = ui.Chart.feature.byFeature({
      features: dailyStats,
      xProperty: 'system:time_start', 
      yProperties: 'precipitation_sum'
    })
    print('mean', meanChart)
    print('sum', sumChart)
    

    https://code.earthengine.google.com/e6e4af4147004c6cbd9fe0f20dd7c9fe