# Cloud Mask Most optical satellite imagery products come with one or more QA-bands that allows the user to assess quality of each pixel and extract pixels that meet their requirements. The most common application for QA-bands is to extract information about cloudy pixels and mask them. But the QA bands contain a wealth of other information that can help you remove low quality data from your analysis. Typically the information contained in QA bands is stored as Bitwise Flags. [More info about bitmasks](https://spatialthoughts.com/2021/08/19/qa-bands-bitmasks-gee/) In Landsat imagery, pixel quality assessment (QA_PIXEL) bands are generated by the CFMask algorithm. Different bit definitions are used because the cirrus band is only available on Landsat 8 and 9. This band is relevant to both Surface Reflectance and Surface Temperature products. |Bit | Landat 5 & 7 |Landsat 8 & 9 | |---------|:----------: |:----------:| |0 | Fill | Fill | |1 | Dilated Cloud|Dilated Cloud| |2|Unused|Cirrus| |3|Cloud|Cloud| |4|Cloud Shadow|Cloud Shadow| |5|Snow|Snow| |6|Clear|Clear| |7|Water|Water| |8-9|Cloud Confidence|Cloud Confidence| |10-11|Cloud Shadow Confidence|Cloud Shadow Confidence| |12-13|Snow/Ice Confidence|Snow/Ice Confidence| |14-15|Unused|Cirrus Confidence| The two following functions will be used to mask clouds, cloud shadows, ... on everny images of an `ImageCollection`. ```js /** * Utility to extract bitmask values. * Look up the bit-ranges in the catalog. * * value - ee.Number or ee.Image to extract from. * fromBit - int or ee.Number with the first bit. * toBit - int or ee.Number with the last bit (inclusive). * Defaults to fromBit. */ function bitwiseExtract(value, fromBit, toBit) { if (toBit === undefined) toBit = fromBit var maskSize = ee.Number(1).add(toBit).subtract(fromBit) var mask = ee.Number(1).leftShift(maskSize).subtract(1) return value.rightShift(fromBit).bitwiseAnd(mask) } ``` ```js var maskClouds = function(image) { var QA = image.select('QA_PIXEL'); var clouds = bitwiseExtract(QA, 3).eq(0) var cloud_shadows = bitwiseExtract(QA, 4).eq(0) var mask = clouds .and(cloud_shadows) var maskedImage = image.updateMask(mask) return maskedImage } ``` --- [bitwiseExtract function](https://gis.stackexchange.com/questions/349371/creating-cloud-free-images-out-of-a-mod09a1-modis-image-in-gee/349401#349401)