<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Bit Reader
Encode/Decode a set of bits
Initializes with parameter options, which must be a dictionary with the following format
Step1:  Internally it computes a dict with
- bit_length (length of the group of bits)
- lshift (left shift)
- shifted (shifted places)
Step2:  DECODE ONE VALUE
Step3:  MATCH ONE VALUE
Step4:  ENCODE A VALUE (EXCLUSIVELLY)
In this case, shadow is 00000100 (4) and not 00000101 (5)
Step5:  ENCODE A VALUE (ALL)
This will get all values (all combinations where the bit is set)
Step6:  ENCODE AND
Step7:  DECODE AN IMAGE
Step8:  BANDS
Step9:  APPLY THE BitReader TO THE BAND THAT HOLDS THE BIT INFORMATION
Step10:  BitReader INFORMATION FOR KNOW COLLECTIONS AVAILABLE IN geetools.cloud_mask MODULE
<ASSISTANT_TASK:>
Python Code:
import ee
ee.Initialize()
from geetools import bitreader, cloud_mask
options = {
 '0-1': {0:'clear', 1:'cloud', 2:'mix'},  # cloud state
 '2-2': {0: 'no_shadow', 1:'shadow'},  # cloud shadow (bit 0 is not needed)
 '6-7': {0:'climatology', 1:'low', 2:'average', 3:'high'}  # land/water flag
 }
reader = bitreader.BitReader(options, 16)
Explanation: Bit Reader
Encode/Decode a set of bits
Initializes with parameter options, which must be a dictionary with the following format:
keys must be a str with the bits places, example: '0-1' means bit 0 and bit 1
values must be a dictionary with the bit value as the key and the category (str) as value. Categories must be unique.
Encode: given a category/categories return a list of possible values
Decode: given a value return a list of categories
Example
MOD09 (http://modis-sr.ltdri.org/guide/MOD09_UserGuide_v1_3.pdf)
(page 28, state1km, 16 bits):
End of explanation
reader.info
print('bit length', reader.bit_length)
Explanation: Internally it computes a dict with
- bit_length (length of the group of bits)
- lshift (left shift)
- shifted (shifted places)
End of explanation
value = 204
bits = reader.getBin(value)
print('204:', bits)
reader.decode(204)
Explanation: DECODE ONE VALUE
End of explanation
reader.match(204, 'cloud')
reader.match(204, 'shadow')
Explanation: MATCH ONE VALUE
End of explanation
reader.encode('shadow')
reader.encode('clear')
reader.encode('no_shadow')
Explanation: ENCODE A VALUE (EXCLUSIVELLY)
In this case, shadow is 00000100 (4) and not 00000101 (5)
End of explanation
print(reader.encodeOne('shadow')[0:100])
print(reader.encodeOne('cloud')[0:100])
Explanation: ENCODE A VALUE (ALL)
This will get all values (all combinations where the bit is set)
End of explanation
print(reader.encodeAnd('cloud', 'shadow')[0:100])
Explanation: ENCODE AND
End of explanation
import ee
import ipygee as ui
Map = ui.Map()
Map.show()
modcol = ee.ImageCollection('MODIS/006/MOD09GA').sort('system:time_start', False)
mod = ee.Image(modcol.first())
Explanation: DECODE AN IMAGE
End of explanation
red = 'sur_refl_b01'
green = 'sur_refl_b04'
blue = 'sur_refl_b03'
qa = 'state_1km'
qa_mask = mod.select(qa)
Map.addLayer(mod, {'bands':[red, green, blue], 'min':0, 'max':5000}, 'Original')
Map.addLayer(qa_mask, {'min':0, 'max':reader.max}, 'QA')
Explanation: BANDS
End of explanation
mask = reader.decodeImage(mod, qa)
Map.addLayer(mask.select(['cloud']), {'min':0, 'max':1}, 'Clouds')
Explanation: APPLY THE BitReader TO THE BAND THAT HOLDS THE BIT INFORMATION
End of explanation
from geetools import cloud_mask
state1km = cloud_mask.BITS_MODIS09GA
state1km
Explanation: BitReader INFORMATION FOR KNOW COLLECTIONS AVAILABLE IN geetools.cloud_mask MODULE
End of explanation
<END_TASK>