1

I have a collection of 457 images, where each image is the total precip in a month, 1981-present.

I want to find the total precip in each quarter i.e. reduce three consecutive images by sum thereby producing another image collection of sie ~ 152

I have the below code, but I am unable to iterate it through all the images

var listofmonthlyprecip = collection.toList(collection.size());// convert collection of 457 images into a list
print (listofmonthlyprecip);

var trial1 = ee.Image(listofmonthlyprecip.get(0));// Jan 1981
var trial2 = ee.Image(listofmonthlyprecip.get(1));// Feb 1981
var trial3 = ee.Image(listofmonthlyprecip.get(2));// Mar 1981

var imgcollection = ee.ImageCollection([trial1, trial2, trial3]); //make collection of first three months 
var quarter_trial = imgcollection.reduce(ee.Reducer.sum());// add values
print (quarter_trial);
Map.addLayer (quarter,{min:0, max: 1143}, 'first quarter'); map

How do I iterate through all images to get sum of values for groups of 3 consecutive images?

1 Answer 1

1

You should first make a list of all quarters and a list of all years. You can then map over both to calculate a summed image for each quarter:

// set the collection (unsure what you use...)
var collection = ee.ImageCollection("IDAHO_EPSCOR/GRIDMET").select('pr');

// set list to map over
var quarters = ee.List.sequence(1,10,3);
var years = ee.List.sequence(1980, 2020);

// Group by year and quarter, reduce within groups by sum();
var byQuarter = ee.ImageCollection.fromImages(
      years.map(function (year) { // map over years
        year = ee.Number(year);
        var images = quarters.map(function(quarter){ // map over quarters
          quarter = ee.Number(quarter);
          var start = ee.Date.fromYMD(year, quarter, 1);
          var yearEnd = ee.Number(ee.Algorithms.If(quarter.eq(10), year.add(1), year));
          var quarterEnd = ee.Number(ee.Algorithms.If(quarter.eq(10), 1, quarter.add(3)));
          var end = ee.Date.fromYMD(yearEnd, quarterEnd, 1);
          return collection.filterDate(start, end).sum().set({
            'system:time_start': start.millis(), 'system:time_end': end.millis(),
            quarterStart: quarter, quarterEnd: quarter.add(3), year: year});
        });
  return images
}).flatten())
    // optionally filter out empty images
    .filter(ee.Filter.listContains('system:band_names', 'pr'));

Unsure what image colleciton you used, so I just picked one with precipitation. See here the full link.

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.