I am looking to make an image collection that holds the 95th percentile of NDVI for each year from 1985 - 2011 for the Landsat 5 record. I am able to create the 95th percentile composites, but I would like to avoid copy and pasting to change the year. This is what a single year would look like. How do I easily loop/map over each year to create an image collection that holds finalmask_1985, finalmask_1986, finalmask_1987.... and so on?

//Load Landsat 5 image collection
var collection_1985= ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')
//Isolate NDVI and time band
var only_NDVI_1985 = collection_1985.select('nd','system:time_start');
//Create image counts mask
var count_1985 = only_NDVI_1985.select('nd').reduce(ee.Reducer.count());
var countmask_1985 = count_1985.select('nd_count').gte(10);
//Make 95th percentile NDVI composite
var ninety_five_percent_1985 = only_NDVI_1985.reduce(ee.Reducer.percentile([95]));
//Create NDVI <.1 mask
var mask95_1985 = ninety_five_percent_1985.select('nd_p95').gte(0.1);
//Apply masks
var lte95_1985 = ninety_five_percent_1985.updateMask(mask95_1985);
var finalmask_1985 = lte95_1985.updateMask(countmask_1985);

Here is the link to the full script: https://code.earthengine.google.com/3674fa123d228d3a5a17b0fbcdeb3f60

1 Answer 1


You want to create a function, taking a year as argument, which creates the image for that year. Then you can map that function over a list of years. Something like this:

var ndviCollection = ee.ImageCollection(
  ee.List.sequence(1985, 2011).map(ndviForYear)

Map.addLayer(ndviCollection.first(), {palette: 'red, orange, yellow, green'})

function processImage(image) {
  return image
      image.normalizedDifference(['B4', 'B3']).rename('ndvi')

function ndviForYear(year) {
  var startDate = ee.Date.fromYMD(year, 1, 1)
  var collection = ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')
    .filterDate(startDate, startDate.advance(1, 'year'))
  var reduced = collection
      .combine(ee.Reducer.percentile([95]), null, true)
    .rename(['count', 'ndvi'])
  return reduced
      reduced.expression('i.count >= 10 and i.ndvi > 0.1', {i: reduced})
    .set('year', year)


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.