[GRASS-user] looping r.mapcalc in a shell script

Blumentrath, Stefan Stefan.Blumentrath at nina.no
Sat Apr 5 13:37:23 PDT 2014

Hei Rajat,

I assume you have two files in your working directory: NDVIFILES and VIQALIST



Then this line of code should do what you want:
paste -d ' ' VIQALIST NDVIFILES | awk '{print "expression=\"NDVI_QA_" substr($1,6,3) "=" $2 " * " $1 "\""}' | xargs -P2 -I{} r.mapcalc {}

Some explanation:
For using paste the order is important, because the paste command concatenates line 1 from file 1 with line 1 from file 2, line 2 from file 1 with line 2 from file 2 and so on. So, please double check that the order of the maps in both lists match (if not, use sort or something).

awk is very handy for table like text files / data. By default it assumes "space" as delimiter for columns. $1 refers to column 1, $2 refers to column 2 (and  $0 would refer to the whole). In the example awk is used to generate a map calculator expression from the two files merged with "paste". substr($1,6,3) extracts the file number (3 letters starting with position 6) from the map names of the maps listed in file 1. I recommend having a look at one of the many awk tutorials online.

xargs is a tool for executing commands in parallel. -P2 means that 2 cores are used, if you have more on your system you can increase the number of cores used.-I{} says that the string {} is replaced by the text is being piped to xargs (you could use othe strings if you like to).

Hope that helps.


From: Rajat Nayak [mailto:rajat27404 at gmail.com]
Sent: 5. april 2014 03:38
To: Blumentrath, Stefan
Subject: Re: [GRASS-user] looping r.mapcalc in a shell script

Dear Stefan,
I have attached a few files for your reference with this mail. There are 3 files for NDVI and 3 for QA. In GRASS 7 I created NDVILIST and QALIST with NDVI and all QA files (each with 23 files for my analysis) respectively using g.mlist.
I'm presently running on ubuntu 12.04.
Hope this helps,
Thanking you,

Rajat Nayak

On Fri, Apr 4, 2014 at 2:21 PM, Blumentrath, Stefan <Stefan.Blumentrath at nina.no<mailto:Stefan.Blumentrath at nina.no>> wrote:
Hi Rajat,

If you send me your two files I can send you a line of code that should work.
In this case I will need to know if you work with GRASS 6 or 7 and your operating system...


From: Rajat Nayak [mailto:rajat27404 at gmail.com<mailto:rajat27404 at gmail.com>]
Sent: 4. april 2014 04:47
To: Blumentrath, Stefan
Subject: Re: [GRASS-user] looping r.mapcalc in a shell script

Thank you Stefan for your reply.
One of the problems I'm facing with all the suggested codes is, the code is unable to find either of the file lists.
Both the filelists were created using g.mlist command, it it worked fine with other loops like reclass.
I will work again on your suggested line of thoughts, and hope will be able to crack it.

Rajat Nayak

On Thu, Apr 3, 2014 at 5:38 PM, Blumentrath, Stefan <Stefan.Blumentrath at nina.no<mailto:Stefan.Blumentrath at nina.no>> wrote:
You could also combine your two lists using the "paste" command, then "awk" in order to create proper map calculator expressions, which you then can send to the map calculator through "xargs".
That way you could process the maps in parallel (if you are on a 64bit system with multi cores)...

Something like (would likely require some adjustments, e.g. regarding field delimiters):
paste NDVIFILES VIQALIST | awk '{print \" $1 "_MASK"=$1 '" * " $2 \"}' | xargs -P 4 r.mapcalc {}


-----Original Message-----
From: grass-user-bounces at lists.osgeo.org<mailto:grass-user-bounces at lists.osgeo.org> [mailto:grass-user-bounces at lists.osgeo.org<mailto:grass-user-bounces at lists.osgeo.org>] On Behalf Of rajatrn
Sent: 3. april 2014 10:31
To: grass-user at lists.osgeo.org<mailto:grass-user at lists.osgeo.org>
Subject: [GRASS-user] looping r.mapcalc in a shell script

I have two layer lists, NDVIFILES and VIQALIST, both developed using g.mlist command in GRASS.
Each file has 22 items in it. I would like to perform a simple raster calculation of the following form, Mask$NDVIFILES [i] = NDVIFILES[i] * VIQALIST[i] I'm unable to create a loop which can sequentially produce the required outputs.
I will be grateful if anyone can help me with creating a loop using bash/shell script.
I know it is simpler to do it in R but will be great if can be implemented on using grass terminal/shell.
Thank you in advance,

View this message in context: http://osgeo-org.1560.x6.nabble.com/looping-r-mapcalc-in-a-shell-script-tp5132797.html
Sent from the Grass - Users mailing list archive at Nabble.com.
grass-user mailing list
grass-user at lists.osgeo.org<mailto:grass-user at lists.osgeo.org>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20140405/21f446f0/attachment.html>

More information about the grass-user mailing list