GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-cb7cbab7e7
c_percentile.c
Go to the documentation of this file.
1 #include <math.h>
2 
3 #include <grass/gis.h>
4 #include <grass/raster.h>
5 #include <grass/stats.h>
6 
7 void c_quant(DCELL *result, DCELL *values, int n, const void *closure)
8 {
9  double quant = *(const double *)closure;
10  double k;
11  int i0, i1;
12 
13  n = sort_cell(values, n);
14 
15  if (n < 1) {
16  Rast_set_d_null_value(result, 1);
17  return;
18  }
19 
20  /* algorithm type 7 of Hyndman and Fan (1996), default in R */
21  k = quant * (n - 1);
22  i0 = (int)floor(k);
23  i1 = (int)ceil(k);
24 
25  *result =
26  (i0 == i1) ? values[i0] : values[i0] * (i1 - k) + values[i1] * (k - i0);
27 }
28 
29 void c_quart1(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
30 {
31  static const double q = 0.25;
32 
33  c_quant(result, values, n, &q);
34 }
35 
36 void c_quart3(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
37 {
38  static const double q = 0.75;
39 
40  c_quant(result, values, n, &q);
41 }
42 
43 void c_perc90(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
44 {
45  static const double q = 0.90;
46 
47  c_quant(result, values, n, &q);
48 }
49 
50 void w_quant(DCELL *result, DCELL (*values)[2], int n, const void *closure)
51 {
52  double quant = *(const double *)closure;
53  DCELL total;
54  int i;
55  DCELL k;
56 
57  n = sort_cell_w(values, n);
58 
59  if (n < 1) {
60  Rast_set_d_null_value(result, 1);
61  return;
62  }
63 
64  total = 0.0;
65  for (i = 0; i < n; i++)
66  total += values[i][1];
67 
68  k = 0.0;
69  for (i = 0; i < n; i++) {
70  k += values[i][1];
71  if (k >= total * quant)
72  break;
73  }
74 
75  *result = values[i][0];
76 }
77 
78 void w_quart1(DCELL *result, DCELL (*values)[2], int n,
79  const void *closure UNUSED)
80 {
81  static const double q = 0.25;
82 
83  w_quant(result, values, n, &q);
84 }
85 
86 void w_quart3(DCELL *result, DCELL (*values)[2], int n,
87  const void *closure UNUSED)
88 {
89  static const double q = 0.75;
90 
91  w_quant(result, values, n, &q);
92 }
93 
94 void w_perc90(DCELL *result, DCELL (*values)[2], int n,
95  const void *closure UNUSED)
96 {
97  static const double q = 0.90;
98 
99  w_quant(result, values, n, &q);
100 }
void w_quant(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_percentile.c:50
void w_perc90(DCELL *result, DCELL(*values)[2], int n, const void *closure UNUSED)
Definition: c_percentile.c:94
void c_quart3(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
Definition: c_percentile.c:36
void w_quart1(DCELL *result, DCELL(*values)[2], int n, const void *closure UNUSED)
Definition: c_percentile.c:78
void c_quart1(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
Definition: c_percentile.c:29
void w_quart3(DCELL *result, DCELL(*values)[2], int n, const void *closure UNUSED)
Definition: c_percentile.c:86
void c_quant(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_percentile.c:7
void c_perc90(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
Definition: c_percentile.c:43
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:628
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47