2

What would be a way to compute moving averages of a time series in GEE? Consider we imported an image collection X and filtered by start and finish dates.

var Y =ee.ImageCollection(X.filterDate(start,finish));

I want to be able to compute moving averages of the time series

1 Answer 1

6

There are issues with this example (clouds), but it illustrates how to do what you want using joins, map and reduce:

var l5toa = ee.ImageCollection("LANDSAT/LT5_L1T_TOA");
var geometry = ee.Geometry.Point([-120.69580078125, 37.49229399862877]);

var collection = l5toa.filterBounds(geometry);

var join = ee.Join.saveAll({
  matchesKey: 'images'
});

var timeField = 'system:time_start';

var diffFilter = ee.Filter.maxDifference({
  difference: 1000 * 60 * 60 * 24 * 17,
  leftField: timeField, 
  rightField: timeField
});

var threeNeighborJoin = join.apply({
  primary: collection, 
  secondary: collection, 
  condition: diffFilter
});

var smoothed = ee.ImageCollection(threeNeighborJoin.map(function(image) {
  var collection = ee.ImageCollection.fromImages(image.get('images'));
  return ee.Image(image).addBands(collection.mean());
}));

var smooth = ee.Image(smoothed.filterDate('2008-06-01', '2008-07-01').first());
Map.addLayer(smooth, {bands: ['B4', 'B3', 'B2'], max: 0.4}, 'original');
Map.addLayer(smooth, {bands: ['B4_1', 'B3_1', 'B2_1'], max: 0.4}, 'smooth');

More info here and here.

2
  • Could you explain the meaning of the syntax of var diffFilter and var threeNeighborJoin? I do understand the temporal cadence of L8 is 17 days, so is 'difference' the calculation of consecutive values in milliseconds? Does threeNeigborjoin mean the average of three consecutive values? What if I want to take the rolling average of 5 consecutive values?
    – tg110
    Feb 4, 2020 at 15:03
  • difference is 17 days in milliseconds. threeNeighborJoin is a FeatureCollection in which each image stores a reference (images) to whatever images pass the filter (i.e. is within 17 days). The mean is computed by collection.mean(). For a 5 day filter, use 34 days in the filter. You can discover all that by printing intermediate results. Feb 5, 2020 at 21:48

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.