1

I updated my post after I resolved the previous errors, but additional errors were produced.

I want to create a controlling shell script that contains water.mapcalc within it. The problem is, I do not know how to save a code in a file called water.mapcalc. I'm trying out the hydrologic simulation.

It is stated in page 12: "For those wishing to repeat the experiment with GRASS enter the model code in a file called water.mapcalc then create a controlling shell script."

I understood the instruction as I should save the following in a raster map called water.mapcalc:

r.mapcalc water.mapcalc = "water = water + eval(x=elev + water,
    if(x>(y=elev[-1,0]+water[-1,0]),
        -.15*if(elev>y, water, x-y),
        .15*if(elev[-1,0]>x, water[-1,0], y-x))+

    if(x>(y=elev[1,0]+water[1,0]),
        -.15*if(elev>y, water, x-y),
        .15*if(elev[1,0]>x, water[1,0], y-x))+

    if(x>(y=elev[0,-1]+water[0,-1]),
        -.15*if(elev>y, water, x-y),
        .15*if(elev[0,-1]>x, water[0,-1], y-x))+

    if(x>(y=elev[0,1]+water[0,1]),
        -.15*if(elev>y, water, x-y),
        .15*if(elev[0,1]>x, water[0,1], y-x))+

    if(x>(y=elev[-1,1]+water[-1,1]),
        -.10*if(elev>y, water, x-y),
        .10*if(elev[-1,1]>x, water[-1,1], y-x))+

    if(x>(y=elev[1,1]+water[1,1]),
        -.10*if(elev>y, water, x-y),
        .10*if(elev[1,1]>x, water[1,1], y-x))+

    if(x>(y=elev[1,-1]+water[1,-1]),
        -.10*if(elev>y, water, x-y),
        .10*if(elev[1,-1]>x, water[1,-1], y-x))+

    if(x>(y=elev[-1,-1]+water[-1,-1]),
        -.10*if(elev>y, water, x-y),
        .10*if(elev[-1,-1]>x, water[-1,-1], y-x)))"

Should I save the model code in a water.mapcalc raster map as what I did above? or should I save a water.mapcalc file in the /bin folder? I tried both but it still produced errors.

I already did the said map (water.mapcalc). and I created the following shell controlling script that needs the file (water.mapcalc). This is the shell script (myscript5.sh) and I saved it in the \bin folder of GRASS:

#!/bin/sh
export GRASS_PNGFILE=/tmp/grass6output.png

r.mapcalc water = 120
d.rast water

r.mapcalc < water.mapcalc
d.rast water

r.mapcalc < water.mapcalc
d.rast water

g.copy rast=water,water.1

r.mapcalc < water.mapcalc
d.rast water

r.mapcalc < water.mapcalc
d.rast water

g.copy rast=water,water.2

Then, I run the sh myscript5.sh in the CMD of GRASS and it produced this message:

PNG: GRASS_TRUECOLOR status: TRUE
PNG: collecting to file: C:/Users/Rhen/AppData/Local/Temp/grass6output.png,
GRASS_WIDTH=768, GRASS_HEIGHT=508
/c/Program Files/GRASS-64/bin/myscript5.sh: line 7:
water.mapcalc: No such file or directory
PNG: GRASS_TRUECOLOR status: TRUE
PNG: collecting to file: C:/Users/Rhen/AppData/Local/Temp/grass6output.png,
GRASS_WIDTH=768, GRASS_HEIGHT=508
/c/Program Files/GRASS-64/bin/myscript5.sh: line 10:
water.mapcalc: No such file or directory
PNG: GRASS_TRUECOLOR status: TRUE
PNG: collecting to file: C:/Users/Rhen/AppData/Local/Temp/grass6output.png,
GRASS_WIDTH=768, GRASS_HEIGHT=508
Copy raster <water@PERMANENT> to current mapset as <water.1>
/c/Program Files/GRASS-64/bin/myscript5.sh: line 15:
water.mapcalc: No such file or directory
PNG: GRASS_TRUECOLOR status: TRUE
PNG: collecting to file: C:/Users/Rhen/AppData/Local/Temp/grass6output.png,
GRASS_WIDTH=768, GRASS_HEIGHT=508
/c/Program Files/GRASS-64/bin/myscript5.sh: line 18:
water.mapcalc: No such file or directory
PNG: GRASS_TRUECOLOR status: TRUE
PNG: collecting to file: C:/Users/Rhen/AppData/Local/Temp/grass6output.png,
GRASS_WIDTH=768, GRASS_HEIGHT=508
Copy raster <water@PERMANENT> to current mapset as <water.2>

the error is: water.mapcalc: No such file or directory. I think there is a problem on how I saved the water.mapcalc file. I do not know what format (raster/.txt/.sh file) and where will I save it (should I also save it in the /bin folder together with my script). I tried all of those and it still did not work. I cannot figure out where the problem is.

11
  • 1
    You want to write a shell script that calls x.mapcalc?
    – underdark
    Commented Feb 19, 2011 at 20:12
  • 1
    x.mapcalc isn't a grass command, do you want to save the output of a mapcalc command? Syntax may have changed since that document was published (1992).
    – scw
    Commented Feb 19, 2011 at 22:05
  • 1
    The code posting contains a series of "funny" characters which are perhaps caused by stackexchange? Let me suggest to also post it at osgeo.pastebin.com for easier editing.
    – markusN
    Commented Feb 20, 2011 at 15:41
  • 1
    Additionally: n='expr$n+1' is lacking white space and has wrong backticks (n=expr $n + 1), also while[!=100] isn't correct (white space) etc. Again, pastebin or whatever would be handy so that corrections can be made (maybe post the final version eventually here).
    – markusN
    Commented Feb 20, 2011 at 15:44
  • 2
    From this user's other question, the code may well have been copied & pasted from a PDF -- this would explain the syntax mangling.
    – Dan S.
    Commented Feb 20, 2011 at 18:34

1 Answer 1

1

Here the "water.mapcalc" script, called :

water = water + eval(x = elev + water, \
if (x > (y = elev[-1, 0] + water[-1, 0]), \
   -.15 * if (elev > y, water, x - y), \
   .15 * if (elev[-1, 0] > x, water[-1, 0], y - x))+ \
 if (x > (y = elev[1, 0] + water[1, 0]), \
   -.15 * if (elev > y, water, x - y), \
   .15 * if (elev[1, 0] > x, water[1, 0], y - x))+ \
 if (x > (y = elev[0, -1] + water[0, -1]), \
   -.15 * if (elev > y, water, x - y), \
   .15 * if (elev[0, -1] > x, water[0, -1], y - x))+ \
 if (x > (y = elev[0, 1] + water[0, 1]), \
   -.15 * if (elev > y, water, x - y), \
   .15 * if (elev[0, 1] > x, water[0, 1], y - x))+ \
 if (x > (y = elev[-1, 1] + water[-1, 1]), \
   -.10 * if (elev > y, water, x - y), \
   .10 * if (elev[-1, 1] > x, water[-1, 1], y - x))+ \
 if (x > (y = elev[1, 1] + water[1, 1]), \
   -.10 * if (elev > y, water, x - y), \
   .10 * if (elev[1, 1] > x, water[1, 1], y - x))+ \
 if (x > (y = elev[1, -1] + water[1, -1]), \
   -.10 * if (elev > y, water, x - y), \
   .10 * if (elev[1, -1] > x, water[1, -1], y - x))+ \
 if (x > (y = elev[-1, -1] + water[-1, -1]), \
   -.10 * if (elev > y, water, x - y), \
   .10 * if (elev[-1, -1] > x, water[-1, -1], y - x)))

and here the launch script (Spearfish data set: http://grass.osgeo.org/download/data.php)

#!/bin/sh

g.region rast=elevation.dem -p

r.mapcalc "water = 120" #12 inches of water in each cell

g.copy rast=elevation.dem,elev

d.mon x0
d.rast water
i=1

while [ $i != 100 ]
do
  n=1
  while [ $n != 10 ]
  do
    r.mapcalc < water.mapcalc # run the simulation
    d.rast water
    n=`expr $n + 1`
  done
  g.copy rast=water,water.$i
  i=`expr $i + 1`
done

hope this helps.

5
  • the water.mapcalc is saved as what format? and where is it saved?
    – teamAngat
    Commented Feb 21, 2011 at 15:32
  • I tried running it, but it still produced the error: /c/Program Files/GRASS-64/bin/s1.sh: line 17: water.mapcalc: No such file or directory
    – teamAngat
    Commented Feb 21, 2011 at 16:02
  • 1
    Did you run the script in the directory where "water.mapcalc" is stored? If not you need to specify the path to the file or (easier) run all from the same directory.
    – markusN
    Commented Feb 22, 2011 at 7:25
  • 1
    Save "water.mapcalc" as ASCII text file.
    – markusN
    Commented Feb 22, 2011 at 7:26
  • thanks. I tried the code with the Spearfish data set. The result of the code is that the map (elevation.dem) is getting smaller every succeeding iteration. I cannot figure out why it is happening. Isn't it that the result for every iteration should just be the transfer of excess height and not the cropping of the edges of the map?
    – teamAngat
    Commented Feb 28, 2011 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.