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