Google Earth Engine Time Series – Analyzing Seasonal Time Series as a Function in GEE

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

I've been trying to write a function for time series data script that currently looks like this:

var fall_1987_mf = final_col
  .select('ndvi','time')
  .filterDate('1987-09-22','1987-12-22')
  .reduce(ee.Reducer.percentile([80]))
  .clip(mf);
Map.addLayer(fall_1987_mf,ndviParams,"fall 1987");

var fall_1988_mf = final_col
  .select('ndvi','time')
  .filterDate('1988-09-22','1988-12-22')
  .reduce(ee.Reducer.percentile([80]))
  .clip(mf);
Map.addLayer(fall_1988_mf,ndviParams,"fall 1989");

var fall_1989_mf = final_col
  .select('ndvi','time')
  .filterDate('1989-09-22','1989-12-22')
  .reduce(ee.Reducer.percentile([80]))
  .clip(mf);
Map.addLayer(fall_1989_mf,ndviParams,"fall 1989");

var fall_1990_mf = final_col
  .select('ndvi','time')
  .filterDate('1990-09-22','1990-12-22')
  .reduce(ee.Reducer.percentile([80]))
  .clip(mf);
Map.addLayer(fall_1990_mf,ndviParams,"fall 1990");

and so on…. This currently runs from 1982 to 2020 and I am going to do it for the three other seasons and I would obviously would prefer a more concise way to code this out. My ideal output is 12 image collections with one for each season in each of the 3 rois (mf, nf, and sf).

Link to full script: https://code.earthengine.google.com/93740a3a82c2354055f618ce00cdec47

Best Answer

You could map over a list of years:

var fall_mf_collection = ee.ImageCollection(
  ee.List.sequence(1982, 2020).map(function(year) {
    year = ee.Number(year)
    return final_col
      .select('ndvi', 'time')
      .filterDate(ee.Date.fromYMD(year, 9, 22), ee.Date.fromYMD(year, 12, 22))
      .reduce(ee.Reducer.percentile([80]))
      .set('year', year)
  })
)

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