evaluate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #include <stdlib.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <grass/gis.h>
  5. #include <grass/raster.h>
  6. #include <grass/glocale.h>
  7. #include "mapcalc.h"
  8. #include "globals.h"
  9. #include "func_proto.h"
  10. /****************************************************************************/
  11. int current_depth, current_row;
  12. int depths, rows;
  13. /* Local variables for map management */
  14. static expression **map_list = NULL;
  15. static int num_maps = 0;
  16. static int max_maps = 0;
  17. /****************************************************************************/
  18. static void extract_maps(expression *e);
  19. static void initialize(expression *e);
  20. static void evaluate(expression *e);
  21. static int append_map(expression *e);
  22. /****************************************************************************/
  23. int append_map(expression *e)
  24. {
  25. /* \brief Append a map to the global map list and reallocate if necessary
  26. */
  27. const char *mapset;
  28. if (num_maps >= max_maps) {
  29. max_maps += 10;
  30. map_list = G_realloc(map_list, max_maps * sizeof(struct expression*));
  31. }
  32. map_list[num_maps] = e;
  33. return num_maps++;
  34. }
  35. void extract_maps(expression * e)
  36. {
  37. /* \brief Search for map names in the expression and add them to the global map list */
  38. int i;
  39. switch (e->type) {
  40. case expr_type_map:
  41. G_debug(1, "Found map %s", e->data.map.name);
  42. append_map(e);
  43. break;
  44. case expr_type_function:
  45. for (i = 1; i <= e->data.func.argc; i++) {
  46. extract_maps(e->data.func.args[i]);
  47. }
  48. break;
  49. case expr_type_binding:
  50. extract_maps(e->data.bind.val);
  51. break;
  52. }
  53. }
  54. /****************************************************************************/
  55. static void allocate_buf(expression * e)
  56. {
  57. e->buf = G_malloc(columns * Rast_cell_size(e->res_type));
  58. }
  59. static void set_buf(expression * e, void *buf)
  60. {
  61. e->buf = buf;
  62. }
  63. /****************************************************************************/
  64. static void initialize_constant(expression * e)
  65. {
  66. allocate_buf(e);
  67. }
  68. static void initialize_variable(expression * e)
  69. {
  70. set_buf(e, e->data.var.bind->data.bind.val->buf);
  71. }
  72. static void initialize_map(expression * e)
  73. {
  74. allocate_buf(e);
  75. e->data.map.idx = open_map(e->data.map.name, e->data.map.mod,
  76. e->data.map.row, e->data.map.col);
  77. }
  78. static void initialize_function(expression * e)
  79. {
  80. int i;
  81. allocate_buf(e);
  82. e->data.func.argv = G_malloc((e->data.func.argc + 1) * sizeof(void *));
  83. e->data.func.argv[0] = e->buf;
  84. for (i = 1; i <= e->data.func.argc; i++) {
  85. initialize(e->data.func.args[i]);
  86. e->data.func.argv[i] = e->data.func.args[i]->buf;
  87. }
  88. }
  89. static void initialize_binding(expression * e)
  90. {
  91. initialize(e->data.bind.val);
  92. set_buf(e, e->data.bind.val->buf);
  93. }
  94. static void initialize(expression * e)
  95. {
  96. switch (e->type) {
  97. case expr_type_constant:
  98. initialize_constant(e);
  99. break;
  100. case expr_type_variable:
  101. initialize_variable(e);
  102. break;
  103. case expr_type_map:
  104. initialize_map(e);
  105. break;
  106. case expr_type_function:
  107. initialize_function(e);
  108. break;
  109. case expr_type_binding:
  110. initialize_binding(e);
  111. break;
  112. default:
  113. G_fatal_error(_("Unknown type: %d"), e->type);
  114. }
  115. }
  116. /****************************************************************************/
  117. static void do_evaluate(void *p)
  118. {
  119. evaluate((struct expression *) p);
  120. }
  121. static void begin_evaluate(struct expression *e)
  122. {
  123. G_begin_execute(do_evaluate, e, &e->worker, 0);
  124. }
  125. static void end_evaluate(struct expression *e)
  126. {
  127. G_end_execute(&e->worker);
  128. }
  129. /****************************************************************************/
  130. static void evaluate_constant(expression * e)
  131. {
  132. int *ibuf = e->buf;
  133. float *fbuf = e->buf;
  134. double *dbuf = e->buf;
  135. int i;
  136. switch (e->res_type) {
  137. case CELL_TYPE:
  138. for (i = 0; i < columns; i++)
  139. ibuf[i] = e->data.con.ival;
  140. break;
  141. case FCELL_TYPE:
  142. for (i = 0; i < columns; i++)
  143. fbuf[i] = e->data.con.fval;
  144. break;
  145. case DCELL_TYPE:
  146. for (i = 0; i < columns; i++)
  147. dbuf[i] = e->data.con.fval;
  148. break;
  149. default:
  150. G_fatal_error(_("Invalid type: %d"), e->res_type);
  151. }
  152. }
  153. static void evaluate_variable(expression * e)
  154. {
  155. /* this is a no-op */
  156. }
  157. static void evaluate_map(expression * e)
  158. {
  159. get_map_row(e->data.map.idx,
  160. e->data.map.mod,
  161. current_depth + e->data.map.depth,
  162. current_row + e->data.map.row,
  163. e->data.map.col, e->buf, e->res_type);
  164. }
  165. static void evaluate_function(expression * e)
  166. {
  167. int i;
  168. int res;
  169. if (e->data.func.argc > 1 && e->data.func.func != f_eval) {
  170. for (i = 1; i <= e->data.func.argc; i++)
  171. begin_evaluate(e->data.func.args[i]);
  172. for (i = 1; i <= e->data.func.argc; i++)
  173. end_evaluate(e->data.func.args[i]);
  174. }
  175. else
  176. for (i = 1; i <= e->data.func.argc; i++)
  177. evaluate(e->data.func.args[i]);
  178. res = (*e->data.func.func) (e->data.func.argc,
  179. e->data.func.argt, e->data.func.argv);
  180. switch (res) {
  181. case E_ARG_LO:
  182. G_fatal_error(_("Too few arguments for function '%s'"),
  183. e->data.func.name);
  184. break;
  185. case E_ARG_HI:
  186. G_fatal_error(_("Too many arguments for function '%s'"),
  187. e->data.func.name);
  188. break;
  189. case E_ARG_TYPE:
  190. G_fatal_error(_("Invalid argument type for function '%s'"),
  191. e->data.func.name);
  192. break;
  193. case E_RES_TYPE:
  194. G_fatal_error(_("Invalid return type for function '%s'"),
  195. e->data.func.name);
  196. break;
  197. case E_INV_TYPE:
  198. G_fatal_error(_("Unknown type for function '%s'"), e->data.func.name);
  199. break;
  200. case E_ARG_NUM:
  201. G_fatal_error(_("Number of arguments for function '%s'"),
  202. e->data.func.name);
  203. break;
  204. case E_WTF:
  205. G_fatal_error(_("Unknown error for function '%s'"),
  206. e->data.func.name);
  207. break;
  208. }
  209. }
  210. static void evaluate_binding(expression * e)
  211. {
  212. evaluate(e->data.bind.val);
  213. }
  214. /****************************************************************************/
  215. static void evaluate(expression * e)
  216. {
  217. switch (e->type) {
  218. case expr_type_constant:
  219. evaluate_constant(e);
  220. break;
  221. case expr_type_variable:
  222. evaluate_variable(e);
  223. break;
  224. case expr_type_map:
  225. evaluate_map(e);
  226. break;
  227. case expr_type_function:
  228. evaluate_function(e);
  229. break;
  230. case expr_type_binding:
  231. evaluate_binding(e);
  232. break;
  233. default:
  234. G_fatal_error(_("Unknown type: %d"), e->type);
  235. }
  236. }
  237. /****************************************************************************/
  238. static expr_list *exprs;
  239. /****************************************************************************/
  240. static void error_handler(void *p)
  241. {
  242. expr_list *l;
  243. for (l = exprs; l; l = l->next) {
  244. expression *e = l->exp;
  245. int fd = e->data.bind.fd;
  246. if (fd >= 0)
  247. unopen_output_map(fd);
  248. }
  249. }
  250. void execute(expr_list * ee)
  251. {
  252. int verbose = isatty(2);
  253. expr_list *l;
  254. int count, n;
  255. exprs = ee;
  256. G_add_error_handler(error_handler, NULL);
  257. for (l = ee; l; l = l->next) {
  258. expression *e = l->exp;
  259. const char *var;
  260. if (e->type != expr_type_binding && e->type != expr_type_function)
  261. G_fatal_error("internal error: execute: invalid type: %d",
  262. e->type);
  263. if (e->type != expr_type_binding)
  264. continue;
  265. var = e->data.bind.var;
  266. if (!overwrite_flag && check_output_map(var))
  267. G_fatal_error(_("output map <%s> exists. To overwrite, "
  268. "use the --overwrite flag"), var);
  269. }
  270. /* Parse each expression and extract all raster maps */
  271. for (l = ee; l; l = l->next) {
  272. expression *e = l->exp;
  273. extract_maps(e);
  274. }
  275. /* Set the region from the input maps*/
  276. if (region_approach == 2)
  277. prepare_region_from_maps_union(map_list, num_maps);
  278. if (region_approach == 3)
  279. prepare_region_from_maps_intersect(map_list, num_maps);
  280. setup_region();
  281. /* Parse each expression and initialize the maps, buffers and variables */
  282. for (l = ee; l; l = l->next) {
  283. expression *e = l->exp;
  284. const char *var;
  285. expression *val;
  286. initialize(e);
  287. if (e->type != expr_type_binding)
  288. continue;
  289. var = e->data.bind.var;
  290. val = e->data.bind.val;
  291. e->data.bind.fd = open_output_map(var, val->res_type);
  292. }
  293. setup_maps();
  294. count = rows * depths;
  295. n = 0;
  296. G_init_workers();
  297. for (current_depth = 0; current_depth < depths; current_depth++) {
  298. for (current_row = 0; current_row < rows; current_row++) {
  299. if (verbose)
  300. G_percent(n, count, 2);
  301. for (l = ee; l; l = l->next) {
  302. expression *e = l->exp;
  303. int fd;
  304. evaluate(e);
  305. if (e->type != expr_type_binding)
  306. continue;
  307. fd = e->data.bind.fd;
  308. put_map_row(fd, e->buf, e->res_type);
  309. }
  310. n++;
  311. }
  312. }
  313. G_finish_workers();
  314. if (verbose)
  315. G_percent(n, count, 2);
  316. for (l = ee; l; l = l->next) {
  317. expression *e = l->exp;
  318. const char *var;
  319. expression *val;
  320. int fd;
  321. if (e->type != expr_type_binding)
  322. continue;
  323. var = e->data.bind.var;
  324. val = e->data.bind.val;
  325. fd = e->data.bind.fd;
  326. close_output_map(fd);
  327. e->data.bind.fd = -1;
  328. if (val->type == expr_type_map) {
  329. if (val->data.map.mod == 'M') {
  330. copy_cats(var, val->data.map.idx);
  331. copy_colors(var, val->data.map.idx);
  332. }
  333. copy_history(var, val->data.map.idx);
  334. }
  335. else
  336. create_history(var, val);
  337. }
  338. G_unset_error_routine();
  339. }
  340. void describe_maps(FILE *fp, expr_list *ee)
  341. {
  342. expr_list *l;
  343. fprintf(fp, "output=");
  344. for (l = ee; l; l = l->next) {
  345. expression *e = l->exp;
  346. const char *var;
  347. if (e->type != expr_type_binding && e->type != expr_type_function)
  348. G_fatal_error("internal error: execute: invalid type: %d",
  349. e->type);
  350. initialize(e);
  351. if (e->type != expr_type_binding)
  352. continue;
  353. var = e->data.bind.var;
  354. fprintf(fp, "%s%s", l != ee ? "," : "", var);
  355. }
  356. fprintf(fp, "\n");
  357. fprintf(fp, "input=");
  358. list_maps(fp, ",");
  359. fprintf(fp, "\n");
  360. }
  361. /****************************************************************************/