main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. stat_func_w *method_w; /* routine to compute new value (weighted) */
  26. int is_int; /* result is an integer */
  27. char *name; /* method name */
  28. char *text; /* menu display - full description */
  29. } menu[] = {
  30. {c_ave, w_ave, 0, "average", "average value"},
  31. {c_count, w_count, 1, "count", "count of non-NULL cells"},
  32. {c_median, w_median, 0, "median", "median value"},
  33. {c_mode, w_mode, 0, "mode", "most frequently occurring value"},
  34. {c_min, NULL, 0, "minimum", "lowest value"},
  35. {c_minx, NULL, 1, "min_raster", "raster with lowest value"},
  36. {c_max, NULL, 0, "maximum", "highest value"},
  37. {c_maxx, NULL, 1, "max_raster", "raster with highest value"},
  38. {c_stddev, w_stddev, 0, "stddev", "standard deviation"},
  39. {c_range, NULL, 0, "range", "range of values"},
  40. {c_sum, w_sum, 0, "sum", "sum of values"},
  41. {c_var, w_var, 0, "variance", "statistical variance"},
  42. {c_divr, NULL, 1, "diversity", "number of different values"},
  43. {c_reg_m, w_reg_m, 0, "slope", "linear regression slope"},
  44. {c_reg_c, w_reg_c, 0, "offset", "linear regression offset"},
  45. {c_reg_r2, w_reg_r2, 0, "detcoeff", "linear regression coefficient of determination"},
  46. {c_reg_t, w_reg_t, 0, "tvalue", "linear regression t-value"},
  47. {c_quart1, w_quart1, 0, "quart1", "first quartile"},
  48. {c_quart3, w_quart3, 0, "quart3", "third quartile"},
  49. {c_perc90, w_perc90, 0, "perc90", "ninetieth percentile"},
  50. {c_quant, w_quant, 0, "quantile", "arbitrary quantile"},
  51. {c_skew, w_skew, 0, "skewness", "skewness"},
  52. {c_kurt, w_kurt, 0, "kurtosis", "kurtosis"},
  53. {NULL, NULL, 0, NULL, NULL}
  54. };
  55. struct input
  56. {
  57. const char *name;
  58. int fd;
  59. DCELL *buf;
  60. DCELL weight;
  61. };
  62. struct output
  63. {
  64. const char *name;
  65. int fd;
  66. DCELL *buf;
  67. stat_func *method_fn;
  68. stat_func_w *method_fn_w;
  69. double quantile;
  70. };
  71. static char *build_method_list(void)
  72. {
  73. char *buf = G_malloc(1024);
  74. char *p = buf;
  75. int i;
  76. for (i = 0; menu[i].name; i++) {
  77. char *q;
  78. if (i)
  79. *p++ = ',';
  80. for (q = menu[i].name; *q; p++, q++)
  81. *p = *q;
  82. }
  83. *p = '\0';
  84. return buf;
  85. }
  86. static int find_method(const char *method_name)
  87. {
  88. int i;
  89. for (i = 0; menu[i].name; i++)
  90. if (strcmp(menu[i].name, method_name) == 0)
  91. return i;
  92. G_fatal_error(_("Unknown method <%s>"), method_name);
  93. return -1;
  94. }
  95. int main(int argc, char *argv[])
  96. {
  97. struct GModule *module;
  98. struct
  99. {
  100. struct Option *input, *file, *output, *method, *weights, *quantile, *range;
  101. } parm;
  102. struct
  103. {
  104. struct Flag *nulls, *lazy;
  105. } flag;
  106. int i;
  107. int num_inputs;
  108. struct input *inputs = NULL;
  109. int num_outputs;
  110. struct output *outputs = NULL;
  111. struct History history;
  112. DCELL *values = NULL, *values_tmp = NULL;
  113. DCELL(*values_w)[2]; /* list of values and weights */
  114. DCELL(*values_w_tmp)[2]; /* list of values and weights */
  115. int have_weights;
  116. int nrows, ncols;
  117. int row, col;
  118. double lo, hi;
  119. G_gisinit(argv[0]);
  120. module = G_define_module();
  121. G_add_keyword(_("raster"));
  122. G_add_keyword(_("aggregation"));
  123. G_add_keyword(_("series"));
  124. module->description =
  125. _("Makes each output cell value a "
  126. "function of the values assigned to the corresponding cells "
  127. "in the input raster map layers.");
  128. parm.input = G_define_standard_option(G_OPT_R_INPUTS);
  129. parm.input->required = NO;
  130. parm.file = G_define_standard_option(G_OPT_F_INPUT);
  131. parm.file->key = "file";
  132. parm.file->description = _("Input file with one raster map name and optional one weight per line, field separator between name and weight is |");
  133. parm.file->required = NO;
  134. parm.output = G_define_standard_option(G_OPT_R_OUTPUT);
  135. parm.output->multiple = YES;
  136. parm.method = G_define_option();
  137. parm.method->key = "method";
  138. parm.method->type = TYPE_STRING;
  139. parm.method->required = YES;
  140. parm.method->options = build_method_list();
  141. parm.method->description = _("Aggregate operation");
  142. parm.method->multiple = YES;
  143. parm.quantile = G_define_option();
  144. parm.quantile->key = "quantile";
  145. parm.quantile->type = TYPE_DOUBLE;
  146. parm.quantile->required = NO;
  147. parm.quantile->description = _("Quantile to calculate for method=quantile");
  148. parm.quantile->options = "0.0-1.0";
  149. parm.quantile->multiple = YES;
  150. parm.weights = G_define_option();
  151. parm.weights->key = "weights";
  152. parm.weights->type = TYPE_DOUBLE;
  153. parm.weights->required = NO;
  154. parm.weights->description = _("Weighting factor for each input map, default value is 1.0 for each input map");
  155. parm.weights->multiple = YES;
  156. parm.range = G_define_option();
  157. parm.range->key = "range";
  158. parm.range->type = TYPE_DOUBLE;
  159. parm.range->key_desc = "lo,hi";
  160. parm.range->description = _("Ignore values outside this range");
  161. flag.nulls = G_define_flag();
  162. flag.nulls->key = 'n';
  163. flag.nulls->description = _("Propagate NULLs");
  164. flag.lazy = G_define_flag();
  165. flag.lazy->key = 'z';
  166. flag.lazy->description = _("Do not keep files open");
  167. if (G_parser(argc, argv))
  168. exit(EXIT_FAILURE);
  169. lo = -1.0 / 0.0; /* -inf */
  170. hi = 1.0 / 0.0; /* inf */
  171. if (parm.range->answer) {
  172. lo = atof(parm.range->answers[0]);
  173. hi = atof(parm.range->answers[1]);
  174. }
  175. if (parm.input->answer && parm.file->answer)
  176. G_fatal_error(_("%s= and %s= are mutually exclusive"),
  177. parm.input->key, parm.file->key);
  178. if (!parm.input->answer && !parm.file->answer)
  179. G_fatal_error(_("Please specify %s= or %s="),
  180. parm.input->key, parm.file->key);
  181. have_weights = 0;
  182. /* process the input maps from the file */
  183. if (parm.file->answer) {
  184. FILE *in;
  185. int max_inputs;
  186. if (strcmp(parm.file->answer, "-") == 0)
  187. in = stdin;
  188. else {
  189. in = fopen(parm.file->answer, "r");
  190. if (!in)
  191. G_fatal_error(_("Unable to open input file <%s>"), parm.file->answer);
  192. }
  193. num_inputs = 0;
  194. max_inputs = 0;
  195. for (;;) {
  196. char buf[GNAME_MAX + 50]; /* Name and weight*/
  197. char tok_buf[GNAME_MAX + 50];
  198. char *name;
  199. int ntokens;
  200. char **tokens;
  201. struct input *p;
  202. double weight = 1.0;
  203. if (!G_getl2(buf, sizeof(buf), in))
  204. break;
  205. strcpy(tok_buf, buf);
  206. tokens = G_tokenize(tok_buf, "|");
  207. ntokens = G_number_of_tokens(tokens);
  208. name = G_chop(tokens[0]);
  209. if (ntokens > 1) {
  210. weight = atof(G_chop(tokens[1]));
  211. if (weight <= 0)
  212. G_fatal_error(_("Weights must be positive"));
  213. if (weight != 1)
  214. have_weights = 1;
  215. }
  216. /* Ignore empty lines */
  217. if (!*name)
  218. continue;
  219. if (num_inputs >= max_inputs) {
  220. max_inputs += 100;
  221. inputs = G_realloc(inputs, max_inputs * sizeof(struct input));
  222. }
  223. p = &inputs[num_inputs++];
  224. p->name = G_store(name);
  225. p->weight = weight;
  226. G_verbose_message(_("Reading raster map <%s> using weight %f..."), p->name, p->weight);
  227. p->buf = Rast_allocate_d_buf();
  228. if (!flag.lazy->answer)
  229. p->fd = Rast_open_old(p->name, "");
  230. }
  231. if (num_inputs < 1)
  232. G_fatal_error(_("No raster map name found in input file"));
  233. fclose(in);
  234. }
  235. else {
  236. int num_weights;
  237. for (i = 0; parm.input->answers[i]; i++)
  238. ;
  239. num_inputs = i;
  240. if (num_inputs < 1)
  241. G_fatal_error(_("Raster map not found"));
  242. /* count weights */
  243. num_weights = 0;
  244. if (parm.weights->answers) {
  245. for (i = 0; parm.weights->answers[i]; i++)
  246. ;
  247. num_weights = i;
  248. }
  249. if (num_weights && num_weights != num_inputs)
  250. G_fatal_error(_("input= and weights= must have the same number of values"));
  251. inputs = G_malloc(num_inputs * sizeof(struct input));
  252. for (i = 0; i < num_inputs; i++) {
  253. struct input *p = &inputs[i];
  254. p->name = parm.input->answers[i];
  255. p->weight = 1.0;
  256. if (num_weights) {
  257. p->weight = (DCELL)atof(parm.weights->answers[i]);
  258. if (p->weight <= 0)
  259. G_fatal_error(_("Weights must be positive"));
  260. if (p->weight != 1)
  261. have_weights = 1;
  262. }
  263. G_verbose_message(_("Reading raster map <%s> using weight %f..."), p->name, p->weight);
  264. p->buf = Rast_allocate_d_buf();
  265. if (!flag.lazy->answer)
  266. p->fd = Rast_open_old(p->name, "");
  267. }
  268. }
  269. /* process the output maps */
  270. for (i = 0; parm.output->answers[i]; i++)
  271. ;
  272. num_outputs = i;
  273. for (i = 0; parm.method->answers[i]; i++)
  274. ;
  275. if (num_outputs != i)
  276. G_fatal_error(_("output= and method= must have the same number of values"));
  277. outputs = G_calloc(num_outputs, sizeof(struct output));
  278. for (i = 0; i < num_outputs; i++) {
  279. struct output *out = &outputs[i];
  280. const char *output_name = parm.output->answers[i];
  281. const char *method_name = parm.method->answers[i];
  282. int method = find_method(method_name);
  283. out->name = output_name;
  284. if (have_weights) {
  285. if (menu[method].method_w) {
  286. out->method_fn = NULL;
  287. out->method_fn_w = menu[method].method_w;
  288. }
  289. else {
  290. G_warning(_("Method %s not compatible with weights, using unweighed version instead"),
  291. method_name);
  292. out->method_fn = menu[method].method;
  293. out->method_fn_w = NULL;
  294. }
  295. menu[method].is_int = 0;
  296. }
  297. else {
  298. out->method_fn = menu[method].method;
  299. out->method_fn_w = NULL;
  300. }
  301. out->quantile = (parm.quantile->answer && parm.quantile->answers[i])
  302. ? atof(parm.quantile->answers[i])
  303. : 0;
  304. out->buf = Rast_allocate_d_buf();
  305. out->fd = Rast_open_new(output_name,
  306. menu[method].is_int ? CELL_TYPE : DCELL_TYPE);
  307. }
  308. /* initialise variables */
  309. values = G_malloc(num_inputs * sizeof(DCELL));
  310. values_tmp = G_malloc(num_inputs * sizeof(DCELL));
  311. values_w = NULL;
  312. values_w_tmp = NULL;
  313. if (have_weights) {
  314. values_w = (DCELL(*)[2]) G_malloc(num_inputs * 2 * sizeof(DCELL));
  315. values_w_tmp = (DCELL(*)[2]) G_malloc(num_inputs * 2 * sizeof(DCELL));
  316. }
  317. nrows = Rast_window_rows();
  318. ncols = Rast_window_cols();
  319. /* process the data */
  320. G_verbose_message(_("Percent complete..."));
  321. for (row = 0; row < nrows; row++) {
  322. G_percent(row, nrows, 2);
  323. if (flag.lazy->answer) {
  324. /* Open the files only on run time */
  325. for (i = 0; i < num_inputs; i++) {
  326. inputs[i].fd = Rast_open_old(inputs[i].name, "");
  327. Rast_get_d_row(inputs[i].fd, inputs[i].buf, row);
  328. Rast_close(inputs[i].fd);
  329. }
  330. }
  331. else {
  332. for (i = 0; i < num_inputs; i++)
  333. Rast_get_d_row(inputs[i].fd, inputs[i].buf, row);
  334. }
  335. for (col = 0; col < ncols; col++) {
  336. int null = 0;
  337. for (i = 0; i < num_inputs; i++) {
  338. DCELL v = inputs[i].buf[col];
  339. if (Rast_is_d_null_value(&v))
  340. null = 1;
  341. else if (parm.range->answer && (v < lo || v > hi)) {
  342. Rast_set_d_null_value(&v, 1);
  343. null = 1;
  344. }
  345. values[i] = v;
  346. if (have_weights) {
  347. values_w[i][0] = v;
  348. values_w[i][1] = inputs[i].weight;
  349. }
  350. }
  351. for (i = 0; i < num_outputs; i++) {
  352. struct output *out = &outputs[i];
  353. if (null && flag.nulls->answer)
  354. Rast_set_d_null_value(&out->buf[col], 1);
  355. else {
  356. if (out->method_fn_w) {
  357. memcpy(values_w_tmp, values_w, num_inputs * 2 * sizeof(DCELL));
  358. (*out->method_fn_w)(&out->buf[col], values_w_tmp, num_inputs, &out->quantile);
  359. }
  360. else {
  361. memcpy(values_tmp, values, num_inputs * sizeof(DCELL));
  362. (*out->method_fn)(&out->buf[col], values_tmp, num_inputs, &out->quantile);
  363. }
  364. }
  365. }
  366. }
  367. for (i = 0; i < num_outputs; i++)
  368. Rast_put_d_row(outputs[i].fd, outputs[i].buf);
  369. }
  370. G_percent(row, nrows, 2);
  371. /* close output maps */
  372. for (i = 0; i < num_outputs; i++) {
  373. struct output *out = &outputs[i];
  374. Rast_close(out->fd);
  375. Rast_short_history(out->name, "raster", &history);
  376. Rast_command_history(&history);
  377. Rast_write_history(out->name, &history);
  378. }
  379. /* Close input maps */
  380. if (!flag.lazy->answer) {
  381. for (i = 0; i < num_inputs; i++)
  382. Rast_close(inputs[i].fd);
  383. }
  384. exit(EXIT_SUCCESS);
  385. }