[GRASS-SVN] r38977 - in grass/branches/develbranch_6: include lib/stats raster/r.series

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Sep 4 18:05:15 EDT 2009


Author: neteler
Date: 2009-09-04 18:05:14 -0400 (Fri, 04 Sep 2009)
New Revision: 38977

Modified:
   grass/branches/develbranch_6/include/stats.h
   grass/branches/develbranch_6/lib/stats/c_ave.c
   grass/branches/develbranch_6/lib/stats/c_count.c
   grass/branches/develbranch_6/lib/stats/c_divr.c
   grass/branches/develbranch_6/lib/stats/c_intr.c
   grass/branches/develbranch_6/lib/stats/c_kurt.c
   grass/branches/develbranch_6/lib/stats/c_max.c
   grass/branches/develbranch_6/lib/stats/c_maxx.c
   grass/branches/develbranch_6/lib/stats/c_median.c
   grass/branches/develbranch_6/lib/stats/c_min.c
   grass/branches/develbranch_6/lib/stats/c_minx.c
   grass/branches/develbranch_6/lib/stats/c_mode.c
   grass/branches/develbranch_6/lib/stats/c_percentile.c
   grass/branches/develbranch_6/lib/stats/c_range.c
   grass/branches/develbranch_6/lib/stats/c_reg.c
   grass/branches/develbranch_6/lib/stats/c_skew.c
   grass/branches/develbranch_6/lib/stats/c_stddev.c
   grass/branches/develbranch_6/lib/stats/c_sum.c
   grass/branches/develbranch_6/lib/stats/c_var.c
   grass/branches/develbranch_6/raster/r.series/main.c
Log:
arbitrary quantiles backported from trunk

Modified: grass/branches/develbranch_6/include/stats.h
===================================================================
--- grass/branches/develbranch_6/include/stats.h	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/include/stats.h	2009-09-04 22:05:14 UTC (rev 38977)
@@ -4,8 +4,8 @@
 
 #include <grass/gis.h>
 
-typedef void stat_func(DCELL *, DCELL *, int);
-typedef void stat_func_w(DCELL *, DCELL(*)[2], int);
+typedef void stat_func(DCELL *, DCELL *, int, const void *);
+typedef void stat_func_w(DCELL *, DCELL(*)[2], int, const void *);
 
 extern stat_func c_ave;
 extern stat_func c_count;
@@ -27,6 +27,7 @@
 extern stat_func c_quart1;
 extern stat_func c_quart3;
 extern stat_func c_perc90;
+extern stat_func c_quant;
 extern stat_func c_skew;
 extern stat_func c_kurt;
 

Modified: grass/branches/develbranch_6/lib/stats/c_ave.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_ave.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_ave.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,6 +1,6 @@
 #include <grass/gis.h>
 
-void c_ave(DCELL * result, DCELL * values, int n)
+void c_ave(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL sum;
     int count;
@@ -23,7 +23,7 @@
 	*result = sum / count;
 }
 
