// Array-based quality mosaic.12// Returns a mosaic built by sorting each stack of pixels by the first band3// in descending order, and taking the highest quality pixel.4function qualityMosaic(bands) {5// Convert to an array, and declare names for the axes and indices along the6// band axis.7var array = bands.toArray();8var imageAxis = 0;9var bandAxis = 1;10var qualityIndex = 0;11var valuesIndex = 1;1213// Slice the quality and values off the main array, and sort the values by the14// quality in descending order.15var quality = array.arraySlice(bandAxis, qualityIndex, qualityIndex + 1);16var values = array.arraySlice(bandAxis, valuesIndex);17var valuesByQuality = values.arraySort(quality.multiply(-1));1819// Get an image where each pixel is the array of band values where the quality20// band is greatest. Note that while the array is 2-D, the first axis is21// length one.22var best = valuesByQuality.arraySlice(imageAxis, 0, 1);2324// Project the best 2D array down to a single dimension, and convert it back25// to a regular scalar image by naming each position along the axis. Note we26// provide the original band names, but slice off the first band since the27// quality band is not part of the result. Also note to get at the band names,28// we have to do some kind of reduction, but it won't really calculate pixels29// if we only access the band names.30var bandNames = bands.min().bandNames().slice(1);31return best.arrayProject([bandAxis]).arrayFlatten([bandNames]);32}3334// Load the l7_l1t collection for the year 2000, and make sure the first band35// is our quality measure, in this case the normalized difference values.36var l7 = ee.ImageCollection('LANDSAT/LE07/C01/T1')37.filterDate('2000-01-01', '2001-01-01');38var withNd = l7.map(function(image) {39return image.normalizedDifference(['B4', 'B3']).addBands(image);40});4142// Build a mosaic using the NDVI of bands 4 and 3, essentially showing the43// greenest pixels from the year 2000.44var greenest = qualityMosaic(withNd);4546// Select out the color bands to visualize. An interesting artifact of this47// approach is that clouds are greener than water. So all the water is white.48var rgb = greenest.select(['B3', 'B2', 'B1']);4950Map.addLayer(rgb, {gain: [1.4, 1.4, 1.1]}, 'Greenest');51Map.setCenter(-90.08789, 16.38339, 11);52535455