main.c 14 KB

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