[GRASS-SVN] r66750 - grass-addons/grass7/imagery/i.spec.sam

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Nov 5 10:25:54 PST 2015


Author: ychemin
Date: 2015-11-05 10:25:54 -0800 (Thu, 05 Nov 2015)
New Revision: 66750

Modified:
   grass-addons/grass7/imagery/i.spec.sam/global.h
   grass-addons/grass7/imagery/i.spec.sam/main.c
   grass-addons/grass7/imagery/i.spec.sam/open.c
   grass-addons/grass7/imagery/i.spec.sam/spectrum.dat
Log:
fixed matrix read, now breaks at checking matrix orthogonality

Modified: grass-addons/grass7/imagery/i.spec.sam/global.h
===================================================================
--- grass-addons/grass7/imagery/i.spec.sam/global.h	2015-11-05 14:12:13 UTC (rev 66749)
+++ grass-addons/grass7/imagery/i.spec.sam/global.h	2015-11-05 18:25:54 UTC (rev 66750)
@@ -13,7 +13,6 @@
 
 #define MAXFILES 255
 
-extern mat_struct *A;
 extern vec_struct *b, *Avector;
 extern int matrixsize;
 extern float curr_angle;
@@ -31,5 +30,5 @@
 extern int  error_fd;
 
 extern char result_name[80];
-extern char *result_prefix, *matrixfile;
+extern char *result_prefix;
 

Modified: grass-addons/grass7/imagery/i.spec.sam/main.c
===================================================================
--- grass-addons/grass7/imagery/i.spec.sam/main.c	2015-11-05 14:12:13 UTC (rev 66749)
+++ grass-addons/grass7/imagery/i.spec.sam/main.c	2015-11-05 18:25:54 UTC (rev 66750)
@@ -40,12 +40,10 @@
 
 struct GModule *module;
 
-mat_struct *A;
 vec_struct *b, *Avector;
 int matrixsize;
 float curr_angle;
 
-char *group;
 struct Ref Ref;
 
 CELL **cell;
@@ -58,9 +56,9 @@
 int  error_fd;
 
 char result_name[80];
-char *result_prefix, *matrixfile;
+char *result_prefix;
 
-int open_files();
+mat_struct  *open_files(char * matrixfile, char *img_grp);
 void spectral_angle();
 CELL myround(double x);
 
@@ -78,6 +76,9 @@
 	struct Option *group, *matrixfile, *output;
     } parm;
 
+    mat_struct *A; /*first use in open.c G_matrix_set()*/
+    char *group;
+
     G_gisinit (argv[0]);
 
     module = G_define_module();
@@ -88,13 +89,10 @@
 
 
     parm.group = G_define_standard_option(G_OPT_I_GROUP);
-    parm.group->description = "Imagery group containing images to be analyzed with Spectral Mixture Analyis";
+    parm.group->description = "Imagery group to target for Spectral Mixture Analyis";
 
-    parm.matrixfile = G_define_option();
-    parm.matrixfile->key = "matrixfile";
-    parm.matrixfile->type = TYPE_STRING;
-    parm.matrixfile->required = YES;
-    parm.matrixfile->description = "Matrix file containing spectral signatures ";
+    parm.matrixfile = G_define_standard_option(G_OPT_F_INPUT);
+    parm.matrixfile->description = "Matrix file containing spectral signatures";
 
     parm.output = G_define_option();
     parm.output->key = "result";
@@ -106,13 +104,11 @@
 	G_fatal_error("Parsing arguments error");
 
     result_prefix = parm.output->answer;
-    group       = parm.group->answer;
-    matrixfile  = parm.matrixfile->answer;
 
+    G_message("%s",result_prefix);
 
-/* here we go... */
-
-    open_files();
+    /*Creating A, the spectral signature matrix here*/
+    A = open_files(parm.matrixfile->answer, parm.group->answer);
    /* Spectral Matrix is stored in A now */
 
   /* Check matrix orthogonality 
@@ -121,6 +117,7 @@
    *          TM data. Photogrammetric Engineering & Remote Sensing, Vol.63, No6.
    */
      
+    G_message("/* Check matrix orthogonality*/"); 
     for (i = 0; i < Ref.nfiles; i++) /* Ref.nfiles = matrixsize*/
     {
      Avector = G_matvect_get_row(A, i);  /* go columnwise through matrix*/
@@ -229,7 +226,7 @@
           /* write a color table */
     }
     G_matrix_free(A);
-    make_history(result_name, group, matrixfile);
+    make_history(result_name, parm.group->answer, parm.matrixfile->answer);
     return(EXIT_SUCCESS);
 } /* main*/
 

Modified: grass-addons/grass7/imagery/i.spec.sam/open.c
===================================================================
--- grass-addons/grass7/imagery/i.spec.sam/open.c	2015-11-05 14:12:13 UTC (rev 66749)
+++ grass-addons/grass7/imagery/i.spec.sam/open.c	2015-11-05 18:25:54 UTC (rev 66750)
@@ -2,7 +2,6 @@
  * (c) Oct/1998 Markus Neteler, Hannover
 */
 
-#include "global.h"
 #include <stdio.h>
 #include <math.h>
 #include <grass/gis.h>
@@ -10,25 +9,35 @@
 #include <grass/imagery.h>
 #include <grass/gmath.h>
 #include <grass/glocale.h>
