r.univar_main.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * r.univar
  3. *
  4. * Calculates univariate statistics from the non-null cells of a GRASS raster map
  5. *
  6. * Copyright (C) 2004-2006, 2012 by the GRASS Development Team
  7. * Author(s): Hamish Bowman, University of Otago, New Zealand
  8. * Extended stats: Martin Landa
  9. * Zonal stats: Markus Metz
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. * This program is a replacement for the r.univar shell script
  16. */
  17. #include <assert.h>
  18. #include <string.h>
  19. #include "globals.h"
  20. param_type param;
  21. zone_type zone_info;
  22. /* ************************************************************************* */
  23. /* Set up the arguments we are expecting ********************************** */
  24. /* ************************************************************************* */
  25. void set_params()
  26. {
  27. param.inputfile = G_define_standard_option(G_OPT_R_MAPS);
  28. param.zonefile = G_define_standard_option(G_OPT_R_MAP);
  29. param.zonefile->key = "zones";
  30. param.zonefile->required = NO;
  31. param.zonefile->description =
  32. _("Raster map used for zoning, must be of type CELL");
  33. param.output_file = G_define_standard_option(G_OPT_F_OUTPUT);
  34. param.output_file->required = NO;
  35. param.output_file->description =
  36. _("Name for output file (if omitted or \"-\" output to stdout)");
  37. param.output_file->guisection = _("Output settings");
  38. param.percentile = G_define_option();
  39. param.percentile->key = "percentile";
  40. param.percentile->type = TYPE_DOUBLE;
  41. param.percentile->required = NO;
  42. param.percentile->multiple = YES;
  43. param.percentile->options = "0-100";
  44. param.percentile->answer = "90";
  45. param.percentile->description =
  46. _("Percentile to calculate (requires extended statistics flag)");
  47. param.percentile->guisection = _("Extended");
  48. param.separator = G_define_standard_option(G_OPT_F_SEP);
  49. param.separator->guisection = _("Formatting");
  50. param.shell_style = G_define_flag();
  51. param.shell_style->key = 'g';
  52. param.shell_style->description =
  53. _("Print the stats in shell script style");
  54. param.shell_style->guisection = _("Formatting");
  55. param.extended = G_define_flag();
  56. param.extended->key = 'e';
  57. param.extended->description = _("Calculate extended statistics");
  58. param.extended->guisection = _("Extended");
  59. param.table = G_define_flag();
  60. param.table->key = 't';
  61. param.table->description = _("Table output format instead of standard output format");
  62. param.table->guisection = _("Formatting");
  63. return;
  64. }
  65. static int open_raster(const char *infile);
  66. static univar_stat *univar_stat_with_percentiles(int map_type);
  67. static void process_raster(univar_stat * stats, int fd, int fdz,
  68. const struct Cell_head *region);
  69. /* *************************************************************** */
  70. /* **** the main functions for r.univar ************************** */
  71. /* *************************************************************** */
  72. int main(int argc, char *argv[])
  73. {
  74. int rasters;
  75. struct Cell_head region;
  76. struct GModule *module;
  77. univar_stat *stats;
  78. char **p, *z;
  79. int fd, fdz, cell_type, min, max;
  80. struct Range zone_range;
  81. const char *mapset, *name;
  82. G_gisinit(argv[0]);
  83. module = G_define_module();
  84. G_add_keyword(_("raster"));
  85. G_add_keyword(_("statistics"));
  86. module->description =
  87. _("Calculates univariate statistics from the non-null cells of a raster map.");
  88. /* Define the different options */
  89. set_params();
  90. if (G_parser(argc, argv))
  91. exit(EXIT_FAILURE);
  92. name = param.output_file->answer;
  93. if (name != NULL && strcmp(name, "-") != 0) {
  94. if (NULL == freopen(name, "w", stdout)) {
  95. G_fatal_error(_("Unable to open file <%s> for writing"), name);
  96. }
  97. }
  98. G_get_window(&region);
  99. /* table field separator */
  100. zone_info.sep = param.separator->answer;
  101. if (strcmp(zone_info.sep, "\\t") == 0)
  102. zone_info.sep = "\t";
  103. if (strcmp(zone_info.sep, "tab") == 0)
  104. zone_info.sep = "\t";
  105. if (strcmp(zone_info.sep, "space") == 0)
  106. zone_info.sep = " ";
  107. if (strcmp(zone_info.sep, "comma") == 0)
  108. zone_info.sep = ",";
  109. zone_info.min = 0.0 / 0.0; /* set to nan as default */
  110. zone_info.max = 0.0 / 0.0; /* set to nan as default */
  111. zone_info.n_zones = 0;
  112. fdz = -1;
  113. /* open zoning raster */
  114. if ((z = param.zonefile->answer)) {
  115. mapset = G_find_raster2(z, "");
  116. fdz = open_raster(z);
  117. cell_type = Rast_get_map_type(fdz);
  118. if (cell_type != CELL_TYPE)
  119. G_fatal_error("Zoning raster must be of type CELL");
  120. if (Rast_read_range(z, mapset, &zone_range) == -1)
  121. G_fatal_error("Can not read range for zoning raster");
  122. Rast_get_range_min_max(&zone_range, &min, &max);
  123. if (Rast_read_cats(z, mapset, &(zone_info.cats)))
  124. G_warning("no category support for zoning raster");
  125. zone_info.min = min;
  126. zone_info.max = max;
  127. zone_info.n_zones = max - min + 1;
  128. }
  129. /* count the input rasters given */
  130. for (p = (char **)param.inputfile->answers, rasters = 0;
  131. *p; p++, rasters++) ;
  132. /* process all input rasters */
  133. int map_type = param.extended->answer ? -2 : -1;
  134. stats = ((map_type == -1)
  135. ? create_univar_stat_struct(-1, 0)
  136. : 0);
  137. for (p = param.inputfile->answers; *p; p++) {
  138. fd = open_raster(*p);
  139. if (map_type != -1) {
  140. /* NB: map_type must match when doing extended stats */
  141. int this_type = Rast_get_map_type(fd);
  142. assert(this_type > -1);
  143. if (map_type < -1) {
  144. /* extended stats */
  145. assert(stats == 0);
  146. map_type = this_type;
  147. stats = univar_stat_with_percentiles(map_type);
  148. }
  149. else if (this_type != map_type) {
  150. G_fatal_error(_("Raster <%s> type mismatch"), *p);
  151. }
  152. }
  153. process_raster(stats, fd, fdz, &region);
  154. /* close input raster */
  155. Rast_close(fd);
  156. }
  157. /* close zoning raster */
  158. if (z)
  159. Rast_close(fdz);
  160. /* create the output */
  161. if (param.table->answer)
  162. print_stats_table(stats);
  163. else
  164. print_stats(stats);
  165. /* release memory */
  166. free_univar_stat_struct(stats);
  167. exit(EXIT_SUCCESS);
  168. }
  169. static int open_raster(const char *infile)
  170. {
  171. const char *mapset;
  172. int fd;
  173. mapset = G_find_raster2(infile, "");
  174. if (mapset == NULL) {
  175. G_fatal_error(_("Raster map <%s> not found"), infile);
  176. }
  177. fd = Rast_open_old(infile, mapset);
  178. return fd;
  179. }
  180. static univar_stat *univar_stat_with_percentiles(int map_type)
  181. {
  182. univar_stat *stats;
  183. unsigned int i, j;
  184. unsigned int n_zones = zone_info.n_zones;
  185. if (n_zones == 0)
  186. n_zones = 1;
  187. i = 0;
  188. while (param.percentile->answers[i])
  189. i++;
  190. stats = create_univar_stat_struct(map_type, i);
  191. for (i = 0; i < n_zones; i++) {
  192. for (j = 0; j < stats[i].n_perc; j++) {
  193. sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j]));
  194. }
  195. }
  196. /* . */
  197. return stats;
  198. }
  199. static void
  200. process_raster(univar_stat * stats, int fd, int fdz, const struct Cell_head *region)
  201. {
  202. /* use G_window_rows(), G_window_cols() here? */
  203. const unsigned int rows = region->rows;
  204. const unsigned int cols = region->cols;
  205. const RASTER_MAP_TYPE map_type = Rast_get_map_type(fd);
  206. const size_t value_sz = Rast_cell_size(map_type);
  207. unsigned int row;
  208. void *raster_row;
  209. CELL *zoneraster_row;
  210. int n_zones = zone_info.n_zones;
  211. raster_row = Rast_allocate_buf(map_type);
  212. if (n_zones)
  213. zoneraster_row = Rast_allocate_c_buf();
  214. for (row = 0; row < rows; row++) {
  215. void *ptr;
  216. CELL *zptr;
  217. unsigned int col;
  218. Rast_get_row(fd, raster_row, row, map_type);
  219. if (n_zones) {
  220. Rast_get_c_row(fdz, zoneraster_row, row);
  221. zptr = zoneraster_row;
  222. }
  223. ptr = raster_row;
  224. for (col = 0; col < cols; col++) {
  225. double val;
  226. int zone = 0;
  227. if (n_zones) {
  228. /* skip NULL cells in zone map */
  229. if (Rast_is_c_null_value(zptr)) {
  230. ptr = G_incr_void_ptr(ptr, value_sz);
  231. zptr++;
  232. continue;
  233. }
  234. zone = *zptr - zone_info.min;
  235. }
  236. /* count all including NULL cells in input map */
  237. stats[zone].size++;
  238. /* can't do stats with NULL cells in input map */
  239. if (Rast_is_null_value(ptr, map_type)) {
  240. ptr = G_incr_void_ptr(ptr, value_sz);
  241. if (n_zones)
  242. zptr++;
  243. continue;
  244. }
  245. if (param.extended->answer) {
  246. /* check allocated memory */
  247. if (stats[zone].n >= stats[zone].n_alloc) {
  248. stats[zone].n_alloc += 1000;
  249. size_t msize;
  250. switch (map_type) {
  251. case DCELL_TYPE:
  252. msize = stats[zone].n_alloc * sizeof(DCELL);
  253. stats[zone].dcell_array =
  254. (DCELL *)G_realloc((void *)stats[zone].dcell_array, msize);
  255. stats[zone].nextp = (void *)&(stats[zone].dcell_array[stats[zone].n]);
  256. break;
  257. case FCELL_TYPE:
  258. msize = stats[zone].n_alloc * sizeof(FCELL);
  259. stats[zone].fcell_array =
  260. (FCELL *)G_realloc((void *)stats[zone].fcell_array, msize);
  261. stats[zone].nextp = (void *)&(stats[zone].fcell_array[stats[zone].n]);
  262. break;
  263. case CELL_TYPE:
  264. msize = stats[zone].n_alloc * sizeof(CELL);
  265. stats[zone].cell_array =
  266. (CELL *)G_realloc((void *)stats[zone].cell_array, msize);
  267. stats[zone].nextp = (void *)&(stats[zone].cell_array[stats[zone].n]);
  268. break;
  269. default:
  270. break;
  271. }
  272. }
  273. /* put the value into stats->XXXcell_array */
  274. memcpy(stats[zone].nextp, ptr, value_sz);
  275. stats[zone].nextp = G_incr_void_ptr(stats[zone].nextp, value_sz);
  276. }
  277. val = ((map_type == DCELL_TYPE) ? *((DCELL *) ptr)
  278. : (map_type == FCELL_TYPE) ? *((FCELL *) ptr)
  279. : *((CELL *) ptr));
  280. stats[zone].sum += val;
  281. stats[zone].sumsq += val * val;
  282. stats[zone].sum_abs += fabs(val);
  283. if (stats[zone].first) {
  284. stats[zone].max = val;
  285. stats[zone].min = val;
  286. stats[zone].first = FALSE;
  287. }
  288. else {
  289. if (val > stats[zone].max)
  290. stats[zone].max = val;
  291. if (val < stats[zone].min)
  292. stats[zone].min = val;
  293. }
  294. ptr = G_incr_void_ptr(ptr, value_sz);
  295. if (n_zones)
  296. zptr++;
  297. stats[zone].n++;
  298. }
  299. if (!(param.shell_style->answer))
  300. G_percent(row, rows, 2);
  301. }
  302. if (!(param.shell_style->answer))
  303. G_percent(rows, rows, 2); /* finish it off */
  304. }