[GRASS-SVN] r34419 - in grass/trunk: lib/python scripts/i.fusion.brovey scripts/i.image.mosaic scripts/i.in.spotvgt scripts/i.tasscap scripts/r.blend scripts/r.buffer scripts/r.fillnulls scripts/r.grow scripts/r.mask scripts/r.plane scripts/r.reclass.area scripts/v.rast.stats

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Nov 20 19:42:56 EST 2008


Author: glynn
Date: 2008-11-20 19:42:56 -0500 (Thu, 20 Nov 2008)
New Revision: 34419

Modified:
   grass/trunk/lib/python/grass.py
   grass/trunk/scripts/i.fusion.brovey/i.fusion.brovey.py
   grass/trunk/scripts/i.image.mosaic/i.image.mosaic.py
   grass/trunk/scripts/i.in.spotvgt/i.in.spotvgt.py
   grass/trunk/scripts/i.tasscap/i.tasscap.py
   grass/trunk/scripts/r.blend/r.blend.py
   grass/trunk/scripts/r.buffer/r.buffer.py
   grass/trunk/scripts/r.fillnulls/r.fillnulls.py
   grass/trunk/scripts/r.grow/r.grow.py
   grass/trunk/scripts/r.mask/r.mask.py
   grass/trunk/scripts/r.plane/r.plane.py
   grass/trunk/scripts/r.reclass.area/r.reclass.area.py
   grass/trunk/scripts/v.rast.stats/v.rast.stats.py
Log:
Add, use grass.mapcalc() function


Modified: grass/trunk/lib/python/grass.py
===================================================================
--- grass/trunk/lib/python/grass.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/lib/python/grass.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -4,6 +4,7 @@
 import subprocess
 import re
 import atexit
+import string
 
 # subprocess wrapper that uses shell on Windows
 
@@ -508,3 +509,11 @@
     for k in ['min', 'max', 'north', 'south', 'east', 'west', 'nsres', 'ewres']:
 	kv[k] = float(kv[k])
     return kv
+
+# interface to r.mapcalc
+
+def mapcalc(exp, **kwargs):
+    t = string.Template(exp)
+    e = t.substitute(**kwargs)
+    if run_command('r.mapcalc', expression = e) != 0:
+	grass.fatal("An error occurred while running r.mapcalc")

Modified: grass/trunk/scripts/i.fusion.brovey/i.fusion.brovey.py
===================================================================
--- grass/trunk/scripts/i.fusion.brovey/i.fusion.brovey.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/i.fusion.brovey/i.fusion.brovey.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -84,7 +84,6 @@
 
 import sys
 import os
-import string
 import grass
 
 def main():
@@ -133,14 +132,11 @@
     # note: for RGB composite then revert brov.red and brov.green!
 
     grass.message("Calculating %s.{red,green,blue}: ..." % out)
