[GRASS-SVN] r29848 - grass/trunk/raster/r.univar2

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Jan 26 15:12:42 EST 2008


Author: martinl
Date: 2008-01-26 15:12:42 -0500 (Sat, 26 Jan 2008)
New Revision: 29848

Modified:
   grass/trunk/raster/r.univar2/globals.h
   grass/trunk/raster/r.univar2/r.univar_main.c
   grass/trunk/raster/r.univar2/r3.univar_main.c
   grass/trunk/raster/r.univar2/stats.c
Log:
r/r3.univar: support for multiple 'percentile' arguments added, #19

Modified: grass/trunk/raster/r.univar2/globals.h
===================================================================
--- grass/trunk/raster/r.univar2/globals.h	2008-01-26 18:14:13 UTC (rev 29847)
+++ grass/trunk/raster/r.univar2/globals.h	2008-01-26 20:12:42 UTC (rev 29848)
@@ -29,7 +29,8 @@
     double sumsq;
     double min;
     double max;
-    int perc;
+    unsigned int n_perc;
+    int *perc;
     double sum_abs;
     int n;
     int size;
@@ -57,7 +58,7 @@
 void heapsort_float(float *data, int n);
 void heapsort_int(int *data, int n);
 int print_stats(univar_stat * stats);
-univar_stat *create_univar_stat_struct(int map_type, int size);
+univar_stat *create_univar_stat_struct(int map_type, int size, int n_perc);
 void free_univar_stat_struct(univar_stat * stats);
 
 #endif

Modified: grass/trunk/raster/r.univar2/r.univar_main.c
===================================================================
--- grass/trunk/raster/r.univar2/r.univar_main.c	2008-01-26 18:14:13 UTC (rev 29847)
+++ grass/trunk/raster/r.univar2/r.univar_main.c	2008-01-26 20:12:42 UTC (rev 29848)
@@ -31,6 +31,7 @@
     param.percentile->key = "percentile";
     param.percentile->type = TYPE_INTEGER;
     param.percentile->required = NO;
+    param.percentile->multiple = YES;
     param.percentile->options = "0-100";
     param.percentile->answer = "90";
     param.percentile->description =
