c_thresh.c 915 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. void c_thresh(DCELL * result, DCELL * values, int n, const void *closure)
  5. {
  6. DCELL thresh, threshx;
  7. double tval = *(const double *)closure;
  8. double epsilon = GRASS_EPSILON;
  9. int i;
  10. Rast_set_d_null_value(&thresh, 1);
  11. Rast_set_d_null_value(&threshx, 1);
  12. for (i = 0; i < n; i++) {
  13. /* already found */
  14. if (! Rast_is_d_null_value(&threshx))
  15. continue;
  16. if (Rast_is_d_null_value(&values[i]))
  17. continue;
  18. G_debug(2, "values[%d] %f, tval %f", i, values[i], tval);
  19. /* for GDD */
  20. epsilon = 10.;
  21. if (fabs(tval - values[i]) < epsilon ) {
  22. thresh = values[i];
  23. threshx = i + 1;
  24. G_debug(2, "values[%d] %f, thresh %f, threshx %f, diff %f", i, values[i], thresh, threshx, tval - values[i]);
  25. }
  26. }
  27. if (Rast_is_d_null_value(&threshx))
  28. Rast_set_d_null_value(result, 1);
  29. else
  30. *result = threshx;
  31. }