r3.mapcalculator 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/sh
  2. ############################################################################
  3. #
  4. # MODULE: r3.mapcalculator v.1.0 for GRASS 5.7 (2004/08/16)
  5. # based on r.mapcalculator for GRASS 5.7
  6. # AUTHOR(S): R. Brunzema (r.brunzema@web.de)
  7. # Michael Barton (michael.barton@asu.edu)
  8. # PURPOSE: Provides GUI front-end to r3.mapcalc
  9. # COPYRIGHT: (C) 2004 by the GRASS Development Team
  10. #
  11. # This program is free software under the GNU General Public
  12. # License (>=v2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. #
  15. #############################################################################
  16. #%Module
  17. #% description: Calculates new grid3D volume from r3.mapcalc expression.
  18. #%End
  19. #%option
  20. #% key: agrid
  21. #% type: string
  22. #% description: A (grid3D file)
  23. #% required : no
  24. #%end
  25. #%option
  26. #% key: bgrid
  27. #% type: string
  28. #% description: B (grid3D file)
  29. #% required : no
  30. #%end
  31. #%option
  32. #% key: cgrid
  33. #% type: string
  34. #% description: C (grid3D file)
  35. #% required : no
  36. #%end
  37. #%option
  38. #% key: dgrid
  39. #% type: string
  40. #% description: D (grid3D file)
  41. #% required : no
  42. #%end
  43. #%option
  44. #% key: egrid
  45. #% type: string
  46. #% description: E (grid3D file)
  47. #% required : no
  48. #%end
  49. #%option
  50. #% key: fgrid
  51. #% type: string
  52. #% description: F (grid3D file)
  53. #% required : no
  54. #%end
  55. #%option
  56. #% key: formula
  57. #% type: string
  58. #% description: Formula (e.g. A-B or A*C+B)
  59. #% required : yes
  60. #%end
  61. #%option
  62. #% key: outfile
  63. #% type: string
  64. #% description: Name for output grid3D volume
  65. #% required : yes
  66. #%end
  67. #%option
  68. #% key: help
  69. #% type: string
  70. #% description: Show help
  71. #% options: -,help,man
  72. #% answer: -
  73. #% required : no
  74. #%end
  75. #%flag
  76. #% key: e
  77. #% description: Expert mode (enter a set of r3.mapcalc expressions)
  78. #%end
  79. #%flag
  80. #% key: o
  81. #% description: Do not overwrite existing map
  82. #%end
  83. display_help(){
  84. echo "" >&2
  85. echo "BRIEF HELP" >&2
  86. echo "" >&2
  87. echo "Enter an r3.mapcalc expression in the" >&2
  88. echo "formula field using this format:" >&2
  89. echo "A+C or (more complex:) exp(A+C)+(B-2)*7" >&2
  90. echo "Where A, B, C are grid3D volumes entered" >&2
  91. echo "in the A field, B field, and C field." >&2
  92. echo "" >&2
  93. echo "Do not enter output file in the formula field:" >&2
  94. echo "Correct: A+B" >&2
  95. echo "Incorrect: newfile = A+B" >&2
  96. echo "Use no blanks!" >&2
  97. echo "" >&2
  98. echo "For details on creating an r3.mapcalc expression," >&2
  99. echo "select 'man' in the help field to see the r3.mapcalc manual page" >&2
  100. echo "(or type g.manual r3.mapcalc from the command line)." >&2
  101. }
  102. if [ -z "$GISBASE" ]
  103. then
  104. echo "You must be in GRASS GIS to run this program" >&2
  105. exit 1
  106. fi
  107. if [ "$1" != "@ARGS_PARSED@" ]
  108. then
  109. exec g.parser "$0" "$@"
  110. fi
  111. case $GIS_OPT_HELP in
  112. help)
  113. display_help
  114. exit 0
  115. ;;
  116. man)
  117. g.manual r3.mapcalc
  118. exit 0
  119. ;;
  120. esac
  121. # use old style mapcalc
  122. if [ "$GIS_FLAG_E" = 1 ] ; then
  123. exec "$GISBASE/etc/grass-xterm-wrapper" -e r3.mapcalc &
  124. exit 0
  125. fi
  126. # Check for required arguments
  127. if [ -z "$GIS_OPT_FORMULA" ]; then
  128. g.message -e "Missing formula."
  129. g.message -e "Please enter a formula in the field formula"
  130. exit 1
  131. fi
  132. if [ -z "$GIS_OPT_OUTFILE" ]; then
  133. g.message -e "Missing name of outputfile."
  134. g.message -e "Please enter a name for the resulting map and try again."
  135. exit 1
  136. elif [ "$GIS_FLAG_O" = 1 ]; then
  137. echo `g.list type=rast | grep -w "$GIS_OPT_OUTFILE" `
  138. outtest=`g.list type=rast | grep -w $GIS_OPT_OUTFILE`
  139. if [ -n "$outtest" ]; then
  140. g.message -e "File $GIS_OPT_OUTFILE exists. Exiting."
  141. exit 0
  142. fi
  143. fi
  144. # replace grid names: two passes are needed to make sure that sed parses all
  145. # grid names. e.g., A+B => agrid+B => agrid+bgrid
  146. command=`
  147. echo "(${GIS_OPT_FORMULA})" | sed \
  148. -e "s/\([^a-zA-Z0-9]\)A\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_AGRID\" \2/g" \
  149. -e "s/\([^a-zA-Z0-9]\)A\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_AGRID\" \2/g" \
  150. -e "s/\([^a-zA-Z0-9]\)B\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_BGRID\" \2/g" \
  151. -e "s/\([^a-zA-Z0-9]\)B\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_BGRID\" \2/g" \
  152. -e "s/\([^a-zA-Z0-9]\)C\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_CGRID\" \2/g" \
  153. -e "s/\([^a-zA-Z0-9]\)C\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_CGRID\" \2/g" \
  154. -e "s/\([^a-zA-Z0-9]\)D\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_DGRID\" \2/g" \
  155. -e "s/\([^a-zA-Z0-9]\)D\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_DGRID\" \2/g" \
  156. -e "s/\([^a-zA-Z0-9]\)E\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_EGRID\" \2/g" \
  157. -e "s/\([^a-zA-Z0-9]\)E\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_EGRID\" \2/g" \
  158. -e "s/\([^a-zA-Z0-9]\)F\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_FGRID\" \2/g" \
  159. -e "s/\([^a-zA-Z0-9]\)F\([^a-zA-Z0-9]\)/\1 \"$GIS_OPT_FGRID\" \2/g"
  160. `
  161. # Show the resulting commandline
  162. g.message message="r3.mapcalc \"$GIS_OPT_OUTFILE\" = \"$command\""
  163. r3.mapcalc "$GIS_OPT_OUTFILE" = "$command" # Start the command
  164. # Check for errors
  165. if [ $? -ne 0 ]; then
  166. g.message -e "Calculating $GIS_OPT_OUTFILE. Try expert mode."
  167. exit 1
  168. fi
  169. g.message "Done."
  170. exit 0