[GRASS-SVN] r36183 - grass/branches/releasebranch_6_4/imagery/i.pca

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Mar 2 18:44:14 EST 2009


Author: hamish
Date: 2009-03-02 18:44:13 -0500 (Mon, 02 Mar 2009)
New Revision: 36183

Modified:
   grass/branches/releasebranch_6_4/imagery/i.pca/description.html
   grass/branches/releasebranch_6_4/imagery/i.pca/local_proto.h
   grass/branches/releasebranch_6_4/imagery/i.pca/main.c
   grass/branches/releasebranch_6_4/imagery/i.pca/support.c
Log:
merge fixes from devbr6

Modified: grass/branches/releasebranch_6_4/imagery/i.pca/description.html
===================================================================
--- grass/branches/releasebranch_6_4/imagery/i.pca/description.html	2009-03-02 22:43:14 UTC (rev 36182)
+++ grass/branches/releasebranch_6_4/imagery/i.pca/description.html	2009-03-02 23:44:13 UTC (rev 36183)
@@ -8,7 +8,7 @@
 the input data in decreasing order of variance
 ("contrast").  The output raster map layers are assigned
 names with .1, .2, ... .n suffixes.  The current geographic
-region definition and mask settings are respected when
+region definition and MASK settings are respected when
 reading the input raster map layers. When the rescale
 option is used, the output files are rescaled to fit the
 min,max range.
@@ -16,36 +16,50 @@
 <H2>OPTIONS</H2>
 
 <H3>Parameters:</H3>
-
 <DL>
-
 <DT><B>input=</B><EM>name,name</EM>[<EM>,name,name</EM>,...] 
-
 <DD>Name of two or more input raster map layers.
 
 <DT><B>output=</B><EM>name</EM> 
-
 <DD>The output raster map layer name to which suffixes are
 added.  Each output raster map layer is assigned this
 user-specified <EM>name</EM> with a numerical (.1, .2, ...
 .n) suffix.
 
 <DT><B>rescale=</B><EM>min,max</EM> 
-
 <DD>The optional output category range. (Default: 0,255) If
 rescale=0,0,  no rescaling is performed on output files.
 
 <DD>If output is rescaled, the output raster will be of type CELL. If 
 the output is not rescaled, the output raster will be of type DCELL.
-
 </DL>
 
+
 <H2>NOTES</H2>
 
 Richards (1986) gives a good example of the application of principal
 components analysis (pca) to a time series of LANDSAT images of a burned
 region in Australia.
+<P>
+Eigenvalue and eigenvector information is stored in the output maps'
+history files. View with <em>r.info</em>.
 
+
+<H2>EXAMPLE</H2>
+
+Using the Spearfish Imagery sample dataset
+
+<div class="code"><pre>
+i.pca in=spot.ms.1,spot.ms.2,spot.ms.3 out=spot_pca
+
+r.info -h spot_pca.1
+   Eigen values, (vectors), and [percent importance]:
+   PC1   1170.12 ( -0.63 -0.65 -0.43 ) [ 88.07% ]
+   PC2    152.49 (  0.23  0.37 -0.90 ) [ 11.48% ]
+   PC3      6.01 (  0.75 -0.66 -0.08 ) [  0.45% ]
+</pre></div>
+
+
 <H2>SEE ALSO</H2>
 
 Richards, John A.,
@@ -61,14 +75,17 @@
 
 <P>
 
-<EM><A HREF="i.cca.html">i.cca</A></EM><br>
-<EM><A HREF="i.class.html">i.class</A></EM><br>
-<EM><A HREF="i.fft.html">i.fft</A></EM><br>
-<EM><A HREF="i.ifft.html">i.ifft</A></EM><br>
-<EM><A HREF="m.eigensystem.html">m.eigensystem</A></EM><br>
-<EM><A HREF="r.covar.html">r.covar</A></EM><br>
-<EM><A HREF="r.mapcalc.html">r.mapcalc</A></EM>
+<EM>
+<A HREF="i.cca.html">i.cca</A><br>
+<A HREF="i.class.html">i.class</A><br>
+<A HREF="i.fft.html">i.fft</A><br>
+<A HREF="i.ifft.html">i.ifft</A><br>
+<A HREF="m.eigensystem.html">m.eigensystem</A><br>
+<A HREF="r.covar.html">r.covar</A><br>
+<A HREF="r.mapcalc.html">r.mapcalc</A>
+</EM>
 
+
 <H2>AUTHOR</H2>
 
 David Satnik, GIS Laboratory
@@ -82,4 +99,5 @@
 Rewritten for GRASS 6.x and major modifications by <br>
 Brad Douglas
 
-<p><i>Last changed: $Date$</i>
+<p>
+<i>Last changed: $Date$</i>

Modified: grass/branches/releasebranch_6_4/imagery/i.pca/local_proto.h
===================================================================
--- grass/branches/releasebranch_6_4/imagery/i.pca/local_proto.h	2009-03-02 22:43:14 UTC (rev 36182)
+++ grass/branches/releasebranch_6_4/imagery/i.pca/local_proto.h	2009-03-02 23:44:13 UTC (rev 36183)
@@ -2,6 +2,6 @@
 #define __LOCAL_PROTO_H__
 
 /* support.c */
