[GIS] Maximum ndvi for 3 months average in Google Earth Engine

google-earth-enginelandsatndvi

I have a ndvi series which I am able to plot using below code:

var ndvi = l8.map(function(image) {
  return image.select().addBands(image.normalizedDifference(['B5', 'B4']));
});
var ndviChart = ui.Chart.image.series(ndvi, point, ee.Reducer.mean(), 500);
print(ndviChart);

My goal is to calculate the average ndvi for every 3 months window and then calculate the maximum ndvi over that 3 months window. How to access data in ui.Chart.series and is there any way by which I can do the above calculation on Google server ?

In javascript it can be done using

<script>
var lista = [22,4,5,6,11];
var maxAvg = 0.0;
for (var i = 0; i < lista.length - 2; i++) {
var avg = 0.0;
for (var j = 0; j < 3; j++) {
   avg = avg + lista[i+j];
}
if (maxAvg < (avg/3.0)) {maxAvg = (avg/3.0)};

}

document.write(maxAvg);
</script>

I want to replace lista with ndviChart series. Can anybody help me in this direction?

Best Answer

You might need some cloud masking in there, too, but here's the three-monthly part. Also note that the output imagery has three bands: mean, min, and max. You can now use that collection to make a chart, export a table, etc.

var ndvi = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR').map(function(image) {
  return image.select().addBands(image.normalizedDifference(['B5', 'B4']));
});

/* Creates a collection of mosaics with a given temporal interval.
 *
 * collection - the collection from which to make composites.
 * start - the date of the first composite (either a string or an ee.Date)
 * count - the number of composites to make
 * interval - The time between composites, in units of "units".
 * units - The units of step (day, week, month, year; see ee ee.Date.advance)
 */
var temporalCollection = function(collection, start, count, interval, units) {
  // Create a sequence of numbers, one for each time interval.
  var sequence = ee.List.sequence(0, ee.Number(count).subtract(1));

  var originalStartDate = ee.Date(start);

  return ee.ImageCollection(sequence.map(function(i) {
    // Get the start date of the current sequence.
    var startDate = originalStartDate.advance(ee.Number(interval).multiply(i), units);

    // Get the end date of the current sequence.
    var endDate = originalStartDate.advance(
      ee.Number(interval).multiply(ee.Number(i).add(1)), units);

    return collection.filterDate(startDate, endDate)
        .reduce(ee.Reducer.mean().combine({
          reducer2: ee.Reducer.minMax(),
          sharedInputs: true
        }));
  }));
};

var threeMonthlyNDVI = temporalCollection(ndvi, ee.Date('2015-01-01'), 12, 3, 'month');

var check = ee.Image(threeMonthlyNDVI.first());
Map.addLayer(check, {bands: 'nd_mean', min: 0, max: 1}, 'check')