[GRASS-SVN] r41096 - grass-addons/raster/r.univar2.zonal

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Feb 18 10:50:13 EST 2010


Author: mmetz
Date: 2010-02-18 10:50:11 -0500 (Thu, 18 Feb 2010)
New Revision: 41096

Modified:
   grass-addons/raster/r.univar2.zonal/globals.h
   grass-addons/raster/r.univar2.zonal/r.univar_main.c
   grass-addons/raster/r.univar2.zonal/r3.univar_main.c
   grass-addons/raster/r.univar2.zonal/stats.c
Log:
support for non-integer percentiles

Modified: grass-addons/raster/r.univar2.zonal/globals.h
===================================================================
--- grass-addons/raster/r.univar2.zonal/globals.h	2010-02-18 15:25:55 UTC (rev 41095)
+++ grass-addons/raster/r.univar2.zonal/globals.h	2010-02-18 15:50:11 UTC (rev 41096)
@@ -30,7 +30,7 @@
     double min;
     double max;
     unsigned int n_perc;
-    int *perc;
+    double *perc;
     double sum_abs;
     int n;
     int size;

Modified: grass-addons/raster/r.univar2.zonal/r.univar_main.c
===================================================================
--- grass-addons/raster/r.univar2.zonal/r.univar_main.c	2010-02-18 15:25:55 UTC (rev 41095)
+++ grass-addons/raster/r.univar2.zonal/r.univar_main.c	2010-02-18 15:50:11 UTC (rev 41096)
@@ -42,7 +42,7 @@
 
     param.percentile = G_define_option();
     param.percentile->key = "percentile";
-    param.percentile->type = TYPE_INTEGER;
+    param.percentile->type = TYPE_DOUBLE;
     param.percentile->required = NO;
     param.percentile->multiple = YES;
     param.percentile->options = "0-100";
@@ -228,7 +228,7 @@
     stats = create_univar_stat_struct(map_type, i);
     for (i = 0; i < n_zones; i++) {
 	for (j = 0; j < stats[i].n_perc; j++) {
-	    sscanf(param.percentile->answers[j], "%i", &(stats[i].perc[j]));
+	    sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j]));
 	}
     }
 

Modified: grass-addons/raster/r.univar2.zonal/r3.univar_main.c
===================================================================
--- grass-addons/raster/r.univar2.zonal/r3.univar_main.c	2010-02-18 15:25:55 UTC (rev 41095)
+++ grass-addons/raster/r.univar2.zonal/r3.univar_main.c	2010-02-18 15:50:11 UTC (rev 41096)
@@ -43,7 +43,7 @@
 
     param.percentile = G_define_option();
     param.percentile->key = "percentile";
-    param.percentile->type = TYPE_INTEGER;
+    param.percentile->type = TYPE_DOUBLE;
     param.percentile->required = NO;
     param.percentile->multiple = YES;
     param.percentile->options = "0-100";
@@ -82,7 +82,7 @@
     univar_stat *stats;
 
     char *infile, *zonemap;
-    void *map, *zmap;
+    void *map, *zmap = NULL;
     G3D_Region region;
     unsigned int i;
     unsigned int rows, cols, depths;
@@ -134,7 +134,7 @@
     zone_info.n_zones = 0;
 
     /* open 3D zoning raster with default region */
-    if ((zonemap = param.zonefile->answer)) {
+    if ((zonemap = param.zonefile->answer) != NULL) {
 	if (NULL == (mapset = G_find_grid3(zonemap, "")))
 	    G3d_fatalError(_("Requested g3d map <%s> not found"), zonemap);
 
@@ -194,6 +194,7 @@
 	i++;
     stats = create_univar_stat_struct(map_type, i);
     for (i = 0; i < zone_info.n_zones; i++) {
+	unsigned int j;
 	for (j = 0; j < stats[i].n_perc; j++) {
 	    sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j]));
 	}

Modified: grass-addons/raster/r.univar2.zonal/stats.c
===================================================================
--- grass-addons/raster/r.univar2.zonal/stats.c	2010-02-18 15:25:55 UTC (rev 41095)
+++ grass-addons/raster/r.univar2.zonal/stats.c	2010-02-18 15:50:11 UTC (rev 41096)
@@ -34,7 +34,7 @@
 	stats[i].max = 0.0 / 0.0;	/*set to nan as default */
 	stats[i].n_perc = n_perc;
 	if (n_perc > 0)
