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
MOD09 (http://modis-sr.ltdri.org/guide/MOD09_UserGuide_v1_3.pdf) (page 28, state1km, 16 bits):
In [1]:
import ee
ee.Initialize()
In [2]:
from geetools import bitreader, cloud_mask
In [3]:
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
}
In [4]:
reader = bitreader.BitReader(options, 16)
Internally it computes a dict with
In [5]:
reader.info
Out[5]:
In [6]:
print('bit length', reader.bit_length)
DECODE ONE VALUE
In [7]:
value = 204
bits = reader.getBin(value)
print('204:', bits)
In [8]:
reader.decode(204)
Out[8]:
MATCH ONE VALUE
In [9]:
reader.match(204, 'cloud')
Out[9]:
In [10]:
reader.match(204, 'shadow')
Out[10]:
ENCODE A VALUE (EXCLUSIVELLY)
In this case, shadow is 00000100 (4) and not 00000101 (5)
In [11]:
reader.encode('shadow')
Out[11]:
In [12]:
reader.encode('clear')
Out[12]:
In [13]:
reader.encode('no_shadow')
Out[13]:
ENCODE A VALUE (ALL)
This will get all values (all combinations where the bit is set)
In [14]:
print(reader.encodeOne('shadow')[0:100])
In [15]:
print(reader.encodeOne('cloud')[0:100])
ENCODE AND
In [16]:
print(reader.encodeAnd('cloud', 'shadow')[0:100])
In [17]:
import ee
In [18]:
import ipygee as ui
In [19]:
Map = ui.Map()
Map.show()
In [20]:
modcol = ee.ImageCollection('MODIS/006/MOD09GA').sort('system:time_start', False)
In [21]:
mod = ee.Image(modcol.first())
BANDS
In [22]:
red = 'sur_refl_b01'
green = 'sur_refl_b04'
blue = 'sur_refl_b03'
In [23]:
qa = 'state_1km'
In [24]:
qa_mask = mod.select(qa)
In [25]:
Map.addLayer(mod, {'bands':[red, green, blue], 'min':0, 'max':5000}, 'Original')
In [26]:
Map.addLayer(qa_mask, {'min':0, 'max':reader.max}, 'QA')
APPLY THE BitReader
TO THE BAND THAT HOLDS THE BIT INFORMATION
In [27]:
mask = reader.decodeImage(mod, qa)
In [28]:
Map.addLayer(mask.select(['cloud']), {'min':0, 'max':1}, 'Clouds')
BitReader
INFORMATION FOR KNOW COLLECTIONS AVAILABLE IN geetools.cloud_mask
MODULE
In [29]:
from geetools import cloud_mask
In [30]:
state1km = cloud_mask.BITS_MODIS09GA
In [31]:
state1km
Out[31]:
In [ ]: