[GRASS-user] resampling question

Dylan Beaudette debeaudette at ucdavis.edu
Thu Oct 20 12:03:46 EDT 2011


On Thursday, October 20, 2011, Kirk Wythers wrote:
> On Oct 20, 2011, at 9:22 AM, Dylan Beaudette wrote:
> 
> > The neat thing about GRASS and other OSS, you can always take a peak at 
the 
> > source code. From my basic understanding of C, I don't think that there is 
> > anything here that would accommodate ties-- so the result of 
> > mode(4,4,4,4,5,5,5,5) would probably be 4 -- as it comes first. This is 
the 
> > behavior in R when using something like which.max(table(x)).
> 
> Thanks for the reply Dylan. As I read through the code you pasted, it looks 
to me that as the variable 'values' gets counted, then in the case of a tie, 
such as this 2 x 2 matrix 
> 
> _____
> | 4 | 5 |
> _____
> | 4 | 5 |
> _____
> 
> would yield a mode result of 4 with the logic that the tie goes to the 
smallest value in sorted list of values? In this case...  4 4 5 5 = mode of 4, 
where as a sorted list such as 1 2 3 4 would yield a mode of 1? 
> 
> Is that right? 
> 

That is my interpretation of the code. Perhaps Glynn or someone else more 
familiar with the raster library can comment further. Also- why not try a 
simple experiment. You can create fake raster data with a text editor and read 
it in via r.in.ascii.

put the following in a text file called 'fake.rast' :
----------------------------------
north:  4
south:  0
east:   4
west:   0
rows:   4
cols:   4
4 4 4 4
4 4 4 4
5 5 5 5
5 5 5 5
------------------------------------

# import
r.in.ascii in=fake.raster out=f
g.region rast=f -p

# check
d.mon wx0
d.rast.num f

# aggregate
g.region res=4 -p
r.resamp.stats --o in=f out=f.mode method=mode

# check
d.erase
d.rast.num f.mode


... which shows that our interpretation is correct. An interesting side-note:

# region is still set to a resolution of 4x4 units = 1 cell
# NN-resampling of the original data gives us 5...
r.out.ascii --q -h f
5 

# aggregated (mode) gives us 4...
r.out.ascii --q -h f.mode
4

Fun!


-- 
Dylan E. Beaudette
USDA-NRCS Soil Scientist
California Soil Resource Lab
http://casoilresource.lawr.ucdavis.edu/


More information about the grass-user mailing list