GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_percentile.c
Go to the documentation of this file.
1 #include <math.h>
2 
3 #include <grass/gis.h>
4 #include <grass/stats.h>
5 
6 void c_quant(DCELL * result, DCELL * values, int n, const void *closure)
7 {
8  double quant = *(const double *)closure;
9  double k;
10  int i0, i1;
11 
12  n = sort_cell(values, n);
13 
14  if (n < 1) {
15  G_set_d_null_value(result, 1);
16  return;
17  }
18 
19  k = n * quant;
20  i0 = (int)floor(k);
21  i1 = (int)ceil(k);
22 
23  *result = (i0 == i1)
24  ? values[i0]
25  : values[i0] * (i1 - k) + values[i1] * (k - i0);
26 }
27 
28 void c_quart1(DCELL * result, DCELL * values, int n, const void *closure)
29 {
30  static const double q = 0.25;
31  c_quant(result, values, n, &q);
32 }
33 
34 void c_quart3(DCELL * result, DCELL * values, int n, const void *closure)
35 {
36  static const double q = 0.75;
37  c_quant(result, values, n, &q);
38 }
39 
40 void c_perc90(DCELL * result, DCELL * values, int n, const void *closure)
41 {
42  static const double q = 0.90;
43  c_quant(result, values, n, &q);
44 }
45 
46 void w_quant(DCELL * result, DCELL(*values)[2], int n, const void *closure)
47 {
48  double quant = *(const double *)closure;
49  DCELL total;
50  int i;
51  DCELL k;
52 
53  n = sort_cell_w(values, n);
54 
55  if (n < 1) {
56  G_set_d_null_value(result, 1);
57  return;
58  }
59 
60  total = 0.0;
61  for (i = 0; i < n; i++)
62  total += values[i][1];
63 
64  k = 0.0;
65  for (i = 0; i < n; i++) {
66  k += values[i][1];
67  if (k >= total * quant)
68  break;
69  }
70 
71  *result = values[i][0];
72 }
73 
74 void w_quart1(DCELL * result, DCELL(*values)[2], int n, const void *closure)
75 {
76  static const double q = 0.25;
77  w_quant(result, values, n, &q);
78 }
79 
80 void w_quart3(DCELL * result, DCELL(*values)[2], int n, const void *closure)
81 {
82  static const double q = 0.75;
83  w_quant(result, values, n, &q);
84 }
85 
86 void w_perc90(DCELL * result, DCELL(*values)[2], int n, const void *closure)
87 {
88  static const double q = 0.90;
89  w_quant(result, values, n, &q);
90 }
void c_quart1(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_percentile.c:28
tuple q
Definition: forms.py:2019
void c_quart3(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_percentile.c:34
int sort_cell_w(DCELL(*array)[2], int n)
Definition: sort_cell.c:34
void G_set_d_null_value(DCELL *dcellVals, int numVals)
Definition: null_val.c:176
void w_quant(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_percentile.c:46
void c_perc90(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_percentile.c:40
void c_quant(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_percentile.c:6
int
Definition: g3dcolor.c:48
void w_perc90(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_percentile.c:86
int sort_cell(DCELL *array, int n)
Definition: sort_cell.c:21
void w_quart3(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_percentile.c:80
void w_quart1(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_percentile.c:74
int n
Definition: dataquad.c:291