-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
c_mode.c
69 lines (55 loc) · 1.04 KB
/
c_mode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/stats.h>
void c_mode(DCELL * result, DCELL * values, int n, const void *closure)
{
DCELL mode;
int max;
DCELL prev;
int count;
int i;
n = sort_cell(values, n);
max = 0;
count = 0;
for (i = 0; i < n; i++) {
if (max == 0 || values[i] != prev) {
prev = values[i];
count = 0;
}
count++;
if (count > max) {
max = count;
mode = prev;
}
}
if (max == 0)
Rast_set_d_null_value(result, 1);
else
*result = mode;
}
void w_mode(DCELL * result, DCELL(*values)[2], int n, const void *closure)
{
DCELL mode;
DCELL max;
DCELL prev;
DCELL count;
int i;
n = sort_cell_w(values, n);
max = 0.0;
count = 0.0;
for (i = 0; i < n; i++) {
if (max == 0.0 || values[i][0] != prev) {
prev = values[i][0];
count = 0.0;
}
count += values[i][1];
if (count > max) {
max = count;
mode = prev;
}
}
if (max == 0.0)
Rast_set_d_null_value(result, 1);
else
*result = mode;
}