resout2d.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*-
  2. * Written by H. Mitasova, I. Kosinovsky, D. Gerdes Summer 1993
  3. * University of Illinois
  4. * US Army Construction Engineering Research Lab
  5. * Copyright 1993, H. Mitasova (University of Illinois),
  6. * I. Kosinovsky, (USA-CERL), and D.Gerdes (USA-CERL)
  7. *
  8. * modified by McCauley in August 1995
  9. * modified by Mitasova in August 1995
  10. *
  11. */
  12. #define MULT 100000
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include <grass/gis.h>
  16. #include <grass/raster.h>
  17. #include <grass/bitmap.h>
  18. #include <grass/linkm.h>
  19. #include <grass/interpf.h>
  20. #include <grass/glocale.h>
  21. /* output cell maps for elevation, aspect, slope and curvatures */
  22. static void do_history(const char *name, const char *input,
  23. const struct interp_params *params)
  24. {
  25. struct History hist;
  26. Rast_short_history(name, "raster", &hist);
  27. if (params->elev)
  28. Rast_append_format_history(&hist, "The elevation map is %s",
  29. params->elev);
  30. Rast_format_history(&hist, HIST_DATSRC_1, "raster map %s", input);
  31. Rast_write_history(name, &hist);
  32. Rast_free_history(&hist);
  33. }
  34. int IL_resample_output_2d(struct interp_params *params, double zmin, double zmax, /* min,max input z-values */
  35. double zminac, double zmaxac, /* min,max interpolated values */
  36. double c1min, double c1max, double c2min, double c2max, double gmin, double gmax, double ertot, /* total interplating func. error */
  37. char *input, /* input file name */
  38. double *dnorm, struct Cell_head *outhd, /* Region with desired resolution */
  39. struct Cell_head *winhd, /* Current region */
  40. char *smooth, int n_points)
  41. /*
  42. * Creates output files as well as history files and color tables for
  43. * them.
  44. */
  45. {
  46. FCELL *cell1; /* cell buffer */
  47. int cf1 = 0, cf2 = 0, cf3 = 0, cf4 = 0, cf5 = 0, cf6 = 0; /* cell file descriptors */
  48. int nrows, ncols; /* current region rows and columns */
  49. int i; /* loop counter */
  50. const char *mapset;
  51. float dat1, dat2;
  52. struct Colors colors, colors2;
  53. double value1, value2;
  54. struct History hist;
  55. struct _Color_Rule_ *rule;
  56. const char *maps;
  57. int cond1, cond2;
  58. CELL val1, val2;
  59. cond2 = ((params->pcurv != NULL) ||
  60. (params->tcurv != NULL) || (params->mcurv != NULL));
  61. cond1 = ((params->slope != NULL) || (params->aspect != NULL) || cond2);
  62. /* change region to output cell file region */
  63. G_verbose_message(_("Temporarily changing the region to desired resolution..."));
  64. Rast_set_output_window(outhd);
  65. mapset = G_mapset();
  66. cell1 = Rast_allocate_f_buf();
  67. if (params->elev)
  68. cf1 = Rast_open_fp_new(params->elev);
  69. if (params->slope)
  70. cf2 = Rast_open_fp_new(params->slope);
  71. if (params->aspect)
  72. cf3 = Rast_open_fp_new(params->aspect);
  73. if (params->pcurv)
  74. cf4 = Rast_open_fp_new(params->pcurv);
  75. if (params->tcurv)
  76. cf5 = Rast_open_fp_new(params->tcurv);
  77. if (params->mcurv)
  78. cf6 = Rast_open_fp_new(params->mcurv);
  79. nrows = outhd->rows;
  80. if (nrows != params->nsizr) {
  81. G_warning(_("First change your rows number(%d) to %d"),
  82. nrows, params->nsizr);
  83. return -1;
  84. }
  85. ncols = outhd->cols;
  86. if (ncols != params->nsizc) {
  87. G_warning(_("First change your columns number(%d) to %d"),
  88. ncols, params->nsizr);
  89. return -1;
  90. }
  91. if (params->elev != NULL) {
  92. G_fseek(params->Tmp_fd_z, 0L, 0); /* seek to the beginning */
  93. for (i = 0; i < params->nsizr; i++) {
  94. /* seek to the right row */
  95. G_fseek(params->Tmp_fd_z, (off_t) (params->nsizr - 1 - i) *
  96. params->nsizc * sizeof(FCELL), 0);
  97. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_z);
  98. Rast_put_f_row(cf1, cell1);
  99. }
  100. }
  101. if (params->slope != NULL) {
  102. G_fseek(params->Tmp_fd_dx, 0L, 0); /* seek to the beginning */
  103. for (i = 0; i < params->nsizr; i++) {
  104. /* seek to the right row */
  105. G_fseek(params->Tmp_fd_dx, (off_t) (params->nsizr - 1 - i) *
  106. params->nsizc * sizeof(FCELL), 0);
  107. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dx);
  108. /*
  109. * for (ii==0;ii<params->nsizc;ii++) { fprintf(stderr,"ii=%d ",ii);
  110. * fprintf(stderr,"%f ",cell1[ii]); }
  111. * fprintf(stderr,"params->nsizc=%d \n",params->nsizc);
  112. */
  113. Rast_put_f_row(cf2, cell1);
  114. }
  115. }
  116. if (params->aspect != NULL) {
  117. G_fseek(params->Tmp_fd_dy, 0L, 0); /* seek to the beginning */
  118. for (i = 0; i < params->nsizr; i++) {
  119. /* seek to the right row */
  120. G_fseek(params->Tmp_fd_dy, (off_t) (params->nsizr - 1 - i) *
  121. params->nsizc * sizeof(FCELL), 0);
  122. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dy);
  123. Rast_put_f_row(cf3, cell1);
  124. }
  125. }
  126. if (params->pcurv != NULL) {
  127. G_fseek(params->Tmp_fd_xx, 0L, 0); /* seek to the beginning */
  128. for (i = 0; i < params->nsizr; i++) {
  129. /* seek to the right row */
  130. G_fseek(params->Tmp_fd_xx, (off_t) (params->nsizr - 1 - i) *
  131. params->nsizc * sizeof(FCELL), 0);
  132. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xx);
  133. Rast_put_f_row(cf4, cell1);
  134. }
  135. }
  136. if (params->tcurv != NULL) {
  137. G_fseek(params->Tmp_fd_yy, 0L, 0); /* seek to the beginning */
  138. for (i = 0; i < params->nsizr; i++) {
  139. /* seek to the right row */
  140. G_fseek(params->Tmp_fd_yy, (off_t) (params->nsizr - 1 - i) *
  141. params->nsizc * sizeof(FCELL), 0);
  142. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_yy);
  143. Rast_put_f_row(cf5, cell1);
  144. }
  145. }
  146. if (params->mcurv != NULL) {
  147. G_fseek(params->Tmp_fd_xy, 0L, 0); /* seek to the beginning */
  148. for (i = 0; i < params->nsizr; i++) {
  149. /* seek to the right row */
  150. G_fseek(params->Tmp_fd_xy, (off_t) (params->nsizr - 1 - i) *
  151. params->nsizc * sizeof(FCELL), 0);
  152. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xy);
  153. Rast_put_f_row(cf6, cell1);
  154. }
  155. }
  156. if (cf1)
  157. Rast_close(cf1);
  158. if (cf2)
  159. Rast_close(cf2);
  160. if (cf3)
  161. Rast_close(cf3);
  162. if (cf4)
  163. Rast_close(cf4);
  164. if (cf5)
  165. Rast_close(cf5);
  166. if (cf6)
  167. Rast_close(cf6);
  168. /* write colormaps and history for output cell files */
  169. /* colortable for elevations */
  170. maps = G_find_file("cell", input, "");
  171. if (params->elev != NULL) {
  172. if (maps == NULL) {
  173. G_warning(_("Raster map <%s> not found"), input);
  174. return -1;
  175. }
  176. Rast_init_colors(&colors2);
  177. /*
  178. * Rast_mark_colors_as_fp(&colors2);
  179. */
  180. if (Rast_read_colors(input, maps, &colors) >= 0) {
  181. if (colors.modular.rules) {
  182. rule = colors.modular.rules;
  183. while (rule->next)
  184. rule = rule->next;
  185. for (; rule; rule = rule->prev) {
  186. value1 = rule->low.value * params->zmult;
  187. value2 = rule->high.value * params->zmult;
  188. Rast_add_modular_d_color_rule(&value1, rule->low.red,
  189. rule->low.grn,
  190. rule->low.blu, &value2,
  191. rule->high.red,
  192. rule->high.grn,
  193. rule->high.blu,
  194. &colors2);
  195. }
  196. }
  197. if (colors.fixed.rules) {
  198. rule = colors.fixed.rules;
  199. while (rule->next)
  200. rule = rule->next;
  201. for (; rule; rule = rule->prev) {
  202. value1 = rule->low.value * params->zmult;
  203. value2 = rule->high.value * params->zmult;
  204. Rast_add_d_color_rule(&value1, rule->low.red,
  205. rule->low.grn, rule->low.blu,
  206. &value2, rule->high.red,
  207. rule->high.grn, rule->high.blu,
  208. &colors2);
  209. }
  210. }
  211. maps = NULL;
  212. maps = G_find_file("cell", params->elev, "");
  213. if (maps == NULL) {
  214. G_warning(_("Raster map <%s> not found"), params->elev);
  215. return -1;
  216. }
  217. Rast_write_colors(params->elev, maps, &colors2);
  218. Rast_quantize_fp_map_range(params->elev, mapset,
  219. zminac - 0.5, zmaxac + 0.5,
  220. (CELL) (zminac - 0.5),
  221. (CELL) (zmaxac + 0.5));
  222. }
  223. else
  224. G_warning(_("No color table for input raster map -- will not create color table"));
  225. }
  226. /* colortable for slopes */
  227. if (cond1 & (!params->deriv)) {
  228. Rast_init_colors(&colors);
  229. val1 = 0;
  230. val2 = 2;
  231. Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 0, &colors);
  232. val1 = 2;
  233. val2 = 5;
  234. Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
  235. val1 = 5;
  236. val2 = 10;
  237. Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
  238. val1 = 10;
  239. val2 = 15;
  240. Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 0, 0, 255, &colors);
  241. val1 = 15;
  242. val2 = 30;
  243. Rast_add_c_color_rule(&val1, 0, 0, 255, &val2, 255, 0, 255, &colors);
  244. val1 = 30;
  245. val2 = 50;
  246. Rast_add_c_color_rule(&val1, 255, 0, 255, &val2, 255, 0, 0, &colors);
  247. val1 = 50;
  248. val2 = 90;
  249. Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 0, 0, 0, &colors);
  250. if (params->slope != NULL) {
  251. maps = NULL;
  252. maps = G_find_file("cell", params->slope, "");
  253. if (maps == NULL) {
  254. G_warning(_("Raster map <%s> not found"), params->slope);
  255. return -1;
  256. }
  257. Rast_write_colors(params->slope, maps, &colors);
  258. Rast_quantize_fp_map_range(params->slope, mapset, 0., 90., 0, 90);
  259. do_history(params->slope, input, params);
  260. }
  261. /* colortable for aspect */
  262. Rast_init_colors(&colors);
  263. val1 = 0;
  264. val2 = 0;
  265. Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 255, &colors);
  266. val1 = 1;
  267. val2 = 90;
  268. Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
  269. val1 = 90;
  270. val2 = 180;
  271. Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
  272. val1 = 180;
  273. val2 = 270;
  274. Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 255, 0, 0, &colors);
  275. val1 = 270;
  276. val2 = 360;
  277. Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 255, 255, 0, &colors);
  278. if (params->aspect != NULL) {
  279. maps = NULL;
  280. maps = G_find_file("cell", params->aspect, "");
  281. if (maps == NULL) {
  282. G_warning(_("Raster map <%s> not found"), params->aspect);
  283. return -1;
  284. }
  285. Rast_write_colors(params->aspect, maps, &colors);
  286. Rast_quantize_fp_map_range(params->aspect, mapset, 0., 360., 0, 360);
  287. do_history(params->aspect, input, params);
  288. }
  289. /* colortable for curvatures */
  290. if (cond2) {
  291. Rast_init_colors(&colors);
  292. dat1 = (FCELL) amin1(c1min, c2min);
  293. dat2 = (FCELL) - 0.01;
  294. Rast_add_f_color_rule(&dat1, 50, 0, 155,
  295. &dat2, 0, 0, 255, &colors);
  296. dat1 = dat2;
  297. dat2 = (FCELL) - 0.001;
  298. Rast_add_f_color_rule(&dat1, 0, 0, 255,
  299. &dat2, 0, 127, 255, &colors);
  300. dat1 = dat2;
  301. dat2 = (FCELL) - 0.00001;
  302. Rast_add_f_color_rule(&dat1, 0, 127, 255,
  303. &dat2, 0, 255, 255, &colors);
  304. dat1 = dat2;
  305. dat2 = (FCELL) 0.00;
  306. Rast_add_f_color_rule(&dat1, 0, 255, 255,
  307. &dat2, 200, 255, 200, &colors);
  308. dat1 = dat2;
  309. dat2 = (FCELL) 0.00001;
  310. Rast_add_f_color_rule(&dat1, 200, 255, 200,
  311. &dat2, 255, 255, 0, &colors);
  312. dat1 = dat2;
  313. dat2 = (FCELL) 0.001;
  314. Rast_add_f_color_rule(&dat1, 255, 255, 0,
  315. &dat2, 255, 127, 0, &colors);
  316. dat1 = dat2;
  317. dat2 = (FCELL) 0.01;
  318. Rast_add_f_color_rule(&dat1, 255, 127, 0,
  319. &dat2, 255, 0, 0, &colors);
  320. dat1 = dat2;
  321. dat2 = (FCELL) amax1(c1max, c2max);
  322. Rast_add_f_color_rule(&dat1, 255, 0, 0,
  323. &dat2, 155, 0, 20, &colors);
  324. maps = NULL;
  325. if (params->pcurv != NULL) {
  326. maps = G_find_file("cell", params->pcurv, "");
  327. if (maps == NULL) {
  328. G_warning(_("Raster map <%s> not found"), params->pcurv);
  329. return -1;
  330. }
  331. Rast_write_colors(params->pcurv, maps, &colors);
  332. fprintf(stderr, "color map written\n");
  333. Rast_quantize_fp_map_range(params->pcurv, mapset,
  334. dat1, dat2,
  335. (CELL) (dat1 * MULT),
  336. (CELL) (dat2 * MULT));
  337. do_history(params->pcurv, input, params);
  338. }
  339. if (params->tcurv != NULL) {
  340. maps = NULL;
  341. maps = G_find_file("cell", params->tcurv, "");
  342. if (maps == NULL) {
  343. G_warning(_("Raster map <%s> not found"), params->tcurv);
  344. return -1;
  345. }
  346. Rast_write_colors(params->tcurv, maps, &colors);
  347. Rast_quantize_fp_map_range(params->tcurv, mapset,
  348. dat1, dat2, (CELL) (dat1 * MULT),
  349. (CELL) (dat2 * MULT));
  350. do_history(params->tcurv, input, params);
  351. }
  352. if (params->mcurv != NULL) {
  353. maps = NULL;
  354. maps = G_find_file("cell", params->mcurv, "");
  355. if (maps == NULL) {
  356. G_warning(_("Raster map <%s> not found"), params->mcurv);
  357. return -1;
  358. }
  359. Rast_write_colors(params->mcurv, maps, &colors);
  360. Rast_quantize_fp_map_range(params->mcurv, mapset,
  361. dat1, dat2,
  362. (CELL) (dat1 * MULT),
  363. (CELL) (dat2 * MULT));
  364. do_history(params->mcurv, input, params);
  365. }
  366. }
  367. }
  368. if (params->elev != NULL) {
  369. if (!G_find_file2("cell", params->elev, "")) {
  370. G_warning(_("Raster map <%s> not found"), params->elev);
  371. return -1;
  372. }
  373. Rast_short_history(params->elev, "raster", &hist);
  374. if (smooth != NULL)
  375. Rast_append_format_history(
  376. &hist, "tension=%f, smoothing=%s",
  377. params->fi * 1000. / (*dnorm), smooth);
  378. else
  379. Rast_append_format_history(
  380. &hist, "tension=%f", params->fi * 1000. / (*dnorm));
  381. Rast_append_format_history(
  382. &hist, "dnorm=%f, zmult=%f", *dnorm, params->zmult);
  383. Rast_append_format_history(
  384. &hist, "KMAX=%d, KMIN=%d, errtotal=%f", params->kmax,
  385. params->kmin, sqrt(ertot / n_points));
  386. Rast_append_format_history(
  387. &hist, "zmin_data=%f, zmax_data=%f", zmin, zmax);
  388. Rast_append_format_history(
  389. &hist, "zmin_int=%f, zmax_int=%f", zminac, zmaxac);
  390. Rast_format_history(&hist, HIST_DATSRC_1, "raster map %s", input);
  391. Rast_write_history(params->elev, &hist);
  392. Rast_free_history(&hist);
  393. }
  394. /* change region to initial region */
  395. G_verbose_message(_("Changing the region back to initial..."));
  396. Rast_set_output_window(winhd);
  397. return 1;
  398. }