39
40
{c_stddev, 0, "stddev", "standard deviation"},
40
41
{c_range, 0, "range", "range of values"},
41
42
{c_sum, 0, "sum", "sum of values"},
42
{c_thresh, 1, "threshold", "threshold value"},
43
43
{c_var, 0, "variance", "statistical variance"},
44
44
{c_divr, 1, "diversity", "number of different values"},
45
45
{c_reg_m, 0, "slope", "linear regression slope"},
46
46
{c_reg_c, 0, "offset", "linear regression offset"},
47
47
{c_reg_r2, 0, "detcoeff", "linear regression coefficient of determination"},
48
{c_reg_t, 0, "tvalue", "linear regression t-value"},
48
49
{c_quart1, 0, "quart1", "first quartile"},
49
50
{c_quart3, 0, "quart3", "third quartile"},
50
51
{c_perc90, 0, "perc90", "ninetieth percentile"},
108
109
struct GModule *module;
111
struct Option *input, *output, *method, *quantile, *threshold, *range;
112
struct Option *input, *file, *output, *method, *weights, *quantile, *range;
115
/* please, remove before GRASS 7 released */
116
struct Flag *nulls, *lazy;
121
struct input *inputs;
121
struct input *inputs = NULL;
123
struct output *outputs;
123
struct output *outputs = NULL;
124
124
struct History history;
125
DCELL *values, *values_tmp;
125
DCELL *values = NULL, *values_tmp = NULL;
126
126
int nrows, ncols;
130
130
G_gisinit(argv[0]);
132
132
module = G_define_module();
133
module->keywords = _("raster, series");
133
G_add_keyword(_("raster"));
134
G_add_keyword(_("aggregation"));
135
G_add_keyword(_("series"));
134
136
module->description =
135
137
_("Makes each output cell value a "
136
138
"function of the values assigned to the corresponding cells "
137
139
"in the input raster map layers.");
139
141
parm.input = G_define_standard_option(G_OPT_R_INPUTS);
142
parm.input->required = NO;
144
parm.file = G_define_standard_option(G_OPT_F_INPUT);
145
parm.file->key = "file";
146
parm.file->description = _("Input file with one raster map name and optional one weight per line, field separator between name and weight is |");
147
parm.file->required = NO;
141
149
parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
142
150
parm.output->multiple = YES;
157
165
parm.quantile->options = "0.0-1.0";
158
166
parm.quantile->multiple = YES;
160
parm.threshold = G_define_option();
161
parm.threshold->key = "threshold";
162
parm.threshold->type = TYPE_DOUBLE;
163
parm.threshold->required = NO;
164
parm.threshold->description = _("Threshold to calculate for method=threshold");
165
parm.threshold->multiple = YES;
167
/* please, remove before GRASS 7 released */
168
flag.quiet = G_define_flag();
169
flag.quiet->key = 'q';
170
flag.quiet->description = _("Run quietly");
168
parm.weights = G_define_option();
169
parm.weights->key = "weights";
170
parm.weights->type = TYPE_DOUBLE;
171
parm.weights->required = NO;
172
parm.weights->description = _("Weighting factor for each input map, default value is 1.0 for each input map");
173
parm.weights->multiple = YES;
172
175
parm.range = G_define_option();
173
176
parm.range->key = "range";
174
177
parm.range->type = TYPE_DOUBLE;
179
182
flag.nulls->key = 'n';
180
183
flag.nulls->description = _("Propagate NULLs");
185
flag.lazy = G_define_flag();
186
flag.lazy->key = 'z';
187
flag.lazy->description = _("Do not keep files open");
182
189
if (G_parser(argc, argv))
183
190
exit(EXIT_FAILURE);
185
/* please, remove before GRASS 7 released */
186
if (flag.quiet->answer) {
187
putenv("GRASS_VERBOSE=0");
188
G_warning(_("The '-q' flag is superseded and will be removed "
189
"in future. Please use '--quiet' instead."));
192
192
if (parm.range->answer) {
193
193
lo = atof(parm.range->answers[0]);
194
194
hi = atof(parm.range->answers[1]);
197
/* process the input maps */
198
for (i = 0; parm.input->answers[i]; i++) ;
202
G_fatal_error(_("No input raster map(s) specified"));
204
inputs = G_malloc(num_inputs * sizeof(struct input));
206
for (i = 0; i < num_inputs; i++) {
207
struct input *p = &inputs[i];
209
p->name = parm.input->answers[i];
210
p->mapset = G_find_cell2(p->name, "");
212
G_fatal_error(_("Raster map <%s> not found"), p->name);
214
G_message(_("Reading raster map <%s>..."), p->name);
215
p->fd = G_open_cell_old(p->name, p->mapset);
217
G_fatal_error(_("Unable to open raster map <%s> in mapset <%s>"),
219
p->buf = G_allocate_d_raster_buf();
197
if (parm.input->answer && parm.file->answer)
198
G_fatal_error(_("%s= and %s= are mutually exclusive"),
199
parm.input->key, parm.file->key);
201
if (!parm.input->answer && !parm.file->answer)
202
G_fatal_error(_("Please specify %s= or %s="),
203
parm.input->key, parm.file->key);
206
/* process the input maps from the file */
207
if (parm.file->answer) {
211
if (strcmp(parm.file->answer, "-") == 0)
214
in = fopen(parm.file->answer, "r");
216
G_fatal_error(_("Unable to open input file <%s>"), parm.file->answer);
223
char buf[GNAME_MAX + 50]; /* Name and weight*/
224
char tok_buf[GNAME_MAX + 50];
231
if (!G_getl2(buf, sizeof(buf), in))
234
strcpy(tok_buf, buf);
235
tokens = G_tokenize(tok_buf, "|");
236
ntokens = G_number_of_tokens(tokens);
239
name = G_chop(tokens[0]);
240
weight = atof(G_chop(tokens[1]));
245
/* Ignore empty lines */
249
if (num_inputs >= max_inputs) {
251
inputs = G_realloc(inputs, max_inputs * sizeof(struct input));
253
p = &inputs[num_inputs++];
255
p->name = G_store(name);
257
G_verbose_message(_("Reading raster map <%s> using weight %f..."), p->name, p->weight);
258
p->buf = Rast_allocate_d_buf();
259
if (!flag.lazy->answer)
260
p->fd = Rast_open_old(p->name, "");
264
G_fatal_error(_("No raster map name found in input file"));
269
for (i = 0; parm.input->answers[i]; i++)
274
G_fatal_error(_("Raster map not found"));
277
if(parm.weights->answers) {
278
for (i = 0; parm.weights->answers[i]; i++)
285
if (num_weights && num_weights != num_inputs)
286
G_fatal_error(_("input= and weights= must have the same number of values"));
288
inputs = G_malloc(num_inputs * sizeof(struct input));
290
for (i = 0; i < num_inputs; i++) {
291
struct input *p = &inputs[i];
293
p->name = parm.input->answers[i];
296
p->weight = (DCELL)atof(parm.weights->answers[i]);
300
G_verbose_message(_("Reading raster map <%s> using weight %f..."), p->name, p->weight);
301
p->buf = Rast_allocate_d_buf();
302
if (!flag.lazy->answer)
303
p->fd = Rast_open_old(p->name, "");
222
307
/* process the output maps */
223
for (i = 0; parm.output->answers[i]; i++) ;
308
for (i = 0; parm.output->answers[i]; i++)
226
for (i = 0; parm.method->answers[i]; i++) ;
312
for (i = 0; parm.method->answers[i]; i++)
227
314
if (num_outputs != i)
228
315
G_fatal_error(_("output= and method= must have the same number of values"));
240
327
out->quantile = (parm.quantile->answer && parm.quantile->answers[i])
241
328
? atof(parm.quantile->answers[i])
243
out->threshold = (parm.threshold->answer && parm.threshold->answers[i])
244
? atof(parm.threshold->answers[i])
246
out->buf = G_allocate_d_raster_buf();
247
out->fd = G_open_raster_new(
248
output_name, menu[method].is_int ? CELL_TYPE : DCELL_TYPE);
250
G_fatal_error(_("Unable to create raster map <%s>"), out->name);
330
out->buf = Rast_allocate_d_buf();
331
out->fd = Rast_open_new(output_name,
332
menu[method].is_int ? CELL_TYPE : DCELL_TYPE);
253
335
/* initialise variables */
254
336
values = G_malloc(num_inputs * sizeof(DCELL));
255
337
values_tmp = G_malloc(num_inputs * sizeof(DCELL));
257
nrows = G_window_rows();
258
ncols = G_window_cols();
339
nrows = Rast_window_rows();
340
ncols = Rast_window_cols();
260
342
/* process the data */
261
343
G_verbose_message(_("Percent complete..."));
263
345
for (row = 0; row < nrows; row++) {
264
346
G_percent(row, nrows, 2);
266
for (i = 0; i < num_inputs; i++)
267
G_get_d_raster_row(inputs[i].fd, inputs[i].buf, row);
348
if (flag.lazy->answer) {
349
/* Open the files only on run time */
350
for (i = 0; i < num_inputs; i++) {
351
inputs[i].fd = Rast_open_old(inputs[i].name, "");
352
Rast_get_d_row(inputs[i].fd, inputs[i].buf, row);
353
Rast_close(inputs[i].fd);
357
for (i = 0; i < num_inputs; i++)
358
Rast_get_d_row(inputs[i].fd, inputs[i].buf, row);
269
361
for (col = 0; col < ncols; col++) {
272
364
for (i = 0; i < num_inputs; i++) {
273
365
DCELL v = inputs[i].buf[col];
275
if (G_is_d_null_value(&v))
367
if (Rast_is_d_null_value(&v))
277
369
else if (parm.range->answer && (v < lo || v > hi)) {
278
G_set_d_null_value(&v, 1);
370
Rast_set_d_null_value(&v, 1);
373
values[i] = v * inputs[i].weight;
285
376
for (i = 0; i < num_outputs; i++) {
286
377
struct output *out = &outputs[i];
288
379
if (null && flag.nulls->answer)
289
G_set_d_null_value(&out->buf[col], 1);
380
Rast_set_d_null_value(&out->buf[col], 1);
291
382
memcpy(values_tmp, values, num_inputs * sizeof(DCELL));
292
(*out->method_fn)(&out->buf[col], values_tmp, num_inputs, &out->threshold);
383
(*out->method_fn)(&out->buf[col], values_tmp, num_inputs, &out->quantile);
297
388
for (i = 0; i < num_outputs; i++)
298
G_put_d_raster_row(outputs[i].fd, outputs[i].buf);
389
Rast_put_d_row(outputs[i].fd, outputs[i].buf);
301
392
G_percent(row, nrows, 2);
394
/* close output maps */
304
395
for (i = 0; i < num_outputs; i++) {
305
396
struct output *out = &outputs[i];
307
G_close_cell(out->fd);
309
G_short_history(out->name, "raster", &history);
310
G_command_history(&history);
311
G_write_history(out->name, &history);
314
for (i = 0; i < num_inputs; i++)
315
G_close_cell(inputs[i].fd);
400
Rast_short_history(out->name, "raster", &history);
401
Rast_command_history(&history);
402
Rast_write_history(out->name, &history);
405
/* Close input maps */
406
if (!flag.lazy->answer) {
407
for (i = 0; i < num_inputs; i++)
408
Rast_close(inputs[i].fd);
317
411
exit(EXIT_SUCCESS);