5

I'm using GRIDMET image collection in Google Earth Engine, which has daily climate data from ~1980 to present. I would like to create an image collection of monthly (May-September: each month) sum precipitation for each year (1986-2016). I've been trying to create a function to do so, but can't figure it out.

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

4
  • With ee.Filter.calendarRange(start,end,field) you can select by month or year. But I don't know how to create a function to do this
    – aldo_tapia
    Oct 12, 2017 at 19:28
  • I'm using GSMaP image collection in Google Earth Engine, which has hourly precipitation data from 2000 to present. I have a script which helps getting data (as a table) from points from each available image in given period. Could anyone please assist me on how to make the function that it will sum the values of hourly data to daily. Please, find the script which should be improved below: //Points var p1 = /* color: #d63000 /ee.Geometry.Point([69.15, 54.8331]), p2 = / color: #98ff00 /ee.Geometry.Point([70.917, 54.433]), p3 = / color: #0b4a8b */ee.Geometry.Point([66.967, 54.367]); // Collect May 3, 2018 at 9:58
  • 2
    Welcome to GIS SE. Please, add a new question instead to add this post as an answer
    – aldo_tapia
    May 3, 2018 at 12:08
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review
    – aldo_tapia
    May 3, 2018 at 12:08

2 Answers 2

17

Here is an example that should get you started:

var modis = ee.ImageCollection('MODIS/MOD13A1');

var months = ee.List.sequence(1, 12);

// Group by month, and then reduce within groups by mean();
// the result is an ImageCollection with one image for each
// month.
var byMonth = ee.ImageCollection.fromImages(
      months.map(function (m) {
        return modis.filter(ee.Filter.calendarRange(m, m, 'month'))
                    .select(1).mean()
                    .set('month', m);
}));
print(byMonth);

Map.addLayer(ee.Image(byMonth.first()));

Obviously you're going to want to replace the mean() with sum(), etc.

3
  • @KevinBarnes If this answers your question please mark this as the correct answer using the grey/green tick to the left of this post.
    – Kersten
    Oct 15, 2017 at 8:06
  • 1
    @Nicholas Clinton- Your GEE code calculates the mean of all months of Jan, all months of Feb..... between 1981-present and hence produces an image collection of only 12 images. What if I were to calculate the sum of precip for each month for each year i.e. sum of precip for Jan 1981, Feb 1981...Dec 2019 i.e. have 12 months * 38 years (2019-1981) ~450 images, where each image is the sum of precip for a month in the time period?
    – tg110
    Jan 20, 2020 at 7:46
  • @Nicholas Clinton, Is this "time aggregation" documented in earth engine? I could not find anything, and it seems it would be worth it, time-wise reduction/composaiting is a very frequent task, but quite unintuitive in EE, yet no discussion on it? tg110 question for example is a very valid one yet little guidance is available for it
    – Matifou
    Jul 2, 2020 at 21:48
2

Answering your other question about how to get one Image for each month:

// Set years and month
var startYear = 1981;
var endYear = 2019;
var years = ee.List.sequence(startYear, endYear);
var months = ee.List.sequence(1, 12);
// load the image collection
var Daily = ee.ImageCollection('MODIS/MOD13A1').select(1)
  .filterBounds(YourBound);

print(Daily, "Daily prec data from 1981-2019")

// 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, 5).format("MM_dd_YYYY");
      return summedImage.set('system:time_start', ee.Date.fromYMD(y, m, 5))
        .rename(date)
        .reduce(ee.Reducer.mean());
      //.set('month', m).set('year', y); // eventually set year and month 
    }));
  return byYear;
})).flatten());
// filter the empty one out
var outputMonthly = byMonth.filter(ee.Filter.listContains('system:band_names',
    'constant').not())
  .sort('system:time_start').toBands();
print(outputMonthly);

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

Export.image.toDrive({
  image: features,
  description: "CHIRPS_monthly_mean_1981-2019",
  scale: 3000,
  folder: "CHIRPS_months",
  region: YourBound
});
1
  • I had to export the images and not he feature collection to outputMonthly. Exporting to the former produced "Error: Image.clipToBoundsAndScale, argument 'input': Invalid type. Expected type: Image<unknown bands>. Actual type: FeatureCollection. (Error code: 3)". Nonetheless, brilliant piece of code @Ludmila!
    – Rion Lerm
    Oct 6, 2023 at 8:02

Your Answer

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

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