shell script

Chris C Rewerts rewerts at ecn.purdue.edu
Sat Aug 1 00:54:29 EDT 1992


I wanted to know the standard deviation of the cell values of a raster map
and didn't see a way to get it from the current tools, so I made a 
shell script to do it using r.stats and awk. I didn't follow the rules
by writing it in 'sh' (it is 'csh'), but it is easily converted.
(the whole thing boils down to a one line command, really)
Anyway, I thought I would share it to the mailing list to mix in with
all the subscribe, unsubscribe, and test messages we've all been getting
biffed with ;-}.

--Chris Rewerts, Agricultural Engineering, Purdue University   

So here it is, enjoy:

--------------------------cut here----------8<-------------------------
#!/bin/csh
# given a name of an raster map, compute
# number of cells, average, max, min, std dev

if ($?GISRC == 0) then
   echo ""
   echo "You must be running grass 4.0 to use this program"
   echo ""
   exit
endif

if ( $#argv < 1) then
        echo Usage: `basename $0` 'mapname'
        exit 1
endif

echo ""
echo "working..."
echo ""

r.stats -1q $1 | awk 'BEGIN {min=999999}\
{x[NR] = $1; total = x[NR] + total; \
if (x[NR] > max) max = x[NR]; if (x[NR] < min) min = x[NR]}  \
END { average = total/NR;\
for (i = 1; i <= NR; i++)\
vari += ((average - x[i]) * (average - x[i]))\
printf "cells\t%d\naverage\t%.2f\nmaximum\t%.0f\nminimun\t%.0f\nstd dev\t%.4f\n", NR, total/NR, max, min, sqrt(vari/(NR-1))}' 

echo ""



More information about the grass-user mailing list