GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
xsub.c
Go to the documentation of this file.
1 
2 #include <grass/gis.h>
3 #include <grass/raster.h>
4 #include <grass/calc.h>
5 
6 /****************************************************************
7 sub(a,b) = a - b
8 ****************************************************************/
9 
10 int f_sub(int argc, const int *argt, void **args)
11 {
12  int i;
13 
14  if (argc < 2)
15  return E_ARG_LO;
16  if (argc > 2)
17  return E_ARG_HI;
18 
19  if (argt[1] != argt[0] || argt[2] != argt[0])
20  return E_ARG_TYPE;
21 
22  switch (argt[0]) {
23  case CELL_TYPE:
24  {
25  CELL *res = args[0];
26  CELL *arg1 = args[1];
27  CELL *arg2 = args[2];
28 
29  for (i = 0; i < columns; i++) {
30  if (IS_NULL_C(&arg1[i]) || IS_NULL_C(&arg2[i]))
31  SET_NULL_C(&res[i]);
32  else
33  res[i] = arg1[i] - arg2[i];
34  }
35  return 0;
36  }
37  case FCELL_TYPE:
38  {
39  FCELL *res = args[0];
40  FCELL *arg1 = args[1];
41  FCELL *arg2 = args[2];
42 
43  for (i = 0; i < columns; i++) {
44  if (IS_NULL_F(&arg1[i]) || IS_NULL_F(&arg2[i]))
45  SET_NULL_F(&res[i]);
46  else
47  res[i] = arg1[i] - arg2[i];
48  }
49  return 0;
50  }
51  case DCELL_TYPE:
52  {
53  DCELL *res = args[0];
54  DCELL *arg1 = args[1];
55  DCELL *arg2 = args[2];
56 
57  for (i = 0; i < columns; i++) {
58  if (IS_NULL_D(&arg1[i]) || IS_NULL_D(&arg2[i]))
59  SET_NULL_D(&res[i]);
60  else
61  res[i] = arg1[i] - arg2[i];
62  }
63  return 0;
64  }
65  default:
66  return E_INV_TYPE;
67  }
68 }
#define CELL_TYPE
Definition: raster.h:11
#define SET_NULL_C(x)
Definition: calc.h:32
double DCELL
Definition: gis.h:614
#define IS_NULL_F(x)
Definition: calc.h:29
int f_sub(int argc, const int *argt, void **args)
Definition: xsub.c:10
#define IS_NULL_C(x)
Definition: calc.h:28
Definition: calc.h:13
int columns
Definition: calc.c:12
#define DCELL_TYPE
Definition: raster.h:13
Definition: calc.h:12
float FCELL
Definition: gis.h:615
#define IS_NULL_D(x)
Definition: calc.h:30
int CELL
Definition: gis.h:613
#define FCELL_TYPE
Definition: raster.h:12
#define SET_NULL_F(x)
Definition: calc.h:33
#define SET_NULL_D(x)
Definition: calc.h:34