4
# Test cases for 2D raster data
7
# - generate 3x3 map, value 1/1.1
8
# - calculate statistics
9
# - compare with known results
15
if [ -z "$GISBASE" ] ; then
16
echo "You must be in GRASS GIS to run this program."
20
#### check if we have awk
21
if [ ! -x "`which awk`" ] ; then
22
echo "$PROG: awk required, please install first" 1>&2
26
# setting environment, so that awk works properly in all languages
31
: ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
32
LOCATION=$GISDBASE/$LOCATION_NAME/$MAPSET
37
# epsilon for doubles in IEEE is 2.220446e-16
38
EPSILON=22204460000000000
40
TMPNAME="`echo ${PID}_tmp_testmap | sed 's+\.+_+g'`"
42
# some functions - keep order here
45
echo "Removing temporary map"
46
g.remove -f type=raster name=$TMPNAME > /dev/null
49
# check if a MASK is already present:
51
USERMASK=usermask_${MASKTMP}
52
if test -f $LOCATION/cell/MASK
54
echo "A user raster mask (MASK) is present. Saving it..."
55
g.rename raster=MASK,$USERMASK > /dev/null
60
echo "Restoring user region"
61
g.region region=$TMPNAME
62
g.remove -f type=region name=$TMPNAME > /dev/null
63
#restore user mask if present:
64
if test -f $LOCATION/cell/$USERMASK ; then
65
echo "Restoring user MASK"
66
g.remove -f type=raster name=MASK > /dev/null
67
g.rename raster=$USERMASK,MASK > /dev/null
73
if [ $1 -ne 0 ] ; then
74
echo "An error occurred."
75
cleanup ; finalcleanup
80
########## test function goes here
88
if [ "$FOUND" = "nan" ] ; then
89
echo "ERROR. $VALUENAME: Expected=$EXPECTED | FOUND=$FOUND"
90
cleanup ; finalcleanup
94
# check for difference + 1
95
DIFF=`echo $EXPECTED $FOUND $EPSILON | awk '{printf "%16f", ($1 - $2) * $3 }'`
97
DIFF=`echo $DIFF | awk '{printf("%f", sqrt($1 * $1))}'`
99
DIFF=`echo $DIFF | awk '{printf("%20d", int($1+0.5))}'`
101
# check if difference > 0
102
if [ $DIFF -gt 0 ] ; then
103
echo "ERROR. $VALUENAME: Expected=$EXPECTED | FOUND=$FOUND"
104
cleanup ; finalcleanup
109
#check if a MASK is already present:
110
MASKTMP=mask.$TMPNAME
111
USERMASK=usermask_${MASKTMP}
112
if test -f $LOCATION/cell/MASK
114
echo "A user raster mask (MASK) is present. Saving it..."
115
g.rename raster=MASK,$USERMASK > /dev/null
119
echo "Saving current & setting test region."
120
g.region save=$TMPNAME
122
g.region s=0 n=$PIXEL w=0 e=$PIXEL res=1 tbres=1
125
########### 2D raster INT tests ###########
127
echo "INT/CELL test."
128
r.mapcalc "$TMPNAME = 1"
131
echo "Univariate statistics of INT/CELL test."
132
eval `r.univar -g $TMPNAME`
134
compare_result 9 $n n
135
compare_result $VALUE $min min
136
compare_result $VALUE $max max
137
compare_result 0 $range range
138
compare_result $VALUE $mean mean
139
compare_result 0 $stddev stddev
140
compare_result 0 $variance variance
141
compare_result 0 $coeff_var coeff_var
142
compare_result 9 $sum sum
145
echo "INT/CELL univariate statistics test successful"
146
echo "##################################"
148
########### 2D raster FCELL tests ###########
150
echo "FLOAT/FCELL test."
151
r.mapcalc "$TMPNAME = $VALUE"
154
echo "Univariate statistics of FLOAT/FCELL test."
155
eval `r.univar -g $TMPNAME`
157
compare_result 9 $n n
158
compare_result $VALUE $min min
159
compare_result $VALUE $max max
160
compare_result 0 $range range
161
compare_result $VALUE $mean mean
162
compare_result 0 $stddev stddev
163
compare_result 0 $variance variance
164
compare_result 0 $coeff_var coeff_var
165
compare_result 9.9 $sum sum
168
echo "FLOAT/FCELL univariate statistics test successful"
169
echo "##################################"
172
# if we arrive here...
175
echo "All tests successful. Congrats."