main.c 5.4 KB

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