'QA with different bits in GEE

I face a problem regarding the selection of good quality data of the MCD64A1 burned area product. Here, is a description of the QA band but I am really confused with the different bits.

What I want to do is to select all the good quality observations over land and mask the collection based on them. I have the following, but it is nt the right way to do this.

    var geometry = /* color: #d63000 */ee.Geometry.Polygon(
            [[[23.821277851635955, 46.07285332090363],
              [23.274708027417205, 45.952681148559265],
              [24.11378883796408, 45.554067690813184],
              [24.89381813483908, 45.84372892769175],
              [24.17146706062033, 46.25167241706428]]]);
    var dataset = ee.ImageCollection('MODIS/006/MCD64A1')
                      .filterBounds(geometry)
        .map(function(image){return image.clip(geometry)}) 
                          .filter(ee.Filter.calendarRange(7,7,'month'));
    var burnedArea = dataset.select('BurnDate','QA');

//good quality observations

var good= (function(img) {
  var goodQA = img.select("QA").eq(1); 
  return img.updateMask(burnedArea .and(goodQA));
});

EDIT

Also, I have tried the following which gives me no error but also no data.

var good= function(img){  
  var qa = img.select(['QA']);
  var mask = qa.bitwiseAnd(0).eq(1).and( 
             qa.bitwiseAnd(1).eq(1)).and( 
             qa.bitwiseAnd(2).eq(1)); 
  return img.updateMask(mask);
};


Solution 1:[1]

I think this code may suit your need:

var good = function(img) {
    var qa = img.select(['QA']);
    var mask = qa.bitwiseAnd(3).eq(3);
    return img.updateMask(mask);
}
burnedArea = burnedArea.map(good);

Basically, this code just modifies the line var mask = ... from your edited code. After the line burnedArea = burnedArea.map(good);, your burnedArea variable will only show pixels that, in QA band, have "bit 0 value 1 and bit 1 value 1".

Since number 3 has a binary form of 11 (bit 0 value 1 and bit 1 value 1), what qa.bitwiseAnd(3) does is turning any pixel in QA band into one out of four values:

  • value of 3 if QA pixel has "bit 0 value 1 and bit 1 value 1" (e.g. 3, 7, 11, 15, etc.)

  • value of 2 if QA pixel has "bit 0 value 0 and bit 1 value 1" (e.g. 2, 6, 10, etc.)

  • value of 1 if QA pixel has "bit 0 value 1 and bit 1 value 0" (e.g. 1, 5, 9, etc.)

  • value of 0 if QA pixel has "bit 0 value 0 and bit 1 value 0" (e.g. 0, 4, 8, etc.)

The .eq(3) part, as you may know already, converts pixels with value of 3 to 1, and the rest to 0.

Hope this clears your confusion over the "bits" stuff.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1