GRASS GIS 8 Programmer's Manual  8.3.1(2023)-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 
5 void c_mode(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
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 
38 void w_mode(DCELL *result, DCELL (*values)[2], int n,
39  const void *closure UNUSED)
40 {
41  DCELL mode;
42  DCELL max;
43  DCELL prev;
44  DCELL count;
45  int i;
46 
47  n = sort_cell_w(values, n);
48 
49  max = 0.0;
50  count = 0.0;
51 
52  for (i = 0; i < n; i++) {
53  if (max == 0.0 || values[i][0] != prev) {
54  prev = values[i][0];
55  count = 0.0;
56  }
57 
58  count += values[i][1];
59 
60  if (count > max) {
61  max = count;
62  mode = prev;
63  }
64  }
65 
66  if (max == 0.0)
67  Rast_set_d_null_value(result, 1);
68  else
69  *result = mode;
70 }
void c_mode(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
Definition: c_mode.c:5
void w_mode(DCELL *result, DCELL(*values)[2], int n, const void *closure UNUSED)
Definition: c_mode.c:38
int count
#define max(a, b)
int sort_cell(DCELL *array, int n)
Definition: sort_cell.c:27
int sort_cell_w(DCELL(*array)[2], int n)
Definition: sort_cell.c:46