stats.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /****************************************************************************
  2. *
  3. * MODULE: r.colors
  4. *
  5. * AUTHOR(S): Michael Shapiro - CERL
  6. * David Johnson
  7. *
  8. * PURPOSE: Allows creation and/or modification of the color table
  9. * for a raster map layer.
  10. *
  11. * COPYRIGHT: (C) 2006 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. ***************************************************************************/
  18. #include <math.h>
  19. #include <stdlib.h>
  20. #include "local_proto.h"
  21. int get_stats(struct maps_info *input_maps, struct Cell_stats *statf) {
  22. CELL *cell;
  23. int row, nrows, ncols;
  24. int fd;
  25. int i;
  26. Rast_init_cell_stats(statf);
  27. for(i = 0; i < input_maps->num; i++) {
  28. fd = Rast_open_old(input_maps->names[i], input_maps->mapsets[i]);
  29. cell = Rast_allocate_c_buf();
  30. nrows = Rast_window_rows();
  31. ncols = Rast_window_cols();
  32. G_verbose_message(_("(%i/%i) Reading raster map <%s>..."),
  33. i + 1, input_maps->num,
  34. G_fully_qualified_name(input_maps->names[i], input_maps->mapsets[i]));
  35. for (row = 0; row < nrows; row++) {
  36. G_percent(row, nrows, 2);
  37. Rast_get_c_row(fd, cell, row);
  38. Rast_update_cell_stats(cell, ncols, statf);
  39. }
  40. G_percent(row, nrows, 2);
  41. Rast_close(fd);
  42. G_free(cell);
  43. }
  44. return 1;
  45. }
  46. void get_fp_stats(struct maps_info *input_maps,
  47. struct FP_stats *statf,
  48. DCELL min, DCELL max, int geometric, int geom_abs, int type) {
  49. DCELL *dcell = NULL;
  50. int row, col, depth, nrows, ncols, ndepths = 1;
  51. int fd;
  52. int i;
  53. char *name;
  54. char *mapset;
  55. RASTER3D_Map *map3d = NULL;
  56. statf->geometric = geometric;
  57. statf->geom_abs = geom_abs;
  58. statf->flip = 0;
  59. if (statf->geometric) {
  60. if (min * max < 0)
  61. G_fatal_error(_("Unable to use logarithmic scaling if range includes zero"));
  62. if (min < 0) {
  63. statf->flip = 1;
  64. min = -min;
  65. max = -max;
  66. }
  67. min = log(min);
  68. max = log(max);
  69. }
  70. if (statf->geom_abs) {
  71. double a = log(fabs(min) + 1);
  72. double b = log(fabs(max) + 1);
  73. int has_zero = min * max < 0;
  74. min = a < b ? a : b;
  75. max = a > b ? a : b;
  76. if (has_zero)
  77. min = 0;
  78. }
  79. statf->count = 1000;
  80. statf->min = min;
  81. statf->max = max;
  82. statf->stats = G_calloc(statf->count + 1, sizeof (unsigned long));
  83. statf->total = 0;
  84. /* Loop over all input maps */
  85. for(i = 0; i < input_maps->num; i++) {
  86. name = input_maps->names[i];
  87. mapset = input_maps->mapsets[i];
  88. if (type == RASTER_TYPE) {
  89. fd = Rast_open_old(name, mapset);
  90. dcell = Rast_allocate_d_buf();
  91. nrows = Rast_window_rows();
  92. ncols = Rast_window_cols();
  93. } else {
  94. /* Initiate the default settings */
  95. Rast3d_init_defaults();
  96. map3d = Rast3d_open_cell_old(name, mapset, RASTER3D_DEFAULT_WINDOW,
  97. RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT);
  98. if (map3d == NULL)
  99. Rast3d_fatal_error(_("Error opening 3d raster map"));
  100. nrows = map3d->window.rows;
  101. ncols = map3d->window.cols;
  102. ndepths = map3d->window.depths;
  103. }
  104. G_verbose_message(_("(%i/%i) Reading map <%s>..."), i, input_maps->num,
  105. G_fully_qualified_name(name, mapset));
  106. for (depth = 0; depth < ndepths; depth++) {
  107. for (row = 0; row < nrows; row++) {
  108. G_percent(row, nrows, 2);
  109. if (type == RASTER_TYPE)
  110. Rast_get_d_row(fd, dcell, row);
  111. for (col = 0; col < ncols; col++) {
  112. DCELL x;
  113. int j;
  114. if (type == RASTER_TYPE)
  115. x = dcell[col];
  116. else
  117. x = Rast3d_get_double(map3d, col, row, depth);
  118. if (Rast_is_d_null_value(&x))
  119. continue;
  120. if (statf->flip)
  121. x = -x;
  122. if (statf->geometric)
  123. x = log(x);
  124. if (statf->geom_abs)
  125. x = log(fabs(x) + 1);
  126. j = (int) floor(statf->count * (x - statf->min) / (statf->max - statf->min));
  127. statf->stats[j]++;
  128. statf->total++;
  129. }
  130. }
  131. }
  132. G_percent(row, nrows, 2);
  133. if (type == RASTER_TYPE) {
  134. Rast_close(fd);
  135. if(dcell)
  136. G_free(dcell);
  137. } else {
  138. Rast3d_close(map3d);
  139. }
  140. }
  141. }