1

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);
};
bad_coder
  • 11,289
  • 20
  • 44
  • 72
geo_dd
  • 283
  • 1
  • 5
  • 22
  • What do you mean by "good quality observations over land"? Are they pixels with a "QA" value of 1 at bit 0 (i.e. Land grid cell), a "QA" value of 1 at bit 1 (i.e. Sufficient valid data) and a "QA" value of 1 at bit 2 (Mapping period shortened)? And do you want to hide or show those pixels? – Kevin Mar 09 '19 at 15:32
  • Hi @Kevin. Hi Kevin, I want the land grid cells (bit 0, value 1), with sufficient data (bit 1,value 1) and shortened mapping period if observations are not reliable (bit 2, value 1). And these are the pixels that I want to keep. – geo_dd Mar 11 '19 at 09:59
  • If I understand correctly, you want to keep pixels that either have (bit 0 value 1 and bit 1 value 1) or (bit 0 value 1, bit 1 value 0, and bit 2 value 1). Is that correct? – Kevin Mar 11 '19 at 15:25
  • Hi @Kevin, from your commet, I want to keep pixels with bit 0 value 1 and bit 1 value 1 – geo_dd Mar 11 '19 at 15:37

1 Answers1

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.

Kevin
  • 338
  • 1
  • 3
  • 9
  • Dear @Kevin, thank you for your answer. It was well explained. Nevertheless, I still got confused about how to choose a given value, as your code used the value 3. What is the logic for choosing this value? Could you extend your previous answer so that this value choice is incorporated? – Philipe Riskalla Leal Apr 08 '21 at 14:29
  • Hi Philipe, I believe what you want to get will determine which value to choose. The value of 3 was what @geo_dd told me in his/her comment. @geo_dd was after pixels with "Land grid cell" (corresponding to value of 1 at bit 0) and "Sufficient valid data" (value of 1 at bit 1). You may find more information about the meaning of each bit of `QA` band of `MODIS/006/MCD64A1` collection in Code Editor. – Kevin Apr 09 '21 at 17:20