GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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 UNUSED)
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,
16  const void *closure UNUSED)
17 {
18  DCELL total;
19  int i;
20  DCELL k;
21 
22  n = sort_cell_w(values, n);
23 
24  if (n < 1) {
25  Rast_set_d_null_value(result, 1);
26  return;
27  }
28 
29  total = 0.0;
30  for (i = 0; i < n; i++)
31  total += values[i][1];
32 
33  k = 0.0;
34  for (i = 0; i < n; i++) {
35  k += values[i][1];
36  if (k >= total / 2)
37  break;
38  }
39 
40  *result = values[i][0];
41 }
void c_median(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
Definition: c_median.c:5
void w_median(DCELL *result, DCELL(*values)[2], int n, const void *closure UNUSED)
Definition: c_median.c:15
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
double DCELL
Definition: gis.h:629
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47