[GRASS-SVN] r36152 - grass/branches/develbranch_6/imagery/i.pca

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Mar 1 03:42:19 EST 2009


Author: hamish
Date: 2009-03-01 03:42:18 -0500 (Sun, 01 Mar 2009)
New Revision: 36152

Modified:
   grass/branches/develbranch_6/imagery/i.pca/local_proto.h
   grass/branches/develbranch_6/imagery/i.pca/main.c
   grass/branches/develbranch_6/imagery/i.pca/support.c
Log:
write out both eigen vectors and values

Modified: grass/branches/develbranch_6/imagery/i.pca/local_proto.h
===================================================================
--- grass/branches/develbranch_6/imagery/i.pca/local_proto.h	2009-03-01 07:21:23 UTC (rev 36151)
+++ grass/branches/develbranch_6/imagery/i.pca/local_proto.h	2009-03-01 08:42:18 UTC (rev 36152)
@@ -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/develbranch_6/imagery/i.pca/main.c
===================================================================
--- grass/branches/develbranch_6/imagery/i.pca/main.c	2009-03-01 07:21:23 UTC (rev 36151)
+++ grass/branches/develbranch_6/imagery/i.pca/main.c	2009-03-01 08:42:18 UTC (rev 36152)
@@ -80,6 +80,7 @@
     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 +167,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/develbranch_6/imagery/i.pca/support.c
===================================================================
--- grass/branches/develbranch_6/imagery/i.pca/support.c	2009-03-01 07:21:23 UTC (rev 36151)
+++ grass/branches/develbranch_6/imagery/i.pca/support.c	2009-03-01 08:42:18 UTC (rev 36152)
@@ -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 (vectors) and values:");
 
-    if (!to_std)
-	G_message(_("Eigen values:"));
+    if(first_map)
+	G_message(_("Eigen (vectors) and values:"));
 
+    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 ( ", i+1);
 	for (j = 0; j < bands; j++) {
 	    sprintf(tmpa, "%.2f ", eigmat[i][j]);
 	    G_strcat(tmpeigen, tmpa);
 	}
-	G_strcat(tmpeigen, ")");
+	G_strcat(tmpeigen, ") ");
+	
+	sprintf(tmpa, "%.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