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