mean value from raster

Malcolm Williamson malcolm at cast.uark.edu
Tue May 23 17:11:21 EDT 1995


On Mon, 22 May 1995, Eric Small wrote:

> 
> Hi,
> 
> This should be an easy question to answer.
> 
> How can you determine the mean value of cells in a raster layer
> not including 0 (no data) values ?
> 
> Presently, i am resorting to turning the raster into an ascii file
> and then computing the mean in fortran.
> 
> Thank you for any help,
> 
> Eric Small
> 
How about this script, which calculates mean, variance, and standard 
deviation?
------------cut--here---------------------------------------------------------
:
g.ask element=cell type=old\
 prompt="Raster map to be analyzed" unixfile=/tmp/$$
. /tmp/$$
rm -f /tmp/$$
if [ "$name" = "" ]
 then
  exit
fi
input="$fullname"
r.stats -za input="$input" | awk 'BEGIN {sum=0.0 ; sum2=0.0}
 NR==1 {min=$1 ; max=$1 ; mincat=$1 ; maxcat=$1 }
 {sum+=$1 ; sum2+=$1*$1 ; N++ }
 {
  if ($1>max) { max=$1 }
  if ($1<min) { min=$1 }
 }
END {
 print "Smallest category value is: ", min
 print "Largest category value is: ", max
 print "Mean category value is: ", sum/N
 print "Variance in categories is: ", (sum2-sum*sum/N)/N
 print "Standard Dev. of cats is: ", sqrt((sum2-sum*sum/N)/N)
}'

----------------cut--here-----------------------------------------------
Unfortunately, I'm not sure of the original source of this script, so I 
can't give proper credit for it. It's an example of the sort of Bourne 
shell scripting that we cover in our Advanced GRASS class (shameless 
advertising plug!! :-) ). Hope you find it useful.

---
Malcolm D. Williamson - GIS Specialist           E-mail: 
malcolm at cast.uark.edu
Center for Advanced Spatial Technologies      Telephone: (501) 575-6159
Ozark Rm. 12                                        Fax: (501) 575-5218 
University of Arkansas              
Fayetteville, AR 72701




More information about the grass-user mailing list