3

What is the function behind two specific line in below coding block ...

1) var cloudBitMask = 1 << 10;
2) var mask = qa.bitwiseAnd(cloudBitMask).eq(0) .and(qa.bitwiseAnd(cirrusBitMask).eq(0));

Block of code:

function maskS2clouds(image) {
  var qa = image.select('QA60');

  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));

  return image.updateMask(mask).divide(10000);
}
var dataset = ee.ImageCollection('COPERNICUS/S2')
                  .filterDate('2018-01-01', '2018-06-30')
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                  .map(maskS2clouds);

var rgbVis = {
  min: 0.0,
  max: 0.3,
  bands: ['B4', 'B3', 'B2'],
};

Map.setCenter(-9.1695, 38.6917, 12);
Map.addLayer(dataset.median(), rgbVis, 'RGB');

1 Answer 1

0

Here's a short explanation: 1) means that you are interested in evaluating the bit 10 of the image, while 2) means that you are creating a mask according to two conditions evaluated on different bits of the image. According to the information provided by GEE about the Sentinel-2 image collection, bit 10 evaluates the presence of clouds (0: absence, 1: presence), while bit 11 evaluates the presence of cirrus (0: absence, 1: presence). Thus, when you define the mask with the previous code, it indicates that you want to keep all the pixels where its bit 10 and 11 are both zeros and mask every pixel where this condition is not met. Finally, the returned image after applying the updateMask, should be a image without clouds or cirrus.

2
  • Thank you Jonathan for the answer. I still confused about the logic of Boolean function in Java. I mean how writing a 1<<10 get us to the 10th bit number ?
    – rez
    Commented Sep 5, 2021 at 20:14
  • Raw binary logic and Java bit operation questions are more appropriate in Stack Overflow proper.
    – Vince
    Commented Sep 5, 2021 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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