-void w_ave(DCELL * result, DCELL(*values)[2], int n)
+void w_ave(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     DCELL sum;
     DCELL count;

Modified: grass/branches/develbranch_6/lib/stats/c_count.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_count.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_count.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,6 +1,6 @@
 #include <grass/gis.h>
 
-void c_count(DCELL * result, DCELL * values, int n)
+void c_count(DCELL * result, DCELL * values, int n, const void *closure)
 {
     int count;
     int i;
@@ -14,7 +14,7 @@
     *result = count;
 }
 
-void w_count(DCELL * result, DCELL(*values)[2], int n)
+void w_count(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     DCELL count;
     int i;

Modified: grass/branches/develbranch_6/lib/stats/c_divr.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_divr.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_divr.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,7 +1,7 @@
 #include <grass/gis.h>
 #include <grass/stats.h>
 
-void c_divr(DCELL * result, DCELL * values, int n)
+void c_divr(DCELL * result, DCELL * values, int n, const void *closure)
 {
     int count;
     DCELL prev;

Modified: grass/branches/develbranch_6/lib/stats/c_intr.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_intr.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_intr.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,7 +1,7 @@
 #include <grass/gis.h>
 #include <grass/stats.h>
 
-void c_intr(DCELL * result, DCELL * values, int n)
+void c_intr(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL center;
     int count;

Modified: grass/branches/develbranch_6/lib/stats/c_kurt.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_kurt.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_kurt.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,6 +1,6 @@
 #include <grass/gis.h>
 
-void c_kurt(DCELL * result, DCELL * values, int n)
+void c_kurt(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL sum, ave, sumsq, sumqt, var;
     int count;
@@ -42,7 +42,7 @@
     *result = sumqt / (count * var * var) - 3;
 }
 
-void w_kurt(DCELL * result, DCELL(*values)[2], int n)
+void w_kurt(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     DCELL sum, ave, sumsq, sumqt, var;
     int count;

Modified: grass/branches/develbranch_6/lib/stats/c_max.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_max.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_max.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,6 +1,6 @@
 #include <grass/gis.h>
 
-void c_max(DCELL * result, DCELL * values, int n)
+void c_max(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL max;
     int i;
@@ -20,3 +20,24 @@
     else
 	*result = max;
 }
+
+void w_max(DCELL * result, DCELL(*values)[2], int n, const void *closure)
+{
+    DCELL max;
+    int i;
+
+    G_set_d_null_value(&max, 1);
+
+    for (i = 0; i < n; i++) {
+	if (G_is_d_null_value(&values[i][0]))
+	    continue;
+
+	if (G_is_d_null_value(&max) || max < values[i][0])
+	    max = values[i][0];
+    }
+
+    if (G_is_d_null_value(&max))
+	G_set_d_null_value(result, 1);
+    else
+	*result = max;
+}

Modified: grass/branches/develbranch_6/lib/stats/c_maxx.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_maxx.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_maxx.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,6 +1,6 @@
 #include <grass/gis.h>
 
-void c_maxx(DCELL * result, DCELL * values, int n)
+void c_maxx(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL max, maxx;
     int i;

Modified: grass/branches/develbranch_6/lib/stats/c_median.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_median.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_median.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,7 +1,7 @@
 #include <grass/gis.h>
 #include <grass/stats.h>
 
-void c_median(DCELL * result, DCELL * values, int n)
+void c_median(DCELL * result, DCELL * values, int n, const void *closure)
 {
     n = sort_cell(values, n);
 
@@ -11,7 +11,7 @@
 	*result = (values[(n - 1) / 2] + values[n / 2]) / 2;
 }
 
-void w_median(DCELL * result, DCELL(*values)[2], int n)
+void w_median(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     DCELL total;
     int i;

Modified: grass/branches/develbranch_6/lib/stats/c_min.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_min.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_min.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,6 +1,6 @@
 #include <grass/gis.h>
 
-void c_min(DCELL * result, DCELL * values, int n)
+void c_min(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL min;
     int i;
@@ -20,3 +20,24 @@
     else
 	*result = min;
 }
+
+void w_min(DCELL * result, DCELL(*values)[2], int n, const void *closure)
+{
+    DCELL min;
+    int i;
+
+    G_set_d_null_value(&min, 1);
+
+    for (i = 0; i < n; i++) {
+	if (G_is_d_null_value(&values[i][0]))
+	    continue;
+
+	if (G_is_d_null_value(&min) || min > values[i][0])
+	    min = values[i][0];
+    }
+
+    if (G_is_d_null_value(&min))
+	G_set_d_null_value(result, 1);
+    else
+	*result = min;
+}

Modified: grass/branches/develbranch_6/lib/stats/c_minx.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_minx.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_minx.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,7 +1,7 @@
 #include <grass/gis.h>
 
 
-void c_minx(DCELL * result, DCELL * values, int n)
+void c_minx(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL min, minx;
     int i;

Modified: grass/branches/develbranch_6/lib/stats/c_mode.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_mode.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_mode.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,7 +1,7 @@
 #include <grass/gis.h>
 #include <grass/stats.h>
 
-void c_mode(DCELL * result, DCELL * values, int n)
+void c_mode(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL mode;
     int max;
@@ -34,7 +34,7 @@
 	*result = mode;
 }
 
-void w_mode(DCELL * result, DCELL(*values)[2], int n)
+void w_mode(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     DCELL mode;
     DCELL max;

Modified: grass/branches/develbranch_6/lib/stats/c_percentile.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_percentile.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_percentile.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,9 +1,11 @@
 #include <math.h>
+
 #include <grass/gis.h>
 #include <grass/stats.h>
 
-static void percentile(DCELL * result, DCELL * values, int n, double percent)
+void c_quant(DCELL * result, DCELL * values, int n, const void *closure)
 {
+    double quant = *(const double *)closure;
     double k;
     int i0, i1;
 
@@ -14,7 +16,7 @@
 	return;
     }
 
-    k = n * percent / 100;
+    k = n * quant;
     i0 = (int)floor(k);
     i1 = (int)ceil(k);
 
@@ -23,24 +25,27 @@
 	: values[i0] * (i1 - k) + values[i1] * (k - i0);
 }
 
-void c_quart1(DCELL * result, DCELL * values, int n)
+void c_quart1(DCELL * result, DCELL * values, int n, const void *closure)
 {
-    percentile(result, values, n, 25.0);
+    static const double q = 0.25;
+    c_quant(result, values, n, &q);
 }
 
-void c_quart3(DCELL * result, DCELL * values, int n)
+void c_quart3(DCELL * result, DCELL * values, int n, const void *closure)
 {
-    percentile(result, values, n, 75.0);
+    static const double q = 0.75;
+    c_quant(result, values, n, &q);
 }
 
-void c_perc90(DCELL * result, DCELL * values, int n)
+void c_perc90(DCELL * result, DCELL * values, int n, const void *closure)
 {
-    percentile(result, values, n, 90.0);
+    static const double q = 0.90;
+    c_quant(result, values, n, &q);
 }
 
-static void percentile_w(DCELL * result, DCELL(*values)[2], int n,
-			 double percent)
+void w_quant(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
+    double quant = *(const double *)closure;
     DCELL total;
     int i;
     DCELL k;
@@ -59,24 +64,27 @@
     k = 0.0;
     for (i = 0; i < n; i++) {
 	k += values[i][1];
-	if (k >= total * percent / 100)
+	if (k >= total * quant)
 	    break;
     }
 
     *result = values[i][0];
 }
 
-void w_quart1(DCELL * result, DCELL(*values)[2], int n)
+void w_quart1(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
-    percentile_w(result, values, n, 25.0);
+    static const double q = 0.25;
+    w_quant(result, values, n, &q);
 }
 
-void w_quart3(DCELL * result, DCELL(*values)[2], int n)
+void w_quart3(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
-    percentile_w(result, values, n, 75.0);
+    static const double q = 0.75;
+    w_quant(result, values, n, &q);
 }
 
-void w_perc90(DCELL * result, DCELL(*values)[2], int n)
+void w_perc90(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
-    percentile_w(result, values, n, 90.0);
+    static const double q = 0.90;
+    w_quant(result, values, n, &q);
 }

Modified: grass/branches/develbranch_6/lib/stats/c_range.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_range.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_range.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,6 +1,6 @@
 #include <grass/gis.h>
 
-void c_range(DCELL * result, DCELL * values, int n)
+void c_range(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL min, max;
     int i;

Modified: grass/branches/develbranch_6/lib/stats/c_reg.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_reg.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_reg.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -73,17 +73,17 @@
 	G_set_d_null_value(result, 1);
 }
 
-void c_reg_m(DCELL * result, DCELL * values, int n)
+void c_reg_m(DCELL * result, DCELL * values, int n, const void *closure)
 {
     regression(result, values, n, REGRESSION_SLOPE);
 }
 
-void c_reg_c(DCELL * result, DCELL * values, int n)
+void c_reg_c(DCELL * result, DCELL * values, int n, const void *closure)
 {
     regression(result, values, n, REGRESSION_OFFSET);
 }
 
-void c_reg_r2(DCELL * result, DCELL * values, int n)
+void c_reg_r2(DCELL * result, DCELL * values, int n, const void *closure)
 {
     regression(result, values, n, REGRESSION_COEFF_DET);
 }
@@ -157,17 +157,17 @@
 	G_set_d_null_value(result, 1);
 }
 
-void w_reg_m(DCELL * result, DCELL(*values)[2], int n)
+void w_reg_m(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     regression_w(result, values, n, REGRESSION_SLOPE);
 }
 
-void w_reg_c(DCELL * result, DCELL(*values)[2], int n)
+void w_reg_c(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     regression_w(result, values, n, REGRESSION_OFFSET);
 }
 
-void w_reg_r2(DCELL * result, DCELL(*values)[2], int n)
+void w_reg_r2(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     regression_w(result, values, n, REGRESSION_COEFF_DET);
 }

Modified: grass/branches/develbranch_6/lib/stats/c_skew.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_skew.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_skew.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,7 +1,7 @@
 #include <math.h>
 #include <grass/gis.h>
 
-void c_skew(DCELL * result, DCELL * values, int n)
+void c_skew(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL sum, ave, sumsq, sumcb, sdev;
     int count;
@@ -43,7 +43,7 @@
     *result = sumcb / (count * sdev * sdev * sdev);
 }
 
-void w_skew(DCELL * result, DCELL(*values)[2], int n)
+void w_skew(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     DCELL sum, ave, sumsq, sumcb, sdev;
     int count;

Modified: grass/branches/develbranch_6/lib/stats/c_stddev.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_stddev.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_stddev.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -2,11 +2,11 @@
 #include <grass/gis.h>
 #include <grass/stats.h>
 
-void c_stddev(DCELL * result, DCELL * values, int n)
+void c_stddev(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL var;
 
-    c_var(&var, values, n);
+    c_var(&var, values, n, closure);
 
     if (G_is_d_null_value(&var))
 	G_set_d_null_value(result, 1);
@@ -14,11 +14,11 @@
 	*result = sqrt(var);
 }
 
-void w_stddev(DCELL * result, DCELL(*values)[2], int n)
+void w_stddev(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     DCELL var;
 
-    w_var(&var, values, n);
+    w_var(&var, values, n, closure);
 
     if (G_is_d_null_value(&var))
 	G_set_d_null_value(result, 1);

Modified: grass/branches/develbranch_6/lib/stats/c_sum.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_sum.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_sum.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,6 +1,6 @@
 #include <grass/gis.h>
 
-void c_sum(DCELL * result, DCELL * values, int n)
+void c_sum(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL sum;
     int count;
@@ -23,7 +23,7 @@
 	*result = sum;
 }
 
-void w_sum(DCELL * result, DCELL(*values)[2], int n)
+void w_sum(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     DCELL sum;
     DCELL count;

Modified: grass/branches/develbranch_6/lib/stats/c_var.c
===================================================================
--- grass/branches/develbranch_6/lib/stats/c_var.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/lib/stats/c_var.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -1,6 +1,6 @@
 #include <grass/gis.h>
 
-void c_var(DCELL * result, DCELL * values, int n)
+void c_var(DCELL * result, DCELL * values, int n, const void *closure)
 {
     DCELL sum, ave, sumsq;
     int count;
@@ -39,7 +39,7 @@
     *result = sumsq / count;
 }
 
-void w_var(DCELL * result, DCELL(*values)[2], int n)
+void w_var(DCELL * result, DCELL(*values)[2], int n, const void *closure)
 {
     DCELL sum, ave, sumsq;
     int count;

Modified: grass/branches/develbranch_6/raster/r.series/main.c
===================================================================
--- grass/branches/develbranch_6/raster/r.series/main.c	2009-09-04 19:25:18 UTC (rev 38976)
+++ grass/branches/develbranch_6/raster/r.series/main.c	2009-09-04 22:05:14 UTC (rev 38977)
@@ -47,6 +47,7 @@
     {c_quart1, 0, "quart1",     "first quartile"},
     {c_quart3, 0, "quart3",     "third quartile"},
     {c_perc90, 0, "perc90",     "ninetieth percentile"},
+    {c_quant,  0, "quantile",   "arbitrary quantile"},
     {c_skew,   0, "skewness",   "skewness"},
     {c_kurt,   0, "kurtosis",   "kurtosis"},
     {NULL,     0, NULL,         NULL}
@@ -65,6 +66,7 @@
     int fd;
     DCELL *buf;
     stat_func *method_fn;
+    double quantile;
 };
 
 static char *build_method_list(void)
@@ -104,7 +106,7 @@
     struct GModule *module;
     struct
     {
-	struct Option *input, *output, *method, *range;
+	struct Option *input, *output, *method, *quantile, *range;
     } parm;
     struct
     {
@@ -145,6 +147,14 @@
     parm.method->description = _("Aggregate operation");
     parm.method->multiple = YES;
 
+    parm.quantile = G_define_option();
+    parm.quantile->key = "quantile";
+    parm.quantile->type = TYPE_DOUBLE;
+    parm.quantile->required = NO;
+    parm.quantile->description = _("Quantile to calculate for method=quantile");
+    parm.quantile->options = "0.0-1.0";
+    parm.quantile->multiple = YES;
+
     /* please, remove before GRASS 7 released */
     flag.quiet = G_define_flag();
     flag.quiet->key = 'q';
@@ -218,6 +228,9 @@
 
 	out->name = output_name;
 	out->method_fn = menu[method].method;
+	out->quantile = (parm.quantile->answer && parm.quantile->answers[i])
+	    ? atof(parm.quantile->answers[i])
+	    : 0;
 	out->buf = G_allocate_d_raster_buf();
 	out->fd = G_open_raster_new(
 	    output_name, menu[method].is_int ? CELL_TYPE : DCELL_TYPE);
@@ -264,7 +277,7 @@
 		    G_set_d_null_value(&out->buf[col], 1);
 		else {
 		    memcpy(values_tmp, values, num_inputs * sizeof(DCELL));
-		    (*out->method_fn)(&out->buf[col], values_tmp, num_inputs);
+		    (*out->method_fn)(&out->buf[col], values_tmp, num_inputs, &out->quantile);
 		}
 	    }
 	}



More information about the grass-commit mailing list