// Extract MODIS QA information from the "state_1km" QA band1// and use it to mask out cloudy and deep ocean areas.2//3// QA Band information is available at:4// https://lpdaac.usgs.gov/products/modis_products_table/mod09ga5// Table 1: 1-kilometer State QA Descriptions (16-bit)678/**9* Returns an image containing just the specified QA bits.10*11* Args:12* image - The QA Image to get bits from.13* start - The first bit position, 0-based.14* end - The last bit position, inclusive.15* name - A name for the output image.16*/17var getQABits = function(image, start, end, newName) {18// Compute the bits we need to extract.19var pattern = 0;20for (var i = start; i <= end; i++) {21pattern += Math.pow(2, i);22}23return image.select([0], [newName])24.bitwiseAnd(pattern)25.rightShift(start);26};2728// Reference a single MODIS MOD09GA image.29var image = ee.Image('MODIS/006/MOD09GA/2012_10_11');3031// Select the QA band32var QA = image.select('state_1km');3334// Get the cloud_state bits and find cloudy areas.35var cloud = getQABits(QA, 0, 1, 'cloud_state')36.expression("b(0) == 1 || b(0) == 2");3738// Get the land_water_flag bits.39var landWaterFlag = getQABits(QA, 3, 5, 'land_water_flag');4041// Create a mask that filters out deep ocean and cloudy areas.42var mask = landWaterFlag.neq(7).and(cloud.not());4344// Add a map layer with the deep ocean and clouds areas masked out.45Map.addLayer(image.updateMask(mask),46{47bands: ['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03'],48min: -100,49max: 200050}, 'MOD09GA 143'51);5253// Add a semi-transparent map layer that displays the clouds.54Map.addLayer(55cloud.updateMask(cloud),56{palette: 'FFFFFF', opacity: 0.8},57'clouds'58);596061