rmapcalc_test.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/sh
  2. # Markus Neteler
  3. # Test cases for 2D raster data
  4. # Tests:
  5. # - generate 3x3 map, value 1/1.1
  6. # - calculate statistics
  7. # - compare with known results
  8. #
  9. # TODO
  10. # - how big EPSILON?
  11. if [ -z "$GISBASE" ] ; then
  12. echo "You must be in GRASS GIS to run this program."
  13. exit 1
  14. fi
  15. #### check if we have awk
  16. if [ ! -x "`which awk`" ] ; then
  17. echo "$PROG: awk required, please install first" 1>&2
  18. exit 1
  19. fi
  20. # setting environment, so that awk works properly in all languages
  21. unset LC_ALL
  22. export LC_NUMERIC=C
  23. eval `g.gisenv`
  24. : ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
  25. LOCATION=$GISDBASE/$LOCATION_NAME/$MAPSET
  26. # some definitions
  27. PIXEL=3
  28. # how big EPSILON?
  29. # epsilon for doubles in IEEE is 2.220446e-16
  30. EPSILON=22204460000000000
  31. PID=$$
  32. TMPNAME="`echo ${PID}_tmp_testmap | sed 's+\.+_+g'`"
  33. # some functions - keep order here
  34. cleanup()
  35. {
  36. echo "Removing temporary map"
  37. g.remove -f type=raster name=$TMPNAME > /dev/null
  38. }
  39. # check if a MASK is already present:
  40. MASKTMP=mask.$TMPNAME
  41. USERMASK=usermask_${MASKTMP}
  42. if test -f $LOCATION/cell/MASK
  43. then
  44. echo "A user raster mask (MASK) is present. Saving it..."
  45. g.rename MASK,$USERMASK > /dev/null
  46. fi
  47. finalcleanup()
  48. {
  49. echo "Restoring user region"
  50. g.region region=$TMPNAME
  51. g.remove -f type=region name=$TMPNAME > /dev/null
  52. #restore user mask if present:
  53. if test -f $LOCATION/cell/$USERMASK ; then
  54. echo "Restoring user MASK"
  55. g.remove -f type=raster name=MASK > /dev/null
  56. g.rename $USERMASK,MASK > /dev/null
  57. fi
  58. }
  59. check_exit_status()
  60. {
  61. if [ $1 -ne 0 ] ; then
  62. echo "An error occurred."
  63. cleanup ; finalcleanup
  64. exit 1
  65. fi
  66. }
  67. ########## test function goes here
  68. compare_result()
  69. {
  70. EXPECTED=$1
  71. FOUND=$2
  72. VALUENAME=$3
  73. # test for NAN
  74. if [ "$FOUND" = "nan" ] ; then
  75. echo "ERROR. $VALUENAME: Expected=$EXPECTED | FOUND=$FOUND"
  76. cleanup ; finalcleanup
  77. exit 1
  78. fi
  79. # check for difference + 1
  80. DIFF=`echo $EXPECTED $FOUND $EPSILON | awk '{printf "%16f", ($1 - $2) * $3 }'`
  81. #make absolute value
  82. DIFF=`echo $DIFF | awk '{printf("%f", sqrt($1 * $1))}'`
  83. #round to integer
  84. DIFF=`echo $DIFF | awk '{printf("%20d", int($1+0.5))}'`
  85. # check if difference > 0
  86. if [ $DIFF -gt 0 ] ; then
  87. echo "ERROR. $VALUENAME: Expected=$EXPECTED | FOUND=$FOUND"
  88. cleanup ; finalcleanup
  89. exit 1
  90. fi
  91. }
  92. #check if a MASK is already present:
  93. MASKTMP=mask.$TMPNAME
  94. USERMASK=usermask_${MASKTMP}
  95. if test -f $LOCATION/cell/MASK
  96. then
  97. echo "A user raster mask (MASK) is present. Saving it..."
  98. g.rename MASK,$USERMASK > /dev/null
  99. check_exit_status $?
  100. fi
  101. echo "Saving current & setting test region."
  102. g.region save=$TMPNAME
  103. check_exit_status $?
  104. g.region s=0 n=$PIXEL w=0 e=$PIXEL res=1 tbres=1
  105. check_exit_status $?
  106. ########### 2D raster INT tests ###########
  107. VALUE=1
  108. echo "INT/CELL test."
  109. r.mapcalc "$TMPNAME = 1"
  110. check_exit_status $?
  111. echo "Univariate statistics of INT/CELL test."
  112. eval `r.univar -g $TMPNAME`
  113. check_exit_status $?
  114. compare_result 9 $n n
  115. compare_result $VALUE $min min
  116. compare_result $VALUE $max max
  117. compare_result 0 $range range
  118. compare_result $VALUE $mean mean
  119. compare_result 0 $stddev stddev
  120. compare_result 0 $variance variance
  121. compare_result 0 $coeff_var coeff_var
  122. compare_result 9 $sum sum
  123. cleanup
  124. echo "INT/CELL univariate statistics test successful"
  125. echo "##################################"
  126. ########### 2D raster FCELL tests ###########
  127. VALUE=1.1
  128. echo "FLOAT/FCELL test."
  129. r.mapcalc "$TMPNAME = $VALUE"
  130. check_exit_status $?
  131. echo "Univariate statistics of FLOAT/FCELL test."
  132. eval `r.univar -g $TMPNAME`
  133. check_exit_status $?
  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.9 $sum sum
  143. cleanup
  144. echo "FLOAT/FCELL univariate statistics test successful"
  145. echo "##################################"
  146. ###########
  147. # if we arrive here...
  148. finalcleanup
  149. echo "All tests successful. Congrats."
  150. exit 0