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