GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
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 = (i0 == i1)
26  ? values[i0]
27  : values[i0] * (i1 - k) + values[i1] * (k - i0);
28 }
29 
30 void c_quart1(DCELL * result, DCELL * values, int n, const void *closure)
31 {
32  static const double q = 0.25;
33  c_quant(result, values, n, &q);
34 }
35 
36 void c_quart3(DCELL * result, DCELL * values, int n, const void *closure)
37 {
38  static const double q = 0.75;
39  c_quant(result, values, n, &q);
40 }
41 
42 void c_perc90(DCELL * result, DCELL * values, int n, const void *closure)
43 {
44  static const double q = 0.90;
45  c_quant(result, values, n, &q);
46 }
47 
48 void w_quant(DCELL * result, DCELL(*values)[2], int n, const void *closure)
49 {
50  double quant = *(const double *)closure;
51  DCELL total;
52  int i;
53  DCELL k;
54 
55  n = sort_cell_w(values, n);
56 
57  if (n < 1) {
58  Rast_set_d_null_value(result, 1);
59  return;
60  }
61 
62  total = 0.0;
63  for (i = 0; i < n; i++)
64  total += values[i][1];
65 
66  k = 0.0;
67  for (i = 0; i < n; i++) {
68  k += values[i][1];
69  if (k >= total * quant)
70  break;
71  }
72 
73  *result = values[i][0];
74 }
75 
76 void w_quart1(DCELL * result, DCELL(*values)[2], int n, const void *closure)
77 {
78  static const double q = 0.25;
79  w_quant(result, values, n, &q);
80 }
81 
82 void w_quart3(DCELL * result, DCELL(*values)[2], int n, const void *closure)
83 {
84  static const double q = 0.75;
85  w_quant(result, values, n, &q);
86 }
87 
88 void w_perc90(DCELL * result, DCELL(*values)[2], int n, const void *closure)
89 {
90  static const double q = 0.90;
91  w_quant(result, values, n, &q);
92 }
int sort_cell(DCELL *, int)
Definition: sort_cell.c:28
void c_quart1(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_percentile.c:30
void c_quart3(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_percentile.c:36
double DCELL
Definition: gis.h:614
void w_quant(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_percentile.c:48
void c_perc90(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_percentile.c:42
void c_quant(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_percentile.c:7
int sort_cell_w(DCELL(*)[2], int)
Definition: sort_cell.c:47
void w_perc90(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_percentile.c:88
void w_quart3(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_percentile.c:82
void w_quart1(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_percentile.c:76
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
Definition: null_val.c:155