[GRASS-dev] GRASS Scripts access to channel headers info

Glynn Clements glynn at gclements.plus.com
Wed Feb 10 20:54:58 EST 2010

António Rocha wrote:

> By the way, I'm having some difficulties in using mapcalc in a python 
> scripts.
> I'm doing this
> grass.mapcalc(options['output']=options['raster1']+options['raster2'])

This isn't valid Python syntax.

grass.mapcalc() accepts a template string followed by keyword
arguments for the substitutions, e.g.:

	grass.mapcalc("${out} = ${rast1} + ${rast2}",
	              out = options['output'],
	              rast1 = options['raster1'],
	              rast2 = options['raster2'])

Although, I normally copy all of the options[] into separate variables
at the beginning of main(), i.e.:

	def main():
	    output = options['output']
	    raster1 = options['raster1']
	    raster2 = options['raster2']


	    grass.mapcalc("${out} = ${rast1} + ${rast2}",
	                  out = output,
	                  rast1 = raster1,
	                  rast2 = raster2)

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-dev mailing list