GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
xif.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  if(a) 1,0,1 1 if a is non zero, 0 otherwise
8  if(a,b) b,0,b b if a is non zero, 0 otherwise
9  if(a,b,c) b,c,b b if a is non zero, c otherwise
10  if(a,b,c,d) d,c,b b if a is positive, c if a is zero, d if a is negative
11 ********************************************************************/
12 
13 static int f_if_i(int argc, const int *argt, void **args)
14 {
15  CELL *res = args[0];
16  DCELL *arg1 = args[1];
17  CELL *arg2 = (argc >= 2) ? args[2] : NULL;
18  CELL *arg3 = (argc >= 3) ? args[3] : NULL;
19  CELL *arg4 = (argc >= 4) ? args[4] : NULL;
20  int i;
21 
22  switch (argc) {
23  case 0:
24  return E_ARG_LO;
25  case 1:
26  for (i = 0; i < columns; i++)
27  if (IS_NULL_D(&arg1[i]))
28  SET_NULL_C(&res[i]);
29  else
30  res[i] = arg1[i] != 0.0 ? 1 : 0;
31  break;
32  case 2:
33  for (i = 0; i < columns; i++)
34  if (IS_NULL_D(&arg1[i]))
35  SET_NULL_C(&res[i]);
36  else if (arg1[i] == 0.0)
37  res[i] = 0;
38  else {
39  if (IS_NULL_C(&arg2[i]))
40  SET_NULL_C(&res[i]);
41  else
42  res[i] = arg2[i];
43  }
44  break;
45  case 3:
46  for (i = 0; i < columns; i++)
47  if (IS_NULL_D(&arg1[i]))
48  SET_NULL_C(&res[i]);
49  else if (arg1[i] == 0.0) {
50  if (IS_NULL_C(&arg3[i]))
51  SET_NULL_C(&res[i]);
52  else
53  res[i] = arg3[i];
54  }
55  else {
56  if (IS_NULL_C(&arg2[i]))
57  SET_NULL_C(&res[i]);
58  else
59  res[i] = arg2[i];
60  }
61  break;
62  case 4:
63  for (i = 0; i < columns; i++)
64  if (IS_NULL_D(&arg1[i]))
65  SET_NULL_C(&res[i]);
66  else if (arg1[i] == 0.0) {
67  if (IS_NULL_C(&arg3[i]))
68  SET_NULL_C(&res[i]);
69  else
70  res[i] = arg3[i];
71  }
72  else if (arg1[i] > 0.0) {
73  if (IS_NULL_C(&arg2[i]))
74  SET_NULL_C(&res[i]);
75  else
76  res[i] = arg2[i];
77  }
78  else { /* (arg1[i] < 0.0) */
79 
80  if (IS_NULL_C(&arg4[i]))
81  SET_NULL_C(&res[i]);
82  else
83  res[i] = arg4[i];
84  }
85  break;
86  default:
87  return E_ARG_HI;
88  }
89 
90  return 0;
91 }
92 
93 static int f_if_f(int argc, const int *argt, void **args)
94 {
95  FCELL *res = args[0];
96  DCELL *arg1 = args[1];
97  FCELL *arg2 = (argc >= 2) ? args[2] : NULL;
98  FCELL *arg3 = (argc >= 3) ? args[3] : NULL;
99  FCELL *arg4 = (argc >= 4) ? args[4] : NULL;
100  int i;
101 
102  switch (argc) {
103  case 0:
104  return E_ARG_LO;
105  case 1:
106  return E_ARG_TYPE;
107  case 2:
108  for (i = 0; i < columns; i++)
109  if (IS_NULL_D(&arg1[i]))
110  SET_NULL_F(&res[i]);
111  else if (arg1[i] == 0.0)
112  res[i] = 0.0;
113  else {
114  if (IS_NULL_F(&arg2[i]))
115  SET_NULL_F(&res[i]);
116  else
117  res[i] = arg2[i];
118  }
119  break;
120  case 3:
121  for (i = 0; i < columns; i++)
122  if (IS_NULL_D(&arg1[i]))
123  SET_NULL_F(&res[i]);
124  else if (arg1[i] == 0.0) {
125  if (IS_NULL_F(&arg3[i]))
126  SET_NULL_F(&res[i]);
127  else
128  res[i] = arg3[i];
129  }
130  else {
131  if (IS_NULL_F(&arg2[i]))
132  SET_NULL_F(&res[i]);
133  else
134  res[i] = arg2[i];
135  }
136  break;
137  case 4:
138  for (i = 0; i < columns; i++)
139  if (IS_NULL_D(&arg1[i]))
140  SET_NULL_F(&res[i]);
141  else if (arg1[i] == 0.0) {
142  if (IS_NULL_F(&arg3[i]))
143  SET_NULL_F(&res[i]);
144  else
145  res[i] = arg3[i];
146  }
147  else if (arg1[i] > 0.0) {
148  if (IS_NULL_F(&arg2[i]))
149  SET_NULL_F(&res[i]);
150  else
151  res[i] = arg2[i];
152  }
153  else { /* (arg1[i] < 0.0) */
154 
155  if (IS_NULL_F(&arg4[i]))
156  SET_NULL_F(&res[i]);
157  else
158  res[i] = arg4[i];
159  }
160  break;
161  default:
162  return E_ARG_HI;
163  }
164 
165  return 0;
166 }
167 
168 static int f_if_d(int argc, const int *argt, void **args)
169 {
170  DCELL *res = args[0];
171  DCELL *arg1 = args[1];
172  DCELL *arg2 = (argc >= 2) ? args[2] : NULL;
173  DCELL *arg3 = (argc >= 3) ? args[3] : NULL;
174  DCELL *arg4 = (argc >= 4) ? args[4] : NULL;
175  int i;
176 
177  switch (argc) {
178  case 0:
179  return E_ARG_LO;
180  case 1:
181  return E_ARG_TYPE;
182  case 2:
183  for (i = 0; i < columns; i++)
184  if (IS_NULL_D(&arg1[i]))
185  SET_NULL_D(&res[i]);
186  else if (arg1[i] == 0.0)
187  res[i] = 0.0;
188  else {
189  if (IS_NULL_D(&arg2[i]))
190  SET_NULL_D(&res[i]);
191  else
192  res[i] = arg2[i];
193  }
194  break;
195  case 3:
196  for (i = 0; i < columns; i++)
197  if (IS_NULL_D(&arg1[i]))
198  SET_NULL_D(&res[i]);
199  else if (arg1[i] == 0.0) {
200  if (IS_NULL_D(&arg3[i]))
201  SET_NULL_D(&res[i]);
202  else
203  res[i] = arg3[i];
204  }
205  else {
206  if (IS_NULL_D(&arg2[i]))
207  SET_NULL_D(&res[i]);
208  else
209  res[i] = arg2[i];
210  }
211  break;
212  case 4:
213  for (i = 0; i < columns; i++)
214  if (IS_NULL_D(&arg1[i]))
215  SET_NULL_D(&res[i]);
216  else if (arg1[i] == 0.0) {
217  if (IS_NULL_D(&arg3[i]))
218  SET_NULL_D(&res[i]);
219  else
220  res[i] = arg3[i];
221  }
222  else if (arg1[i] > 0.0) {
223  if (IS_NULL_D(&arg2[i]))
224  SET_NULL_D(&res[i]);
225  else
226  res[i] = arg2[i];
227  }
228  else { /* (arg1[i] < 0.0) */
229 
230  if (IS_NULL_D(&arg4[i]))
231  SET_NULL_D(&res[i]);
232  else
233  res[i] = arg4[i];
234  }
235  break;
236  default:
237  return E_ARG_HI;
238  }
239 
240  return 0;
241 }
242 
243 int f_if(int argc, const int *argt, void **args)
244 {
245  if (argc < 1)
246  return E_ARG_LO;
247  if (argc > 4)
248  return E_ARG_HI;
249 
250  if (argt[1] != DCELL_TYPE)
251  return E_ARG_TYPE;
252  if (argc >= 2 && argt[2] != argt[0])
253  return E_ARG_TYPE;
254  if (argc >= 3 && argt[3] != argt[0])
255  return E_ARG_TYPE;
256  if (argc >= 4 && argt[4] != argt[0])
257  return E_ARG_TYPE;
258 
259  switch (argt[0]) {
260  case CELL_TYPE:
261  return f_if_i(argc, argt, args);
262  case FCELL_TYPE:
263  return f_if_f(argc, argt, args);
264  case DCELL_TYPE:
265  return f_if_d(argc, argt, args);
266  default:
267  return E_INV_TYPE;
268  }
269 }
270 
271 int c_if(int argc, int *argt)
272 {
273  if (argc < 1)
274  return E_ARG_LO;
275  if (argc > 4)
276  return E_ARG_HI;
277 
278  argt[0] = CELL_TYPE;
279 
280  if (argc >= 2 && argt[2] == FCELL_TYPE)
281  argt[0] = FCELL_TYPE;
282  if (argc >= 3 && argt[3] == FCELL_TYPE)
283  argt[0] = FCELL_TYPE;
284  if (argc >= 4 && argt[4] == FCELL_TYPE)
285  argt[0] = FCELL_TYPE;
286 
287  if (argc >= 2 && argt[2] == DCELL_TYPE)
288  argt[0] = DCELL_TYPE;
289  if (argc >= 3 && argt[3] == DCELL_TYPE)
290  argt[0] = DCELL_TYPE;
291  if (argc >= 4 && argt[4] == DCELL_TYPE)
292  argt[0] = DCELL_TYPE;
293 
294  argt[1] = DCELL_TYPE;
295  if (argc >= 2)
296  argt[2] = argt[0];
297  if (argc >= 3)
298  argt[3] = argt[0];
299  if (argc >= 4)
300  argt[4] = argt[0];
301 
302  return 0;
303 }
#define CELL_TYPE
Definition: raster.h:11
int f_if(int argc, const int *argt, void **args)
Definition: xif.c:243
#define SET_NULL_C(x)
Definition: calc.h:32
double DCELL
Definition: gis.h:614
#define IS_NULL_F(x)
Definition: calc.h:29
#define NULL
Definition: ccmath.h:32
#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 c_if(int argc, int *argt)
Definition: xif.c:271
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