GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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 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 
43 void w_var(DCELL *result, DCELL (*values)[2], int n, const void *closure UNUSED)
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 c_var(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
Definition: c_var.c:4
void w_var(DCELL *result, DCELL(*values)[2], int n, const void *closure UNUSED)
Definition: c_var.c:43
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)
Definition: defs/raster.h:408
double DCELL
Definition: gis.h:629
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
int count