+#include "global.h"
 
 int G_matrix_read2(FILE * fp, mat_struct * out); /* Modified version of G_matrix_read(..). */
 
-int open_files()
+mat_struct *open_files(char *matrixfile, char *img_grp)
 {
     char *name, *mapset;
     FILE *fp;
     int i;
+    mat_struct A_input, *A;
 
-/* read in matrix file with spectral library */
+    /* read in matrix file with spectral library */
 
     fp=fopen(matrixfile,"r");
     if (fp == NULL)
     	G_fatal_error("ERROR: Matrixfile %s not found.\n",matrixfile);
     /* Read data and close file */
-    if ((G_matrix_read2(fp, A) < 0))
+    if ((G_matrix_read2(fp, &A_input) < 0))
 	G_fatal_error(_("Unable to read matrix file %s."), matrixfile);
     fclose(fp);
-    
+
+    G_message("matrixfile read");
+
+    A = G_matrix_init(A_input.rows, A_input.cols, A_input.rows);
+    if (A == NULL)
+        G_fatal_error(_("Unable to allocate memory for matrix"));
+
+    A = G_matrix_copy(&A_input);
+
     if(A->rows < A->cols)
 	G_fatal_error("Need m (rows) >= n (cols) to obtain least squares fit\n");
     /*Only for debug, so temporary disabled*/
@@ -41,15 +50,16 @@
     */
     matrixsize=A->cols;
 
+    G_message("/* open input files from group */");
 /* open input files from group */
-    if (!I_find_group(group))
+    if (!I_find_group(img_grp))
     {
-	G_fatal_error("group=%s - not found\n", group);
+	G_fatal_error("group=%s - not found\n", img_grp);
     }
-    I_get_group_ref(group, &Ref);
+    I_get_group_ref(img_grp, &Ref);
     if (Ref.nfiles <= 1)
     {
-	G_message("Group %s\n", group);
+	G_message("Group %s\n", img_grp);
 	if (Ref.nfiles <= 0)
 	    G_message("doesn't have any files\n");
 	else
@@ -59,10 +69,11 @@
    /* Error check: input file number must be equal to matrix size */
     if (Ref.nfiles != matrixsize) 
     {
-	G_message("Error: Number of %i input files in group <%s>\n", Ref.nfiles, group);
+	G_message("Error: Number of %i input files in group <%s>\n", Ref.nfiles, img_grp);
  	G_fatal_error("       does not match matrix size (%i cols).\n", A->cols);
     }
 
+   G_message("/* get memory for input files */");
    /* get memory for input files */
     cell = (CELL **) G_malloc (Ref.nfiles * sizeof (CELL *));
     cellfd = (int *) G_malloc (Ref.nfiles * sizeof (int));
@@ -76,6 +87,7 @@
 	    G_fatal_error("Unable to proceed\n");
     }
 
+    G_message("/* open files for results*/");
   /* open files for results*/
     result_cell = (CELL **) G_malloc (Ref.nfiles * sizeof (CELL *));
     resultfd = (int *) G_malloc (Ref.nfiles * sizeof (int));
@@ -87,8 +99,8 @@
 	resultfd[i] = Rast_open_c_new (result_name);
     }
 
-
- return(matrixsize); /* give back number of output files (= Ref.nfiles) */
+    G_message("open.c: Returning A");
+ return A; /* give back number of output files (= Ref.nfiles) */
 }
 
 int G_matrix_read2(FILE * fp, mat_struct * out)
@@ -97,6 +109,7 @@
     int rows, cols;
     int i, j, row;
     double val;
+    int err;
 
     /* skip comments */
     for (;;) {
@@ -111,19 +124,20 @@
 	return -1;
     }
 
+    G_message("Set Matrix rows:%d by cols:%d",rows,cols);
+    err = G_matrix_set(out, rows, cols, rows);
+    G_message("Set Matrix rows:%d by cols:%d is done (err:%d)",rows,cols,err);
 
-    G_matrix_set(out, rows, cols, rows);
 
-
     for (i = 0; i < rows; i++) {
 	if (fscanf(fp, "row%d:", &row) != 1) {
-	    G_warning(_("Input format error"));
+	    G_warning(_("Input format error at row %d"),row);
 	    return -1;
 	}
 
 	for (j = 0; j < cols; j++) {
-	    if (fscanf(fp, "%lf:", &val) != 1) {
-		G_warning(_("Input format error"));
+	    if (fscanf(fp, " %lf", &val) != 1) {
+		G_warning(_("Input format error at col %d"),j);
 		return -1;
 	    }
 

Modified: grass-addons/grass7/imagery/i.spec.sam/spectrum.dat
===================================================================
--- grass-addons/grass7/imagery/i.spec.sam/spectrum.dat	2015-11-05 14:12:13 UTC (rev 66749)
+++ grass-addons/grass7/imagery/i.spec.sam/spectrum.dat	2015-11-05 18:25:54 UTC (rev 66750)
@@ -4,9 +4,10 @@
 # 2. Saltbush
 # 3. Ground
 # 4. Dry Grass
-#
+#row0:  8.87  13.14  11.71  35.85 
 Matrix: 4 by 4
-row0:  8.87  13.14  11.71  35.85 
+row0:  8.87  13.14  11.71  35.85
 row1: 13.59  20.12  19.61  50.66
 row2: 28.26  34.82  38.27  40.1
 row3: 10.54  16.35  23.7   38.98
+



More information about the grass-commit mailing list