raw_stats.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. #include "global.h"
  6. int raw_stats(int fd[], int with_coordinates, int with_xy, int with_labels)
  7. {
  8. CELL null_cell;
  9. void **rast, **rastp;
  10. char str1[50];
  11. register int i;
  12. int row, col, nulls_found;
  13. double Rast_row_to_northing(), G_col_to_easting();
  14. struct Cell_head window;
  15. char nbuf[100], ebuf[100];
  16. RASTER_MAP_TYPE *map_type;
  17. /* allocate i/o buffers for each raster map */
  18. rast = (void **)G_calloc(nfiles, sizeof(void *));
  19. rastp = (void **)G_calloc(nfiles, sizeof(void *));
  20. map_type = (RASTER_MAP_TYPE *) G_calloc(nfiles, sizeof(RASTER_MAP_TYPE));
  21. for (i = 0; i < nfiles; i++) {
  22. /* if fp map and report real data, not cat indexes, set type to DCELL */
  23. if (is_fp[i] && !raw_output && !as_int)
  24. map_type[i] = Rast_get_map_type(fd[i]);
  25. else
  26. map_type[i] = CELL_TYPE;
  27. rast[i] = Rast_allocate_buf(map_type[i]);
  28. }
  29. /* get window */
  30. if (with_coordinates)
  31. G_get_set_window(&window);
  32. /* here we go */
  33. Rast_set_c_null_value(&null_cell, 1);
  34. for (row = 0; row < nrows; row++) {
  35. G_percent(row, nrows, 2);
  36. /* read the rows and set the pointers */
  37. for (i = 0; i < nfiles; i++) {
  38. Rast_get_row(fd[i], rast[i], row, map_type[i]);
  39. rastp[i] = rast[i];
  40. }
  41. if (with_coordinates)
  42. G_format_northing(Rast_row_to_northing(row + .5, &window), nbuf,
  43. G_projection() == PROJECTION_LL ? -1 : 0);
  44. for (col = 0; col < ncols; col++) {
  45. if (no_nulls || no_nulls_all) {
  46. nulls_found = 0;
  47. for (i = 0; i < nfiles; i++) {
  48. /*
  49. Rast_set_d_value(zero_val, 0.0, map_type[i]);
  50. if (Rast_raster_cmp(rastp[i], zero_val, map_type[i]) != 0)
  51. break;
  52. */
  53. if (Rast_is_null_value(rastp[i], map_type[i]))
  54. nulls_found++;
  55. }
  56. if ((nulls_found == nfiles) || (nulls_found && no_nulls)) {
  57. for (i = 0; i < nfiles; i++)
  58. rastp[i] = G_incr_void_ptr(rastp[i],
  59. Rast_cell_size(map_type
  60. [i]));
  61. continue;
  62. }
  63. }
  64. if (with_coordinates) {
  65. G_format_easting(Rast_col_to_easting(col + .5, &window), ebuf,
  66. G_projection() == PROJECTION_LL ? -1 : 0);
  67. fprintf(stdout, "%s%s%s%s", ebuf, fs, nbuf, fs);
  68. }
  69. if (with_xy)
  70. fprintf(stdout, "%d%s%d%s", col + 1, fs, row + 1, fs);
  71. for (i = 0; i < nfiles; i++) {
  72. if (Rast_is_null_value(rastp[i], map_type[i])) {
  73. fprintf(stdout, "%s%s", i ? fs : "", no_data_str);
  74. if (with_labels)
  75. fprintf(stdout, "%s%s", fs,
  76. Rast_get_c_cat(&null_cell, &labels[i]));
  77. }
  78. else if (map_type[i] == CELL_TYPE) {
  79. fprintf(stdout, "%s%ld", i ? fs : "",
  80. (long)*((CELL *) rastp[i]));
  81. if (with_labels && !is_fp[i])
  82. fprintf(stdout, "%s%s", fs,
  83. Rast_get_c_cat((CELL *) rastp[i], &labels[i]));
  84. }
  85. else if (map_type[i] == FCELL_TYPE) {
  86. sprintf(str1, "%.8g", *((FCELL *) rastp[i]));
  87. G_trim_decimal(str1);
  88. G_strip(str1);
  89. fprintf(stdout, "%s%s", i ? fs : "", str1);
  90. if (with_labels)
  91. fprintf(stdout, "%s%s", fs,
  92. Rast_get_f_cat((FCELL *) rastp[i],
  93. &labels[i]));
  94. }
  95. else if (map_type[i] == DCELL_TYPE) {
  96. sprintf(str1, "%.16g", *((DCELL *) rastp[i]));
  97. G_trim_decimal(str1);
  98. G_strip(str1);
  99. fprintf(stdout, "%s%s", i ? fs : "", str1);
  100. if (with_labels)
  101. fprintf(stdout, "%s%s", fs,
  102. Rast_get_d_cat((DCELL *) rastp[i],
  103. &labels[i]));
  104. }
  105. else
  106. G_fatal_error(_("Invalid map type"));
  107. rastp[i] =
  108. G_incr_void_ptr(rastp[i], Rast_cell_size(map_type[i]));
  109. }
  110. fprintf(stdout, "\n");
  111. }
  112. }
  113. G_percent(row, nrows, 2);
  114. return 0;
  115. }