[GRASS-user] map calculator in python loop

dear all,
i’m dealing this time with r.mapcalc. I have different rasters which i grouped using g.list.
Now r_list contains all my raster. I want to multiply those rasters to another raster (called “costante”) in order to obtain a new map, that i want to call as r followed by the letter “k”
Here it is what i’ve done:
import grass.script as gscript

r_list = gscript.read_command(“g.list” , type = “rast” ).strip().split(“\n”)
c = " costante"
for r in r_list:
gscript.mapcalc ( " %s = %s * %s " % ((r+“k”) , r , c))

i get the following traceback message:

Invalid map <1>
parse error
ERROR: parse error
ERROR: An error occurred while running r.mapcalc
I don’t really know where i’m wrong!

Hi Alessandro,
actually, I have no experience and no idea of the scripting modul of grass. But I believe you need something like this:

script.core.list_strings(type, pattern=None, mapset=None, exclude=None, flag='')[source]

    List of elements as strings.

    Returns the output from running g.list, as a list of qualified names.
    Parameters:

        type (str) – element type (raster, vector, raster_3d, region, ...)
        pattern (str) – pattern string
        mapset (str) – mapset name (if not given use search path)
        exclude (str) – pattern string to exclude maps from the research
        flag (str) – pattern type: ‘r’ (basic regexp), ‘e’ (extended regexp), or ‘’ (glob pattern)

    Returns:

    list of elements

You find more here:
https://grass.osgeo.org/grass72/manuals/libpython/script.html?highlight=list_strings#script.core.list_strings

regards

Stefan

Alessandro Sebastiani <alessandro.sebastiani@uniroma1.it> hat am 12. Dezember 2018 um 12:59 geschrieben:

dear all,
i'm dealing this time with r.mapcalc. I have different rasters which i
grouped using g.list.
Now r_list contains all my raster. I want to multiply those rasters to
another raster (called "costante") in order to obtain a new map, that i
want to call as r followed by the letter "k"
Here it is what i've done:
import grass.script as gscript
>>> r_list = gscript.read_command("g.list" , type = "rast"
).strip().split("\n")
>>> c = " costante"
>>> for r in r_list:
>>> gscript.mapcalc ( " %s = %s * %s " % ((r+"k") , r , c))

i get the following traceback message:

Invalid map <1>
parse error
ERROR: parse error
ERROR: An error occurred while running r.mapcalc
I don't really know where i'm wrong!
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

On 12/12/18 12:59, Alessandro Sebastiani wrote:

dear all,
i'm dealing this time with r.mapcalc. I have different rasters which i grouped using g.list.
Now r_list contains all my raster. I want to multiply those rasters to another raster (called "costante") in order to obtain a new map, that i want to call as r followed by the letter "k"
Here it is what i've done:
import grass.script as gscript
>>> r_list = gscript.read_command("g.list" , type = "rast" ).strip().split("\n")
>>> c = " costante"
>>> for r in r_list:
>>> gscript.mapcalc ( " %s = %s * %s " % ((r+"k") , r , c))

Try

" %sk = %s * %s " % (r , r , c) "

Moritz

I get the same error after trying with your suggestion! I tried to define rasters in different ways, like

c = [“costante”] ,

but nothing seems to change

Il giorno mer 12 dic 2018 alle ore 10:22 Moritz Lennert <mlennert@club.worldonline.be> ha scritto:

On 12/12/18 12:59, Alessandro Sebastiani wrote:

dear all,
i’m dealing this time with r.mapcalc. I have different rasters which i
grouped using g.list.
Now r_list contains all my raster. I want to multiply those rasters to
another raster (called “costante”) in order to obtain a new map, that i
want to call as r followed by the letter “k”
Here it is what i’ve done:
import grass.script as gscript

r_list = gscript.read_command(“g.list” , type = “rast”
).strip().split(“\n”)
c = " costante"
for r in r_list:
gscript.mapcalc ( " %s = %s * %s " % ((r+“k”) , r , c))

Try

" %sk = %s * %s " % (r , r , c) "

Moritz

Hi,

What do you want to do with "k" ?

Can you send what return gscript.verbose(( " %s = %s * %s ") % (r , r , c))

