[GIS] Google Earth Engine Mask application

data-qualitygoogle-earth-enginemaskingmodis

I'm using GEE in order to extract evapotranspiration (ET) data from MODIS (MOD16A2v6).

I want to extract only ET band in order to make some further process.
To do so, I apply some filters (like filterDate and a clip over my area of interest). Then I want also use the Quality Band information (bits) in order to create some mask function to keep only best quality data.

The problem is that when I want to load the image collection, after I applied my masks, GEE does not allow me to select only the ET band in a new variable.
I don't understand why. In my way of thinking, I would say that I load the entire Modis product collection with all its bands. Apply my filters and Masks on that collection (based on the QA band) and then select only the first product (band ET). But obviously it's not correct, I must be wrong somewhere.

Here is the part of my code related to that problem:

//*************** FUNCTIONS DEFINITIONS *******************

// ------------ Selection of data on the study area
var clip = function(img){
  return img.clip(subWet);
}; // détermine sur quelle zone on veut extraire les images modis. Ici, les 6 lagunes


// ------------- Filters

/* Create a function that select images on basis of QA information (stored in QA band).
Return only images according to specified bits. 

  Function Arguments:
*   image - The QA Image to get bits from.
*   start - The first bit position, 0-based.
*   end   - The last bit position, inclusive.
*   name  - A name for the output image.
*/

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);
};

// --- Mask out according to MODLAND_QC bits QA band (bit 0)
var mask1 = function(image) {
  // Select the QA band.
  var QA = image.select('ET_QC');
  // Get the internal_cloud_algorithm_flag bit.
  var internalCloud = getQABits(QA, 0, 1, 'Modland_QC');
  // Return an image masking out cloudy areas.
  return image.updateMask(internalCloud.eq(0));
};

// --- Mask out cloudy pixels - cloud state  from state QA band (bits 3 and 4)
var mask2= function(image) {
  // Select the QA band.
  var QA = image.select('ET_QC');
  // Get the internal_cloud_algorithm_flag bit.
  var cloud = getQABits(QA, 3, 4, 'cloud_state')
                        .expression("b(3) == 1 || b(3) == 2"); // sélection des deux bits que l'on dégage
  // Return an image masking out cloudy areas.
  return image.updateMask(cloud.not())//.internalCloud.eq(0));
};

// --- Mask out according to quality score -   from state QA band (bits 5to 7)
var mask3= function(image) {
  // Select the QA band.
  var QA = image.select('ET_QC');
  // Get the internal_cloud_algorithm_flag bit.
  var qscore = getQABits(QA, 5, 7, 'quality score')
                        .expression("b(5) == 2 || b(5) == 3 || b(5)==4"); // sélection des deux bits que l'on dégage
  // Return an image masking out cloudy areas.
  return image.updateMask(qscore.not())//.internalCloud.eq(0));
};

// ***************** CHARGEMENT DES IMAGES ****************

var dataset = ee.ImageCollection(MOD16A2v6)
var datafiltered=dataset
                  .map(clip) // définie pour notre zone clipée
                  .filterDate('2001-01-01', '2001-05-25')
                  .map(mask1)
                  .map(mask2)
                  .map(mask3)// function QA bits

var ET = datafiltered.select('ET') // sélection de la bande ET dans l'ensemble du produit modis

Best Answer

When you select the QA band, the QA image has only one band (you selected one image). Applying a function expression on that image with other band numbers than zero is not possible, because there is only one band in that image, thus:

// Select the QA band.
  var QA = image.select('ET_QC');
  var cloud = getQABits(QA, 3, 4, 'cloud_state')
                        .expression("b(0) == 1 || b(0) == 2"); // sélection des deux bits que l'on dégage

AND

  // Select the QA band.
  var QA = image.select('ET_QC');
  // Get the internal_cloud_algorithm_flag bit.
  var qscore = getQABits(QA, 5, 7, 'quality score')
                        .expression("b(0) == 2 || b(0) == 3 || b(0)==4"); // sélection des deux bits que l'on dégage

Link code

Related Question