read_rules.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <grass/raster.h>
  5. #include <grass/glocale.h>
  6. #include "global.h"
  7. #define INCR 20
  8. int read_range(void)
  9. {
  10. struct FPRange drange;
  11. struct Range range;
  12. CELL tmp_min, tmp_max;
  13. DCELL tmp_dmin, tmp_dmax;
  14. char buff[1024];
  15. int i;
  16. /* read the fpranges and ranges of all input maps */
  17. for (i = 0; i < noi; i++) {
  18. if (Rast_read_fp_range(name[i], G_mapset(), &drange) <= 0) {
  19. sprintf(buff, "Can't read f_range for map %s", name[i]);
  20. G_fatal_error("%s", buff);
  21. }
  22. Rast_get_fp_range_min_max(&drange, &tmp_dmin, &tmp_dmax);
  23. if (Rast_read_range(name[i], G_mapset(), &range) <= 0) {
  24. sprintf(buff, "Can't read range for map %s", name[i]);
  25. G_fatal_error("%s", buff);
  26. }
  27. Rast_get_range_min_max(&range, &tmp_min, &tmp_max);
  28. if (!i || tmp_max > old_max || Rast_is_c_null_value(&old_max))
  29. old_max = tmp_max;
  30. if (!i || tmp_min < old_min || Rast_is_c_null_value(&old_min))
  31. old_min = tmp_min;
  32. if (!i || tmp_dmax > old_dmax || Rast_is_d_null_value(&old_dmax))
  33. old_dmax = tmp_dmax;
  34. if (!i || tmp_dmin < old_dmin || Rast_is_d_null_value(&old_dmin))
  35. old_dmin = tmp_dmin;
  36. } /* for loop */
  37. return 0;
  38. }
  39. int report_range(void)
  40. {
  41. char buff[300], buff2[300];
  42. if (Rast_is_d_null_value(&old_dmin) || Rast_is_d_null_value(&old_dmax))
  43. G_message(_("Old data range is empty"));
  44. else {
  45. sprintf(buff, "%.15g", old_dmin);
  46. sprintf(buff2, "%.15g", old_dmax);
  47. G_trim_decimal(buff);
  48. G_trim_decimal(buff2);
  49. G_message(_("Old data range is %s to %s"), buff, buff2);
  50. }
  51. if (Rast_is_c_null_value(&old_min) || Rast_is_c_null_value(&old_max))
  52. G_message(_("Old integer data range is empty"));
  53. else
  54. G_message(_("Old integer data range is %d to %d"),
  55. (int)old_min, (int)old_max);
  56. return 0;
  57. }
  58. int read_rules(const char *filename)
  59. {
  60. char buf[1024];
  61. DCELL dLow, dHigh;
  62. CELL iLow, iHigh;
  63. int line, n, nrules = 0;
  64. int first = 1;
  65. DCELL dmin, dmax;
  66. CELL cmin, cmax;
  67. FILE *fp;
  68. if (strcmp(filename, "-") == 0)
  69. fp = stdin;
  70. else {
  71. fp = fopen(filename, "r");
  72. if (!fp)
  73. G_fatal_error(_("unable to open input file <%s>"), filename);
  74. }
  75. read_range();
  76. report_range();
  77. if (isatty(fileno(fp)))
  78. fprintf(stderr, _("\nEnter the rule or 'help' for the format description or 'end' to exit:\n"));
  79. Rast_quant_init(&quant_struct);
  80. for (line = 1;; line++) {
  81. if (isatty(fileno(fp)))
  82. fprintf(stderr, "> ");
  83. if (!G_getl2(buf, sizeof(buf), fp))
  84. break;
  85. for (n = 0; buf[n]; n++)
  86. if (buf[n] == ',')
  87. buf[n] = ' ';
  88. G_strip(buf);
  89. G_chop(buf);
  90. if (*buf == 0)
  91. continue;
  92. if (*buf == '#')
  93. continue;
  94. if (strcmp(buf, "end") == 0) {
  95. if (nrules == 0)
  96. break; /* if no new rules have been specified */
  97. /* give warning when quant rules do not cover the whole range of map */
  98. Rast_quant_get_limits(&quant_struct, &dmin, &dmax, &cmin, &cmax);
  99. if ((dmin > old_dmin || dmax < old_dmax) && !first)
  100. G_warning(_("quant rules do not cover the whole range map"));
  101. break;
  102. }
  103. if (strcmp(buf, "help") == 0) {
  104. fprintf(stderr,
  105. "Enter a rule in one of these formats:\n"
  106. "float_low:float_high:int_low:int_high\n"
  107. "float_low:float_high:int_val (i.e. int_high == int_low)\n"
  108. "*:float_val:int_val (interval [inf, float_val])\n"
  109. "float_val:*:int_val (interval [float_val, inf])\n");
  110. }
  111. /* we read and record into quant table all values, even int as doubles
  112. we convert the range and domain values to the right format when we
  113. lookup the values in the quant table */
  114. switch (sscanf(buf, "%lf:%lf:%d:%d", &dLow, &dHigh, &iLow, &iHigh)) {
  115. case 3:
  116. Rast_quant_add_rule(&quant_struct, dLow, dHigh, iLow, iLow);
  117. nrules++;
  118. first = 0;
  119. break;
  120. case 4:
  121. Rast_quant_add_rule(&quant_struct, dLow, dHigh, iLow, iHigh);
  122. nrules++;
  123. first = 0;
  124. break;
  125. default:
  126. if (sscanf(buf, "%lf:*:%d", &dLow, &iLow) == 2) {
  127. Rast_quant_set_pos_infinite_rule(&quant_struct, dLow, iLow);
  128. nrules++;
  129. first = 0;
  130. }
  131. else if (sscanf(buf, "*:%lf:%d", &dHigh, &iLow) == 2) {
  132. Rast_quant_set_neg_infinite_rule(&quant_struct, dHigh, iLow);
  133. nrules++;
  134. first = 0;
  135. }
  136. else if (strcmp(buf, "help") == 0)
  137. break;
  138. else
  139. G_warning(_("%s is not a valid rule"), buf);
  140. break;
  141. } /* switch */
  142. } /* loop */
  143. if (fp != stdin)
  144. fclose(fp);
  145. return nrules;
  146. }