main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /****************************************************************************
  2. *
  3. * MODULE: r.series
  4. * AUTHOR(S): Glynn Clements <glynn gclements.plus.com> (original contributor)
  5. * Hamish Bowman <hamish_b yahoo.com>, Jachym Cepicky <jachym les-ejk.cz>,
  6. * Martin Wegmann <wegmann biozentrum.uni-wuerzburg.de>
  7. * PURPOSE:
  8. * COPYRIGHT: (C) 2002-2008 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public
  11. * License (>=v2). Read the file COPYING that comes with GRASS
  12. * for details.
  13. *
  14. *****************************************************************************/
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <grass/gis.h>
  19. #include <grass/raster.h>
  20. #include <grass/glocale.h>
  21. #include <grass/stats.h>
  22. struct menu
  23. {
  24. stat_func *method; /* routine to compute new value */
  25. int is_int; /* result is an integer */
  26. char *name; /* method name */
  27. char *text; /* menu display - full description */
  28. } menu[] = {
  29. {c_ave, 0, "average", "average value"},
  30. {c_count, 1, "count", "count of non-NULL cells"},
  31. {c_median, 0, "median", "median value"},
  32. {c_mode, 0, "mode", "most frequently occuring value"},
  33. {c_min, 0, "minimum", "lowest value"},
  34. {c_minx, 1, "min_raster", "raster with lowest value"},
  35. {c_max, 0, "maximum", "highest value"},
  36. {c_maxx, 1, "max_raster", "raster with highest value"},
  37. {c_stddev, 0, "stddev", "standard deviation"},
  38. {c_range, 0, "range", "range of values"},
  39. {c_sum, 0, "sum", "sum of values"},
  40. {c_var, 0, "variance", "statistical variance"},
  41. {c_divr, 1, "diversity", "number of different values"},
  42. {c_reg_m, 0, "slope", "linear regression slope"},
  43. {c_reg_c, 0, "offset", "linear regression offset"},
  44. {c_reg_r2, 0, "detcoeff", "linear regression coefficient of determination"},
  45. {c_reg_t, 0, "tvalue", "linear regression t-value"},
  46. {c_quart1, 0, "quart1", "first quartile"},
  47. {c_quart3, 0, "quart3", "third quartile"},
  48. {c_perc90, 0, "perc90", "ninetieth percentile"},
  49. {c_quant, 0, "quantile", "arbitrary quantile"},
  50. {c_skew, 0, "skewness", "skewness"},
  51. {c_kurt, 0, "kurtosis", "kurtosis"},
  52. {NULL, 0, NULL, NULL}
  53. };
  54. struct input
  55. {
  56. const char *name;
  57. int fd;
  58. DCELL *buf;
  59. DCELL weight;
  60. };
  61. struct output
  62. {
  63. const char *name;
  64. int fd;
  65. DCELL *buf;
  66. stat_func *method_fn;
  67. double quantile;
  68. };
  69. static char *build_method_list(void)
  70. {
  71. char *buf = G_malloc(1024);
  72. char *p = buf;
  73. int i;
  74. for (i = 0; menu[i].name; i++) {
  75. char *q;
  76. if (i)
  77. *p++ = ',';
  78. for (q = menu[i].name; *q; p++, q++)
  79. *p = *q;
  80. }
  81. *p = '\0';
  82. return buf;
  83. }
  84. static int find_method(const char *method_name)
  85. {
  86. int i;
  87. for (i = 0; menu[i].name; i++)
  88. if (strcmp(menu[i].name, method_name) == 0)
  89. return i;
  90. G_fatal_error(_("Unknown method <%s>"), method_name);
  91. return -1;
  92. }
  93. int main(int argc, char *argv[])
  94. {
  95. struct GModule *module;
  96. struct
  97. {
  98. struct Option *input, *file, *output, *method, *weights, *quantile, *range;
  99. } parm;
  100. struct
  101. {
  102. struct Flag *nulls, *lazy;
  103. } flag;
  104. int i;
  105. int num_inputs;
  106. int num_weights;
  107. struct input *inputs = NULL;
  108. int num_outputs;
  109. struct output *outputs = NULL;
  110. struct History history;
  111. DCELL *values = NULL, *values_tmp = NULL;
  112. int nrows, ncols;
  113. int row, col;
  114. double lo, hi;
  115. G_gisinit(argv[0]);
  116. module = G_define_module();
  117. G_add_keyword(_("raster"));
  118. G_add_keyword(_("aggregation"));
  119. G_add_keyword(_("series"));
  120. module->description =
  121. _("Makes each output cell value a "
  122. "function of the values assigned to the corresponding cells "
  123. "in the input raster map layers.");
  124. parm.input = G_define_standard_option(G_OPT_R_INPUTS);
  125. parm.input->required = NO;
  126. parm.file = G_define_standard_option(G_OPT_F_INPUT);
  127. parm.file->key = "file";
  128. parm.file->description = _("Input file with one raster map name and optional one weight per line, field separator between name and weight is |");
  129. parm.file->required = NO;
  130. parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
  131. parm.output->multiple = YES;
  132. parm.method = G_define_option();
  133. parm.method->key = "method";
  134. parm.method->type = TYPE_STRING;
  135. parm.method->required = YES;
  136. parm.method->options = build_method_list();
  137. parm.method->description = _("Aggregate operation");
  138. parm.method->multiple = YES;
  139. parm.quantile = G_define_option();
  140. parm.quantile->key = "quantile";
  141. parm.quantile->type = TYPE_DOUBLE;
  142. parm.quantile->required = NO;
  143. parm.quantile->description = _("Quantile to calculate for method=quantile");
  144. parm.quantile->options = "0.0-1.0";
  145. parm.quantile->multiple = YES;
  146. parm.weights = G_define_option();
  147. parm.weights->key = "weights";
  148. parm.weights->type = TYPE_DOUBLE;
  149. parm.weights->required = NO;
  150. parm.weights->description = _("Weighting factor for each input map, default value is 1.0 for each input map");
  151. parm.weights->multiple = YES;
  152. parm.range = G_define_option();
  153. parm.range->key = "range";
  154. parm.range->type = TYPE_DOUBLE;
  155. parm.range->key_desc = "lo,hi";
  156. parm.range->description = _("Ignore values outside this range");
  157. flag.nulls = G_define_flag();
  158. flag.nulls->key = 'n';
  159. flag.nulls->description = _("Propagate NULLs");
  160. flag.lazy = G_define_flag();
  161. flag.lazy->key = 'z';
  162. flag.lazy->description = _("Don't keep files open");
  163. if (G_parser(argc, argv))
  164. exit(EXIT_FAILURE);
  165. if (parm.range->answer) {
  166. lo = atof(parm.range->answers[0]);
  167. hi = atof(parm.range->answers[1]);
  168. }
  169. if (parm.input->answer && parm.file->answer)
  170. G_fatal_error(_("input= and file= are mutually exclusive"));
  171. if (!parm.input->answer && !parm.file->answer)
  172. G_fatal_error(_("Please specify input= or file="));
  173. /* process the input maps from the file */
  174. if (parm.file->answer) {
  175. FILE *in;
  176. int max_inputs;
  177. in = fopen(parm.file->answer, "r");
  178. if (!in)
  179. G_fatal_error(_("Unable to open input file <%s>"), parm.file->answer);
  180. num_inputs = 0;
  181. max_inputs = 0;
  182. for (;;) {
  183. char buf[GNAME_MAX + 50]; /* Name and weight*/
  184. char tok_buf[GNAME_MAX + 50];
  185. char *name;
  186. int ntokens;
  187. char **tokens;
  188. struct input *p;
  189. double weight = 1.0;
  190. if (!G_getl2(buf, sizeof(buf), in))
  191. break;
  192. strcpy(tok_buf, buf);
  193. tokens = G_tokenize(tok_buf, "|");
  194. ntokens = G_number_of_tokens(tokens);
  195. if(ntokens > 1) {
  196. name = G_chop(tokens[0]);
  197. weight = atof(G_chop(tokens[1]));
  198. } else {
  199. name = G_chop(buf);
  200. }
  201. /* Ignore empty lines */
  202. if (!*name)
  203. continue;
  204. if (num_inputs >= max_inputs) {
  205. max_inputs += 100;
  206. inputs = G_realloc(inputs, max_inputs * sizeof(struct input));
  207. }
  208. p = &inputs[num_inputs++];
  209. p->name = G_store(name);
  210. p->weight = weight;
  211. G_verbose_message(_("Reading raster map <%s> using weight %f..."), p->name, p->weight);
  212. p->buf = Rast_allocate_d_buf();
  213. if (!flag.lazy->answer)
  214. p->fd = Rast_open_old(p->name, "");
  215. }
  216. if (num_inputs < 1)
  217. G_fatal_error(_("No raster map name found in input file"));
  218. fclose(in);
  219. }
  220. else {
  221. for (i = 0; parm.input->answers[i]; i++)
  222. ;
  223. num_inputs = i;
  224. if (num_inputs < 1)
  225. G_fatal_error(_("Raster map not found"));
  226. /* count weights */
  227. if(parm.weights->answers) {
  228. for (i = 0; parm.weights->answers[i]; i++)
  229. ;
  230. num_weights = i;
  231. } else {
  232. num_weights = 0;
  233. }
  234. if (num_weights && num_weights != num_inputs)
  235. G_fatal_error(_("input= and weights= must have the same number of values"));
  236. inputs = G_malloc(num_inputs * sizeof(struct input));
  237. for (i = 0; i < num_inputs; i++) {
  238. struct input *p = &inputs[i];
  239. p->name = parm.input->answers[i];
  240. if(num_weights)
  241. p->weight = (DCELL)atof(parm.weights->answers[i]);
  242. else
  243. p->weight = 1.0;
  244. G_verbose_message(_("Reading raster map <%s> using weight %f..."), p->name, p->weight);
  245. p->buf = Rast_allocate_d_buf();
  246. if (!flag.lazy->answer)
  247. p->fd = Rast_open_old(p->name, "");
  248. }
  249. }
  250. /* process the output maps */
  251. for (i = 0; parm.output->answers[i]; i++)
  252. ;
  253. num_outputs = i;
  254. for (i = 0; parm.method->answers[i]; i++)
  255. ;
  256. if (num_outputs != i)
  257. G_fatal_error(_("output= and method= must have the same number of values"));
  258. outputs = G_calloc(num_outputs, sizeof(struct output));
  259. for (i = 0; i < num_outputs; i++) {
  260. struct output *out = &outputs[i];
  261. const char *output_name = parm.output->answers[i];
  262. const char *method_name = parm.method->answers[i];
  263. int method = find_method(method_name);
  264. out->name = output_name;
  265. out->method_fn = menu[method].method;
  266. out->quantile = (parm.quantile->answer && parm.quantile->answers[i])
  267. ? atof(parm.quantile->answers[i])
  268. : 0;
  269. out->buf = Rast_allocate_d_buf();
  270. out->fd = Rast_open_new(output_name,
  271. menu[method].is_int ? CELL_TYPE : DCELL_TYPE);
  272. }
  273. /* initialise variables */
  274. values = G_malloc(num_inputs * sizeof(DCELL));
  275. values_tmp = G_malloc(num_inputs * sizeof(DCELL));
  276. nrows = Rast_window_rows();
  277. ncols = Rast_window_cols();
  278. /* process the data */
  279. G_verbose_message(_("Percent complete..."));
  280. for (row = 0; row < nrows; row++) {
  281. G_percent(row, nrows, 2);
  282. if (flag.lazy->answer) {
  283. /* Open the files only on run time */
  284. for (i = 0; i < num_inputs; i++) {
  285. inputs[i].fd = Rast_open_old(inputs[i].name, "");
  286. Rast_get_d_row(inputs[i].fd, inputs[i].buf, row);
  287. Rast_close(inputs[i].fd);
  288. }
  289. }
  290. else {
  291. for (i = 0; i < num_inputs; i++)
  292. Rast_get_d_row(inputs[i].fd, inputs[i].buf, row);
  293. }
  294. for (col = 0; col < ncols; col++) {
  295. int null = 0;
  296. for (i = 0; i < num_inputs; i++) {
  297. DCELL v = inputs[i].buf[col];
  298. if (Rast_is_d_null_value(&v))
  299. null = 1;
  300. else if (parm.range->answer && (v < lo || v > hi)) {
  301. Rast_set_d_null_value(&v, 1);
  302. null = 1;
  303. }
  304. values[i] = v * inputs[i].weight;
  305. }
  306. for (i = 0; i < num_outputs; i++) {
  307. struct output *out = &outputs[i];
  308. if (null && flag.nulls->answer)
  309. Rast_set_d_null_value(&out->buf[col], 1);
  310. else {
  311. memcpy(values_tmp, values, num_inputs * sizeof(DCELL));
  312. (*out->method_fn)(&out->buf[col], values_tmp, num_inputs, &out->quantile);
  313. }
  314. }
  315. }
  316. for (i = 0; i < num_outputs; i++)
  317. Rast_put_d_row(outputs[i].fd, outputs[i].buf);
  318. }
  319. G_percent(row, nrows, 2);
  320. /* close output maps */
  321. for (i = 0; i < num_outputs; i++) {
  322. struct output *out = &outputs[i];
  323. Rast_close(out->fd);
  324. Rast_short_history(out->name, "raster", &history);
  325. Rast_command_history(&history);
  326. Rast_write_history(out->name, &history);
  327. }
  328. /* Close input maps */
  329. if (!flag.lazy->answer) {
  330. for (i = 0; i < num_inputs; i++)
  331. Rast_close(inputs[i].fd);
  332. }
  333. exit(EXIT_SUCCESS);
  334. }