GRASS GIS 8 Programmer's Manual 8.2.1RC1(2022)-exported
c_mode.c
Go to the documentation of this file.
1#include <grass/gis.h>
2#include <grass/raster.h>
3#include <grass/stats.h>
4
5void c_mode(DCELL * result, DCELL * values, int n, const void *closure)
6{
7 DCELL mode;
8 int max;
9 DCELL prev;
10 int count;
11 int i;
12
13 n = sort_cell(values, n);
14
15 max = 0;
16 count = 0;
17
18 for (i = 0; i < n; i++) {
19 if (max == 0 || values[i] != prev) {
20 prev = values[i];
21 count = 0;
22 }
23
24 count++;
25
26 if (count > max) {
27 max = count;
28 mode = prev;
29 }
30 }
31
32 if (max == 0)
33 Rast_set_d_null_value(result, 1);
34 else
35 *result = mode;
36}
37
38void w_mode(DCELL * result, DCELL(*values)[2], int n, const void *closure)
39{
40 DCELL mode;
41 DCELL max;
42 DCELL prev;
43 DCELL count;
44 int i;
45
46 n = sort_cell_w(values, n);
47
48 max = 0.0;
49 count = 0.0;
50
51 for (i = 0; i < n; i++) {
52 if (max == 0.0 || values[i][0] != prev) {
53 prev = values[i][0];
54 count = 0.0;
55 }
56
57 count += values[i][1];
58
59 if (count > max) {
60 max = count;
61 mode = prev;
62 }
63 }
64
65 if (max == 0.0)
66 Rast_set_d_null_value(result, 1);
67 else
68 *result = mode;
69}
void c_mode(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_mode.c:5
void w_mode(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_mode.c:38
int count
#define max(a, b)
int sort_cell(DCELL *array, int n)
Definition: sort_cell.c:28
int sort_cell_w(DCELL(*array)[2], int n)
Definition: sort_cell.c:47