Extract groups of/individual bit values from UInt16 array [SEC=UNCLASSIFIED]
![](https://secure.gravatar.com/avatar/b5cc99b26cd789752a6fd6b1aec74e5b.jpg?s=120&d=mm&r=g)
I'm working with some MODIS satellite imagery. MODIS data includes a quality flag mask. For the particular dataset I'm working with, this is a two dimensional unsigned 16 bit integer array. The quality flags are stored as one or more bits in each integer value: Bits are numbered from 0 (least significant bit) Bit Long name Key 0-1 MODLAND_QA 00=VI produced, good quality 01=VI produced, but check other QA 10=Pixel produced, but most probably cloudy 11=Pixel not produced due to other reasons than clouds 2-5 VI usefulness 0000=Highest quality 0001=Lower quality 0010=Decreasing quality 0100=Decreasing quality 1000=Decreasing quality 1001=Decreasing quality 1010=Decreasing quality 1100=Lowest quality 1101=Quality so low that it is not useful 1110=L1B data faulty 1111=Not useful for any other reason/not processed ...<SNIP>... 15 Possible shadow 0=No 1=Yes Some typical values are: arr=numpy.array([51199,37013,36885,36889,34841,2062,34837,2061,35033,349 61,2185,37013,36885,2185,4109,4233], dtype=numpy.uint16) How would I extract groups of/individual bit values from such an array? Regards Luke Pinner ------ If you have received this transmission in error please notify us immediately by return e-mail and delete all copies. If this e-mail or any attachments have been sent to you in error, that error does not constitute waiver of any confidentiality, privilege or copyright in respect of information in the e-mail or attachments. Please consider the environment before printing this email. ------
![](https://secure.gravatar.com/avatar/b0f62d137f9ea1d0b6cc4e7e6f61b119.jpg?s=120&d=mm&r=g)
On Tue, Oct 12, 2010 at 11:14 PM, Pinner, Luke < Luke.Pinner@environment.gov.au> wrote:
I'm working with some MODIS satellite imagery. MODIS data includes a quality flag mask. For the particular dataset I'm working with, this is a two dimensional unsigned 16 bit integer array. The quality flags are stored as one or more bits in each integer value:
Bits are numbered from 0 (least significant bit) Bit Long name Key 0-1 MODLAND_QA 00=VI produced, good quality 01=VI produced, but check other QA 10=Pixel produced, but most probably cloudy 11=Pixel not produced due to other reasons than clouds 2-5 VI usefulness 0000=Highest quality 0001=Lower quality 0010=Decreasing quality 0100=Decreasing quality 1000=Decreasing quality 1001=Decreasing quality 1010=Decreasing quality 1100=Lowest quality 1101=Quality so low that it is not useful 1110=L1B data faulty 1111=Not useful for any other reason/not processed ...<SNIP>... 15 Possible shadow 0=No 1=Yes
Some typical values are: arr=numpy.array([51199,37013,36885,36889,34841,2062,34837,2061,35033,349 61,2185,37013,36885,2185,4109,4233], dtype=numpy.uint16)
How would I extract groups of/individual bit values from such an array?
You could use the shift (>>) and bitwise 'and' (&) operators: In [50]: arr Out[50]: array([51199, 37013, 36885, 36889, 34841, 2062, 34837, 2061, 35033, 34961, 2185, 37013, 36885, 2185, 4109, 4233], dtype=uint16) In [51]: qa = arr & 3 In [52]: qa Out[52]: array([3, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=uint16) In [53]: usefulness = (arr >> 2) & 15 In [54]: usefulness Out[54]: array([15, 5, 5, 6, 6, 3, 5, 3, 6, 4, 2, 5, 5, 2, 3, 2], dtype=uint16) In [55]: shadow = arr >> 15 In [56]: shadow Out[56]: array([1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0], dtype=uint16) Warren
![](https://secure.gravatar.com/avatar/13d4ef1dbfd985740cdf7c04ce11fe92.jpg?s=120&d=mm&r=g)
On 10/12/2010 9:14 PM, Pinner, Luke wrote:
I'm working with some MODIS satellite imagery. MODIS data includes a quality flag mask. For the particular dataset I'm working with, this is a two dimensional unsigned 16 bit integer array. The quality flags are stored as one or more bits in each integer value:
I've written a cython wrapper around some C++ functions that use the the std::bitset datatype to read bits in the MODIS cloud mask (the MYD035/MOD035 files). Email me if you'd like a copy with a simple demo, it wouldn't be hard to adapt to the quality flag mask -- regards, Phil Austin
participants (3)
-
Philip Austin
-
Pinner, Luke
-
Warren Weckesser