I have to mask all the cloudy pixels, and because the QA info is stored in bits I am trying to decode them using this code. However when I chart the masked timeseries, I get an error: Error generating chart: Data column(s) for axis #0 cannot be of type string. Is this code robust for masking modis cloudy pixels?

var M = ee.ImageCollection("MODIS/006/MOD09GA"),
geometry1 = /* color: #d63000 */ee.Geometry.Point([10.644168108701706, 64.4188479841206]),
single = ee.Image("MODIS/006/MOD09GA/2012_10_11");

 var getQABits = function(single, 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 single.select([0], [newName])

// Select the QA band
var QA = single.select('state_1km');

// Get the cloud_state bits and find cloudy areas.
var cloud = getQABits(QA, 0, 1, 'cloud_state')
                .expression("b(0) == 0 || b(0) == 2 ");

// Get the land_water_flag bits.
var landWaterFlag = getQABits(QA, 3, 5, 'land_water_flag');

// Create a mask that filters out deep ocean and cloudy areas.
//var mask = landWaterFlag.neq(7).and(cloud.not());

var filter = function(image){ 
var mask = landWaterFlag.neq(7).and(cloud.not())
return ee.Image(image).updateMask(mask)

var B1= M.filterDate('2010-04-01', '2014-10-31').select('sur_refl_b01')
var gooddata = B1.map(filter)

 // chart b1 all quality data
 var chartb1 = ui.Chart.image.seriesByRegion({
 imageCollection: B1, 
 regions: geometry1, 
 reducer: ee.Reducer.mean(),
  scale: 1000,
  band: 'sur_refl_b01',

// chart b1 masked quality data
var chartb1mask = ui.Chart.image.seriesByRegion({
imageCollection: gooddata, 
regions: geometry1, 
reducer: ee.Reducer.mean(),
scale: 1000,
band: 'sur_refl_b01',

You get an error when charting because you don't have any unmasked data. Your cloud expression is incorrect. According to the data catalog, values that are 0 and 3 are assumed to not be clouds. So your cloud expression should probably be something like this:

b(0) != 0 && b(0) != 3


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.