r3.univar_main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * r3.univar
  3. *
  4. * Calculates univariate statistics from the non-null 3d cells of a raster3d map
  5. *
  6. * Copyright (C) 2004-2007 by the GRASS Development Team
  7. * Author(s): Soeren Gebbert
  8. * Based on r.univar from Hamish Bowman, University of Otago, New Zealand
  9. * and Martin Landa
  10. * heapsort code from http://de.wikipedia.org/wiki/Heapsort
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. */
  17. #include "globals.h"
  18. param_type param;
  19. /* ************************************************************************* */
  20. /* Set up the arguments we are expecting ********************************** */
  21. /* ************************************************************************* */
  22. void set_params(void)
  23. {
  24. param.inputfile = G_define_standard_option(G_OPT_R3_INPUT);
  25. param.percentile = G_define_option();
  26. param.percentile->key = "percentile";
  27. param.percentile->type = TYPE_INTEGER;
  28. param.percentile->required = NO;
  29. param.percentile->multiple = YES;
  30. param.percentile->options = "0-100";
  31. param.percentile->answer = "90";
  32. param.percentile->description =
  33. _("Percentile to calculate (requires extended statistics flag)");
  34. param.shell_style = G_define_flag();
  35. param.shell_style->key = 'g';
  36. param.shell_style->description =
  37. _("Print the stats in shell script style");
  38. param.extended = G_define_flag();
  39. param.extended->key = 'e';
  40. param.extended->description = _("Calculate extended statistics");
  41. return;
  42. }
  43. /* *************************************************************** */
  44. /* **** the main functions for r3.univar ************************* */
  45. /* *************************************************************** */
  46. int main(int argc, char *argv[])
  47. {
  48. float val_f; /* for misc use */
  49. double val_d; /* for misc use */
  50. int first = TRUE; /* min/max init flag */
  51. int map_type;
  52. univar_stat *stats;
  53. char *infile;
  54. void *map;
  55. G3D_Region region;
  56. unsigned int i;
  57. unsigned int rows, cols, depths;
  58. unsigned int x, y, z;
  59. struct GModule *module;
  60. G_gisinit(argv[0]);
  61. module = G_define_module();
  62. module->keywords = _("raster3d, statistics");
  63. module->description =
  64. _("Calculates univariate statistics from the non-null 3d cells of a raster3d map.");
  65. /* Define the different options */
  66. set_params();
  67. if (G_parser(argc, argv))
  68. exit(EXIT_FAILURE);
  69. /*Set the defaults */
  70. G3d_initDefaults();
  71. /*get the current region */
  72. G3d_getWindow(&region);
  73. cols = region.cols;
  74. rows = region.rows;
  75. depths = region.depths;
  76. infile = param.inputfile->answer;
  77. if (NULL == G_find_grid3(infile, ""))
  78. G3d_fatalError(_("Requested g3d map <%s> not found"), infile);
  79. /*Open all maps with default region */
  80. map =
  81. G3d_openCellOld(infile, G_find_grid3(infile, ""), &region,
  82. G3D_TILE_SAME_AS_FILE, G3D_USE_CACHE_DEFAULT);
  83. if (map == NULL)
  84. G3d_fatalError(_("Error opening g3d map <%s>"), infile);
  85. map_type = G3d_tileTypeMap(map);
  86. i = 0;
  87. while (param.percentile->answers[i])
  88. i++;
  89. stats = create_univar_stat_struct(map_type, cols * rows * depths, i);
  90. for (i = 0; i < stats->n_perc; i++) {
  91. sscanf(param.percentile->answers[i], "%i", &stats->perc[i]);
  92. }
  93. stats->n = 0;
  94. for (z = 0; z < depths; z++) { /*From the bottom to the top */
  95. if (!(param.shell_style->answer))
  96. G_percent(z, depths - 1, 10);
  97. for (y = 0; y < rows; y++) {
  98. for (x = 0; x < cols; x++) {
  99. if (map_type == FCELL_TYPE) {
  100. G3d_getValue(map, x, y, z, &val_f, map_type);
  101. if (!G3d_isNullValueNum(&val_f, map_type)) {
  102. if (param.extended->answer)
  103. stats->fcell_array[stats->n] = val_f;
  104. stats->sum += val_f;
  105. stats->sumsq += (val_f * val_f);
  106. stats->sum_abs += fabs(val_f);
  107. if (first) {
  108. stats->max = val_f;
  109. stats->min = val_f;
  110. first = FALSE;
  111. }
  112. else {
  113. if (val_f > stats->max)
  114. stats->max = val_f;
  115. if (val_f < stats->min)
  116. stats->min = val_f;
  117. }
  118. stats->n++;
  119. }
  120. }
  121. else if (map_type == DCELL_TYPE) {
  122. G3d_getValue(map, x, y, z, &val_d, map_type);
  123. if (!G3d_isNullValueNum(&val_d, map_type)) {
  124. if (param.extended->answer)
  125. stats->dcell_array[stats->n] = val_d;
  126. stats->sum += val_d;
  127. stats->sumsq += val_d * val_d;
  128. stats->sum_abs += fabs(val_d);
  129. if (first) {
  130. stats->max = val_d;
  131. stats->min = val_d;
  132. first = FALSE;
  133. }
  134. else {
  135. if (val_d > stats->max)
  136. stats->max = val_d;
  137. if (val_d < stats->min)
  138. stats->min = val_d;
  139. }
  140. stats->n++;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /* create the output */
  147. print_stats(stats);
  148. /* release memory */
  149. free_univar_stat_struct(stats);
  150. exit(EXIT_SUCCESS);
  151. }