raster_md5test.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 md5sum
  7. # - compare with known results
  8. if [ -z "$GISBASE" ] ; then
  9. echo "You must be in GRASS GIS to run this program."
  10. exit 1
  11. fi
  12. #### check if we have sed
  13. if [ ! -x "`which sed`" ] ; then
  14. echo "$PROG: sed required, please install first" 1>&2
  15. exit 1
  16. fi
  17. #### check if we have md5sum
  18. if [ ! -x "`which md5sum`" ] ; then
  19. echo "$PROG: md5sum required, please install first" 1>&2
  20. exit 1
  21. fi
  22. #### check if we have cut
  23. if [ ! -x "`which cut`" ] ; then
  24. echo "$PROG: cut required, please install first" 1>&2
  25. exit 1
  26. fi
  27. # setting environment, so that awk works properly in all languages
  28. unset LC_ALL
  29. export LC_NUMERIC=C
  30. eval `g.gisenv`
  31. : ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
  32. LOCATION=$GISDBASE/$LOCATION_NAME/$MAPSET
  33. # some definitions
  34. PIXEL=3
  35. PID=$$
  36. TMPNAME="`echo ${PID}_tmp_testmap | sed 's+\.+_+g'`"
  37. # some functions - keep order here
  38. cleanup()
  39. {
  40. echo "Removing temporary map"
  41. g.remove -f type=raster name=$TMPNAME > /dev/null
  42. }
  43. # check if a MASK is already present:
  44. MASKTMP=mask.$TMPNAME
  45. USERMASK=usermask_${MASKTMP}
  46. if test -f $LOCATION/cell/MASK
  47. then
  48. echo "A user raster mask (MASK) is present. Saving it..."
  49. g.rename raster=MASK,$USERMASK > /dev/null
  50. fi
  51. finalcleanup()
  52. {
  53. echo "Restoring user region"
  54. g.region region=$TMPNAME
  55. g.remove -f type=region name=$TMPNAME > /dev/null
  56. #restore user mask if present:
  57. if test -f $LOCATION/cell/$USERMASK ; then
  58. echo "Restoring user MASK"
  59. g.remove -f type=raster name=MASK > /dev/null
  60. g.rename raster=$USERMASK,MASK > /dev/null
  61. fi
  62. }
  63. check_exit_status()
  64. {
  65. if [ $1 -ne 0 ] ; then
  66. echo "An error occurred."
  67. cleanup ; finalcleanup
  68. exit 1
  69. fi
  70. }
  71. ########## test function goes here
  72. check_md5sum()
  73. {
  74. EXPECTED="$1"
  75. FOUND="$2"
  76. # test for NAN
  77. if [ "$FOUND" = "nan" ] ; then
  78. echo "ERROR. $VALUENAME: Expected=$EXPECTED | FOUND=$FOUND"
  79. cleanup ; finalcleanup
  80. exit 1
  81. fi
  82. if [ "$EXPECTED" != "$FOUND" ] ; then
  83. echo "ERROR. The md5sum differs."
  84. cleanup ; finalcleanup
  85. exit 1
  86. fi
  87. }
  88. echo "Saving current & setting test region."
  89. g.region save=$TMPNAME
  90. check_exit_status $?
  91. g.region s=0 n=$PIXEL w=0 e=$PIXEL res=1 tbres=1
  92. check_exit_status $?
  93. ########### 2D raster INT tests ###########
  94. VALUE=1
  95. echo "INT/CELL md5sum test."
  96. r.mapcalc "$TMPNAME = 1"
  97. check_exit_status $?
  98. echo "MD5 checksum on output of INT/CELL test."
  99. MD5="`r.out.ascii $TMPNAME dp=15 | md5sum | cut -d' ' -f1`"
  100. check_md5sum "549e7dabe70df893803690571d2e1503" "$MD5"
  101. cleanup
  102. echo "INT/CELL md5sum test successful"
  103. echo "##################################"
  104. ########### 2D raster FCELL tests ###########
  105. VALUE=1.1
  106. echo "FLOAT/FCELL md5sum test."
  107. r.mapcalc "$TMPNAME = $VALUE"
  108. check_exit_status $?
  109. echo "MD5 checksum on output of FLOAT/FCELL test."
  110. MD5="`r.out.ascii $TMPNAME dp=15 | md5sum | cut -d' ' -f1`"
  111. check_md5sum "379f3d880b6d509051af6b4ccf470762" "$MD5"
  112. cleanup
  113. echo "FLOAT/FCELL md5sum test successful"
  114. echo "##################################"
  115. ###########
  116. # if we arrive here...
  117. finalcleanup
  118. echo "All tests successful. Congrats."
  119. exit 0