<P>&nbsp;Thank you very much Glynn and the Grass Community</P><P>This has worked perfectly!!!!</P><P>Andrew</P><P>&nbsp;</P><P><BR><BR><FONT SIZE=2 STYLE=font-size:9pt><B>Glynn Clements &lt;glynn@gclements.plus.com&gt;</B></FONT><BR><FONT SIZE=2 STYLE=font-size:9pt>05/28/2007 02:52 AM CET</FONT><BR><BR> <FONT SIZE=2 STYLE=font-size:9pt>To</FONT> &nbsp; <FONT SIZE=2 STYLE=font-size:9pt>andrew.haywood@poyry.com</FONT><BR> <FONT SIZE=2 STYLE=font-size:9pt>cc</FONT> &nbsp; <FONT SIZE=2 STYLE=font-size:9pt>grassuser@grass.itc.it, grassuser-bounces@grass.itc.it</FONT><BR> <FONT SIZE=2 STYLE=font-size:9pt>bcc</FONT> &nbsp; <BR> <FONT SIZE=2 STYLE=font-size:9pt>Subject</FONT> &nbsp; <FONT SIZE=2 STYLE=font-size:9pt>Re: [GRASS-user] help to use r.mapcalc with decimal/binary (modisdata)</FONT><BR> &nbsp;<BR><BR></P><P><FONT FACE="Monospace,Courier">andrew.haywood@poyry.com wrote:<BR></FONT><BR><FONT FACE="Monospace,Courier">&gt; Okay - unfortunately i am now totally confused. I have tried to read up on<BR>&gt; binary data (to little or no avail!!!). My understanding is the MODIS<BR>&gt; suf_refl_qc data that i have import is a unsigned 16 bit integer number. I<BR>&gt; would like to select out pixels using this raster &nbsp;to create a cloud mask<BR>&gt; based on some of the bit codes.<BR>&gt;<BR>&gt; for example identify &quot; band 1 quality = missing input&quot; would have the<BR>&gt; following<BR>&gt;<BR>&gt; &nbsp;* * * * 1 0 1 1 * * * * * * * * * - where * represents placeholder<BR>&gt;<BR>&gt; If i try and follow Glynn's logic he is saying divide the number by 16 and<BR>&gt; then take the modulus 16 and then equate it to &quot;11&quot; to get the subset.<BR>&gt;<BR>&gt; I am unsure how I choose the &quot;11&quot; and how do i change the syntax to pull<BR>&gt; out the different masks i may be interested in.<BR>&gt;<BR>&gt; 0 0 * * * * * * * * * * * * * * &nbsp; ideal quality all bands<BR>&gt; 0 1 * * * * * * * * * * * * * * less than ideal quality<BR>&gt; * * 0 0 * * * * * * * * * * * * clear<BR>&gt; * * 0 1 * * * * * * * * * * * * cloudy<BR>&gt; * * 1 0 * * * * * * * * * * * * mixed<BR>&gt; * * * * 0 0 0 0* * * * * * * * highest quality band 1<BR>&gt; * * * * * * * * * 0 0 0 0* * * highest quality band 2<BR>&gt; etc...<BR>&gt;<BR>&gt; Any help would be greatly appreciated. At this stage im plugging in the<BR>&gt; decimal numbers into binary calculator and using r.reclass to create a<BR>&gt; MASK.<BR></FONT><BR><FONT FACE="Monospace,Courier">First, binary numbers are normally written with the bit 0 (the &quot;units&quot;<BR>digit) on the right, so &quot;band 1 quality = missing input&quot; would be:<BR></FONT><BR><FONT FACE="Monospace,Courier">1 1 1 1 1 1<BR>5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0</FONT><BR><BR><FONT FACE="Monospace,Courier">* * * * * * * * 1 0 1 1 * * * *<BR></FONT><BR><FONT FACE="Monospace,Courier">To shift everything right by N bits, divide by 2^N, so dividing by 16<BR>(2^4) shifts everything right by 4 places, moving bits 4-7 to bits<BR>0-3:<BR></FONT><BR><FONT FACE="Monospace,Courier">* * * * * * * * * * * * 1 0 1 1<BR></FONT><BR><FONT FACE="Monospace,Courier">[Similarly, dividing a decimal number by 10^4 = 10,000 shifts it right<BR>by 4 digits: 12345678 / 10000 = 1234]<BR></FONT><BR><FONT FACE="Monospace,Courier">To keep the bottom N bits and discard the rest, take the remainder<BR>(modulus) after division by 2^N, so modulus 16 (2^4) keeps the bottom<BR>4 bits:<BR></FONT><BR><FONT FACE="Monospace,Courier">0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1<BR></FONT><BR><FONT FACE="Monospace,Courier">[Similarly dividing a decimal number by 10^4 = 10,000 and taking the<BR>remainder takes the bottom 4 digits: 12345678/10000 = 1234 remainder<BR>5678, so 12345678 % 10000 = 5678]<BR></FONT><BR><FONT FACE="Monospace,Courier">The binary number &quot;1011&quot;<BR>= 1*2^3 + 0*2&quot;2 + 1*2^1 + 1*2^0<BR>= 1*8 + 0*4 + 1*2 + 1*1<BR>= 8 + 2 + 1<BR>= 11</FONT><BR><BR><FONT FACE="Monospace,Courier">So, given your original bit definitions<BR></FONT><BR><FONT FACE="Monospace,Courier">bitsdescriptiondividemodulusresult range<BR>0-1MODLAND QA140-3<BR>2-3cloud state440-3<BR>4-7band 1 data quality16160-15<BR>8-11band 2 data quality256160-15<BR>12atmospheric correction409620-1<BR>13adjacency correction819220-1<BR>14different orbit1638420-1<BR>15unused3276820-1</FONT><BR><BR><FONT FACE="Monospace,Courier">For the right-hand side:<BR></FONT><BR><FONT FACE="Monospace,Courier">MODLAND QA (2 bits):<BR></FONT><BR><FONT FACE="Monospace,Courier">binarydecimaldescription<BR>000corrected product produced at ideal quality -- all bands<BR>011corrected product produced, less than ideal quality -- some or all bands<BR>102corrected product not produced due to cloud effects -- all bands<BR>113corrected product not produced for other reasons -- some or all</FONT><BR><BR><FONT FACE="Monospace,Courier">Cloud state (2 bits):<BR></FONT><BR><FONT FACE="Monospace,Courier">binarydecimaldescription<BR>000clear<BR>011cloudy<BR>102mixed<BR>113not set, assumed clear</FONT><BR><BR><FONT FACE="Monospace,Courier">band quality (4 bits):<BR></FONT><BR><FONT FACE="Monospace,Courier">binarydecimaldescription<BR>00000highest quality<BR>00011<BR>00102<BR>00113<BR>01004<BR>01015<BR>01106<BR>01117<BR>10008dead detector; data has been copied from adjacent detector<BR>10019solar zenith &gt;= 86 degrees<BR>101010solar zenith &gt;= 85 and &lt; 86 degrees<BR>101111missing input<BR>110012internal constant used in place of climatological data for at least one atmospheric constant<BR>110113correction out of bounds, pixel constrained to extreme allowable value<BR>111014L1B data faulty<BR>111115not processed due to deep ocean or clouds</FONT><BR><BR><FONT FACE="Monospace,Courier">A shell script which uses r.mapcalc to split a QC map given as its<BR>first argument into separate maps for each field:<BR></FONT><BR><FONT FACE="Monospace,Courier">#!/bin/sh<BR>src=$1<BR>r.mapcalc &lt;&lt;EOF<BR>$src.modland &nbsp; &nbsp;= ($src / 1 &nbsp; &nbsp;) % &nbsp;4<BR>$src.cloud &nbsp; &nbsp; &nbsp;= ($src / 4 &nbsp; &nbsp;) % &nbsp;4<BR>$src.band1qual &nbsp;= ($src / 16 &nbsp; ) % 16<BR>$src.band2qual &nbsp;= ($src / 256 &nbsp;) % 16<BR>$src.atmos_corr = ($src / 4096 ) % &nbsp;2<BR>$src.adj_corr &nbsp; = ($src / 8192 ) % &nbsp;2<BR>$src.diff_orbit = ($src / 16384) % &nbsp;2<BR>$src.unused &nbsp; &nbsp; = ($src / 32768) % &nbsp;2<BR>EOF<BR></FONT><BR><FONT FACE="Monospace,Courier">--<BR>Glynn Clements &lt;glynn@gclements.plus.com&gt;</FONT></P><BR>
This email and any files transmitted with it are confidential, may be legally privileged and are intended solely for the use of the individual or entity to whom they are addressed. If you are not the intended recipient, you are hereby notified that any use, distribution, or reproduction of the contents of this email is strictly prohibited and may be unlawful. If you are not the intended recipient, please notify the sender by return email and destroy all copies of the original message including any attachments thereto. <BR>
Thank you.<BR>