Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/examples/javascripts/ModisQaBands.js
2313 views
1
// Extract MODIS QA information from the "state_1km" QA band
2
// and use it to mask out cloudy and deep ocean areas.
3
//
4
// QA Band information is available at:
5
// https://lpdaac.usgs.gov/products/modis_products_table/mod09ga
6
// Table 1: 1-kilometer State QA Descriptions (16-bit)
7
8
9
/**
10
* Returns an image containing just the specified QA bits.
11
*
12
* Args:
13
* image - The QA Image to get bits from.
14
* start - The first bit position, 0-based.
15
* end - The last bit position, inclusive.
16
* name - A name for the output image.
17
*/
18
var getQABits = function(image, start, end, newName) {
19
// Compute the bits we need to extract.
20
var pattern = 0;
21
for (var i = start; i <= end; i++) {
22
pattern += Math.pow(2, i);
23
}
24
return image.select([0], [newName])
25
.bitwiseAnd(pattern)
26
.rightShift(start);
27
};
28
29
// Reference a single MODIS MOD09GA image.
30
var image = ee.Image('MODIS/006/MOD09GA/2012_10_11');
31
32
// Select the QA band
33
var QA = image.select('state_1km');
34
35
// Get the cloud_state bits and find cloudy areas.
36
var cloud = getQABits(QA, 0, 1, 'cloud_state')
37
.expression("b(0) == 1 || b(0) == 2");
38
39
// Get the land_water_flag bits.
40
var landWaterFlag = getQABits(QA, 3, 5, 'land_water_flag');
41
42
// Create a mask that filters out deep ocean and cloudy areas.
43
var mask = landWaterFlag.neq(7).and(cloud.not());
44
45
// Add a map layer with the deep ocean and clouds areas masked out.
46
Map.addLayer(image.updateMask(mask),
47
{
48
bands: ['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03'],
49
min: -100,
50
max: 2000
51
}, 'MOD09GA 143'
52
);
53
54
// Add a semi-transparent map layer that displays the clouds.
55
Map.addLayer(
56
cloud.updateMask(cloud),
57
{palette: 'FFFFFF', opacity: 0.8},
58
'clouds'
59
);
60
61