How to apply flags -olci- need help

Hello!

I’m struggling while trying to apply flags to pixels, i.e. to mask out pixels with a flag set.
Sensor is OLCI.

**** preamble ****
I read band1
readband1=p.getBand(‘Oa01_reflectance’)
band1=np.zeros(w*h, np.dtype(‘float32’))
readband1.readPixels(0,0,w,h,band1)

I know that pixel number 147 is valid.
In [142]: band1[147]
Out[142]: 0.039545815

I read also the flag bands:
readmask_lsb=p.getBand(‘WQSF_lsb’)
readmask_msb=p.getBand(‘WQSF_msb’)
mask_lsb=np.zeros(wh,‘int32’)
mask_msb=np.zeros(w
h,‘int32’)
readmask_lsb.readPixels(0,0,w,h,mask_lsb)
readmask_msb.readPixels(0,0,w,h,mask_msb)

and pixel 147 has the following flags active:
In [148]: np.binary_repr(mask_lsb[147], width=32)
Out[148]: ‘00010010000010000010000000010010’
In [149]: np.binary_repr(mask_msb[147], width=32)
Out[149]: ‘00000000000000000000000001000000’

**** /preamble ****

So I see that e.g. flag CLOUD is set.

What should I do to mask out pixels with CLOUD flag set?
I tried all possible variations of
readband1.setValidPixelExpression(‘WQSF_lsb_CLOUD ? NaN: Oa01_reflectance’)

In [154]: readband1.getValidMaskExpression()
Out[154]: ‘(WQSF_lsb_CLOUD ? NaN: Oa01_reflectance) && Oa01_reflectance.raw != 65535.0’

But when I read again band1 ( readband1.readPixels(0,0,w,h,band1) ), still I get pixel 147 valid
In [156]: band1[147]
Out[156]: 0.039545815

Sorry for the long and maybe stupid question, but I’m sort of stuck on this point!

Many many thanks in advance for any help!

Mario

I have to admit, this is not straight forward. Try the following:

band.setValidPixelExpression('WQSF_lsb_CLOUD ? NaN: Oa01_reflectance')
# create the arrays
raster  = numpy.zeros(width, dtype=numpy.float32)
valids  = numpy.zeros(width, dtype=numpy.uint8)
# read the raster and the valid mask
band.readPixels(0, y, width, 1, raster)
band.readValidMask(0, y, width, 1, valids)
# invert the valid mask, because numpy.ma.array needs it.
invalids= numpy.where(valids == 0, 1, 0)
# get the masked data
data = numpy.ma.array(raster, mask=invalids, fill_value=numpy.nan)

There is also an example in the snappy folder, named snappy_ndvi_with_masks.py.
It’s also available on GitHub.

hallo! thanks for your reply.
Unfortunately, it does not change …
the array valids still report pixel number 147 as valid, despite it has the CLOUD flag set …

Oh, now I see.
The expression is not correct.
Just use ‘not WQSF_lsb_CLOUD’
Then everything will be valid where the cloud flag is not set.
The expression you have used would be good to create a new band by the Band Maths operator.

Actually the correct expression is ‘WQSF_lsb_CLOUD’ !
(this will put to invalid where the flag is set)

Thanks for your support!

I believe that if I want to check multiple flags at the same time, I may use || and && operators

Sorry, you were right … the correct expression is ‘not WQSF_lsb_CLOUD’.
I was making confusion between two different flags!
To apply different flags simoultaneously:

FLAGS= ‘not (WQSF_lsb_INVALID | WQSF_lsb_LAND | WQSF_lsb_CLOUD | WQSF_lsb_SNOW_ICE )’
band.setValidPixelExpression(FLAGS)