-int write_support(int, char *, double **);
+int write_support(int, char *, double **, double *);
 
 #endif

Modified: grass/branches/releasebranch_6_4/imagery/i.pca/main.c
===================================================================
--- grass/branches/releasebranch_6_4/imagery/i.pca/main.c	2009-03-02 22:43:14 UTC (rev 36182)
+++ grass/branches/releasebranch_6_4/imagery/i.pca/main.c	2009-03-02 23:44:13 UTC (rev 36183)
@@ -66,7 +66,12 @@
 
     /* Define options */
     opt_in = G_define_standard_option(G_OPT_R_INPUTS);
+    opt_in->description = _("Name of two or more input raster maps");
+
     opt_out = G_define_standard_option(G_OPT_R_OUTPUT);
+    opt_out->label = _("Base name for output raster maps");
+    opt_out->description =
+	_("A numerical suffix will be added for each component map");
 
     opt_scale = G_define_option();
     opt_scale->key = "rescale";
@@ -75,11 +80,12 @@
     opt_scale->required = NO;
     opt_scale->answer = "0,255";
     opt_scale->description =
-	_("Rescaling range output (For no rescaling use 0,0)");
+	_("Rescaling range for output maps (for no rescaling use 0,0)");
 
     if (G_parser(argc, argv))
 	exit(EXIT_FAILURE);
 
+
     /* determine number of bands passed in */
     for (bands = 0; opt_in->answers[bands] != NULL; bands++) ;
 
@@ -166,7 +172,7 @@
 	sprintf(outname, "%s.%d", opt_out->answer, i + 1);
 
 	/* write colors and history to file */
-	write_support(bands, outname, eigmat);
+	write_support(bands, outname, eigmat, eigval);
 
 	/* close output file */
 	G_unopen_cell(inp_fd[i]);

Modified: grass/branches/releasebranch_6_4/imagery/i.pca/support.c
===================================================================
--- grass/branches/releasebranch_6_4/imagery/i.pca/support.c	2009-03-02 22:43:14 UTC (rev 36182)
+++ grass/branches/releasebranch_6_4/imagery/i.pca/support.c	2009-03-02 23:44:13 UTC (rev 36183)
@@ -4,10 +4,10 @@
 
 
 /* function prototypes */
-static int write_history(int, char *, double **);
+static int write_history(int, char *, double **, double *);
 
 
-int write_support(int bands, char *outname, double **eigmat)
+int write_support(int bands, char *outname, double **eigmat, double *eigval)
 {
     char *mapset = G_mapset();
     struct Colors colors;
@@ -26,44 +26,51 @@
     if (G_write_colors(outname, mapset, &colors) < 0)
 	G_message(_("Unable to write color table for raster map <%s>"), outname);
 
-    return write_history(bands, outname, eigmat);
+    return write_history(bands, outname, eigmat, eigval);
 }
 
 
-static int write_history(int bands, char *outname, double **eigmat)
+static int write_history(int bands, char *outname, double **eigmat, double *eigval)
 {
     int i, j;
-    static int to_std = 0;	/* write to stderr? */
+    static int first_map = TRUE;     /* write to stderr? */
     struct History hist;
+    double eigval_total = 0.0;
 
     G_short_history(outname, "raster", &hist);
-    sprintf(hist.edhist[0], "Eigen vectors:");
+    sprintf(hist.edhist[0], "Eigen values, (vectors), and [percent importance]:");
 
-    if (!to_std)
-	G_message(_("Eigen values:"));
+    if(first_map)
+	G_message(_("Eigen values, (vectors), and [percent importance]:"));
 
+    for (i = 0; i < bands; i++)
+	eigval_total += eigval[i];
+
     for (i = 0; i < bands; i++) {
-	char tmpeigen[80], tmpa[80];
+	char tmpeigen[256], tmpa[80];
 
-	sprintf(tmpeigen, "( ");
+	sprintf(tmpeigen, "PC%d %9.2f ( ", i+1, eigval[i]);
 	for (j = 0; j < bands; j++) {
-	    sprintf(tmpa, "%.2f ", eigmat[i][j]);
+	    sprintf(tmpa, "%5.2f ", eigmat[i][j]);
 	    G_strcat(tmpeigen, tmpa);
 	}
-	G_strcat(tmpeigen, ")");
+	G_strcat(tmpeigen, ") ");
+	
+	sprintf(tmpa, "[ %5.2f%% ]", eigval[i] * 100/eigval_total);
+	G_strcat(tmpeigen, tmpa);
 
 	sprintf(hist.edhist[i + 1], tmpeigen);
 
 	/* write eigen values to screen */
-	if (!to_std)
+	if(first_map)
 	    G_message("%s", tmpeigen);
     }
 
     hist.edlinecnt = i + 1;
     G_command_history(&hist);
 
-    /* only write to stderr the first time */
-    to_std = 1;
+    /* only write to stderr the first time (this fn runs for every output map) */
+    first_map = FALSE;
 
     return G_write_history(outname, &hist);
 }



More information about the grass-commit mailing list