GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-6596d49e63
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
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
Definition: null_val.c:153
int sort_cell(DCELL *, int)
Definition: sort_cell.c:27
int sort_cell_w(DCELL(*)[2], int)
Definition: sort_cell.c:46
#define max(x, y)
Definition: draw2.c:30
double DCELL
Definition: gis.h:628
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
int count