r.univar_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. param.use_rast_region = G_define_flag();
  64. param.use_rast_region->key = 'r';
  65. param.use_rast_region->description = _("Use the native resolution and extent of the raster map, instead of the current region");
  66. return;
  67. }
  68. static int open_raster(const char *infile);
  69. static univar_stat *univar_stat_with_percentiles(int map_type);
  70. static void process_raster(univar_stat * stats, int fd, int fdz,
  71. const struct Cell_head *region);
  72. /* *************************************************************** */
  73. /* **** the main functions for r.univar ************************** */
  74. /* *************************************************************** */
  75. int main(int argc, char *argv[])
  76. {
  77. int rasters;
  78. struct Cell_head region;
  79. struct GModule *module;
  80. univar_stat *stats;
  81. char **p, *z;
  82. int fd, fdz, cell_type, min, max;
  83. struct Range zone_range;
  84. const char *mapset, *name;
  85. G_gisinit(argv[0]);
  86. module = G_define_module();
  87. G_add_keyword(_("raster"));
  88. G_add_keyword(_("statistics"));
  89. G_add_keyword(_("univariate statistics"));
  90. G_add_keyword(_("zonal statistics"));
  91. module->label =
  92. _("Calculates univariate statistics from the non-null cells of a raster map.");
  93. module->description =
  94. _("Statistics include number of cells counted, minimum and maximum cell"
  95. " values, range, arithmetic mean, population variance, standard deviation,"
  96. " coefficient of variation, and sum.");
  97. /* Define the different options */
  98. set_params();
  99. if (G_parser(argc, argv))
  100. exit(EXIT_FAILURE);
  101. if (param.zonefile->answer && param.use_rast_region->answer) {
  102. G_fatal_error(_("zones option and region flag -r are mutually exclusive"));
  103. }
  104. name = param.output_file->answer;
  105. if (name != NULL && strcmp(name, "-") != 0) {
  106. if (NULL == freopen(name, "w", stdout)) {
  107. G_fatal_error(_("Unable to open file <%s> for writing"), name);
  108. }
  109. }
  110. /* table field separator */
  111. zone_info.sep = G_option_to_separator(param.separator);
  112. zone_info.min = 0.0 / 0.0; /* set to nan as default */
  113. zone_info.max = 0.0 / 0.0; /* set to nan as default */
  114. zone_info.n_zones = 0;
  115. fdz = -1;
  116. /* open zoning raster */
  117. if ((z = param.zonefile->answer)) {
  118. mapset = G_find_raster2(z, "");
  119. fdz = open_raster(z);
  120. cell_type = Rast_get_map_type(fdz);
  121. if (cell_type != CELL_TYPE)
  122. G_fatal_error("Zoning raster must be of type CELL");
  123. if (Rast_read_range(z, mapset, &zone_range) == -1)
  124. G_fatal_error("Can not read range for zoning raster");
  125. Rast_get_range_min_max(&zone_range, &min, &max);
  126. if (Rast_read_cats(z, mapset, &(zone_info.cats)))
  127. G_warning("no category support for zoning raster");
  128. zone_info.min = min;
  129. zone_info.max = max;
  130. zone_info.n_zones = max - min + 1;
  131. }
  132. /* count the input rasters given */
  133. for (p = (char **)param.inputfile->answers, rasters = 0;
  134. *p; p++, rasters++) ;
  135. /* process all input rasters */
  136. int map_type = param.extended->answer ? -2 : -1;
  137. stats = ((map_type == -1)
  138. ? create_univar_stat_struct(-1, 0)
  139. : 0);
  140. for (p = param.inputfile->answers; *p; p++) {
  141. /* Check if the native extent and resolution
  142. of the input map should be used */
  143. if(param.use_rast_region->answer) {
  144. mapset = G_find_raster2(*p, "");
  145. Rast_get_cellhd(*p, mapset, &region);
  146. /* Set the computational region */
  147. Rast_set_window(&region);
  148. } else {
  149. G_get_window(&region);
  150. }
  151. fd = open_raster(*p);
  152. if (map_type != -1) {
  153. /* NB: map_type must match when doing extended stats */
  154. int this_type = Rast_get_map_type(fd);
  155. assert(this_type > -1);
  156. if (map_type < -1) {
  157. /* extended stats */
  158. assert(stats == 0);
  159. map_type = this_type;
  160. stats = univar_stat_with_percentiles(map_type);
  161. }
  162. else if (this_type != map_type) {
  163. G_fatal_error(_("Raster <%s> type mismatch"), *p);
  164. }
  165. }
  166. process_raster(stats, fd, fdz, &region);
  167. /* close input raster */
  168. Rast_close(fd);
  169. }
  170. /* close zoning raster */
  171. if (z)
  172. Rast_close(fdz);
  173. /* create the output */
  174. if (param.table->answer)
  175. print_stats_table(stats);
  176. else
  177. print_stats(stats);
  178. /* release memory */
  179. free_univar_stat_struct(stats);
  180. exit(EXIT_SUCCESS);
  181. }
  182. static int open_raster(const char *infile)
  183. {
  184. const char *mapset;
  185. int fd;
  186. mapset = G_find_raster2(infile, "");
  187. if (mapset == NULL) {
  188. G_fatal_error(_("Raster map <%s> not found"), infile);
  189. }
  190. fd = Rast_open_old(infile, mapset);
  191. return fd;
  192. }
  193. static univar_stat *univar_stat_with_percentiles(int map_type)
  194. {
  195. univar_stat *stats;
  196. unsigned int i, j;
  197. unsigned int n_zones = zone_info.n_zones;
  198. if (n_zones == 0)
  199. n_zones = 1;
  200. i = 0;
  201. while (param.percentile->answers[i])
  202. i++;
  203. stats = create_univar_stat_struct(map_type, i);
  204. for (i = 0; i < n_zones; i++) {
  205. for (j = 0; j < stats[i].n_perc; j++) {
  206. sscanf(param.percentile->answers[j], "%lf", &(stats[i].perc[j]));
  207. }
  208. }
  209. /* . */
  210. return stats;
  211. }
  212. static void
  213. process_raster(univar_stat * stats, int fd, int fdz, const struct Cell_head *region)
  214. {
  215. /* use G_window_rows(), G_window_cols() here? */
  216. const unsigned int rows = region->rows;
  217. const unsigned int cols = region->cols;
  218. const RASTER_MAP_TYPE map_type = Rast_get_map_type(fd);
  219. const size_t value_sz = Rast_cell_size(map_type);
  220. unsigned int row;
  221. void *raster_row;
  222. CELL *zoneraster_row = NULL;
  223. int n_zones = zone_info.n_zones;
  224. raster_row = Rast_allocate_buf(map_type);
  225. if (n_zones)
  226. zoneraster_row = Rast_allocate_c_buf();
  227. for (row = 0; row < rows; row++) {
  228. void *ptr;
  229. CELL *zptr = NULL;
  230. unsigned int col;
  231. Rast_get_row(fd, raster_row, row, map_type);
  232. if (n_zones) {
  233. Rast_get_c_row(fdz, zoneraster_row, row);
  234. zptr = zoneraster_row;
  235. }
  236. ptr = raster_row;
  237. for (col = 0; col < cols; col++) {
  238. double val;
  239. int zone = 0;
  240. if (n_zones) {
  241. /* skip NULL cells in zone map */
  242. if (Rast_is_c_null_value(zptr)) {
  243. ptr = G_incr_void_ptr(ptr, value_sz);
  244. zptr++;
  245. continue;
  246. }
  247. zone = *zptr - zone_info.min;
  248. }
  249. /* count all including NULL cells in input map */
  250. stats[zone].size++;
  251. /* can't do stats with NULL cells in input map */
  252. if (Rast_is_null_value(ptr, map_type)) {
  253. ptr = G_incr_void_ptr(ptr, value_sz);
  254. if (n_zones)
  255. zptr++;
  256. continue;
  257. }
  258. if (param.extended->answer) {
  259. /* check allocated memory */
  260. if (stats[zone].n >= stats[zone].n_alloc) {
  261. stats[zone].n_alloc += 1000;
  262. size_t msize;
  263. switch (map_type) {
  264. case DCELL_TYPE:
  265. msize = stats[zone].n_alloc * sizeof(DCELL);
  266. stats[zone].dcell_array =
  267. (DCELL *)G_realloc((void *)stats[zone].dcell_array, msize);
  268. stats[zone].nextp = (void *)&(stats[zone].dcell_array[stats[zone].n]);
  269. break;
  270. case FCELL_TYPE:
  271. msize = stats[zone].n_alloc * sizeof(FCELL);
  272. stats[zone].fcell_array =
  273. (FCELL *)G_realloc((void *)stats[zone].fcell_array, msize);
  274. stats[zone].nextp = (void *)&(stats[zone].fcell_array[stats[zone].n]);
  275. break;
  276. case CELL_TYPE:
  277. msize = stats[zone].n_alloc * sizeof(CELL);
  278. stats[zone].cell_array =
  279. (CELL *)G_realloc((void *)stats[zone].cell_array, msize);
  280. stats[zone].nextp = (void *)&(stats[zone].cell_array[stats[zone].n]);
  281. break;
  282. default:
  283. break;
  284. }
  285. }
  286. /* put the value into stats->XXXcell_array */
  287. memcpy(stats[zone].nextp, ptr, value_sz);
  288. stats[zone].nextp = G_incr_void_ptr(stats[zone].nextp, value_sz);
  289. }
  290. val = ((map_type == DCELL_TYPE) ? *((DCELL *) ptr)
  291. : (map_type == FCELL_TYPE) ? *((FCELL *) ptr)
  292. : *((CELL *) ptr));
  293. stats[zone].sum += val;
  294. stats[zone].sumsq += val * val;
  295. stats[zone].sum_abs += fabs(val);
  296. if (stats[zone].first) {
  297. stats[zone].max = val;
  298. stats[zone].min = val;
  299. stats[zone].first = FALSE;
  300. }
  301. else {
  302. if (val > stats[zone].max)
  303. stats[zone].max = val;
  304. if (val < stats[zone].min)
  305. stats[zone].min = val;
  306. }
  307. ptr = G_incr_void_ptr(ptr, value_sz);
  308. if (n_zones)
  309. zptr++;
  310. stats[zone].n++;
  311. }
  312. if (!(param.shell_style->answer))
  313. G_percent(row, rows, 2);
  314. }
  315. if (!(param.shell_style->answer))
  316. G_percent(rows, rows, 2); /* finish it off */
  317. }