File: evaluate.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (455 lines) | stat: -rw-r--r-- 10,958 bytes parent folder | download | duplicates (2)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/glocale.h>

#include "mapcalc.h"
#include "globals.h"
#include "func_proto.h"

/****************************************************************************/

int current_depth, current_row;
int depths, rows;

/* Local variables for map management */
static expression **map_list = NULL;
static int num_maps = 0;
static int max_maps = 0;

/****************************************************************************/

static void extract_maps(expression *e);
static void initialize(expression *e);
static void evaluate(expression *e);
static int append_map(expression *e);

/****************************************************************************/

int append_map(expression *e)
{
    /* \brief Append a map to the global map list and reallocate if necessary
     */
    if (num_maps >= max_maps) {
        max_maps += 10;
        map_list = G_realloc(map_list, max_maps * sizeof(struct expression *));
    }

    map_list[num_maps] = e;

    return num_maps++;
}

void extract_maps(expression *e)
{
    /* \brief Search for map names in the expression and add them to the global
     * map list */
    int i;

    switch (e->type) {
    case expr_type_map:
        G_debug(1, "Found map %s", e->data.map.name);
        append_map(e);
        break;
    case expr_type_function:
        for (i = 1; i <= e->data.func.argc; i++) {
            extract_maps(e->data.func.args[i]);
        }
        break;
    case expr_type_binding:
        extract_maps(e->data.bind.val);
        break;
    }
}

/****************************************************************************/

static void allocate_buf(expression *e)
{
    e->buf = G_malloc(columns * Rast_cell_size(e->res_type));
}

static void set_buf(expression *e, void *buf)
{
    e->buf = buf;
}

/****************************************************************************/

static void initialize_constant(expression *e)
{
    allocate_buf(e);
}

static void initialize_variable(expression *e)
{
    set_buf(e, e->data.var.bind->data.bind.val->buf);
}

static void initialize_map(expression *e)
{
    allocate_buf(e);

    e->data.map.idx = open_map(e->data.map.name, e->data.map.mod,
                               e->data.map.row, e->data.map.col);
}

static void initialize_function(expression *e)
{
    int i;

    allocate_buf(e);

    e->data.func.argv = G_malloc((e->data.func.argc + 1) * sizeof(void *));
    e->data.func.argv[0] = e->buf;

    for (i = 1; i <= e->data.func.argc; i++) {
        initialize(e->data.func.args[i]);
        e->data.func.argv[i] = e->data.func.args[i]->buf;
    }
}

static void initialize_binding(expression *e)
{
    initialize(e->data.bind.val);
    set_buf(e, e->data.bind.val->buf);
}

static void initialize(expression *e)
{

    switch (e->type) {
    case expr_type_constant:
        initialize_constant(e);
        break;
    case expr_type_variable:
        initialize_variable(e);
        break;
    case expr_type_map:
        initialize_map(e);
        break;
    case expr_type_function:
        initialize_function(e);
        break;
    case expr_type_binding:
        initialize_binding(e);
        break;
    default:
        G_fatal_error(_("Unknown type: %d"), e->type);
    }
}

/****************************************************************************/

static void do_evaluate(void *p)
{
    evaluate((struct expression *)p);
}

static void begin_evaluate(struct expression *e)
{
    G_begin_execute(do_evaluate, e, &e->worker, 0);
}

static void end_evaluate(struct expression *e)
{
    G_end_execute(&e->worker);
}

/****************************************************************************/

static void evaluate_constant(expression *e)
{
    int *ibuf = e->buf;
    float *fbuf = e->buf;
    double *dbuf = e->buf;
    int i;

    switch (e->res_type) {
    case CELL_TYPE:
        for (i = 0; i < columns; i++)
            ibuf[i] = e->data.con.ival;
        break;

    case FCELL_TYPE:
        for (i = 0; i < columns; i++)
            fbuf[i] = e->data.con.fval;
        break;

    case DCELL_TYPE:
        for (i = 0; i < columns; i++)
            dbuf[i] = e->data.con.fval;
        break;
    default:
        G_fatal_error(_("Invalid type: %d"), e->res_type);
    }
}

static void evaluate_variable(expression *e UNUSED)
{
    /* this is a no-op */
}

static void evaluate_map(expression *e)
{
    get_map_row(
        e->data.map.idx, e->data.map.mod, current_depth + e->data.map.depth,
        current_row + e->data.map.row, e->data.map.col, e->buf, e->res_type);
}

