[GIS] How to apply a Cloud Mask in Google Earth Engine? (Landsat 5 TM 8-Day NDVI Composite)

cloud-gisgoogle-earth-enginelandsatmaskingndvi

I need to download ndvi values for some pixels for 2000-2001 using the Landsat 5 TM 8-Day NDVI Composite, but since there is no quality band as in mod13q1 there's no way to filter by quality. Therefore, I want to make a mask using an image collection with information about clouds or shadows so I can mask those pixels with high o low values.
I have the script to download the ndvi values but I don't know how to apply the mask neither how to create the mask..

any experience with that?

Code:

var puntos = ee.FeatureCollection('ft:11qUO03jhF4MiECWgd5Rev536WFERNiqWf5qpqXwb');

var landsat = ee.ImageCollection('LANDSAT/LT5_L1T_8DAY_NDVI').select('NDVI')
                                     .filterDate('2000-01-01', '2000-02-28');


var f_extraer_puntos = function(i){return i.reduceRegions({collection: puntos, reducer: ee.Reducer.mean(), scale: 30, })};

// #Extract the values by running reduceRegions over each image in the image collection.
var valores_ = landsat.map(f_extraer_puntos);

var ext_puntos = ee.FeatureCollection(valores_.flatten());
// #Turn the result into a feature collection

Export.table.toDrive({ collection: ext_puntos, description:'LT5_NDVI_puntos', fileFormat: 'CSV' }); 

Best Answer

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.