-    t = string.Template(
-	'''eval(k = float("$pan") / ("$ms1" + "$ms2" + "$ms3"))
+    e = '''eval(k = float("$pan") / ("$ms1" + "$ms2" + "$ms3"))
 	   "$out.red"   = "$ms3" * k
 	   "$out.green" = "$ms2" * k
-	   "$out.blue"  = "$ms1" * k''')
-    e = t.substitute(out = out, pan = pan, ms1 = ms1, ms2 = ms2, ms3 = ms3)
-    if grass.run_command('r.mapcalc', expression = e) != 0:
-	grass.fatal("An error occurred while running r.mapcalc")
+	   "$out.blue"  = "$ms1" * k'''
+    grass.mapcalc(e, out = out, pan = pan, ms1 = ms1, ms2 = ms2, ms3 = ms3)
 
     # Maybe?
     #r.colors   $GIS_OPT_OUTPUTPREFIX.red col=grey

Modified: grass/trunk/scripts/i.image.mosaic/i.image.mosaic.py
===================================================================
--- grass/trunk/scripts/i.image.mosaic/i.image.mosaic.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/i.image.mosaic/i.image.mosaic.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -44,7 +44,6 @@
 
 import sys
 import os
-import string
 import grass
 
 def copy_colors(fh, map, offset):
@@ -90,10 +89,8 @@
 
     grass.message("Mosaicing %d images..." % count)
 
-    t = string.Template("$output = " + make_expression(1, count))
-    print t.template
-    e = t.substitute(output = output, **parms)
-    grass.run_command('r.mapcalc', expression = e)
+    grass.mapcalc("$output = " + make_expression(1, count),
+		  output = output, **parms)
 
     #modify the color table:
     p = grass.feed_command('r.colors', map = output, rules='-')

Modified: grass/trunk/scripts/i.in.spotvgt/i.in.spotvgt.py
===================================================================
--- grass/trunk/scripts/i.in.spotvgt/i.in.spotvgt.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/i.in.spotvgt/i.in.spotvgt.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -182,8 +182,7 @@
 
     grass.message("Remapping digital numbers to NDVI...")
     tmpname = "%s_%s" % (name, pid)
-    e = "%s = 0.004 * %s - 0.1" % (tmpname, name)
-    grass.run_command('r.mapcalc', expression = e)
+    grass.mapcalc("$tmpname = 0.004 * $name - 0.1", tmpname = tmpname, name = name)
     grass.run_command('g.remove', rast = name, quiet = True)
     grass.run_command('g.rename', rast = (tmpname, name), quiet = True)
 
@@ -251,8 +250,8 @@
     grass.message("Filtering NDVI map by Status Map quality layer...")
 
     filtfile = "%s_filt" % name
-    e = "%s = if(%s >= 248, %s, null())" % (filtfile, smfile, name)
-    grass.run_command('r.mapcalc', expression = e)
+    grass.mapcalc("$filtfile = if($smfile >= 248, $name, null())",
+		  filtfile = filtfile, smfile = smfile, name = name)
     grass.run_command('r.colors', map = filtfile, color = 'ndvi', quiet = True)
     grass.message("Filtered SPOT VEGETATION NDVI map <%s>." % filtfile)
 

Modified: grass/trunk/scripts/i.tasscap/i.tasscap.py
===================================================================
--- grass/trunk/scripts/i.tasscap/i.tasscap.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/i.tasscap/i.tasscap.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -100,7 +100,6 @@
 
 import sys
 import os
-import string
 import grass
 
 parms = [[( 0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863),
@@ -118,9 +117,9 @@
 names = ["Brightness", "Greenness", "Wetness", "Haze"]
 
 def calc1(out, bands, k1, k2, k3, k4, k5, k7, k0 = 0):
-    t = string.Template("$out = $k1 * $band1 + $k2 * $band2 + $k3 * $band3 + $k4 * $band4 + $k5 * $band5 + $k7 * $band7 + $k0")
-    e = t.substitute(out = out, k1 = k1, k2 = k2, k3 = k3, k4 = k4, k5 = k5, k7 = k7, k0 = k0, **bands)
-    grass.run_command('r.mapcalc', expression = e)
+    grass.mapcalc(
+	"$out = $k1 * $band1 + $k2 * $band2 + $k3 * $band3 + $k4 * $band4 + $k5 * $band5 + $k7 * $band7 + $k0",
+	out = out, k1 = k1, k2 = k2, k3 = k3, k4 = k4, k5 = k5, k7 = k7, k0 = k0, **bands)
     grass.run_command('r.colors', map = out, color = 'grey')
 
 def calcN(options, i, n):

Modified: grass/trunk/scripts/r.blend/r.blend.py
===================================================================
--- grass/trunk/scripts/r.blend/r.blend.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/r.blend/r.blend.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -76,17 +76,16 @@
     frac2 = perc_inv / 100.0
 
     grass.message("Calculating the three component maps...")
-    template = string.Template("$output.$$ch = $frac1 * $$ch#$first + $frac2 * $$ch#$second")
-    s = template.substitute(output = output,
-			    first = first, second = second,
-			    frac1 = frac1, frac2 = frac2)
-    template = string.Template(s)
-    cmd = []
-    for ch in ['r','g','b']:
-	map = "%s.%s" % (output, ch)
-	cmd.append(template.substitute(ch = ch))
-    grass.run_command('r.mapcalc', expression = ';'.join(cmd))
 
+    template = string.Template("$$output.$ch = $$frac1 * $ch#$$first + $$frac2 * $ch#$$second")
+    cmd = [template.substitute(ch = ch) for ch in ['r','g','b']]
+    cmd = ';'.join(cmd)
+
+    grass.mapcalc(cmd,
+		  output = output,
+		  first = first, second = second,
+		  frac1 = frac1, frac2 = frac2)
+
     for ch in ['r','g','b']:
 	map = "%s.%s" % (output, ch)
 	grass.run_command('r.colors', map = map, color = 'grey255')

Modified: grass/trunk/scripts/r.buffer/r.buffer.py
===================================================================
--- grass/trunk/scripts/r.buffer/r.buffer.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/r.buffer/r.buffer.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -60,7 +60,6 @@
 import sys
 import os
 import atexit
-import string
 import math
 import grass
 
@@ -104,21 +103,18 @@
 		      distance = temp_dist)
 
     if zero:
-	t = string.Template("$temp_src = if($input == 0,null(),1)")
+	exp = "$temp_src = if($input == 0,null(),1)"
     else:
-	t = string.Template("$temp_src = if(isnull($input),null(),1)")
+	exp = "$temp_src = if(isnull($input),null(),1)"
 
-    e = t.substitute(temp_src = temp_src, input = input)
-    grass.run_command('r.mapcalc', expression = e)
+    grass.mapcalc(exp, temp_src = temp_src, input = input)
 
-    s = "$output = if(!isnull($input),$input,%s)"
+    exp = "$output = if(!isnull($input),$input,%s)"
     for n, dist2 in enumerate(distances2):
-	s %= "if($dist <= %f,%d,%%s)" % (dist2,n + 2)
-    s %= "null()"
+	exp %= "if($dist <= %f,%d,%%s)" % (dist2,n + 2)
+    exp %= "null()"
 
-    t = string.Template(s)
-    e = t.substitute(output = output, input = temp_src, dist = temp_dist)
-    grass.run_command('r.mapcalc', expression = e)
+    grass.mapcalc(exp, output = output, input = temp_src, dist = temp_dist)
 
     p = grass.feed_command('r.category', map = output, rules = '-')
     p.stdin.write("1:distances calculated from these locations\n")

Modified: grass/trunk/scripts/r.fillnulls/r.fillnulls.py
===================================================================
--- grass/trunk/scripts/r.fillnulls/r.fillnulls.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/r.fillnulls/r.fillnulls.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -97,8 +97,8 @@
 
     grass.message("Locating and isolating NULL areas...")
     #creating 0/1 map:
-    e = "%s = if(isnull(%s),1,null())" % (tmp1, input)
-    grass.run_command('r.mapcalc', expression = e)
+    grass.mapcalc("$tmp1 = if(isnull($input),1,null())",
+		  tmp1 = tmp1, input = input)
 
     #generate a ring:
     # the buffer is set to three times the map resolution so you get nominally
@@ -113,7 +113,7 @@
     if grass.run_command('r.buffer', input = tmp1, distances = res, out = tmp1 + '.buf') != 0:
 	grass.fatal("abandoned. Removing temporary map, restoring user mask if needed:")
 
-    grass.run_command('r.mapcalc', expression = "MASK=if(%s.buf==2,1,null())" % tmp1)
+    grass.mapcalc("MASK=if($tmp1.buf==2,1,null())", tmp1 = tmp1)
 
     # now we only see the outlines of the NULL areas if looking at INPUT.
     # Use this outline (raster border) for interpolating the fill data:

Modified: grass/trunk/scripts/r.grow/r.grow.py
===================================================================
--- grass/trunk/scripts/r.grow/r.grow.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/r.grow/r.grow.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -75,7 +75,6 @@
 import sys
 import os
 import atexit
-import string
 import math
 import grass
 
@@ -125,10 +124,10 @@
     grass.run_command('r.grow.distance',  input = input, metric = metric,
 		      distance = temp_dist, value = temp_val)
 
-    t = string.Template("$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))")
-    e = t.substitute(output = output, input = input, radius = radius,
-		     old = old, new = new, dist = temp_dist)
-    grass.run_command('r.mapcalc', expression = e)
+    grass.mapcalc(
+	"$output = if(!isnull($input),$old,if($dist < $radius,$new,null()))",
+	output = output, input = input, radius = radius,
+	old = old, new = new, dist = temp_dist)
 
     grass.run_command('r.colors', map = output, raster = input)
 

Modified: grass/trunk/scripts/r.mask/r.mask.py
===================================================================
--- grass/trunk/scripts/r.mask/r.mask.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/r.mask/r.mask.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -81,7 +81,7 @@
 	    global tmp
 	    tmp = "r_mask_%d" % os.getpid()
 	    grass.run_command('g.rename', rast = ('MASK',tmp), quiet = True)
-	    grass.run_command('r.mapcalc', expr = "MASK=if(isnull(%s),1,null())" % tmp)
+	    grass.mapcalc("MASK=if(isnull($tmp),1,null())", tmp = tmp)
 	    grass.run_command('g.remove', rast = tmp, quiet = True)
 	    grass.message("Inverted MASK created.")
 	else:

Modified: grass/trunk/scripts/r.plane/r.plane.py
===================================================================
--- grass/trunk/scripts/r.plane/r.plane.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/r.plane/r.plane.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -110,9 +110,8 @@
     else:
 	round = ""
 
-    t = string.Template('$name = $type($round(x() * $kx + y() * $ky + $kz))')
-    e = t.substitute(name = name, type = type, round = round, kx = kx, ky = ky, kz = kz)
-    grass.run_command('r.mapcalc', expression = e)
+    grass.mapcalc("$name = $type($round(x() * $kx + y() * $ky + $kz))",
+		  name = name, type = type, round = round, kx = kx, ky = ky, kz = kz)
 
     grass.raster_history(name)
 

Modified: grass/trunk/scripts/r.reclass.area/r.reclass.area.py
===================================================================
--- grass/trunk/scripts/r.reclass.area/r.reclass.area.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/r.reclass.area/r.reclass.area.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -113,7 +113,7 @@
 
     grass.message("Written: %s" % outfile)
 
-    grass.run_command('r.mapcalc', expression = "%s = %s" % (outfile, recfile))
+    grass.mapcalc("$outfile = $recfile", outfile = outfile, recfile = recfile)
     grass.run_command('g.remove', rast = [recfile, clumpfile], quiet = True)
 
 if __name__ == "__main__":

Modified: grass/trunk/scripts/v.rast.stats/v.rast.stats.py
===================================================================
--- grass/trunk/scripts/v.rast.stats/v.rast.stats.py	2008-11-21 00:01:20 UTC (rev 34418)
+++ grass/trunk/scripts/v.rast.stats/v.rast.stats.py	2008-11-21 00:42:56 UTC (rev 34419)
@@ -228,8 +228,8 @@
     for i in cats:
 	grass.verbose("Processing category %s (%d/%d)" % (i, currnum, number))
 	grass.run_command('g.remove', rast = 'MASK', quiet = True, stderr = nuldev)
-	grass.run_command('r.mapcalc', quiet = True,
-			  expression = "MASK = if(%s_%s == %s, 1, null())" % (vector, tmpname, i))
+	grass.mapcalc("MASK = if($name == $i, 1, null())",
+		      name = "%s_%s" % (vector, tmpname), i = i)
 
         #n, min, max, range, mean, stddev, variance, coeff_var, sum
 	# How to test r.univar $? exit status? using -e creates the real possibility of out-of-memory errors



More information about the grass-commit mailing list