cell_stats.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdlib.h>
  2. #include <grass/glocale.h>
  3. #include "global.h"
  4. int cell_stats(int fd[], int with_percents, int with_counts,
  5. int with_areas, int do_sort, int with_labels, char *fmt)
  6. {
  7. CELL **cell;
  8. int i;
  9. int row;
  10. double unit_area;
  11. int planimetric = 0;
  12. int compute_areas;
  13. double G_area_of_cell_at_row();
  14. /* allocate i/o buffers for each raster map */
  15. cell = (CELL **) G_calloc(nfiles, sizeof(CELL *));
  16. for (i = 0; i < nfiles; i++)
  17. cell[i] = Rast_allocate_c_buf();
  18. /* if we want area totals, set this up.
  19. * distinguish projections which are planimetric (all cells same size)
  20. * from those which are not (e.g., lat-long)
  21. */
  22. unit_area = 0.0;
  23. if (with_areas) {
  24. switch (G_begin_cell_area_calculations()) {
  25. case 0: /* areas don't make sense, but ignore this for now */
  26. case 1:
  27. planimetric = 1;
  28. unit_area = G_area_of_cell_at_row(0);
  29. break;
  30. default:
  31. planimetric = 0;
  32. break;
  33. }
  34. }
  35. compute_areas = with_areas && !planimetric;
  36. /* here we go */
  37. initialize_cell_stats(nfiles);
  38. for (row = 0; row < nrows; row++) {
  39. if (compute_areas)
  40. unit_area = G_area_of_cell_at_row(row);
  41. G_percent(row, nrows, 2);
  42. for (i = 0; i < nfiles; i++) {
  43. Rast_get_c_row(fd[i], cell[i], row);
  44. /* include max FP value in nsteps'th bin */
  45. if(is_fp[i])
  46. fix_max_fp_val(cell[i], ncols);
  47. /* we can't compute hash on null values, so we change all
  48. nulls to max+1, set NULL_CELL to max+1, and later compare
  49. with NULL_CELL to chack for nulls */
  50. reset_null_vals(cell[i], ncols);
  51. }
  52. update_cell_stats(cell, ncols, unit_area);
  53. }
  54. G_percent(row, nrows, 2);
  55. sort_cell_stats(do_sort);
  56. print_cell_stats(fmt, with_percents, with_counts, with_areas, with_labels,
  57. fs);
  58. return 0;
  59. }