@@ -53,7 +54,7 @@
 /* *************************************************************** */
 int main(int argc, char *argv[])
 {
-
+    unsigned int i;
     unsigned int row, col;	/* counters */
     unsigned int rows, cols;	/*  totals  */
 
@@ -103,8 +104,13 @@
     rows = region.rows;		/* use G_window_rows(), G_window_cols() here? */
     cols = region.cols;
 
-    stats = create_univar_stat_struct(map_type, cols * rows);
-    sscanf(param.percentile->answer, "%i", &stats->perc);
+    i = 0;
+    while(param.percentile->answers[i])
+	i++;
+    stats = create_univar_stat_struct(map_type, cols * rows, i);
+    for(i = 0; i < stats->n_perc; i++) {
+	sscanf(param.percentile->answers[i], "%i", &stats->perc[i]);
+    }
 
     raster_row = G_calloc(cols, G_raster_size(map_type));
 

Modified: grass/trunk/raster/r.univar2/r3.univar_main.c
===================================================================
--- grass/trunk/raster/r.univar2/r3.univar_main.c	2008-01-26 18:14:13 UTC (rev 29847)
+++ grass/trunk/raster/r.univar2/r3.univar_main.c	2008-01-26 20:12:42 UTC (rev 29848)
@@ -32,6 +32,7 @@
     param.percentile->key = "percentile";
     param.percentile->type = TYPE_INTEGER;
     param.percentile->required = NO;
+    param.percentile->multiple = YES;
     param.percentile->options = "0-100";
     param.percentile->answer = "90";
     param.percentile->description =
@@ -65,6 +66,7 @@
     char *infile;
     void *map;
     G3D_Region region;
+    unsigned int i;
     unsigned int rows, cols, depths;
     unsigned int x, y, z;
 
@@ -110,9 +112,15 @@
 	G3d_fatalError(_("Error opening g3d map <%s>"), infile);
 
     map_type = G3d_tileTypeMap(map);
-    stats = create_univar_stat_struct(map_type, cols * rows * depths);
-    sscanf(param.percentile->answer, "%i", &stats->perc);
 
+    i = 0;
+    while(param.percentile->answers[i])
+	i++;
+    stats = create_univar_stat_struct(map_type, cols * rows, i);
+    for(i = 0; i < stats->n_perc; i++) {
+	sscanf(param.percentile->answers[i], "%i", &stats->perc[i]);
+    }
+
     stats->n = 0;
     for (z = 0; z < depths; z++) {	/*From the bottom to the top */
 	if (!(param.shell_style->answer))

Modified: grass/trunk/raster/r.univar2/stats.c
===================================================================
--- grass/trunk/raster/r.univar2/stats.c	2008-01-26 18:14:13 UTC (rev 29847)
+++ grass/trunk/raster/r.univar2/stats.c	2008-01-26 20:12:42 UTC (rev 29848)
@@ -16,7 +16,7 @@
 /* *************************************************************** */
 /* **** univar_stat constructor ********************************** */
 /* *************************************************************** */
-univar_stat *create_univar_stat_struct(int map_type, int size)
+univar_stat *create_univar_stat_struct(int map_type, int size, int n_perc)
 {
     univar_stat *stats;
 
@@ -26,7 +26,11 @@
     stats->sumsq = 0.0;
     stats->min = 0.0 / 0.0;	/*set to nan as default */
     stats->max = 0.0 / 0.0;	/*set to nan as default */
-    stats->perc = 0;
+    stats->n_perc = n_perc;
+    if (n_perc > 0)
+	stats->perc = (int *) G_malloc (n_perc * sizeof (int));
+    else
+	stats->perc = NULL;
     stats->sum_abs = 0.0;
     stats->n = 0;
     stats->size = size;
@@ -54,6 +58,8 @@
 /* *************************************************************** */
 void free_univar_stat_struct(univar_stat * stats)
 {
+    if (stats->perc) 
+	G_free(stats->perc);
     if (stats->dcell_array)
 	G_free(stats->dcell_array);
     if (stats->fcell_array)
@@ -75,9 +81,10 @@
     char sum_str[100];
     double mean, variance, stdev, var_coef;
     /*for extendet stats */
-    double quartile_25 = 0.0, quartile_75 = 0.0, quartile_perc = 0.0;
+    double quartile_25 = 0.0, quartile_75 = 0.0, *quartile_perc;
     double median = 0.0;
-    int qpos_25, qpos_75, qpos_perc;
+    unsigned int i;
+    int qpos_25, qpos_75, *qpos_perc;
 
 
     /* all these calculations get promoted to doubles, so any DIV0 becomes nan */
@@ -127,9 +134,13 @@
 
     /* TODO: mode, skewness, kurtosis */
     if (param.extended->answer) {
+	qpos_perc = (int *) G_calloc(stats->n_perc, sizeof(int));
+	quartile_perc = (double *) G_calloc(stats->n_perc, sizeof(double));
+	for (i = 0; i < stats->n_perc; i++) {
+	    qpos_perc[i] = (int) (stats->n * stats->perc[i] / 100. - 0.5);
+	}
 	qpos_25 = (int)(stats->n * 0.25 - 0.5);
 	qpos_75 = (int)(stats->n * 0.75 - 0.5);
-	qpos_perc = (int)(stats->n * stats->perc / 100. - 0.5);
 
 	switch (stats->map_type) {
 	case CELL_TYPE:
@@ -143,7 +154,9 @@
 		    (double)(stats->cell_array[stats->n / 2 - 1] +
 			     stats->cell_array[stats->n / 2]) / 2.0;
 	    quartile_75 = (double)stats->cell_array[qpos_75];
-	    quartile_perc = (double)stats->cell_array[qpos_perc];
+	    for (i = 0; i < stats->n_perc; i++) {
+		quartile_perc[i] = (double) stats->cell_array[qpos_perc[i]];
+	    }
 	    break;
 
 	case FCELL_TYPE:
@@ -157,7 +170,9 @@
 		    (double)(stats->fcell_array[stats->n / 2 - 1] +
 			     stats->fcell_array[stats->n / 2]) / 2.0;
 	    quartile_75 = (double)stats->fcell_array[qpos_75];
-	    quartile_perc = (double)stats->fcell_array[qpos_perc];
+	    for (i = 0; i < stats->n_perc; i++) {
+		quartile_perc[i] = (double)stats->fcell_array[qpos_perc[i]];
+	    }
 	    break;
 
 	case DCELL_TYPE:
@@ -171,7 +186,9 @@
 		    (stats->dcell_array[stats->n / 2 - 1] +
 		     stats->dcell_array[stats->n / 2]) / 2.0;
 	    quartile_75 = stats->dcell_array[qpos_75];
-	    quartile_perc = stats->dcell_array[qpos_perc];
+	    for (i = 0; i < stats->n_perc; i++) {
+		quartile_perc[i] = stats->dcell_array[qpos_perc[i]];
+	    }
 	    break;
 
 	default:
@@ -182,7 +199,15 @@
 	    fprintf(stdout, "first_quartile=%g\n", quartile_25);
 	    fprintf(stdout, "median=%g\n", median);
 	    fprintf(stdout, "third_quartile=%g\n", quartile_75);
-	    fprintf(stdout, "percentile_%d=%g\n", stats->perc, quartile_perc);
+	    for (i = 0; i < stats->n_perc; i++) {
+		if (stats->perc[i] == 25 ||
+		    stats->perc[i] == 50 ||
+		    stats->perc[i] == 75) {
+		    /* skip 1st quartile, median, 3rd quartile */
+		    continue;
+		}
+		fprintf(stdout, "percentile_%d=%g\n", stats->perc[i], quartile_perc[i]);
+	    }
 	}
 	else {
 	    fprintf(stdout, "1st quartile: %g\n", quartile_25);
@@ -192,22 +217,32 @@
 		fprintf(stdout, "median (even number of cells): %g\n", median);
 	    fprintf(stdout, "3rd quartile: %g\n", quartile_75);
 
-	    if (stats->perc % 10 == 1 && stats->perc != 11)
-		fprintf(stdout, "%dst percentile: %g\n", stats->perc,
-			quartile_perc);
-	    else if (stats->perc % 10 == 2 && stats->perc != 12)
-		fprintf(stdout, "%dnd percentile: %g\n", stats->perc,
-			quartile_perc);
-	    else if (stats->perc % 10 == 3 && stats->perc != 13)
-		fprintf(stdout, "%drd percentile: %g\n", stats->perc,
-			quartile_perc);
-	    else
-		fprintf(stdout, "%dth percentile: %g\n", stats->perc,
-			quartile_perc);
+
+	    for (i = 0; i < stats->n_perc; i++) {
+		if (stats->perc[i] == 25 ||
+		    stats->perc[i] == 50 ||
+		    stats->perc[i] == 75) {
+		    /* skip 1st quartile, median, 3rd quartile */
+		    continue;
+		}
+		if (stats->perc[i] % 10 == 1 && stats->perc[i] != 11)
+		    fprintf(stdout, "%dst percentile: %g\n", stats->perc[i],
+			    quartile_perc[i]);
+		else if (stats->perc[i] % 10 == 2 && stats->perc[i] != 12)
+		    fprintf(stdout, "%dnd percentile: %g\n", stats->perc[i],
+			    quartile_perc[i]);
+		else if (stats->perc[i] % 10 == 3 && stats->perc[i] != 13)
+		    fprintf(stdout, "%drd percentile: %g\n", stats->perc[i],
+			    quartile_perc[i]);
+		else
+		    fprintf(stdout, "%dth percentile: %g\n", stats->perc[i],
+			    quartile_perc[i]);
+	    }
 	}
+	G_free ((void *) quartile_perc);
+	G_free ((void *) qpos_perc);
     }
 
-
     if (!(param.shell_style->answer))
 	G_message("\n");
 



More information about the grass-commit mailing list