[GRASS-SVN] r66301 - grass-addons/grass7/imagery/i.spec.sam
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Sep 23 01:34:04 PDT 2015
Author: ychemin
Date: 2015-09-23 01:34:04 -0700 (Wed, 23 Sep 2015)
New Revision: 66301
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
Log:
continuing the modification towards Gmath
Modified: grass-addons/grass7/imagery/i.spec.sam/global.h
===================================================================
--- grass-addons/grass7/imagery/i.spec.sam/global.h 2015-09-23 07:43:43 UTC (rev 66300)
+++ grass-addons/grass7/imagery/i.spec.sam/global.h 2015-09-23 08:34:04 UTC (rev 66301)
@@ -1,5 +1,6 @@
#ifndef __GLOBAL_H__
#define __GLOBAL_H__
+#endif
#include <grass/config.h>
#include <grass/gis.h>
@@ -11,8 +12,10 @@
#define MAXFILES 255
-extern MAT *A;
-extern VEC *b, *Avector;
+//extern MAT *A;
+extern mat_struct *A;
+//extern VEC *b, *Avector;
+extern vec_struct *b, *Avector;
extern int matrixsize;
extern float curr_angle;
Modified: grass-addons/grass7/imagery/i.spec.sam/main.c
===================================================================
--- grass-addons/grass7/imagery/i.spec.sam/main.c 2015-09-23 07:43:43 UTC (rev 66300)
+++ grass-addons/grass7/imagery/i.spec.sam/main.c 2015-09-23 08:34:04 UTC (rev 66301)
@@ -20,14 +20,13 @@
#include <math.h>
#include <grass/gis.h>
#include <grass/raster.h>
-/*#include <grass/gmath.h>*/
+#include <grass/imagery.h>
+#include <grass/gmath.h>
#include "local_proto.h"
#include "global.h"
-#include <meschach/matrix.h>
-#include <meschach/matrix2.h>
-MAT *A;
-VEC *b, *Avector;
+mat_struct *A;
+vec_struct *b, *Avector;
int matrixsize;
float curr_angle;
@@ -86,7 +85,7 @@
parm.result->description = "Raster map prefix to hold spectral angles";
if (G_parser(argc,argv))
- exit(1);
+ G_fatal_error("Parsing arguments error");
result_prefix = parm.result->answer;
group = parm.group->answer;
@@ -114,21 +113,21 @@
b = get_row(A, j, VNULL); /* compare with next col in A */
spectral_angle();
anglefield[i][j]= curr_angle;
- V_FREE(b);
+ G_vector_free(b);
}
}
- V_FREE(Avector);
+ G_vector_free(Avector);
}
/* print out the result */
- fprintf(stderr,"Orthogonality check of Matrix A:\n");
+ G_message("Orthogonality check of Matrix A:\n");
for (i = 0; i < Ref.nfiles ; i++)
for (j = 0; j < Ref.nfiles ; j++)
{
if (j !=i)
- fprintf(stderr," Angle between row %i and row %i: %g\n", (i+1), (j+1), anglefield[i][j]);
+ G_message(" Angle between row %i and row %i: %g\n", (i+1), (j+1), anglefield[i][j]);
}
- fprintf(stderr,"\n");
+ G_message(stderr,"\n");
/* check it */
for (i = 0; i < Ref.nfiles ; i++)
@@ -136,27 +135,24 @@
if (j !=i)
if (anglefield[i][j] < 10.0)
{
- fprintf(stderr,"ERROR: Spectral entries row %i|%i in your matrix are linear dependent!\n",i,j);
+ G_message("ERROR: Spectral entries row %i|%i in your matrix are linear dependent!\n",i,j);
error=1;
}
if (!error)
- fprintf(stderr,"Spectral matrix is o.k. Proceeding...\n");
+ G_message("Spectral matrix is o.k. Proceeding...\n");
/* check singular values of Matrix A
* Ref: Boardman, J.W. 1989: Inversion of imaging spectrometry data
- * using singular value decomposition. IGARSS 1989: 12th Canadian
+ * using singular value decomposition. IGARSS 1989: 12th Canadian
* symposium on Remote Sensing. Vol.4 pp.2069-2072
*/
- fprintf(stderr,"\n");
- fprintf(stderr,"Singular values of Matrix A:");
+ G_message("Singular values of Matrix A:");
svd_values = svd(A, MNULL, MNULL, VNULL);
v_output(svd_values);
- fprintf(stderr,"\n");
if (error)
{
- fprintf(stderr,"Exiting...\n");
- exit(-1);
+ G_fatal_error("Exiting...\n");
}
/* alright, start Spectral angle mapping */
@@ -188,10 +184,10 @@
Avector = get_row(A, i, VNULL); /* go row-wise through matrix*/
spectral_angle();
result_cell[i][col] = myround (curr_angle);
- V_FREE(Avector);
+ G_vector_free(Avector);
}
- V_FREE(b);
+ G_vector_free(b);
} /* columns loop */
@@ -215,9 +211,9 @@
/* write a color table */
}
- M_FREE(A);
+ G_matrix_free(A);
make_history(result_name, group, matrixfile);
- exit(0);
+ return;
} /* main*/
Modified: grass-addons/grass7/imagery/i.spec.sam/open.c
===================================================================
--- grass-addons/grass7/imagery/i.spec.sam/open.c 2015-09-23 07:43:43 UTC (rev 66300)
+++ grass-addons/grass7/imagery/i.spec.sam/open.c 2015-09-23 08:34:04 UTC (rev 66301)
@@ -1,16 +1,5 @@
/* Spectral angle mapping
* (c) Oct/1998 Markus Neteler, Hannover
- *
- **************************************************************************
- ** Matrix computations based on Meschach Library
- ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
- **************************************************************************
- *
- * Cited references are from
- Steward, D.E, Leyk, Z. 1994: Meschach: Matrix computations in C.
- Proceedings of the centre for Mathematics and its Applicaions.
- The Australian National University. Vol. 32.
- ISBN 0 7315 1900 0
*/
#include "global.h"
@@ -18,9 +7,11 @@
#include <math.h>
#include <grass/gis.h>
#include <grass/raster.h>
-#include <meschach/matrix.h>
+#include <grass/imagery.h>
#include <grass/gmath.h>
+int G_matrix_read2(FILE * fp, mat_struct * out); /* Modified version of G_matrix_read(..). */
+
int open_files()
{
char *name, *mapset;
@@ -32,28 +23,27 @@
fp=fopen(matrixfile,"r");
if (fp == NULL)
G_fatal_error("ERROR: Matrixfile %s not found.\n",matrixfile);
- A = m_finput(fp, MNULL);
- fclose (fp);
+ /* Read data and close file */
+ if ((G_matrix_read2(fp, &A) < 0))
+ G_fatal_error(_("Unable to read matrix file %s."), matrixfile);
+ fclose(fp);
- /*IF GRASS/GMATH.H
if(A->rows < A->cols)
- */
- if ( A->m < A->n )
G_fatal_error("Need m (rows) >= n (cols) to obtain least squares fit\n");
+ /*Only for debug, so temporary disabled*/
+ /*
G_verbose_message("Your spectral matrix = ");
if (G_verbose() > G_verbose_std())
{
m_output(A);
}
- matrixsize = A->n;
- /*IF GRASS/GMATH.H
+ */
matrixsize=A->cols;
- */
+
/* open input files from group */
if (!I_find_group(group))
{
- G_message("group=%s - not found\n", group);
- exit(1);
+ G_fatal_error("group=%s - not found\n", group);
}
I_get_group_ref(group, &Ref);
if (Ref.nfiles <= 1)
@@ -69,7 +59,7 @@
if (Ref.nfiles != matrixsize)
{
G_message("Error: Number of %i input files in group <%s>\n", Ref.nfiles, group);
- G_fatal_error(" does not match matrix size (%i cols).\n", A->n);
+ G_fatal_error(" does not match matrix size (%i cols).\n", A->cols);
}
/* get memory for input files */
@@ -99,3 +89,47 @@
return(matrixsize); /* give back number of output files (= Ref.nfiles) */
}
+
+int G_matrix_read2(FILE * fp, mat_struct * out)
+{
+ char buff[100];
+ int rows, cols;
+ int i, j, row;
+ double val;
+
+ /* skip comments */
+ for (;;) {
+ if (!G_getl(buff, sizeof(buff), fp))
+ return -1;
+ if (buff[0] != '#')
+ break;
+ }
+
+ if (sscanf(buff, "Matrix: %d by %d", &rows, &cols) != 2) {
+ G_warning(_("Input format error1"));
+ return -1;
+ }
+
+
+ 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"));
+ return -1;
+ }
+
+ for (j = 0; j < cols; j++) {
+ if (fscanf(fp, "%lf:", &val) != 1) {
+ G_warning(_("Input format error"));
+ return -1;
+ }
+
+ fgetc(fp);
+ G_matrix_set_element(out, i, j, val);
+ }
+ }
+
+ return 0;
+}
More information about the grass-commit
mailing list