瀏覽代碼

threshold parameter added

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@41667 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Neteler 15 年之前
父節點
當前提交
c088d91e0e
共有 3 個文件被更改,包括 53 次插入2 次删除
  1. 1 0
      include/stats.h
  2. 38 0
      lib/stats/c_thresh.c
  3. 14 2
      raster/r.series/main.c

+ 1 - 0
include/stats.h

@@ -20,6 +20,7 @@ extern stat_func c_minx;
 extern stat_func c_mode;
 extern stat_func c_stddev;
 extern stat_func c_sum;
+extern stat_func c_thresh;
 extern stat_func c_var;
 extern stat_func c_range;
 extern stat_func c_reg_m;

+ 38 - 0
lib/stats/c_thresh.c

@@ -0,0 +1,38 @@
+#include <math.h>
+
+#include <grass/gis.h>
+#include <grass/raster.h>
+
+void c_thresh(DCELL * result, DCELL * values, int n, const void *closure)
+{
+    DCELL thresh, threshx;
+    double tval = *(const double *)closure;
+    double epsilon = GRASS_EPSILON;
+    int i;
+
+    Rast_set_d_null_value(&thresh, 1);
+    Rast_set_d_null_value(&threshx, 1);
+
+    for (i = 0; i < n; i++) {
+	/* already found */
+	if (! Rast_is_d_null_value(&threshx))
+	    continue;
+
+	if (Rast_is_d_null_value(&values[i]))
+	    continue;
+
+	G_debug(2, "values[%d] %f, tval %f", i, values[i], tval);
+	/* for GDD */
+	epsilon = 10.;
+	if (fabs(tval - values[i]) < epsilon ) {
+	    thresh = values[i];
+	    threshx = i + 1;
+	    G_debug(2, "values[%d] %f, thresh %f, threshx %f, diff %f", i, values[i], thresh, threshx, tval - values[i]);
+	}
+    }
+
+    if (Rast_is_d_null_value(&threshx))
+	Rast_set_d_null_value(result, 1);
+    else
+	*result = threshx;
+}

+ 14 - 2
raster/r.series/main.c

@@ -40,6 +40,7 @@ struct menu
     {c_stddev, 0, "stddev",     "standard deviation"},
     {c_range,  0, "range",      "range of values"},
     {c_sum,    0, "sum",        "sum of values"},
+    {c_thresh, 1, "threshold",  "threshold value"},
     {c_var,    0, "variance",   "statistical variance"},
     {c_divr,   1, "diversity",  "number of different values"},
     {c_reg_m,  0, "slope",      "linear regression slope"},
@@ -68,6 +69,7 @@ struct output
     DCELL *buf;
     stat_func *method_fn;
     double quantile;
+    double threshold;
 };
 
 static char *build_method_list(void)
@@ -107,7 +109,7 @@ int main(int argc, char *argv[])
     struct GModule *module;
     struct
     {
-	struct Option *input, *output, *method, *quantile, *range;
+	struct Option *input, *output, *method, *quantile, *threshold, *range;
     } parm;
     struct
     {
@@ -155,6 +157,13 @@ int main(int argc, char *argv[])
     parm.quantile->options = "0.0-1.0";
     parm.quantile->multiple = YES;
 
+    parm.threshold = G_define_option();
+    parm.threshold->key = "threshold";
+    parm.threshold->type = TYPE_DOUBLE;
+    parm.threshold->required = NO;
+    parm.threshold->description = _("Threshold to calculate for method=threshold");
+    parm.threshold->multiple = YES;
+
     parm.range = G_define_option();
     parm.range->key = "range";
     parm.range->type = TYPE_DOUBLE;
@@ -215,6 +224,9 @@ int main(int argc, char *argv[])
 	out->quantile = (parm.quantile->answer && parm.quantile->answers[i])
 	    ? atof(parm.quantile->answers[i])
 	    : 0;
+	out->threshold = (parm.threshold->answer && parm.threshold->answers[i])
+	    ? atof(parm.threshold->answers[i])
+	    : 0;
 	out->buf = Rast_allocate_d_buf();
 	out->fd = Rast_open_new(output_name,
 				menu[method].is_int ? CELL_TYPE : DCELL_TYPE);
@@ -259,7 +271,7 @@ int main(int argc, char *argv[])
 		    Rast_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->quantile);
+		    (*out->method_fn)(&out->buf[col], values_tmp, num_inputs, &out->threshold);
 		}
 	    }
 	}