GRASS 8 Programmer's Manual 8.6.0dev(2026)-37f5195129
Loading...
Searching...
No Matches
c_var.c
Go to the documentation of this file.
1#include <grass/gis.h>
2#include <grass/raster.h>
3
4void c_var(DCELL *result, DCELL *values, int n, const void *closure G_UNUSED)
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
43void w_var(DCELL *result, DCELL (*values)[2], int n,
44 const void *closure G_UNUSED)
45{
46 DCELL sum, ave, sumsq;
48 int i;
49
50 sum = 0.0;
51 count = 0;
52
53 for (i = 0; i < n; i++) {
54 if (Rast_is_d_null_value(&values[i][0]))
55 continue;
56
57 sum += values[i][0] * values[i][1];
58 count += values[i][1];
59 }
60
61 if (count == 0) {
62 Rast_set_d_null_value(result, 1);
63 return;
64 }
65
66 ave = sum / count;
67
68 sumsq = 0;
69
70 for (i = 0; i < n; i++) {
71 DCELL d;
72
73 if (Rast_is_d_null_value(&values[i][0]))
74 continue;
75
76 d = values[i][0] - ave;
77 sumsq += d * d * values[i][1];
78 }
79
80 *result = sumsq / count;
81}
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
Definition null_val.c:153
#define Rast_is_d_null_value(dcellVal)
stat_func_w w_var
stat_func c_var
#define G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:46
double DCELL
Definition gis.h:635
int count