[GRASS-user] i.group with patterns as name for input

I am using 'i.group' to create groups of maps whose names begin with a common
prefix (e.g. "prova_*").
I was wondering if there is a way to create a list of such maps in the
"i.group" function for the "input" argument", similarly to what "pattern"
option of "g.list" (and other functions) does.

For example: if my maps are [prova_01, prova_02, ..., prova100], I would
like to insert them in the same group by doing something like:

    run_command("i.group", type="raster", input="prova_*")

Any suggestion? Ah, I am using Python for grass scripting. Thanks in advance

    Umberto

--
View this message in context: http://osgeo-org.1560.x6.nabble.com/i-group-with-patterns-as-name-for-input-tp5196465.html
Sent from the Grass - Users mailing list archive at Nabble.com.

umberto.minora wrote:

..

For example: if my maps are [prova_01, prova_02, ..., prova100], I would
like to insert them in the same group by doing something like:
    run_command("i.group", type="raster", input="prova_*")

Any suggestion? Ah, I am using Python for grass scripting. Thanks in advance

You can use the output of "g.list" (as a list) as an input to "i.group"'s input.

Nikos

On Wed, Apr 1, 2015 at 4:16 PM, umberto.minora <umberto.minora@unimi.it> wrote:

For example: if my maps are [prova_01, prova_02, ..., prova100], I would
like to insert them in the same group by doing something like:

    run_command("i.group", type="raster", input="prova_*")

Any suggestion? Ah, I am using Python for grass scripting. Thanks in advance

{{{
from grass.pygrass.gis import Mapset

mset = Mapset()
run_command("i.group", input=mset.glist("raster", pattern="prova_*"))
}}}

not tested... but should work.

Pietro

Dear Nikos (and again Pietro, thanks)

I am trying this, but GRASS ouputs a Windows error, then the script
continues (very strange) and outputs an error:

from grass.script.core import run_command
from grass.script import parse_command

dem="khumbu_dem"
aspect= "khumbu_aspect"
slope= "khumbu_slope"

outFolder=
"D:\UMBE\PhD\Nepal\khumbu_article\shadow\r_sun_hourly\prova_hourly_shadow"

day_month=[range(1,3),range(3,5)]

for m in xrange(len(day_month)):
  group_list=
  for d in day_month[m]:
    run_command("r.sun.hourly",
          flags="b",
          elevation=dem,
          aspect=aspect,
          slope=slope,
          start_time=0,
          end_time=3,
          day=d,
          year=2014,
          beam_rad_basename="prova"+ str(m+1)+ "_"+ str(d)) # Underscore and hour
are added to the base name for each map

  group_list= parse_command("g.list",
                type="raster",
                pattern="prova"+ str(m+1)).keys()
                
  run_command("i.group",
        group="prova"+ str(m+1),
        input=group_list)

  run_command("r.out.gdal",
        flags="c",
        type="Byte",
        input="prova"+ str(m+1),
        output=outFolder+ "\prova"+ str(m+1)+ ".tif",
        nodata=255)

As you can see I am using "i.group" to create a list (named "group_list")
which updates at each iteration (as you suggested in an old quesion
<http://lists.osgeo.org/pipermail/grass-user/2014-November/071197.html&gt; I
posted). Anyway, I receive this error after the first "r.sun.hourly" loop:

Traceback (most recent call last):
  File "D:\UMBE\PhD\Nepal\khumbu_article\shadow\r_sun_hourly
\prova_r-sun-hourly.py", line 32, in <module>
    input=group_list)
  File "C:\OSGeo4W\apps\grass\grass-7.0.0\etc\python\grass\s
cript\core.py", line 375, in run_command
    return handle_errors(returncode, returncode, args,
kwargs)
  File "C:\OSGeo4W\apps\grass\grass-7.0.0\etc\python\grass\s
cript\core.py", line 310, in handle_errors
    returncode=returncode)
grass.exceptions.CalledModuleError: Module run None
['i.group', 'input=', 'group=prova1'] ended with error
Process ended with non-zero return code 255. See errors in
the (error) output.

It could be related to the fact that the option "input" of "i.group" should
be something like "a,b,c" and not "'a','b','c'" (as my variable "group_list"
is like)?

--
View this message in context: http://osgeo-org.1560.x6.nabble.com/i-group-with-patterns-as-name-for-input-tp5196465p5196494.html
Sent from the Grass - Users mailing list archive at Nabble.com.

On 01.04.2015 16:48, umberto.minora wrote:

Dear Nikos (and again Pietro, thanks)

..

Did you try Pietro's suggestion? It's much easier I think. Otherwise, try:

from grass.script import core as grass

and then use "grass.read_command" instead of "parse...".

Nikos

I will try both your helpful suggestions the next hours/days and let you know
about any result as soon as I proceed.

Umberto

--
View this message in context: http://osgeo-org.1560.x6.nabble.com/i-group-with-patterns-as-name-for-input-tp5196465p5196503.html
Sent from the Grass - Users mailing list archive at Nabble.com.

I tried Pietro's suggestion:

{{{
from grass.pygrass.gis import Mapset

mset = Mapset()
run_command("i.group", input=mset.glist("raster", pattern="prova_*"))
}}}

Only, I had to add the "group" argument and it worked perfectly, thanks for
the help.

{{{
run_command("i.group", group="prova", input=mset.glist("raster",
pattern="prova_*"))
}}}

--
View this message in context: http://osgeo-org.1560.x6.nabble.com/i-group-with-patterns-as-name-for-input-tp5196465p5200183.html
Sent from the Grass - Users mailing list archive at Nabble.com.

On Tue, Apr 7, 2015 at 10:41 AM, umberto.minora <umberto.minora@unimi.it> wrote:

I tried Pietro's suggestion:

...

Only, I had to add the "group" argument and it worked perfectly, thanks for
the help.

{{{
run_command("i.group", group="prova", input=mset.glist("raster",
pattern="prova_*"))
}}}

I have added that here:
http://grasswiki.osgeo.org/wiki/GRASS_Python_Scripting_Library#i.group_with_patterns_as_name_for_input

(feel free to edit/expand these Wiki pages!)

Markus