浏览代码

parallelize the r.univar calls, reduce insignificant noise in the output table

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@52128 15284696-431f-4ddb-bdfa-cd5b030d7da7
Hamish Bowman 13 年之前
父节点
当前提交
982d2897c4
共有 2 个文件被更改,包括 58 次插入8 次删除
  1. 12 1
      scripts/i.oif/i.oif.html
  2. 46 7
      scripts/i.oif/i.oif.py

+ 12 - 1
scripts/i.oif/i.oif.html

@@ -7,6 +7,7 @@ comprising the highest scoring combination from <em>i.oif</em> are used as the
 three color channels required for <em>d.rgb</em> or <em>r.composite</em>.
 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".
 <p>The analysis is saved to a file in the current directory called "i.oif.result".
 
 
+
 <h2>NOTES</h2>
 <h2>NOTES</h2>
 
 
 Colour Composites in BGR order: important band combinations (example: 
 Colour Composites in BGR order: important band combinations (example: 
@@ -36,6 +37,13 @@ vegetational stress and mortality. Roads are less evident as band 3 is blue.
 <li> 457: shows soil texture classes (clay, loam, sandy).
 <li> 457: shows soil texture classes (clay, loam, sandy).
 </ul>
 </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>
 <h2>EXAMPLE</h2>
 
 
 North Carolina sample dataset:
 North Carolina sample dataset:
@@ -50,6 +58,7 @@ i.oif image1=lsat7_2002_10 image2=lsat7_2002_20 image3=lsat7_2002_30 \
 
 
 Jensen, 1996. Introductory digital image processing. Prentice Hall, p.98. ISBN 0-13-205840-5
 Jensen, 1996. Introductory digital image processing. Prentice Hall, p.98. ISBN 0-13-205840-5
 
 
+
 <h2>SEE ALSO</h2>
 <h2>SEE ALSO</h2>
 
 
 <em>
 <em>
@@ -59,9 +68,11 @@ Jensen, 1996. Introductory digital image processing. Prentice Hall, p.98. ISBN 0
 <a href="r.univar.html">r.univar</a>
 <a href="r.univar.html">r.univar</a>
 </em>
 </em>
 
 
+
 <h2>AUTHOR</h2>
 <h2>AUTHOR</h2>
 
 
 Markus Neteler, ITC-Irst, Trento, Italy<br>
 Markus Neteler, ITC-Irst, Trento, Italy<br>
 Updated to GRASS 5.7 by Michael Barton, Arizona State University
 Updated to GRASS 5.7 by Michael Barton, Arizona State University
 
 
-<p><i>Last changed: $Date$</i>
+<p>
+<i>Last changed: $Date$</i>

+ 46 - 7
scripts/i.oif/i.oif.py

@@ -57,6 +57,10 @@
 #% key: g
 #% key: g
 #% description: Print in shell script style
 #% description: Print in shell script style
 #% End
 #% End
+#% Flag
+#% key: s
+#% description: Process bands serially (default: run in parallel)
+#% End
 
 
 import sys
 import sys
 import os
 import os
@@ -86,6 +90,7 @@ def perms():
 
 
 def main():
 def main():
     shell = flags['g']
     shell = flags['g']
+    serial = flags['s']
     image = {}
     image = {}
     for band in bands:
     for band in bands:
 	image[band] = options['image%d' % band]
 	image[band] = options['image%d' % band]
@@ -93,11 +98,45 @@ def main():
     # calculate the Stddev for TM bands
     # calculate the Stddev for TM bands
     grass.message(_("Calculating Standard deviations for all bands..."))
     grass.message(_("Calculating Standard deviations for all bands..."))
     stddev = {}
     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..."))
     grass.message(_("Calculating Correlation Matrix..."))
     correlation = {}
     correlation = {}
@@ -118,9 +157,9 @@ def main():
                     "(Best combination comes first):"))
                     "(Best combination comes first):"))
     
     
     if shell:
     if shell:
-	fmt = "%d%d%d:%f\n"
+	fmt = "%d%d%d:%.4f\n"
     else:
     else:
-	fmt = "%d%d%d:  %f\n"
+	fmt = "%d%d%d:  %.4f\n"
 
 
     outf = file('i.oif.result', 'w')
     outf = file('i.oif.result', 'w')
     for v, p in oif:
     for v, p in oif: