Firstly, I have to point out that on brights surfaces as snow and bare sand the cloudmask algorithm doesn't perform well. You can find that easily and quickly on the NASA site. See for example here
Next, if you make a mosaic over sufficient images, you will end up with pixels you prefer and so the mosaicing itself filters out the clouds if you would do the mosaic correctly.
After a bit of research, it seems that clouds are higher in SWIR bands, so reversing the band order of a SWIR band and applying that as input to the quality mosaic will filter out clouds. Unfortunately, both shadow and water are lower in SWIR values, so you will end up with lots of shadow pixels.
What seems to perform better is apply the quality mosaic on the thermal infrared band, as clouds a relatively colder that the surface.
Here is the code I used. You will have to play around with it to get best results. You could maybe filter out winter or summer images or apply the quality mosaic on different (combinations of) bands:
var geometry = /* color: #00ffff */ee.Geometry.Polygon(
[[[92.57694067490729, -70.98282239796863],
[57.06912817490718, -70.57777747363254],
[51.35623754990718, -76.61497412799037],
[91.96170629990729, -77.28946992862909],
[92.66483129990729, -71.18228294956876]]]);
// TOA L7 dataset
var imageCollection = ee.ImageCollection("LANDSAT/LE07/C01/T2_TOA")
.filterDate('1999-12-01', '2003-03-01')
.filterBounds(geometry);
// Reverse the values of the SWIR band for the quality mosaic
var imageCollection = imageCollection.map(function(image){
return image.addBands(image.select('B5').subtract(1).abs().rename('QAband'));
});
// apply the quality mosaic for the SWIR and TIR bands
var mosaic_SWIR = imageCollection.qualityMosaic('QAband');
var mosaic_TIR = imageCollection.qualityMosaic('B6_VCID_1');
// Visualize the results
var visParams = {bands: ['B4', 'B3', 'B2'],min: 0, max:1};
Map.addLayer(mosaic_SWIR, visParams, 'With SWIR');
Map.addLayer(mosaic_TIR, visParams, 'With TIR');
Map.centerObject(geometry)
Best Answer
The Landsat 8 Collection 2 encompasses Level-1 (top of atmosphere reflectance) and Level-2 (atmospherically corrected surface reflectance) products. Since Landsat 8 Collection 2 level-2 correspond to surface reflectance and;
it should be used to generate NDVI.
However, you need to apply scaling factor i.e.,
0.0000275 + -0.2
for Level-2 before calculating NDVI. You should mask pixels which are contaminated by clouds and shadows as well.Here, I would recommend a worth reading article "A survival guide to Landsat preprocessing" for further enlightenment.
More information about Landsat Collection 2 Level-2 Science Products can be accessed from here.