GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-1d42e580e2
c_skew.c
Go to the documentation of this file.
1 #include <math.h>
2 
3 #include <grass/gis.h>
4 #include <grass/raster.h>
5 
6 void c_skew(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
7 {
8  DCELL sum, ave, sumsq, sumcb, sdev;
9  int count;
10  int i;
11 
12  sum = 0.0;
13  count = 0;
14 
15  for (i = 0; i < n; i++) {
16  if (Rast_is_d_null_value(&values[i]))
17  continue;
18 
19  sum += values[i];
20  count++;
21  }
22 
23  if (count == 0) {
24  Rast_set_d_null_value(result, 1);
25  return;
26  }
27 
28  ave = sum / count;
29 
30  sumsq = 0;
31  sumcb = 0;
32 
33  for (i = 0; i < n; i++) {
34  DCELL d;
35 
36  if (Rast_is_d_null_value(&values[i]))
37  continue;
38 
39  d = values[i] - ave;
40  sumsq += d * d;
41  sumcb += d * d * d;
42  }
43 
44  sdev = sqrt(sumsq / count);
45 
46  *result = sumcb / (count * sdev * sdev * sdev);
47 }
48 
49 void w_skew(DCELL *result, DCELL (*values)[2], int n,
50  const void *closure UNUSED)
51 {
52  DCELL sum, ave, sumsq, sumcb, sdev;
53  DCELL count;
54  int i;
55 
56  sum = 0.0;
57  count = 0;
58 
59  for (i = 0; i < n; i++) {
60  if (Rast_is_d_null_value(&values[i][0]))
61  continue;
62 
63  sum += values[i][0] * values[i][1];
64  count += values[i][1];
65  }
66 
67  if (count == 0) {
68  Rast_set_d_null_value(result, 1);
69  return;
70  }
71 
72  ave = sum / count;
73 
74  sumsq = 0;
75  sumcb = 0;
76 
77  for (i = 0; i < n; i++) {
78  DCELL d;
79 
80  if (Rast_is_d_null_value(&values[i][0]))
81  continue;
82 
83  d = values[i][0] - ave;
84  sumsq += d * d * values[i][1];
85  sumcb += d * d * d * values[i][1];
86  }
87 
88  sdev = sqrt(sumsq / count);
89 
90  *result = sumcb / (count * sdev * sdev * sdev);
91 }
void c_skew(DCELL *result, DCELL *values, int n, const void *closure UNUSED)
Definition: c_skew.c:6
void w_skew(DCELL *result, DCELL(*values)[2], int n, const void *closure UNUSED)
Definition: c_skew.c:49
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:407
double DCELL
Definition: gis.h:628
#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