[GRASS-SVN] r70392 - grass/branches/releasebranch_7_2/raster/r.quantile

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jan 17 14:11:50 PST 2017


Author: mmetz
Date: 2017-01-17 14:11:49 -0800 (Tue, 17 Jan 2017)
New Revision: 70392

Modified:
   grass/branches/releasebranch_7_2/raster/r.quantile/main.c
Log:
r.quantile: backport trunk r69774

Modified: grass/branches/releasebranch_7_2/raster/r.quantile/main.c
===================================================================
--- grass/branches/releasebranch_7_2/raster/r.quantile/main.c	2017-01-17 21:51:25 UTC (rev 70391)
+++ grass/branches/releasebranch_7_2/raster/r.quantile/main.c	2017-01-17 22:11:49 UTC (rev 70392)
@@ -11,6 +11,7 @@
  *
  *****************************************************************************/
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <math.h>
 #include <grass/gis.h>
@@ -52,6 +53,9 @@
 
 static inline double get_quantile(int n)
 {
+    if (n >= num_quants)
+	return (double)total + total;
+
     return (double)total * quants[n];
 }
 
@@ -103,7 +107,8 @@
 	unsigned int count = slots[slot];
 	unsigned long accum2 = accum + count;
 
-	if (accum2 > next) {
+	if (accum2 > next ||
+	    (slot == num_slots - 1 && accum2 == next)) {
 	    struct bin *b = &bins[bin];
 
 	    slot_bins[slot] = ++bin;
@@ -196,7 +201,7 @@
     int quant;
 
     for (quant = 0; quant < num_quants; quant++) {
-	struct bin *b;
+	struct bin *b = &bins[bin];
 	double next = get_quantile(quant);
 	double k, v;
 	int i0, i1;
@@ -212,6 +217,8 @@
 	    i0 = (int)floor(k);
 	    i1 = (int)ceil(k);
 
+	    if (i0 > b->count - 1)
+		i0 = b->count - 1;
 	    if (i1 > b->count - 1)
 		i1 = b->count - 1;
 
@@ -224,9 +231,9 @@
 	    v = max;
 
 	if (recode)
-	    printf("%f:%f:%i\n", prev_v, v, quant + 1);
+	    fprintf(stdout, "%f:%f:%i\n", prev_v, v, quant + 1);
 	else
-	    printf("%d:%f:%f\n", quant, 100 * quants[quant], v);
+	    fprintf(stdout, "%d:%f:%f\n", quant, 100 * quants[quant], v);
 
 	prev_v = v;
     }
@@ -240,7 +247,7 @@
     struct GModule *module;
     struct
     {
-	struct Option *input, *quant, *perc, *slots;
+	struct Option *input, *quant, *perc, *slots, *file;
     } opt;
     struct {
 	struct Flag *r;
@@ -282,6 +289,12 @@
     opt.slots->description = _("Number of bins to use");
     opt.slots->answer = "1000000";
 
+    opt.file = G_define_standard_option(G_OPT_F_OUTPUT);
+    opt.file->key = "file";
+    opt.file->required = NO;
+    opt.file->description =
+	_("Name for output file (if omitted or \"-\" output to stdout)");
+
     flag.r = G_define_flag();
     flag.r->key = 'r';
     flag.r->description = _("Generate recode rules based on quantile-defined intervals");
@@ -292,6 +305,12 @@
     num_slots = atoi(opt.slots->answer);
     recode = flag.r->answer;
 
+    if (opt.file->answer != NULL && strcmp(opt.file->answer, "-") != 0) {
+	if (NULL == freopen(opt.file->answer, "w", stdout)) {
+	    G_fatal_error(_("Unable to open file <%s> for writing"), opt.file->answer);
+	}
+    }
+
     if (opt.perc->answer) {
 	int i;
 



More information about the grass-commit mailing list