Reducing Image Collection by seasons annually in Earth Engine

google-earth-enginegoogle-earth-engine-javascript-api

The goal I am trying to achieve is to output imagery for my roi representing maximum NDVI for each season for each year in my Landsat time series (1982 – 2020). I know how to make greenest pixel composites in earth engine and divide it annually using this method:

function ndviForYear(year) {
  var startDate = ee.Date.fromYMD(year, 1, 1);
  var collection = final_col
    .filterDate(startDate, startDate.advance(1, 'year'));
  var reduced = collection
    .select('nd','system:time_start')
    .reduce(ee.Reducer.count()
      .combine(ee.Reducer.percentile([80]), null, true)
    );
  return reduced
    .select('nd_p80','system:time_start_p80')
    .set('year', year);
}

var ndviCollection = ee.ImageCollection(
  ee.List.sequence(1982, 2020).map(ndviForYear)
);

I want a similar output, but instead of one image for each year, I want maximum (80th percentile) seasonal NDVI — so four images per year (winter, spring, summer, fall). Does anyone know how to easily modify this script to achieve this?

Here is the full code link: https://code.earthengine.google.com/88ebcdcce75e477236b8f74f222319e9
Here is the link to the roi asset: https://code.earthengine.google.com/?asset=users/alo266/Sierra_Ancha

Best Answer

The following is the update of your code, resulting in 4 images in each year for every year.


// create start index for each season
var season_start = ee.List.sequence(0, 9, 3);

function ndviForYear(year) {
  var startDate = ee.Date.fromYMD(year, 1, 1);

  var make_datelist = function (n) {
    return startDate.advance(n, "month");
  };

  // have start date of every season
  var fourSeasons = season_start.map(make_datelist); 

  var computeNDVI = function (d1) {
    var start = ee.Date(d1);
    var end = start.advance(3, "month");
    var date_range = ee.DateRange(start, end);

    var collection = final_col.filterDate(
      startDate,
      startDate.advance(1, "year")
    );

    collection = collection.filterDate(date_range);
    var reduced = collection
      .select("nd", "system:time_start")
      .reduce(
        ee.Reducer.count().combine(ee.Reducer.percentile([80]), null, true)
      );
    return reduced.set("year", year);
 
};
  return fourSeasons.map(computeNDVI);
}

var ndviCollection = ee
  .ImageCollection(ee.List.sequence(1982, 2020).map(ndviForYear).flatten())
  .select('nd_p80', 'system:time_start_p80');

print(ndviCollection);

Full code here https://code.earthengine.google.com/fd503af08a1b5f7782f6b9097e1da7a3

Related Question