Frank
Le 12/12/2018 à 17:34, Alessandro Sebastiani a écrit :

I get the same error after trying with your suggestion! I tried to define rasters in different ways, like
>> c = ["costante"] ,
but nothing seems to change

Il giorno mer 12 dic 2018 alle ore 10:22 Moritz Lennert <mlennert@club.worldonline.be <mailto:mlennert@club.worldonline.be>> ha scritto:

    On 12/12/18 12:59, Alessandro Sebastiani wrote:
    > dear all,
    > i'm dealing this time with r.mapcalc. I have different rasters
    which i
    > grouped using g.list.
    > Now r_list contains all my raster. I want to multiply those
    rasters to
    > another raster (called "costante") in order to obtain a new
    map, that i
    > want to call as r followed by the letter "k"
    > Here it is what i've done:
    > import grass.script as gscript
    > >>> r_list = gscript.read_command("g.list" , type = "rast"
    > ).strip().split("\n")
    > >>> c = " costante"
    > >>> for r in r_list:
    > >>> gscript.mapcalc ( " %s = %s * %s " % ((r+"k") , r , c))

    Try

    " %sk = %s * %s " % (r , r , c) "

    Moritz

_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

dear all,

i found where the error was! i didn’t set the “g.list” command properly, in fact the map calculator could not find maps . However , here it is how i ran the mapcalc:

gscript.mapcalc ( " %s = %s * %s " % ((r+“k”) , r , c))

to Frank : k is just a suffix i wanted to use in order to give to the new map a name similar to the older map (r in this case)

Il giorno mer 12 dic 2018 alle ore 13:01 Frank David <frank.david@geophom.fr> ha scritto:

Hi,

What do you want to do with “k” ?

Can you send what return gscript.verbose(( " %s = %s * %s ") % (r , r , c))

Frank
Le 12/12/2018 à 17:34, Alessandro Sebastiani a écrit :

I get the same error after trying with your suggestion! I tried to
define rasters in different ways, like

c = [“costante”] ,
but nothing seems to change

Il giorno mer 12 dic 2018 alle ore 10:22 Moritz Lennert
<mlennert@club.worldonline.be mailto:[mlennert@club.worldonline.be](mailto:mlennert@club.worldonline.be)>
ha scritto:

On 12/12/18 12:59, Alessandro Sebastiani wrote:

dear all,
i’m dealing this time with r.mapcalc. I have different rasters
which i
grouped using g.list.
Now r_list contains all my raster. I want to multiply those
rasters to
another raster (called “costante”) in order to obtain a new
map, that i
want to call as r followed by the letter “k”
Here it is what i’ve done:
import grass.script as gscript

r_list = gscript.read_command(“g.list” , type = “rast”
).strip().split(“\n”)
c = " costante"
for r in r_list:
gscript.mapcalc ( " %s = %s * %s " % ((r+“k”) , r , c))

Try

" %sk = %s * %s " % (r , r , c) "

Moritz


grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user


grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

Hi Alessandro,
it would be great, if you also comunicate the actual solution, i.e. the proper "g.list"-command, or where you made the mistake. The mapcalc-call seems not to bee what caused the troubles. If someone runs into the same trouble as you, he would have the right hint immediately.

regards

Stefan

Alessandro Sebastiani <alessandro.sebastiani@uniroma1.it> hat am 13. Dezember 2018 um 09:15 geschrieben:

dear all,
i found where the error was! i didn't set the "g.list" command properly, in
fact the map calculator could not find maps . However , here it is how i
ran the mapcalc:
> gscript.mapcalc ( " %s = %s * %s " % ((r+"k") , r , c))
to Frank : k is just a suffix i wanted to use in order to give to the new
map a name similar to the older map (r in this case)

Il giorno mer 12 dic 2018 alle ore 13:01 Frank David <frank.david@geophom.fr>
ha scritto:

