GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
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)
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, 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 }
int sort_cell(DCELL *, int)
Definition: sort_cell.c:28
void w_mode(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_mode.c:38
double DCELL
Definition: gis.h:603
int count
#define max(x, y)
Definition: draw2.c:32
int sort_cell_w(DCELL(*)[2], int)
Definition: sort_cell.c:47
void c_mode(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_mode.c:5
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
Definition: null_val.c:155