-	    stats[i].perc = (int *)G_malloc(n_perc * sizeof(int));
+	    stats[i].perc = (double *)G_malloc(n_perc * sizeof(double));
 	else
 	    stats[i].perc = NULL;
 	stats[i].sum_abs = 0.0;
@@ -240,8 +240,10 @@
 		fprintf(stdout, "median=%g\n", median);
 		fprintf(stdout, "third_quartile=%g\n", quartile_75);
 		for (i = 0; i < stats[z].n_perc; i++) {
-		    fprintf(stdout, "percentile_%d=%g\n", stats[z].perc[i],
-			    quartile_perc[i]);
+		    char buf[24];
+		    sprintf(buf, "%.15g", stats[z].perc[i]);
+		    G_strchg(buf, '.', '_');
+		    fprintf(stdout, "percentile_%s=%g\n", buf, quartile_perc[i]);
 		}
 	    }
 	    else {
@@ -255,18 +257,26 @@
 
 
 		for (i = 0; i < stats[z].n_perc; i++) {
-		    if (stats[z].perc[i] % 10 == 1 && stats[z].perc[i] != 11)
-			fprintf(stdout, "%dst percentile: %g\n", stats[z].perc[i],
+		    if (stats[z].perc[i] == (int)stats[z].perc[i]) {
+			/* percentile is an exact integer */
+			if ((int)stats[z].perc[i] % 10 == 1 && (int)stats[z].perc[i] != 11)
+			    fprintf(stdout, "%dst percentile: %g\n", (int)stats[z].perc[i],
+				    quartile_perc[i]);
+			else if ((int)stats[z].perc[i] % 10 == 2 && (int)stats[z].perc[i] != 12)
+			    fprintf(stdout, "%dnd percentile: %g\n", (int)stats[z].perc[i],
+				    quartile_perc[i]);
+			else if ((int)stats[z].perc[i] % 10 == 3 && (int)stats[z].perc[i] != 13)
+			    fprintf(stdout, "%drd percentile: %g\n", (int)stats[z].perc[i],
+				    quartile_perc[i]);
+			else
+			    fprintf(stdout, "%dth percentile: %g\n", (int)stats[z].perc[i],
+				    quartile_perc[i]);
+		    }
+		    else {
+			/* percentile is not an exact integer */
+			fprintf(stdout, "%.15g percentile: %g\n", stats[z].perc[i],
 				quartile_perc[i]);
-		    else if (stats[z].perc[i] % 10 == 2 && stats[z].perc[i] != 12)
-			fprintf(stdout, "%dnd percentile: %g\n", stats[z].perc[i],
-				quartile_perc[i]);
-		    else if (stats[z].perc[i] % 10 == 3 && stats[z].perc[i] != 13)
-			fprintf(stdout, "%drd percentile: %g\n", stats[z].perc[i],
-				quartile_perc[i]);
-		    else
-			fprintf(stdout, "%dth percentile: %g\n", stats[z].perc[i],
-				quartile_perc[i]);
+		    }
 		}
 	    }
 	    G_free((void *)quartile_perc);
@@ -313,7 +323,19 @@
 	fprintf(stdout, "%smedian", zone_info.sep);
 	fprintf(stdout, "%sthird_quart", zone_info.sep);
 	for (i = 0; i < stats[0].n_perc; i++) {
-	    fprintf(stdout, "%sperc_%d", zone_info.sep, stats[0].perc[i]);
+
+	    if (stats[0].perc[i] == (int)stats[0].perc[i]) {
+		/* percentile is an exact integer */
+		fprintf(stdout, "%sperc_%d", zone_info.sep, (int)stats[0].perc[i]);
+	    }
+	    else {
+		/* percentile is not an exact integer */
+
+		char buf[24];
+		sprintf(buf, "%.15g", stats[0].perc[i]);
+		G_strchg(buf, '.', '_');
+		fprintf(stdout, "%sperc_%s", zone_info.sep, buf);
+	    }
 	}
     }
     fprintf(stdout, "\n");



More information about the grass-commit mailing list