[GRASS-user] help with r.series

Dear All,

I have created a few list using g.mlist command in grass7. Each list has a few raster layers. For example; FILE2000 has lists of all monthly layers for the year 2000, FILE2001 has layers for 2001…so on.

Now I want to create one layer each for each of these lists (each year). I can use “r.series” command with method=“average”, to get a layer with averaged values across all the months for a particular year. Now I have 14 such lists corresponding to 14 years and do not want to repeat the process for each year. Is there a way to automate this process using grass prompt? I don’t want to move to R, would like to complete all the work in GRASS.

Thank you

Regards

Rajat Nayak

Hi Rajat,

On Thu, Sep 4, 2014 at 1:51 PM, Rajat Nayak <rajat27404@gmail.com> wrote:

I have created a few list using g.mlist command in grass7. Each list has a
few raster layers. For example; FILE2000 has lists of all monthly layers for
the year 2000, FILE2001 has layers for 2001...so on.
Now I want to create one layer each for each of these lists (each year). I
can use "r.series" command with method="average", to get a layer with
averaged values across all the months for a particular year. Now I have 14
such lists corresponding to 14 years and do not want to repeat the process
for each year. Is there a way to automate this process using grass prompt? I
don't want to move to R, would like to complete all the work in GRASS.

Yes you can do it, using BASH or python, personally I prefer/use python.
A possible solution for grass6/7 could be something like:

{{{

from grass import script

# define the pattern to select the maps that will be aggregate
PATTERNS = ('rast2001*', 'rast2002*', 'rast2003*') # etc.

# define the name of the output names
OUTPUTS = ('out2001', 'out2002', 'out2003')

# start a cycle for each pattern and for each output
for pattern, output in zip(PATTERNS, OUTPUTS):
    # get the list of input maps
    inputs = script.mlist_strings('rast', pattern=pattern)
    # run your command
    script.run_command('r.series', input=inputs, output=output,
method='average')

}}}

Rajat Nayak wrote:

I have created a few list using g.mlist command in grass7. Each list has a
few raster layers. For example; FILE2000 has lists of all monthly layers
for the year 2000, FILE2001 has layers for 2001...so on.
Now I want to create one layer each for each of these lists (each year). I
can use "r.series" command with method="average", to get a layer with
averaged values across all the months for a particular year. Now I have 14
such lists corresponding to 14 years and do not want to repeat the process
for each year. Is there a way to automate this process using grass prompt?
I don't want to move to R, would like to complete all the work in GRASS.

bash:
  for file in FILE20?? ; do
      r.series file=$file output=output${file#file} method=average
  done

Python:
  import os
  import grass.script as grass
  for file in os.listdir('.'):
      if not file.startswith('FILE20'):
          continue
      output = 'output' + file[4:]
      grass.run_command('r.series', file=file, output=output,
                        method='average')

--
Glynn Clements <glynn@gclements.plus.com>