forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
192 lines (147 loc) · 5.39 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/****************************************************************************
*
* MODULE: r.mapcalc
* AUTHOR(S): Michael Shapiro, CERL (original contributor)
* rewritten 2002: Glynn Clements <glynn gclements.plus.com>
* PURPOSE:
* COPYRIGHT: (C) 1999-2007 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*
*****************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <grass/glocale.h>
#include "mapcalc.h"
/****************************************************************************/
int overwrite_flag;
long seed_value;
long seeded;
int region_approach;
/****************************************************************************/
static expr_list *result;
/****************************************************************************/
static expr_list *parse_file(const char *filename)
{
expr_list *res;
FILE *fp;
if (strcmp(filename, "-") == 0)
return parse_stream(stdin);
fp = fopen(filename, "r");
if (!fp)
G_fatal_error(_("Unable to open input file <%s>"), filename);
res = parse_stream(fp);
fclose(fp);
return res;
}
/****************************************************************************/
int main(int argc, char **argv)
{
struct GModule *module;
struct Option *expr, *file, *seed, *region;
struct Flag *random, *describe;
int all_ok;
G_gisinit(argv[0]);
module = G_define_module();
G_add_keyword(_("raster"));
G_add_keyword(_("algebra"));
module->description = _("Raster map calculator.");
module->overwrite = 1;
expr = G_define_option();
expr->key = "expression";
expr->type = TYPE_STRING;
expr->required = NO;
expr->description = _("Expression to evaluate");
expr->guisection = _("Expression");
region = G_define_option();
region->key = "region";
region->type = TYPE_STRING;
region->required = NO;
region->answer = "current";
region->options = "current,intersect,union";
region->description = _("The computational region that should be used.\n"
" - current uses the current region of the mapset.\n"
" - intersect computes the intersection region between\n"
" all input maps and uses the smallest resolution\n"
" - union computes the union extent of all map regions\n"
" and uses the smallest resolution");
file = G_define_standard_option(G_OPT_F_INPUT);
file->key = "file";
file->required = NO;
file->description = _("File containing expression(s) to evaluate");
file->guisection = _("Expression");
seed = G_define_option();
seed->key = "seed";
seed->type = TYPE_INTEGER;
seed->required = NO;
seed->description = _("Seed for rand() function");
random = G_define_flag();
random->key = 's';
random->description = _("Generate random seed (result is non-deterministic)");
describe = G_define_flag();
describe->key = 'l';
describe->description = _("List input and output maps");
if (argc == 1)
{
char **p = G_malloc(3 * sizeof(char *));
p[0] = argv[0];
p[1] = G_store("file=-");
p[2] = NULL;
argv = p;
argc = 2;
}
if (G_parser(argc, argv))
exit(EXIT_FAILURE);
overwrite_flag = module->overwrite;
if (expr->answer && file->answer)
G_fatal_error(_("%s= and %s= are mutually exclusive"),
expr->key, file->key);
if (seed->answer && random->answer)
G_fatal_error(_("%s= and -%c are mutually exclusive"),
seed->key, random->key);
if (expr->answer)
result = parse_string(expr->answer);
else if (file->answer)
result = parse_file(file->answer);
else
result = parse_stream(stdin);
if (!result)
G_fatal_error(_("parse error"));
if (seed->answer) {
seed_value = atol(seed->answer);
G_srand48(seed_value);
seeded = 1;
G_debug(3, "Read random seed from seed=: %ld", seed_value);
}
if (random->answer) {
seed_value = G_srand48_auto();
seeded = 1;
G_debug(3, "Generated random seed (-s): %ld", seed_value);
}
/* Set the global variable of the region setup approach */
region_approach = 1;
if (G_strncasecmp(region->answer, "union", 5) == 0)
region_approach = 2;
if (G_strncasecmp(region->answer, "intersect", 9) == 0)
region_approach = 3;
G_debug(1, "Region answer %s region approach %i", region->answer,
region_approach);
if (describe->answer) {
describe_maps(stdout, result);
return EXIT_SUCCESS;
}
pre_exec();
execute(result);
post_exec();
all_ok = 1;
if (floating_point_exception_occurred) {
G_warning(_("Floating point error(s) occurred in the calculation"));
all_ok = 0;
}
return all_ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
/****************************************************************************/