[GRASS-user] Calculate average of 3 highest values for a set of raster maps

Nikos Alexandris nik at nikosalexandris.net
Fri Mar 15 00:56:12 PDT 2013


[apologies for taking the liberty to re-sort previous posts]

Milton Cezar Ribeiro:

> > I have a set of 7 raster maps.
> > I am able to calculate several stats with these mapas using r.series.
> > Now I need to calculate the mean of the tree highest values of my 7 maps.
> > Any idea of how can I do that?

José Miguel Barrios wrote:
> A possible approach could be, for example, to do the following with each
> map:

> r.stats -1 input=map1 output=cellvaluesmap1
 
> It will create a file containing the cell values for each map.  Then you
> can read this file into R and average the three highest values.  Something
> like:
 
> values.map1<-read.table("cellvaluesmap1")
> threehighest<-values.map1$V1[order(values.map1,decreasing=FALSE)][1:3]
> mean3highest<-mean(threehighest)

If in Linux, there are several ways to do this.  One (very) nice approach as 
shown in <http://stackoverflow.com/a/3096575/1172302>, could be:

# get the numbers
r.stats -1 input=map1 output=Values1

# sort and keep first 3
sort -rn Values1  |  head -3 > Values1.sorted

# sum and divide by 3
echo "( $( paste -sd+ Values1.sorted ) ) / 3" | bc

# or a one-liner!
sort -rn numbers | head -3  | echo "( $( paste -sd+ ) ) / 3" | bc

Other options would be "awk" or "python". Also, note that  r.stats  has also 
the "-x" and "-g" flags.  You could grab them as well and, if doing the math 
"externally", know where to place the results each time?

However, it seems like a very "resources" hungry approach, if your maps are 
many and large.  There must be a "smart"(er) way to do this directly using 
r.mapcalc...

Best, Nikos
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 230 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20130315/1f876f2e/attachment.pgp>


More information about the grass-user mailing list