[GRASS-SVN] r58323 - grass/trunk/raster/r.topmodel

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Nov 28 08:32:12 PST 2013


Author: hcho
Date: 2013-11-28 08:32:12 -0800 (Thu, 28 Nov 2013)
New Revision: 58323

Modified:
   grass/trunk/raster/r.topmodel/file_io.c
Log:
nch parameter now automatically determined from the number of distance increments to avoid mistakes

Modified: grass/trunk/raster/r.topmodel/file_io.c
===================================================================
--- grass/trunk/raster/r.topmodel/file_io.c	2013-11-28 16:05:11 UTC (rev 58322)
+++ grass/trunk/raster/r.topmodel/file_io.c	2013-11-28 16:32:12 UTC (rev 58323)
@@ -37,7 +37,7 @@
     topidxstats.Aatb_r = NULL;
     misc.ncells = 0;
 
-    for (i = 0, j = 0; !feof(fp);) {
+    for (i = 0; !feof(fp);) {
 	double atb;
 	double Aatb_r;
 
@@ -45,16 +45,16 @@
 
 	if (sscanf(buf, "%lf %lf", &atb, &Aatb_r) == 2) {
 	    topidxstats.atb = (double *)G_realloc(topidxstats.atb,
-			    (j + 1) * sizeof(double));
+			    (i + 1) * sizeof(double));
 	    topidxstats.Aatb_r = (double *)G_realloc(topidxstats.Aatb_r,
-			    (j + 1) * sizeof(double));
-	    topidxstats.atb[j] = atb;
-	    topidxstats.Aatb_r[j] = Aatb_r;
-	    misc.ncells += (int)topidxstats.Aatb_r[j++];
+			    (i + 1) * sizeof(double));
+	    topidxstats.atb[i] = atb;
+	    topidxstats.Aatb_r[i] = Aatb_r;
+	    misc.ncells += (int)topidxstats.Aatb_r[i++];
 	}
     }
 
-    misc.ntopidxclasses = j;
+    misc.ntopidxclasses = i;
 
     fclose(fp);
 
@@ -127,21 +127,23 @@
 	    break;
     }
 
-    for (; !feof(fp);) {
-	get_line(fp, buf);
+    params.d = NULL;
+    params.Ad_r = NULL;
 
-	if (sscanf(buf, "%d", &(params.nch)) == 1)
-	    break;
-    }
+    for (i = 0; !feof(fp);) {
+	double d;
+	double Ad_r;
 
-    params.d = (double *)G_malloc(params.nch * sizeof(double));
-    params.Ad_r = (double *)G_malloc(params.nch * sizeof(double));
-
-    for (i = 0; i < params.nch && !feof(fp);) {
 	get_line(fp, buf);
 
-	if (sscanf(buf, "%lf %lf", &(params.d[i]), &(params.Ad_r[i])) == 2)
-	    i++;
+	if (sscanf(buf, "%lf %lf", &d, &Ad_r) == 2) {
+	    params.d = (double *)G_realloc(params.d,
+			    (i + 1) * sizeof(double));
+	    params.Ad_r = (double *)G_realloc(params.Ad_r,
+			    (i + 1) * sizeof(double));
+	    params.d[i] = d;
+	    params.Ad_r[i++] = Ad_r;
+	}
     }
 
     params.nch = i;



More information about the grass-commit mailing list