GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
c_var.c
Go to the documentation of this file.
1 #include <grass/gis.h>
2 #include <grass/raster.h>
3 
4 void c_var(DCELL * result, DCELL * values, int n, const void *closure)
5 {
6  DCELL sum, ave, sumsq;
7  int count;
8  int i;
9 
10  sum = 0.0;
11  count = 0;
12 
13  for (i = 0; i < n; i++) {
14  if (Rast_is_d_null_value(&values[i]))
15  continue;
16 
17  sum += values[i];
18  count++;
19  }
20 
21  if (count == 0) {
22  Rast_set_d_null_value(result, 1);
23  return;
24  }
25 
26  ave = sum / count;
27 
28  sumsq = 0;
29 
30  for (i = 0; i < n; i++) {
31  DCELL d;
32 
33  if (Rast_is_d_null_value(&values[i]))
34  continue;
35 
36  d = values[i] - ave;
37  sumsq += d * d;
38  }
39 
40  *result = sumsq / count;
41 }
42 
43 void w_var(DCELL * result, DCELL(*values)[2], int n, const void *closure)
44 {
45  DCELL sum, ave, sumsq;
46  DCELL count;
47  int i;
48 
49  sum = 0.0;
50  count = 0;
51 
52  for (i = 0; i < n; i++) {
53  if (Rast_is_d_null_value(&values[i][0]))
54  continue;
55 
56  sum += values[i][0] * values[i][1];
57  count += values[i][1];
58  }
59 
60  if (count == 0) {
61  Rast_set_d_null_value(result, 1);
62  return;
63  }
64 
65  ave = sum / count;
66 
67  sumsq = 0;
68 
69  for (i = 0; i < n; i++) {
70  DCELL d;
71 
72  if (Rast_is_d_null_value(&values[i][0]))
73  continue;
74 
75  d = values[i][0] - ave;
76  sumsq += d * d * values[i][1];
77  }
78 
79  *result = sumsq / count;
80 }
void w_var(DCELL *result, DCELL(*values)[2], int n, const void *closure)
Definition: c_var.c:43
#define Rast_is_d_null_value(dcellVal)
Definition: defs/raster.h:414
void c_var(DCELL *result, DCELL *values, int n, const void *closure)
Definition: c_var.c:4
double DCELL
Definition: gis.h:614
int count
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
Definition: null_val.c:155