GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
c_median.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_median(DCELL * result, DCELL * values, int n, const void *closure)
6 {
7  n = sort_cell(values, n);
8 
9  if (n < 1)
10  Rast_set_d_null_value(result, 1);
11  else
12  *result = (values[(n - 1) / 2] + values[n / 2]) / 2;
13 }
14 
15 void w_median(DCELL * result, DCELL(*values)[2], int n, const void *closure)
16 {
17  DCELL total;
18  int i;
19  DCELL k;
20 
21  n = sort_cell_w(values, n);
22 
23  if (n < 1) {
24  Rast_set_d_null_value(result, 1);
25  return;
26  }
27 
28  total = 0.0;
29  for (i = 0; i < n; i++)
30  total += values[i][1];
31 
32  k = 0.0;
33  for (i = 0; i < n; i++) {
34  k += values[i][1];
35  if (k >= total / 2)
36  break;
37  }
38 
39  *result = values[i][0];
40 }
int sort_cell(DCELL *, int)
Definition: sort_cell.c:28
double DCELL
Definition: gis.h:614
void w_median(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_median.c:15
int sort_cell_w(DCELL(*)[2], int)
Definition: sort_cell.c:47
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
Definition: null_val.c:155
void c_median(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_median.c:5