[GRASS-SVN] r52126 - grass/branches/develbranch_6/scripts/i.oif

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jun 18 16:26:30 PDT 2012


Author: hamish
Date: 2012-06-18 16:26:30 -0700 (Mon, 18 Jun 2012)
New Revision: 52126

Modified:
   grass/branches/develbranch_6/scripts/i.oif/description.html
   grass/branches/develbranch_6/scripts/i.oif/i.oif
Log:
parallelize the r.univar calls

Modified: grass/branches/develbranch_6/scripts/i.oif/description.html
===================================================================
--- grass/branches/develbranch_6/scripts/i.oif/description.html	2012-06-18 14:10:57 UTC (rev 52125)
+++ grass/branches/develbranch_6/scripts/i.oif/description.html	2012-06-18 23:26:30 UTC (rev 52126)
@@ -1,6 +1,7 @@
 <h2>DESCRIPTION</h2>
 
-<em>i.oif</em> calculates the Optimum Index Factor for LANDSAT TM bands 1,2,3,4,5 and 7.
+<em>i.oif</em> calculates the Optimum Index Factor for LANDSAT TM bands 1,2,3,4,5
+and 7.
 <p>
 The Optimum Index Factor is calculated to determine the band combination which
 shows the maximum information when combined into a composite image. The bands
@@ -9,6 +10,7 @@
 <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: 
@@ -38,6 +40,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,8 +59,10 @@
 
 <h2>REFERENCE</h2>
 
-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>
 
 <em>
@@ -61,9 +72,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/branches/develbranch_6/scripts/i.oif/i.oif
===================================================================
--- grass/branches/develbranch_6/scripts/i.oif/i.oif	2012-06-18 14:10:57 UTC (rev 52125)
+++ grass/branches/develbranch_6/scripts/i.oif/i.oif	2012-06-18 23:26:30 UTC (rev 52126)
@@ -72,7 +72,13 @@
 #% key: g
 #% description: Print in shell script style
 #% End
+#%flag
+#% key: s
+#% description: Process bands serially (default: run in parallel)
+#%end
 
+
+
 if  [ -z "$GISBASE" ]
 then
 	echo "You must be in GRASS GIS to run this program" >&2
@@ -102,22 +108,56 @@
 temp_bands="`g.tempfile $$`"
 
 # save the Stddev for TM bands
-g.message "Calculating Standard deviations for all bands:"
-g.message "band 1"
-r.univar -g "$GIS_OPT_IMAGE1" | grep stddev | cut -f2 -d= > "$temp_stddev"
-g.message "band 2"
-r.univar -g "$GIS_OPT_IMAGE2" | grep stddev | cut -f2 -d= >> "$temp_stddev"
-g.message "band 3"
-r.univar -g "$GIS_OPT_IMAGE3" | grep stddev | cut -f2 -d= >> "$temp_stddev"
-g.message "band 4"
-r.univar -g "$GIS_OPT_IMAGE4" | grep stddev | cut -f2 -d= >> "$temp_stddev"
-g.message "band 5"
-r.univar -g "$GIS_OPT_IMAGE5" | grep stddev | cut -f2 -d= >> "$temp_stddev"
-g.message "band 7"
-r.univar -g "$GIS_OPT_IMAGE7" | grep stddev | cut -f2 -d= >> "$temp_stddev"
+g.message "Calculating Standard deviations for all bands"
+if [ 1 -eq $GIS_FLAG_S ] ; then
+   # run serially
+   g.message "band 1"
+   r.univar -g "$GIS_OPT_IMAGE1" | grep stddev | cut -f2 -d= > "$temp_stddev"
+   g.message "band 2"
+   r.univar -g "$GIS_OPT_IMAGE2" | grep stddev | cut -f2 -d= >> "$temp_stddev"
+   g.message "band 3"
+   r.univar -g "$GIS_OPT_IMAGE3" | grep stddev | cut -f2 -d= >> "$temp_stddev"
+   g.message "band 4"
+   r.univar -g "$GIS_OPT_IMAGE4" | grep stddev | cut -f2 -d= >> "$temp_stddev"
+   g.message "band 5"
+   r.univar -g "$GIS_OPT_IMAGE5" | grep stddev | cut -f2 -d= >> "$temp_stddev"
+   g.message "band 7"
+   r.univar -g "$GIS_OPT_IMAGE7" | grep stddev | cut -f2 -d= >> "$temp_stddev"
+else
+   #run in parallel
+   if [ -z "$WORKERS" ] ; then
+      WORKERS=6
+   fi
 
+   # parallel launching: could use GNU Parallel (an advanced form of xargs), but
+   # it may not be available. so we use a more generic approach
+   # see http://www.gnu.org/software/parallel/
+   # and http://grass.osgeo.org/wiki/OpenMP#Alternatives
+
+   # poor man's multi-threading for a multi-core CPU
+   for n in 1 2 3 4 5 7 ; do
+      MODULUS=`echo "$n $WORKERS" | awk '{print $1 % $2}'`
+      MAP=`eval "echo \\$GIS_OPT_IMAGE$n"`
+      g.message -i "band $n, <$MAP>  % $MODULUS"
+      if [ "$MODULUS" -eq 0  -o "$n" -eq 7 ] ; then
+	 r.univar -g "$MAP" | grep '^stddev=' | cut -f2 -d= > "$temp_stddev.b$n"
+	 wait
+      else
+     	 r.univar -g "$MAP" | grep '^stddev=' | cut -f2 -d= > "$temp_stddev.b$n" &
+      fi
+   done
+   wait
+
+   cat "$temp_stddev.b1" "$temp_stddev.b2" "$temp_stddev.b3" \
+       "$temp_stddev.b4" "$temp_stddev.b5" "$temp_stddev.b7" \
+       > "$temp_stddev"
+
+   \rm -f "$temp_stddev".b[1-7]
+fi
+
 g.message "Calculating Correlation Matrix"
-r.covar -r map=$GIS_OPT_IMAGE1,$GIS_OPT_IMAGE2,$GIS_OPT_IMAGE3,$GIS_OPT_IMAGE4,$GIS_OPT_IMAGE5,$GIS_OPT_IMAGE7 |tail -6 > "$temp_correlation"
+r.covar -r map="$GIS_OPT_IMAGE1,$GIS_OPT_IMAGE2,$GIS_OPT_IMAGE3,$GIS_OPT_IMAGE4,$GIS_OPT_IMAGE5,$GIS_OPT_IMAGE7" \
+   | tail -6 > "$temp_correlation"
 
 # Calculate all combinations
 g.message "Calculating OIF for the 20 band combinations..."



More information about the grass-commit mailing list