[GRASS-user] Grass, Python, mapcalc

S. Koukoulas (lists) sotkouklistes at gmail.com
Fri Dec 28 05:26:07 PST 2012

First, Daniel's example worked for me - (I wasn't aware of the changes
in r.mapcalc and in a similar problem I made use of older posts to solve
my problems).

Now, if you want to go the other way then you must make sure that
globalx changes to global2, global3 etc. So your problem should be in
this line:
exp = 'out=sun+global' + str(x)

I am not using python, but you should only join the strings of 'global'
and str(x).  e.g. 'global'+str(x) should become one everytime. You could
do this before calling mapcalc e.g. g='global'+str(x)
exp = 'out=sun+g' OR just check the use of brackets and quotes in the
"exp=..." line

On 12/28/2012 03:08 PM, Paul Meems wrote:
> Thanks Sotiris,
> I've tried your suggestion but converted it to Python because I'm on
> Windows.
> This is my script:
> grass.run_command('g.mremove', rast = ('out*'))
> grass.mapcalc('sun=global1', quiet=False, verbose=False, overwrite=True)
> for x in range(2, 10, 1):
>   exp = 'out=sun+global' + str(x)
>   print exp
>   grass.mapcalc(exp, quiet=False, verbose=False, overwrite=True)
>   grass.run_command('g.rename', flag='o', rast = ('out','sun'))
> This doesn't result in the expected outcome.
> sun is equal to global1 and no values are added
> out is global1+global2 but is no more values are added. But mapcalc is
> called several times.
> I've tested this with:
> r.what --v -f -n input=global1 east_north=156287.225281,383024.737026
> r.what --v -f -n input=sun east_north=156287.225281,383024.737026
> r.what --v -f -n input=out east_north=156287.225281,383024.737026
> Not only am I a new user to GRASS but I'm also still a novice in
> Python. Most likely it is something in my script.
> If somebody has a different approach to get the solar radiation of a
> whole year I'm happy to try that.
> Thanks,
> Paul
>     Date: Fri, 28 Dec 2012 14:36:15 +0200
>     From: "S. Koukoulas (lists)" <sotkouklistes at gmail.com
>     <mailto:sotkouklistes at gmail.com>>
>     To: grass-user at lists.osgeo.org <mailto:grass-user at lists.osgeo.org>
>     Subject: Re: [GRASS-user] Grass, Python, mapcalc
>     Message-ID: <50DD923F.806 at gmail.com <mailto:50DD923F.806 at gmail.com>>
>     Content-Type: text/plain; charset=windows-1252
>     To my experience with r.mapcalc you cannot do what yo do with
>     programming languages... e.g. i=i+1 will not work directly.
>     Instead you
>     could try a more indirect way, something like (here is an example with
>     bash shell script):
>     #g.mremove rast=out* (to remove previous stuff, if needed)
>     r.mapcalc sun=global1
>     for a in ?seq 2 10?; do
>     r.mapcalc out = sun + global$a
>     g.rename --o rast=out,sun
>     done
>     basically the main idea is to use an intermediate map and then before
>     the loop ends, pass it (with g.rename) to the map variable you want.
>     There might be a more elegant way with python, but hopefully this
>     shell
>     script example might help you.
>     HTH,
>     sotiris
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user

More information about the grass-user mailing list