1

I am trying to retrieve monthly rainfall averages for points stored in a feature collection. I have checked other posts with similar questions but I cannot seem to figure out how to adapt their solutions to my case. This is my code for the daily precipitation values.

How would I have to modify it to get monthly values?

var fg_points = 
ee.FeatureCollection('ft:17ZWOtlTiq6ehKrp8KhmDhsxwP0lVKHaGYaJqo_Uj');
//generate point and dates
var start = ee.Date('1994-01-01');
var finish = ee.Date('2017-01-01');
var daily = ee.ImageCollection('UCSB-CHG/CHIRPS/DAILY')
.filterDate(start, finish);


var mapfunc = function(feat) {
  var geom = feat.geometry()
  var addProp = function(img, f) {
    var newf = ee.Feature(f)
    var date = img.date().format("MM_dd_YYYY")
    var value = img.reduceRegion(ee.Reducer.first(), geom, 
    30).get('precipitation')
    return ee.Feature(ee.Algorithms.If(value,
                                   newf.set(date, ee.String(value)),
                                   newf.set(date, ee.String('No data'))))
  }
  var newfeat = ee.Feature(daily.iterate(addProp, feat))
  return newfeat
 };

 var newft = fg_points.map(mapfunc);


Export.table.toDrive(newft,
"PrecipitationHTL",
"PrecipitationHTL",
"PrecipitationHTL");
1

It is probably best to first create summed monthly image composites. You can do that as follows: see also here

// Set years and month
var startYear = 1994;
var endYear = 2017;
var years = ee.List.sequence(startYear, endYear);
var months = ee.List.sequence(1,12);
// load the image collection
var Daily = ee.ImageCollection("UCSB-CHG/CHIRPS/DAILY");

// make monthly summed mosaics
// loop over the years and months to get summed monthly images
var byMonth = ee.ImageCollection(ee.FeatureCollection(years.map(function(y){
  var yearCollection = Daily.filter(ee.Filter.calendarRange(y, y, 'year'));
  var byYear = ee.ImageCollection.fromImages(
    months.map(function(m) {
      var summedImage = yearCollection.filter(ee.Filter.calendarRange(m, m, 'month'))
                  .reduce(ee.Reducer.sum()); 
      var date = ee.Date.fromYMD(y, m, 1).format("MM_dd_YYYY");
      return summedImage.set('system:time_start', ee.Date.fromYMD(y, m, 1)).rename(date);
      //.set('month', m).set('year', y); // eventually set year and month 
  }));
  return byYear;
})).flatten());

Then you can filter out the dummy image and put all bands of the image collection into one image.

// filter the empty one out
var outputMonthly = byMonth.filter(ee.Filter.listContains('system:band_names', 'constant').not())
                    .sort('system:time_start').toBands();
print(outputMonthly);

Once you have one image with summed precipitation and the bands named to the first day of the month, you can apply reduceregions on that image to get all the values of summed precipitation in one go. The nice thing is also that it will return null vlaues if the points have no data, so no need to use the algorithm IF:

var features = outputMonthly.reduceRegions(fg_points, ee.Reducer.first(), 30);
print(features);


Export.table.toDrive(features,
"PrecipitationHTL",
"PrecipitationHTL",
"PrecipitationHTL");

link script Note that I added some random points as input, as your feat collection was not public

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.