0

I don't use GEE a lot but I have to use it to extract daily rainfall data from the CHIRPS database. It should be simple but I am stuck. The outcome should be one CSV file with three columns: polygon code, date, mean rainfall (using zonal statistics). I tried the following code. It works for a single image but I am struggling to map it over the image collection.

var imgcol = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY"),
    coc = ee.FeatureCollection("users/asifishti/CoC_subdis")

//The shapefile is available here:
//https://code.earthengine.google.com/?asset=users/asifishti/CoC_subdis


//Time range
var img_col = imgcol.filter(ee.Filter.date ('2005-01-01', '2010-12-30'));
var image = img_col.first()

//deriving the date of the image
var date= image.get('system:index')

//zonal statistics
var mean_rain = function (feature){
  var rain = img.reduceRegion({
    reducer: ee.Reducer.mean(),
    geometry: feature.geometry(),
    scale: 5500, //the CHIRPS dataset has 0.05 degree resolution
    maxPixels: 5e12
  });
  var sdis_rain = feature.set('Mean Rainfall', rain.get('precipitation'))
  .set ("Date", date);
  return sdis_rain;
};

//running the function for all features in the shapefile
var rainfall = coc.map(mean_rain);
print (rainfall);

Export.table.toDrive ({
  collection: rainfall,
  description: 'Mean Rain',
  selectors: ['subdistric', 'Date', 'Mean Rainfall']
  })

1 Answer 1

1

Not sure if this is what you are after:

var totalOutput = ee.ImageCollection(img_col).map(function(image){
  //deriving the date of the image
  var date= image.get('system:index')
  
  var test = coc.map(function(feature){ 
    var rain = image.reduceRegion({
      reducer: ee.Reducer.mean(),
      geometry: feature.geometry(),
      scale: 5500, //the CHIRPS dataset has 0.05 degree resolution
      maxPixels: 5e12
    });
    
   return ee.Feature(feature.set('MeanRain', rain.get('precipitation'))
                            .set ("Date", date))
  })
  
  return ee.FeatureCollection(test);
}).flatten()

print(totalOutput.limit(50))

https://code.earthengine.google.com/c7371aced206da99d9cd6e1a169d943f?asset=users%2Fasifishti%2FCoC_subdis

A quick look gave me a lot of 0 values for rainfall so maybe double check!

2
  • Thanks so much! I'm not worried about the 0 values as it is daily rainfall data.
    – Evan
    Aug 18, 2021 at 21:39
  • No worries. If you believe this helped it might be nice to accept this as the correct answer
    – Jobbo90
    Aug 19, 2021 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.