GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
xnmax.c
Go to the documentation of this file.
1 
2 #include <stdlib.h>
3 
4 #include <grass/gis.h>
5 #include <grass/raster.h>
6 #include <grass/calc.h>
7 
8 /****************************************************************
9 max(x0,x1,...,xn) returns maximum value
10 ****************************************************************/
11 
12 int f_nmax(int argc, const int *argt, void **args)
13 {
14  int i, j;
15 
16  if (argc < 1)
17  return E_ARG_LO;
18 
19  for (i = 1; i <= argc; i++)
20  if (argt[i] != argt[0])
21  return E_ARG_TYPE;
22 
23  switch (argt[0]) {
24  case CELL_TYPE:
25  {
26  CELL *res = args[0];
27  CELL **argz = (CELL **) args;
28 
29  for (i = 0; i < columns; i++) {
30  int nul = 1;
31  CELL max;
32 
33  for (j = 1; j <= argc; j++)
34  if (IS_NULL_C(&argz[j][i]))
35  continue;
36  else if (nul)
37  max = argz[j][i], nul = 0;
38  else if (max < argz[j][i])
39  max = argz[j][i], nul = 0;
40  if (nul)
41  SET_NULL_C(&res[i]);
42  else
43  res[i] = max;
44  }
45  return 0;
46  }
47  case FCELL_TYPE:
48  {
49  FCELL *res = args[0];
50  FCELL **argz = (FCELL **) args;
51 
52  for (i = 0; i < columns; i++) {
53  int nul = 1;
54  FCELL max;
55 
56  for (j = 1; j <= argc; j++)
57  if (IS_NULL_F(&argz[j][i]))
58  continue;
59  else if (nul)
60  max = argz[j][i], nul = 0;
61  else if (max < argz[j][i])
62  max = argz[j][i], nul = 0;
63  if (nul)
64  SET_NULL_F(&res[i]);
65  else
66  res[i] = max;
67  }
68 
69  return 0;
70  }
71  case DCELL_TYPE:
72  {
73  DCELL *res = args[0];
74  DCELL **argz = (DCELL **) args;
75 
76  for (i = 0; i < columns; i++) {
77  int nul = 1;
78  DCELL max;
79 
80  for (j = 1; j <= argc; j++)
81  if (IS_NULL_D(&argz[j][i]))
82  continue;
83  else if (nul)
84  max = argz[j][i], nul = 0;
85  else if (max < argz[j][i])
86  max = argz[j][i], nul = 0;
87  if (nul)
88  SET_NULL_D(&res[i]);
89  else
90  res[i] = max;
91  }
92 
93  return 0;
94  }
95  default:
96  return E_INV_TYPE;
97  }
98 }
#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_nmax(int argc, const int *argt, void **args)
Definition: xnmax.c:12
#define max(x, y)
Definition: draw2.c:32
#define IS_NULL_C(x)
Definition: calc.h:28
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