get_stats.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <grass/gis.h>
  6. #include <grass/raster.h>
  7. #include <grass/spawn.h>
  8. #include "options.h"
  9. #include "dhist.h"
  10. static void run_stats(const char *mapname, const char *tempfile)
  11. {
  12. char buf[32];
  13. const char *argv[12];
  14. int argc = 0;
  15. argv[argc++] = "r.stats";
  16. argv[argc++] = "-r";
  17. if (cat_ranges)
  18. argv[argc++] = "-C";
  19. argv[argc++] = type == COUNT
  20. ? "-c"
  21. : "-a";
  22. argv[argc++] = mapname;
  23. if (!cat_ranges) {
  24. sprintf(buf, "nsteps=%d", nsteps);
  25. argv[argc++] = buf;
  26. }
  27. argv[argc++] = SF_REDIRECT_FILE;
  28. argv[argc++] = SF_STDOUT;
  29. argv[argc++] = SF_MODE_OUT;
  30. argv[argc++] = tempfile;
  31. argv[argc++] = NULL;
  32. if (G_vspawn_ex(argv[0], argv) != 0)
  33. G_fatal_error("error running r.stats");
  34. }
  35. int get_stats(const char *mapname, struct stat_list *dist_stats) /* linked list of stats */
  36. {
  37. char buf[1024]; /* input buffer for reading stats */
  38. int done = 0;
  39. char *tempfile; /* temp file name */
  40. FILE *fd; /* temp file pointer */
  41. long int cat; /* a category value */
  42. long int stat; /* a category stat value */
  43. struct stat_node *ptr = NULL;
  44. int first;
  45. /* write stats to a temp file */
  46. tempfile = G_tempfile();
  47. is_fp = Rast_map_is_fp(mapname, "");
  48. if (is_fp) {
  49. if (cat_ranges) {
  50. if (Rast_read_cats(mapname, "", &cats) < 0)
  51. G_fatal_error("Can't read category file");
  52. if (Rast_number_of_cats(&cats) <= 0) {
  53. G_warning("There are no labeled cats, using nsteps argument");
  54. cat_ranges = 0;
  55. }
  56. }
  57. if (Rast_read_fp_range(map_name, "", &fp_range) <= 0)
  58. G_fatal_error("Can't read frange file");
  59. }
  60. run_stats(mapname, tempfile);
  61. /* open temp file and read the stats into a linked list */
  62. fd = fopen(tempfile, "r");
  63. if (fd == NULL) {
  64. perror("opening r.stats output file");
  65. G_fatal_error("unable to continue.");
  66. }
  67. dist_stats->ptr = NULL;
  68. dist_stats->count = 0;
  69. dist_stats->sumstat = 0;
  70. first = 1;
  71. while (!done) {
  72. if (fgets(buf, sizeof(buf), fd) != NULL) {
  73. /* WARNING!!!!!!
  74. * this will be very wrong if type!=COUNT
  75. * since the stat prodcued by r.stats will be a floating point value
  76. * possibly less than 1 (shapiro)
  77. */
  78. if (sscanf(buf, "* %ld", &stat) == 1) {
  79. dist_stats->null_stat = stat;
  80. if (stat > dist_stats->maxstat && nodata)
  81. dist_stats->maxstat = stat;
  82. if (stat < dist_stats->minstat && nodata)
  83. dist_stats->minstat = stat;
  84. if (nodata)
  85. dist_stats->sumstat += stat;
  86. }
  87. else if (sscanf(buf, "%ld %ld", &cat, &stat) == 2) {
  88. /* count stats */
  89. dist_stats->count++;
  90. /* sum stats */
  91. dist_stats->sumstat += stat;
  92. /* a max or a min stat? */
  93. if (first) {
  94. dist_stats->maxstat = stat;
  95. dist_stats->minstat = stat;
  96. dist_stats->maxcat = cat;
  97. dist_stats->mincat = cat;
  98. first = 0;
  99. }
  100. if (stat > dist_stats->maxstat)
  101. dist_stats->maxstat = stat;
  102. if (stat < dist_stats->minstat)
  103. dist_stats->minstat = stat;
  104. /* a max or a min cat? */
  105. if (cat > dist_stats->maxcat)
  106. dist_stats->maxcat = cat;
  107. if (cat < dist_stats->mincat)
  108. dist_stats->mincat = cat;
  109. /* put it in the list */
  110. if (dist_stats->ptr == NULL) {
  111. /* first in list */
  112. dist_stats->ptr = (struct stat_node *)
  113. G_malloc(sizeof(struct stat_node));
  114. dist_stats->ptr->cat = cat;
  115. dist_stats->ptr->stat = stat;
  116. dist_stats->ptr->next = NULL;
  117. ptr = dist_stats->ptr;
  118. }
  119. else {
  120. ptr->next = (struct stat_node *)
  121. G_malloc(sizeof(struct stat_node));
  122. ptr->next->cat = cat;
  123. ptr->next->stat = stat;
  124. ptr->next->next = NULL; /* mod: shapiro */
  125. ptr = ptr->next;
  126. }
  127. }
  128. }
  129. else
  130. done = 1;
  131. }
  132. fclose(fd);
  133. unlink(tempfile);
  134. return 0;
  135. }