working with Bitmasks

Extract the QA bits to mask clouds and cirrus

The code below uses the getQABits helper function to extract information from the stateQA band. Click here for the example.

// import data
var collection = ee.ImageCollection("MODIS/MOD09A1")

// Define dates
var iniDate = ee.Date.fromYMD(2010,1,1);
var endDate = ee.Date.fromYMD(2010,1,31);

// bands
var modisBands = ['sur_refl_b03','sur_refl_b04','sur_refl_b01','sur_refl_b02','sur_refl_b06','sur_refl_b07'];
var lsBands = ['blue','green','red','nir','swir1','swir2'];

// helper function to extract the QA bits
function getQABits(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[0], [newName])

// create cloud free composite
var first = collection.filterDate(iniDate,endDate).first().select(modisBands,lsBands);

// vis parameters
var visParams = {bands:['red','green','blue'],min:0,max:3000,gamma:1.3};
Map.addLayer(first,visParams,"first image rgb")

/*Bits 8-9: Cirrus detected
    0: None
    1: Small
    2: Average
    3: High*/
var cirrus =   getQABits(collection.filterDate(iniDate,endDate).first().select('StateQA'),8,9,'internal_quality_flag');
Map.addLayer(cirrus.eq(0),{min:0,max:1},"no cirrus")
Map.addLayer(cirrus.eq(1),{min:0,max:1},"small cirrus")
Map.addLayer(cirrus.eq(2),{min:0,max:1},"average cirrus")
Map.addLayer(cirrus.eq(3),{min:0,max:1},"high cirrus")

/*Bit 10: Internal cloud algorithm flag
    0: No cloud
    1: Cloud*/
var clouds =   getQABits(collection.filterDate(iniDate,endDate).first().select('StateQA'),10,10,'internal_quality_flag');

/* Bit 13: Pixel is adjacent to cloud
    0: No
    1: Yes*/
var adjClouds =   getQABits(collection.filterDate(iniDate,endDate).first().select('StateQA'),13,13,'internal_quality_flag');
Map.addLayer(adjClouds.eq(0),{min:0,max:1},"adjacent to cloud")

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s