[GRASS-user] help to use r.mapcalc with decimal/binary (modis data)

Glynn Clements glynn at gclements.plus.com
Thu May 24 20:01:43 EDT 2007

andrew.haywood at poyry.com wrote:

> i am having some issues with analysing  MOD09 modis data in grass.  I 
> would like to mask out the cloud pixels using the below quality control 
> band.
> when i import this band the unsigned 16bit integer is converted in decimal 
> and I am unsure how to  use r.mapcalc to create a mask based on the 
> different bits. 
> I am unsure how I should go about this (see *** section below). I would 
> like to try different selections to see which would make the best mask but 
> i am unsure how to do this against a binary field 
> eg. r.mapcalc MASK="if(sur_refl_qc_250 == **** clear state **** , 1, 
> null())"
> how do I select out the different bits to create the mask?
> At this stage i am thinking of trying to convert the decimal back to 
> binary (not sure how to do this ) and then subselect columns (bits) eg 0-1 
> or 2-3 and the placing an if statement (eg. if column01 == "00" or if 
> column23 = "00").


	r.mapcalc MASK="if(sur_refl_qc_250 & 3 == 0 , 1, null())"

For the 

Given your description of the various bits, the coresponding masks are:

	Bits	Mask
	0-1	0x0003
	2-3	0x000C
	4-7	0x00F0
	8-11	0x0F00
	12	0x1000
	13	0x2000
	14	0x4000
	15	0x8000

The value to compare will need to be shifted into the correct range,
e.g. for "band 1 quality = missing input":

	r.mapcalc MASK="if(sur_refl_qc_250 & 0x00F0 == 0x00B0 , 1, null())"

If you're unfamiliar with binary arithmetic, there's some information


Glynn Clements <glynn at gclements.plus.com>

More information about the grass-user mailing list