I have imported L2 modis files from a subscription to the near real time image support into MATLAB. I have the int32 flags array, but i do not know how to extract each byte corresponding to each flag. I know that the 1st byte is atm corr failure, 2nd land, and so on; and I know that when i successfully extract the flag, for instance 2 (land) i should have an array of the same dimension than the data but with 0 where is water and 1 where is land but I do not know how to do this.
Anyone can give some ligth of how can i do this in matlab? I really appreciate any hint.
Best regards, Gabriel Yuras
Altought it is quite late, I will post and answer for future reference:
You have to convert each element of the INT32 matrix into binary format. This is easily done using the built-in matlab
funcion dec2bin, but first you should check either if the integer is negative or positive. Below is an example:
suppose 'l2_flags' is the int32 matrix, then for the value in the row=i, column=j:
> if l2_flags(i,j) >= 0 % positive or zero pass directly to DEC2BIN, with a leading '0'
> bin = ('0' dec2bin(l2_flags(i,j),n-1));
> else % negative, pass the negative of the input to DEC2BIN, add leading '1'
> bin = ('1' dec2bin(-l2_flags(i,j),n-1));
the result, 'bin' has 32 bits, each one representing a flag.
hope it helps, Uriel.
Thanks a lot Uriel:
I wrote in to matlab and seems to work fine with bin = ['0' dec2bin(l2_flags(i,j),n-1)]; replacing the external parenthesis for square braquets and doing n=2.
My question is if this n=2 is it ok or should I use another number?.
I forgot to mention that N is the minimum number of bits used in the binary representation.
So it's okay to use N=2, nevertheless I would suggest to use N=32, bearing in mind that the non-significative zeros are also flags...
Sorry for the posting two years later ;)
I make it the following way:
% read flags from a seawifs or modis HDF image
% convert them to unsigned integer
% with this method 32nd flag is lost, but it's fast
l2_flags = uint32(abs(double(hdfread('S2001232103832.L2_MLAC.x.hdf', 'l2_flags'))));
% make the land mask matrix of the same size as l2_flags. Zeros = land, ones - water
landMask = (bitand(l2_flags, 2) > 0);
bitand makes bitwise comparison of each element in l2flags with 2 (2 in decimal = 00000010 in binary, if only one byte is used) If second bit of an l2_flags element is not zero then bitand return 00000010, which is more than zero (and equal to 2 actually).