c_mode.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include <grass/gis.h>
  2. #include <grass/raster.h>
  3. #include <grass/stats.h>
  4. void c_mode(DCELL * result, DCELL * values, int n, const void *closure)
  5. {
  6. DCELL mode;
  7. int max;
  8. DCELL prev;
  9. int count;
  10. int i;
  11. n = sort_cell(values, n);
  12. max = 0;
  13. count = 0;
  14. for (i = 0; i < n; i++) {
  15. if (max == 0 || values[i] != prev) {
  16. prev = values[i];
  17. count = 0;
  18. }
  19. count++;
  20. if (count > max) {
  21. max = count;
  22. mode = prev;
  23. }
  24. }
  25. if (max == 0)
  26. Rast_set_d_null_value(result, 1);
  27. else
  28. *result = mode;
  29. }
  30. void w_mode(DCELL * result, DCELL(*values)[2], int n, const void *closure)
  31. {
  32. DCELL mode;
  33. DCELL max;
  34. DCELL prev;
  35. DCELL count;
  36. int i;
  37. n = sort_cell_w(values, n);
  38. max = 0.0;
  39. count = 0.0;
  40. for (i = 0; i < n; i++) {
  41. if (max == 0.0 || values[i][0] != prev) {
  42. prev = values[i][0];
  43. count = 0.0;
  44. }
  45. count += values[i][1];
  46. if (count > max) {
  47. max = count;
  48. mode = prev;
  49. }
  50. }
  51. if (max == 0.0)
  52. Rast_set_d_null_value(result, 1);
  53. else
  54. *result = mode;
  55. }