Google Earth Engine – Add Per Pixel Day of the Year Information Band to Max() Reducer

google-earth-enginereducers

I need to use max reducer keeping as additional band information from which day of the year the data comes from.

Got a function adding a day of the year band to the image, that I can map a collection with:

var addDate = function(image){
  var doy = image.date().getRelative('day', 'year');
  var doyBand = ee.Image.constant(doy).uint16().rename('doy')
  doyBand = doyBand.updateMask(image.select('B8').mask())
  return image.addBands(doyBand);
};

But how do I now having some collection, for example:

var collection = ee.ImageCollection('LANDSAT/LT05/C01/T1')
  .filterDate('2008-01-01', '2008-12-31')
  .filter(ee.Filter.eq('WRS_PATH', 44))
  .filter(ee.Filter.eq('WRS_ROW', 34));

reduce it with max() reducer, but so it would add additional bands with information from which day of the year every pixel with maximum value was taken?

To be more specific: Lets say that I need for example only max of B1. As an output I would like to have 2 bands – 1st: max() of B1s from the collection, second – DOY for every pixel (from which day the max value image of that pixel in the collection was).

Best Answer

You do this with the numInputs argument on the max reducer.

var reducer = ee.Reducer.max(2)

However, that will only work for 1 band plus the DOY band. If you're taking the max over all bands, then the max values for a single pixel could be from different days, so there would be multiple DOY values for that pixel.