You can use the ee.Reducer.max()
reducer as the input to ee.ImageCollection.reduce()
with the numInputs
parameter to retain the values of additional bands from the "max image".
Here is an example that adds date bands (milliseconds from Unix epoch and YYYYMMdd) to each image in a MODIS NDVI collection and then reduces the collection to NDVI max and retains the date bands which can be used to determine date of the max NDVI (per-pixel) or even find the original image using a filter on system:time_start
that matches a value in the "millis" band.
// Adds milliseconds from Unix epoch and date encoded as YYYYMMdd
// as bands to a given image.
function addDateBands(img) {
var imgDate = img.date();
var millis = imgDate.millis();
var date = ee.Number.parse(imgDate.format('YYYYMMdd'));
var dateBands = ee.Image([millis, date]).rename(['millis', 'date']).toLong();
return img.addBands(dateBands).set('date', date);
}
// Applies per-pixel max reduction of image collection that returns the max
// of the target band and also the corresponding band values from the max image.
function getMaxAndOthers(col, maxBand, otherBands) {
var theseBands = [maxBand].concat(otherBands);
col = col.select(theseBands);
return col.reduce(ee.Reducer.max(theseBands.length)).rename(theseBands);
}
// Import a MODIS NDVI collection for the year 2021.
var ndvi = ee.ImageCollection('MODIS/061/MOD13Q1')
.filterDate('2021-01-01', '2022-01-01')
.select('NDVI');
// Add date bands to each image.
var ndviWithDate = ndvi.map(addDateBands);
// Get per-pixel max NDVI value and corresponding band values from max image.
var ndviMax = getMaxAndOthers(ndviWithDate, 'NDVI', ['millis', 'date']);
// Visualize max NDVI and corresponding date band (YYYYMMdd).
Map.addLayer(
ndviMax,
{bands: ['NDVI'], min: 0, max: 9000, palette: ['white', 'green']},
'NDVI max');
Map.addLayer(
ndviMax,
{bands: ['date'], min: 20210101, max: 20211219},
'Date for NDVI max');
Code Editor script