Your feature collection is private, so I can't apply your code. But, here you have how to mask clouds from Landsat 5 Collection 1 Tier 2 TOA Reflectance and compute NDVI:
var region = /* color: #0b4a8b */ee.Geometry.Polygon(
[[[-71.9000244140625, -30.817346256492073],
[-70.7080078125, -30.826780904779778],
[-70.6640625, -30.372875188118016],
[-71.8670654296875, -30.410781790845878]]]),
landsat = ee.ImageCollection("LANDSAT/LT05/C01/T2_TOA");
var collection = landsat.filterBounds(region).filterDate('2000-01-01', '2000-02-28');
var getQABits = function(image, start, end, newName) {
// Compute the bits we need to extract.
var pattern = 0;
for (var i = start; i <= end; i++) {
pattern += Math.pow(2, i);
}
// Return a single band image of the extracted QA bits, giving the band
// a new name.
return image.select([0], [newName])
.bitwiseAnd(pattern)
.rightShift(start);
};
// A function to mask out cloudy pixels.
var cloud_shadows = function(image) {
// Select the QA band.
var QA = image.select(['BQA']);
// Get the internal_cloud_algorithm_flag bit.
return getQABits(QA, 7,8, 'Cloud_shadows');
// Return an image masking out cloudy areas.
};
// A function to mask out cloudy pixels.
var clouds = function(image) {
// Select the QA band.
var QA = image.select(['BQA']);
// Get the internal_cloud_algorithm_flag bit.
return getQABits(QA, 4,4, 'Cloud').neq(1);
// Return an image masking out cloudy areas.
};
var maskClouds = function(image) {
var cs = cloud_shadows(image);
var c = clouds(image);
image = image.updateMask(cs);
return image.updateMask(c);
};
var NDVI = function(image) {
return image.normalizedDifference(['B5', 'B4']).rename('NDVI');
};
var ndvi = collection.map(maskClouds).map(NDVI);
Map.addLayer(collection, {bands: ['B4', 'B3', 'B2'],min: [0,0,0],max: [0.2, 0.2, 0.2]}, 'Landsat with clouds');
Map.addLayer(ndvi,{min: -1, max: 1, palette: ['blue', 'white', 'green']}, 'NDVI cloud free');
Link to reproducible example: https://code.earthengine.google.com/2602e7e0f67bd31e1f53379dcd326148
You need to check Landsat documentation to know how to decode BQA from this collection.
For the record, here is a good way to do this:
var imageCollection = ee.ImageCollection("LANDSAT/LT05/C01/T1");
var months = ee.List.sequence(1, 12);
var composites = ee.ImageCollection.fromImages(months.map(function(m) {
var filtered = imageCollection.filter(ee.Filter.calendarRange({
start: m,
field: 'month'
}));
var composite = ee.Algorithms.Landsat.simpleComposite(filtered);
return composite.normalizedDifference(['B4', 'B3']).rename('NDVI')
.set('month', m);
}));
print(composites);
var check = ee.Image(composites.first());
Map.addLayer(check, {min: 0, max: 1}, 'check');
For the search robots, this is a "temporally grouped reduction."
Best Answer
You can use qualityMosiac exactly similar for Sentinel-2 as for Landsat. The method you are looking for is described here. What you need is adding a NDVI band to the image collection of Sentinel-2 and use qualityMosaic('NDVI') to obtain the per pixel values of the corresponding pixel with the highest NDVI value.
Link script