[GRASS-SVN] r52128 - grass/trunk/scripts/i.oif

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jun 18 18:49:36 PDT 2012


Author: hamish
Date: 2012-06-18 18:49:35 -0700 (Mon, 18 Jun 2012)
New Revision: 52128

Modified:
   grass/trunk/scripts/i.oif/i.oif.html
   grass/trunk/scripts/i.oif/i.oif.py
Log:
parallelize the r.univar calls, reduce insignificant noise in the output table

Modified: grass/trunk/scripts/i.oif/i.oif.html
===================================================================
--- grass/trunk/scripts/i.oif/i.oif.html	2012-06-19 01:23:40 UTC (rev 52127)
+++ grass/trunk/scripts/i.oif/i.oif.html	2012-06-19 01:49:35 UTC (rev 52128)
@@ -7,6 +7,7 @@
 three color channels required for <em>d.rgb</em> or <em>r.composite</em>.
 <p>The analysis is saved to a file in the current directory called "i.oif.result".
 
+
 <h2>NOTES</h2>
 
 Colour Composites in BGR order: important band combinations (example: 
@@ -36,6 +37,13 @@
 <li> 457: shows soil texture classes (clay, loam, sandy).
 </ul>
 
+<p>
+By default the module will calculate standard deviations for all bands in
+parallel. To run serially use the <b>-s</b> flag. If the <tt>WORKERS</tt>
+environment variable is set, the number of concurrent processes will be
+limited to that number of jobs.
+
+
 <h2>EXAMPLE</h2>
 
 North Carolina sample dataset:
@@ -50,6 +58,7 @@
 
 Jensen, 1996. Introductory digital image processing. Prentice Hall, p.98. ISBN 0-13-205840-5
 
+
 <h2>SEE ALSO</h2>
 
 <em>
@@ -59,9 +68,11 @@
 <a href="r.univar.html">r.univar</a>
 </em>
 
+
 <h2>AUTHOR</h2>
 
 Markus Neteler, ITC-Irst, Trento, Italy<br>
 Updated to GRASS 5.7 by Michael Barton, Arizona State University
 
-<p><i>Last changed: $Date$</i>
+<p>
+<i>Last changed: $Date$</i>

Modified: grass/trunk/scripts/i.oif/i.oif.py
===================================================================
--- grass/trunk/scripts/i.oif/i.oif.py	2012-06-19 01:23:40 UTC (rev 52127)
+++ grass/trunk/scripts/i.oif/i.oif.py	2012-06-19 01:49:35 UTC (rev 52128)
@@ -57,6 +57,10 @@
 #% key: g
 #% description: Print in shell script style
 #% End
+#% Flag
+#% key: s
+#% description: Process bands serially (default: run in parallel)
+#% End
 
 import sys
 import os
@@ -86,6 +90,7 @@
 
 def main():
     shell = flags['g']
+    serial = flags['s']
     image = {}
     for band in bands:
 	image[band] = options['image%d' % band]
@@ -93,12 +98,46 @@
     # calculate the Stddev for TM bands
     grass.message(_("Calculating Standard deviations for all bands..."))
     stddev = {}
-    for band in bands:
-	grass.verbose("band %d" % band)
-	s = grass.read_command('r.univar', flags = 'g', map = image[band])
-	kv = grass.parse_key_val(s)
-	stddev[band] = float(kv['stddev'])
 
+    if serial:
+        for band in bands:
+	    grass.verbose("band %d" % band)
+	    s = grass.read_command('r.univar', flags = 'g', map = image[band])
+	    kv = grass.parse_key_val(s)
+	    stddev[band] = float(kv['stddev'])
+    else:
+	# run all bands in parallel
+	if "WORKERS" in os.environ:
+            workers = int(os.environ["WORKERS"])
+	else:
+	    workers = 6
+
+	proc = {}
+	pout = {}
+
+	# spawn jobs in the background
+	for band in bands:
+	    grass.debug("band %d, <%s>  %% %d" % (band, image[band], band % workers))
+	    proc[band] = grass.pipe_command('r.univar', flags = 'g', map = image[band])
+	    if band % workers is 0:
+		# wait for the ones launched so far to finish
+		for bandp in bands[:band]:
+		    if not proc[bandp].stdout.closed:
+			pout[bandp] = proc[bandp].communicate()[0]
+		    proc[bandp].wait()
+
+	# wait for jobs to finish, collect the output
+	for band in bands:
+	    if not proc[band].stdout.closed:
+		pout[band] = proc[band].communicate()[0]
+	    proc[band].wait()
+
+	# parse the results
+        for band in bands:
+	    kv = grass.parse_key_val(pout[band])
+	    stddev[band] = float(kv['stddev'])
+
+
     grass.message(_("Calculating Correlation Matrix..."))
     correlation = {}
     s = grass.read_command('r.covar', flags = 'r', map = [image[band] for band in bands])
@@ -118,9 +157,9 @@
                     "(Best combination comes first):"))
     
     if shell:
-	fmt = "%d%d%d:%f\n"
+	fmt = "%d%d%d:%.4f\n"
     else:
-	fmt = "%d%d%d:  %f\n"
+	fmt = "%d%d%d:  %.4f\n"
 
     outf = file('i.oif.result', 'w')
     for v, p in oif:



More information about the grass-commit mailing list