static void evaluate_function(expression *e)
{
    int i;
    int res;

    if (e->data.func.argc > 1 && e->data.func.func != f_eval) {
        for (i = 1; i <= e->data.func.argc; i++)
            begin_evaluate(e->data.func.args[i]);

        for (i = 1; i <= e->data.func.argc; i++)
            end_evaluate(e->data.func.args[i]);
    }
    else
        for (i = 1; i <= e->data.func.argc; i++)
            evaluate(e->data.func.args[i]);

    res = (*e->data.func.func)(e->data.func.argc, e->data.func.argt,
                               e->data.func.argv);

    switch (res) {
    case E_ARG_LO:
        G_fatal_error(_("Too few arguments for function '%s'"),
                      e->data.func.name);
        break;
    case E_ARG_HI:
        G_fatal_error(_("Too many arguments for function '%s'"),
                      e->data.func.name);
        break;
    case E_ARG_TYPE:
        G_fatal_error(_("Invalid argument type for function '%s'"),
                      e->data.func.name);
        break;
    case E_RES_TYPE:
        G_fatal_error(_("Invalid return type for function '%s'"),
                      e->data.func.name);
        break;
    case E_INV_TYPE:
        G_fatal_error(_("Unknown type for function '%s'"), e->data.func.name);
        break;
    case E_ARG_NUM:
        G_fatal_error(_("Number of arguments for function '%s'"),
                      e->data.func.name);
        break;
    case E_WTF:
        G_fatal_error(_("Unknown error for function '%s'"), e->data.func.name);
        break;
    }
}

static void evaluate_binding(expression *e)
{
    evaluate(e->data.bind.val);
}

/****************************************************************************/

static void evaluate(expression *e)
{
    switch (e->type) {
    case expr_type_constant:
        evaluate_constant(e);
        break;
    case expr_type_variable:
        evaluate_variable(e);
        break;
    case expr_type_map:
        evaluate_map(e);
        break;
    case expr_type_function:
        evaluate_function(e);
        break;
    case expr_type_binding:
        evaluate_binding(e);
        break;
    default:
        G_fatal_error(_("Unknown type: %d"), e->type);
    }
}

/****************************************************************************/

static expr_list *exprs;

/****************************************************************************/

static void error_handler(void *p UNUSED)
{
    expr_list *l;

    for (l = exprs; l; l = l->next) {
        expression *e = l->exp;
        int fd = e->data.bind.fd;

        if (fd >= 0)
            unopen_output_map(fd);
    }
}

void execute(expr_list *ee)
{
    int verbose = isatty(2);
    expr_list *l;
    int count, n;

    exprs = ee;
    G_add_error_handler(error_handler, NULL);

    for (l = ee; l; l = l->next) {
        expression *e = l->exp;
        const char *var;

        if (e->type != expr_type_binding && e->type != expr_type_function)
            G_fatal_error("internal error: execute: invalid type: %d", e->type);

        if (e->type != expr_type_binding)
            continue;

        var = e->data.bind.var;

        if (!overwrite_flag && check_output_map(var))
            G_fatal_error(_("output map <%s> exists. To overwrite, "
                            "use the --overwrite flag"),
                          var);
    }

    /* Parse each expression and extract all raster maps */
    for (l = ee; l; l = l->next) {
        expression *e = l->exp;

        extract_maps(e);
    }

    /* Set the region from the input maps */
    if (region_approach == 2)
        prepare_region_from_maps_union(map_list, num_maps);
    if (region_approach == 3)
        prepare_region_from_maps_intersect(map_list, num_maps);

    setup_region();

    /* Parse each expression and initialize the maps, buffers and variables */
    for (l = ee; l; l = l->next) {
        expression *e = l->exp;
        const char *var;
        expression *val;

        initialize(e);

        if (e->type != expr_type_binding)
            continue;

        var = e->data.bind.var;
        val = e->data.bind.val;
        e->data.bind.fd = open_output_map(var, val->res_type);
    }

    setup_maps();

    count = rows * depths;
    n = 0;

    G_init_workers();

    for (current_depth = 0; current_depth < depths; current_depth++) {
        for (current_row = 0; current_row < rows; current_row++) {
            if (verbose)
                G_percent(n, count, 2);

            for (l = ee; l; l = l->next) {
                expression *e = l->exp;
                int fd;

                evaluate(e);

                if (e->type != expr_type_binding)
                    continue;

                fd = e->data.bind.fd;
                put_map_row(fd, e->buf, e->res_type);
            }

            n++;
        }
    }

    G_finish_workers();

    if (verbose)
        G_percent(n, count, 2);

    close_maps();

    for (l = ee; l; l = l->next) {
        expression *e = l->exp;
        const char *var;
        expression *val;
        int fd;

        if (e->type != expr_type_binding)
            continue;

        var = e->data.bind.var;
        val = e->data.bind.val;
        fd = e->data.bind.fd;

        close_output_map(fd);
        e->data.bind.fd = -1;

        if (val->type == expr_type_map) {
            if (val->data.map.mod == 'M') {
                copy_cats(var, val->data.map.idx);
                copy_colors(var, val->data.map.idx);
            }

            copy_history(var, val->data.map.idx);
        }
        else
            create_history(var, val);
    }

    G_unset_error_routine();
}

void describe_maps(FILE *fp, expr_list *ee)
{
    expr_list *l;

    fprintf(fp, "output=");

    for (l = ee; l; l = l->next) {
        expression *e = l->exp;
        const char *var;

        if (e->type != expr_type_binding && e->type != expr_type_function)
            G_fatal_error("internal error: execute: invalid type: %d", e->type);

        initialize(e);

        if (e->type != expr_type_binding)
            continue;

        var = e->data.bind.var;
        fprintf(fp, "%s%s", l != ee ? "," : "", var);
    }

    fprintf(fp, "\n");

    fprintf(fp, "input=");
    list_maps(fp, ",");
    fprintf(fp, "\n");
}

/****************************************************************************/