> Hi,
>
> What do you want to do with "k" ?
>
> Can you send what return gscript.verbose(( " %s = %s * %s ") % (r , r ,
> c))
>
> Frank
> Le 12/12/2018 à 17:34, Alessandro Sebastiani a écrit :
> > I get the same error after trying with your suggestion! I tried to
> > define rasters in different ways, like
> > >> c = ["costante"] ,
> > but nothing seems to change
> >
> > Il giorno mer 12 dic 2018 alle ore 10:22 Moritz Lennert
> > <mlennert@club.worldonline.be <mailto:mlennert@club.worldonline.be>>
> > ha scritto:
> >
> > On 12/12/18 12:59, Alessandro Sebastiani wrote:
> > > dear all,
> > > i'm dealing this time with r.mapcalc. I have different rasters
> > which i
> > > grouped using g.list.
> > > Now r_list contains all my raster. I want to multiply those
> > rasters to
> > > another raster (called "costante") in order to obtain a new
> > map, that i
> > > want to call as r followed by the letter "k"
> > > Here it is what i've done:
> > > import grass.script as gscript
> > > >>> r_list = gscript.read_command("g.list" , type = "rast"
> > > ).strip().split("\n")
> > > >>> c = " costante"
> > > >>> for r in r_list:
> > > >>> gscript.mapcalc ( " %s = %s * %s " % ((r+"k") , r , c))
> >
> > Try
> >
> > " %sk = %s * %s " % (r , r , c) "
> >
> > Moritz
> >
> >
> > _______________________________________________
> > grass-user mailing list
> > grass-user@lists.osgeo.org
> > https://lists.osgeo.org/mailman/listinfo/grass-user
> _______________________________________________
> grass-user mailing list
> grass-user@lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/grass-user
_______________________________________________
grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

Hi Stefan and hi to everybody

the original “g.list” command was

r_list = gscript.read_command(“g.list” , type = “rast” ).strip().split()

i forgot to put (“\n”) as input parameter in the .split method. Howeever i wrote it while writing my script in this mail since i use it like 100 % of the time. In this way the mapcalc couldn’t find maps since my iteration variable was not the raster itself but every single letter composing its name. Next time i’ll be using copy&paste to avoid silly mistakes

regards

A.

Il giorno gio 13 dic 2018 alle ore 04:26 <st_kiefer@web.de> ha scritto:

Hi Alessandro,
it would be great, if you also comunicate the actual solution, i.e. the proper “g.list”-command, or where you made the mistake. The mapcalc-call seems not to bee what caused the troubles. If someone runs into the same trouble as you, he would have the right hint immediately.

regards

Stefan

Alessandro Sebastiani <alessandro.sebastiani@uniroma1.it> hat am 13. Dezember 2018 um 09:15 geschrieben:

dear all,
i found where the error was! i didn’t set the “g.list” command properly, in
fact the map calculator could not find maps . However , here it is how i
ran the mapcalc:

gscript.mapcalc ( " %s = %s * %s " % ((r+“k”) , r , c))
to Frank : k is just a suffix i wanted to use in order to give to the new
map a name similar to the older map (r in this case)

Il giorno mer 12 dic 2018 alle ore 13:01 Frank David <frank.david@geophom.fr>
ha scritto:

Hi,

What do you want to do with “k” ?

Can you send what return gscript.verbose(( " %s = %s * %s ") % (r , r ,
c))

Frank
Le 12/12/2018 à 17:34, Alessandro Sebastiani a écrit :

I get the same error after trying with your suggestion! I tried to
define rasters in different ways, like

c = [“costante”] ,
but nothing seems to change

Il giorno mer 12 dic 2018 alle ore 10:22 Moritz Lennert
<mlennert@club.worldonline.be mailto:[mlennert@club.worldonline.be](mailto:mlennert@club.worldonline.be)>
ha scritto:

On 12/12/18 12:59, Alessandro Sebastiani wrote:

dear all,
i’m dealing this time with r.mapcalc. I have different rasters
which i
grouped using g.list.
Now r_list contains all my raster. I want to multiply those
rasters to
another raster (called “costante”) in order to obtain a new
map, that i
want to call as r followed by the letter “k”
Here it is what i’ve done:
import grass.script as gscript

r_list = gscript.read_command(“g.list” , type = “rast”
).strip().split(“\n”)
c = " costante"
for r in r_list:
gscript.mapcalc ( " %s = %s * %s " % ((r+“k”) , r , c))

Try

" %sk = %s * %s " % (r , r , c) "

Moritz


grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user


grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user


grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user


grass-user mailing list
grass-user@lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user