output2d.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*-
  2. * Written by H. Mitasova, I. Kosinovsky, D. Gerdes Summer 1992
  3. * Copyright 1992, H. Mitasova
  4. * I. Kosinovsky, and D.Gerdes
  5. *
  6. * modified by McCauley in August 1995
  7. * modified by Mitasova in August 1995
  8. * modified by Mitasova in August 1999 (fix for elev color)
  9. * modified by Brown in September 1999 (fix for Timestamps)
  10. * Modified by Mitasova in Nov. 1999 (write given tension into hist)
  11. * Last modification: 2006-12-13
  12. *
  13. */
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include <grass/gis.h>
  17. #include <grass/raster.h>
  18. #include <grass/bitmap.h>
  19. #include <grass/linkm.h>
  20. #include <grass/interpf.h>
  21. #include <grass/glocale.h>
  22. #define MULT 100000
  23. static void do_history(const char *name, int vect, const char *input,
  24. const struct interp_params *params)
  25. {
  26. struct History hist;
  27. Rast_short_history(name, "raster", &hist);
  28. if (params->elev)
  29. Rast_append_format_history(&hist, "The elevation map is %s",
  30. params->elev);
  31. Rast_format_history(&hist, HIST_DATSRC_1, "%s %s",
  32. vect ? "vector map" : "site file",
  33. input);
  34. Rast_command_history(&hist);
  35. Rast_write_history(name, &hist);
  36. if (params->ts)
  37. G_write_raster_timestamp(name, params->ts);
  38. Rast_free_history(&hist);
  39. }
  40. int IL_output_2d(struct interp_params *params, struct Cell_head *cellhd, /* current region */
  41. double zmin, double zmax, /* min,max input z-values */
  42. double zminac, double zmaxac, double c1min, double c1max, /* min,max interpolated values */
  43. double c2min, double c2max, double gmin, double gmax, double ertot, /* total interplating func. error */
  44. char *input, /* input file name */
  45. double dnorm, int dtens, int vect, int n_points)
  46. /*
  47. * Creates output files as well as history files and color tables for
  48. * them.
  49. */
  50. {
  51. FCELL *cell1;
  52. int cf1 = 0, cf2 = 0, cf3 = 0, cf4 = 0, cf5 = 0, cf6 = 0;
  53. int nrows, ncols;
  54. int i, ii;
  55. double zstep;
  56. FCELL data1, data2;
  57. struct Colors colors;
  58. struct History hist;
  59. char *type;
  60. const char *mapset = NULL;
  61. int cond1, cond2;
  62. FCELL dat1, dat2;
  63. CELL val1, val2;
  64. cond2 = ((params->pcurv != NULL) || (params->tcurv != NULL)
  65. || (params->mcurv != NULL));
  66. cond1 = ((params->slope != NULL) || (params->aspect != NULL) || cond2);
  67. Rast_set_window(cellhd);
  68. cell1 = Rast_allocate_f_buf();
  69. /*
  70. * G_set_embedded_null_value_mode(1);
  71. */
  72. if (params->elev)
  73. cf1 = Rast_open_fp_new(params->elev);
  74. if (params->slope)
  75. cf2 = Rast_open_fp_new(params->slope);
  76. if (params->aspect)
  77. cf3 = Rast_open_fp_new(params->aspect);
  78. if (params->pcurv)
  79. cf4 = Rast_open_fp_new(params->pcurv);
  80. if (params->tcurv)
  81. cf5 = Rast_open_fp_new(params->tcurv);
  82. if (params->mcurv)
  83. cf6 = Rast_open_fp_new(params->mcurv);
  84. nrows = cellhd->rows;
  85. if (nrows != params->nsizr) {
  86. G_warning(_("First change your rows number to nsizr! %d %d"),
  87. nrows, params->nsizr);
  88. return -1;
  89. }
  90. ncols = cellhd->cols;
  91. if (ncols != params->nsizc) {
  92. G_warning(_("First change your cols number to nsizc %d %d"),
  93. ncols, params->nsizc);
  94. return -1;
  95. }
  96. if (params->elev != NULL) {
  97. G_fseek(params->Tmp_fd_z, 0L, 0); /* seek to the beginning */
  98. for (i = 0; i < params->nsizr; i++) {
  99. /* seek to the right row */
  100. G_fseek(params->Tmp_fd_z, (off_t) (params->nsizr - 1 - i) *
  101. params->nsizc * sizeof(FCELL), 0);
  102. ii = fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_z);
  103. /*
  104. * for(j=0;j<params->nsizc;j++) fprintf(stderr,"%f ",cell1[j]);
  105. * fprintf(stderr,"\n");
  106. */
  107. Rast_put_f_row(cf1, cell1);
  108. }
  109. }
  110. if (params->slope != NULL) {
  111. G_fseek(params->Tmp_fd_dx, 0L, 0); /* seek to the beginning */
  112. for (i = 0; i < params->nsizr; i++) {
  113. /* seek to the right row */
  114. G_fseek(params->Tmp_fd_dx, (off_t) (params->nsizr - 1 - i) *
  115. params->nsizc * sizeof(FCELL), 0);
  116. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dx);
  117. Rast_put_f_row(cf2, cell1);
  118. }
  119. }
  120. if (params->aspect != NULL) {
  121. G_fseek(params->Tmp_fd_dy, 0L, 0); /* seek to the beginning */
  122. for (i = 0; i < params->nsizr; i++) {
  123. /* seek to the right row */
  124. G_fseek(params->Tmp_fd_dy, (off_t) (params->nsizr - 1 - i) *
  125. params->nsizc * sizeof(FCELL), 0);
  126. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dy);
  127. Rast_put_f_row(cf3, cell1);
  128. }
  129. }
  130. if (params->pcurv != NULL) {
  131. G_fseek(params->Tmp_fd_xx, 0L, 0); /* seek to the beginning */
  132. for (i = 0; i < params->nsizr; i++) {
  133. /* seek to the right row */
  134. G_fseek(params->Tmp_fd_xx, (off_t) (params->nsizr - 1 - i) *
  135. params->nsizc * sizeof(FCELL), 0);
  136. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xx);
  137. Rast_put_f_row(cf4, cell1);
  138. }
  139. }
  140. if (params->tcurv != NULL) {
  141. G_fseek(params->Tmp_fd_yy, 0L, 0); /* seek to the beginning */
  142. for (i = 0; i < params->nsizr; i++) {
  143. /* seek to the right row */
  144. G_fseek(params->Tmp_fd_yy, (off_t) (params->nsizr - 1 - i) *
  145. params->nsizc * sizeof(FCELL), 0);
  146. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_yy);
  147. Rast_put_f_row(cf5, cell1);
  148. }
  149. }
  150. if (params->mcurv != NULL) {
  151. G_fseek(params->Tmp_fd_xy, 0L, 0); /* seek to the beginning */
  152. for (i = 0; i < params->nsizr; i++) {
  153. /* seek to the right row */
  154. G_fseek(params->Tmp_fd_xy, (off_t) (params->nsizr - 1 - i) *
  155. params->nsizc * sizeof(FCELL), 0);
  156. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xy);
  157. Rast_put_f_row(cf6, cell1);
  158. }
  159. }
  160. if (cf1)
  161. Rast_close(cf1);
  162. if (cf2)
  163. Rast_close(cf2);
  164. if (cf3)
  165. Rast_close(cf3);
  166. if (cf4)
  167. Rast_close(cf4);
  168. if (cf5)
  169. Rast_close(cf5);
  170. if (cf6)
  171. Rast_close(cf6);
  172. /* colortable for elevations */
  173. Rast_init_colors(&colors);
  174. zstep = (FCELL) (zmaxac - zminac) / 5.;
  175. for (i = 1; i <= 5; i++) {
  176. data1 = (FCELL) (zminac + (i - 1) * zstep);
  177. data2 = (FCELL) (zminac + i * zstep);
  178. switch (i) {
  179. case 1:
  180. Rast_add_f_color_rule(&data1, 0, 191, 191,
  181. &data2, 0, 255, 0, &colors);
  182. break;
  183. case 2:
  184. Rast_add_f_color_rule(&data1, 0, 255, 0,
  185. &data2, 255, 255, 0, &colors);
  186. break;
  187. case 3:
  188. Rast_add_f_color_rule(&data1, 255, 255, 0,
  189. &data2, 255, 127, 0, &colors);
  190. break;
  191. case 4:
  192. Rast_add_f_color_rule(&data1, 255, 127, 0,
  193. &data2, 191, 127, 63, &colors);
  194. break;
  195. case 5:
  196. Rast_add_f_color_rule(&data1, 191, 127, 63,
  197. &data2, 20, 20, 20, &colors);
  198. break;
  199. }
  200. }
  201. if (params->elev != NULL) {
  202. mapset = G_find_file("cell", params->elev, "");
  203. if (mapset == NULL) {
  204. G_warning(_("Raster map <%s> not found"), params->elev);
  205. return -1;
  206. }
  207. Rast_write_colors(params->elev, mapset, &colors);
  208. Rast_quantize_fp_map_range(params->elev, mapset,
  209. (DCELL) zminac - 0.5, (DCELL) zmaxac + 0.5,
  210. (CELL) (zminac - 0.5), (CELL) (zmaxac + 0.5));
  211. }
  212. /* colortable for slopes */
  213. if (cond1) {
  214. if (!params->deriv) {
  215. /*
  216. * smin = (CELL) ((int)(gmin*scig)); smax = (CELL) gmax; fprintf
  217. * (stderr, "min %d max %d \n", smin,smax); Rast_make_rainbow_colors
  218. * (&colors,smin,smax);
  219. */
  220. Rast_init_colors(&colors);
  221. val1 = 0;
  222. val2 = 2;
  223. Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 0, &colors);
  224. val1 = 2;
  225. val2 = 5;
  226. Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
  227. val1 = 5;
  228. val2 = 10;
  229. Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
  230. val1 = 10;
  231. val2 = 15;
  232. Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 0, 0, 255, &colors);
  233. val1 = 15;
  234. val2 = 30;
  235. Rast_add_c_color_rule(&val1, 0, 0, 255, &val2, 255, 0, 255, &colors);
  236. val1 = 30;
  237. val2 = 50;
  238. Rast_add_c_color_rule(&val1, 255, 0, 255, &val2, 255, 0, 0, &colors);
  239. val1 = 50;
  240. val2 = 90;
  241. Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 0, 0, 0, &colors);
  242. }
  243. else {
  244. Rast_init_colors(&colors);
  245. dat1 = (FCELL) - 5.0; /* replace by min dx, amin1 (c1min,
  246. * c2min); */
  247. dat2 = (FCELL) - 0.1;
  248. Rast_add_f_color_rule(&dat1, 127, 0, 255,
  249. &dat2, 0, 0, 255, &colors);
  250. dat1 = dat2;
  251. dat2 = (FCELL) - 0.01;
  252. Rast_add_f_color_rule(&dat1, 0, 0, 255,
  253. &dat2, 0, 127, 255, &colors);
  254. dat1 = dat2;
  255. dat2 = (FCELL) - 0.001;
  256. Rast_add_f_color_rule(&dat1, 0, 127, 255,
  257. &dat2, 0, 255, 255, &colors);
  258. dat1 = dat2;
  259. dat2 = (FCELL) 0.0;
  260. Rast_add_f_color_rule(&dat1, 0, 255, 255,
  261. &dat2, 200, 255, 200, &colors);
  262. dat1 = dat2;
  263. dat2 = (FCELL) 0.001;
  264. Rast_add_f_color_rule(&dat1, 200, 255, 200,
  265. &dat2, 255, 255, 0, &colors);
  266. dat1 = dat2;
  267. dat2 = (FCELL) 0.01;
  268. Rast_add_f_color_rule(&dat1, 255, 255, 0,
  269. &dat2, 255, 127, 0, &colors);
  270. dat1 = dat2;
  271. dat2 = (FCELL) 0.1;
  272. Rast_add_f_color_rule(&dat1, 255, 127, 0,
  273. &dat2, 255, 0, 0, &colors);
  274. dat1 = dat2;
  275. dat2 = (FCELL) 5.0; /* replace by max dx, amax1 (c1max,
  276. * c2max); */
  277. Rast_add_f_color_rule(&dat1, 255, 0, 0,
  278. &dat2, 255, 0, 200, &colors);
  279. }
  280. if (params->slope != NULL) {
  281. mapset = G_find_file("cell", params->slope, "");
  282. if (mapset == NULL) {
  283. G_warning(_("Raster map <%s> not found"), params->slope);
  284. return -1;
  285. }
  286. Rast_write_colors(params->slope, mapset, &colors);
  287. Rast_quantize_fp_map_range(params->slope, mapset, 0., 90., 0, 90);
  288. do_history(params->slope, vect, input, params);
  289. }
  290. /* colortable for aspect */
  291. if (!params->deriv) {
  292. Rast_init_colors(&colors);
  293. val1 = 0;
  294. val2 = 0;
  295. Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 255, &colors);
  296. val1 = 1;
  297. val2 = 90;
  298. Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
  299. val1 = 90;
  300. val2 = 180;
  301. Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
  302. val1 = 180;
  303. val2 = 270;
  304. Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 255, 0, 0, &colors);
  305. val1 = 270;
  306. val2 = 360;
  307. Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 255, 255, 0, &colors);
  308. }
  309. else {
  310. Rast_init_colors(&colors);
  311. dat1 = (FCELL) - 5.0; /* replace by min dy, amin1 (c1min,
  312. * c2min); */
  313. dat2 = (FCELL) - 0.1;
  314. Rast_add_f_color_rule(&dat1, 127, 0, 255,
  315. &dat2, 0, 0, 255, &colors);
  316. dat1 = dat2;
  317. dat2 = (FCELL) - 0.01;
  318. Rast_add_f_color_rule(&dat1, 0, 0, 255,
  319. &dat2, 0, 127, 255, &colors);
  320. dat1 = dat2;
  321. dat2 = (FCELL) - 0.001;
  322. Rast_add_f_color_rule(&dat1, 0, 127, 255,
  323. &dat2, 0, 255, 255, &colors);
  324. dat1 = dat2;
  325. dat2 = (FCELL) 0.0;
  326. Rast_add_f_color_rule(&dat1, 0, 255, 255,
  327. &dat2, 200, 255, 200, &colors);
  328. dat1 = dat2;
  329. dat2 = (FCELL) 0.001;
  330. Rast_add_f_color_rule(&dat1, 200, 255, 200,
  331. &dat2, 255, 255, 0, &colors);
  332. dat1 = dat2;
  333. dat2 = (FCELL) 0.01;
  334. Rast_add_f_color_rule(&dat1, 255, 255, 0,
  335. &dat2, 255, 127, 0, &colors);
  336. dat1 = dat2;
  337. dat2 = (FCELL) 0.1;
  338. Rast_add_f_color_rule(&dat1, 255, 127, 0,
  339. &dat2, 255, 0, 0, &colors);
  340. dat1 = dat2;
  341. dat2 = (FCELL) 5.0; /* replace by max dy, amax1 (c1max,
  342. * c2max); */
  343. Rast_add_f_color_rule(&dat1, 255, 0, 0,
  344. &dat2, 255, 0, 200, &colors);
  345. }
  346. if (params->aspect != NULL) {
  347. mapset = G_find_file("cell", params->aspect, "");
  348. if (mapset == NULL) {
  349. G_warning(_("Raster map <%s> not found"), params->aspect);
  350. return -1;
  351. }
  352. Rast_write_colors(params->aspect, mapset, &colors);
  353. Rast_quantize_fp_map_range(params->aspect, mapset, 0., 360., 0, 360);
  354. do_history(params->aspect, vect, input, params);
  355. }
  356. /* colortable for curvatures */
  357. if (cond2) {
  358. Rast_init_colors(&colors);
  359. dat1 = (FCELL) amin1(c1min, c2min); /* for derivatives use min
  360. * dxx,dyy,dxy */
  361. dat2 = (FCELL) - 0.01;
  362. Rast_add_f_color_rule(&dat1, 127, 0, 255,
  363. &dat2, 0, 0, 255, &colors);
  364. dat1 = dat2;
  365. dat2 = (FCELL) - 0.001;
  366. Rast_add_f_color_rule(&dat1, 0, 0, 255,
  367. &dat2, 0, 127, 255, &colors);
  368. dat1 = dat2;
  369. dat2 = (FCELL) - 0.00001;
  370. Rast_add_f_color_rule(&dat1, 0, 127, 255,
  371. &dat2, 0, 255, 255, &colors);
  372. dat1 = dat2;
  373. dat2 = (FCELL) 0.0;
  374. Rast_add_f_color_rule(&dat1, 0, 255, 255,
  375. &dat2, 200, 255, 200, &colors);
  376. dat1 = dat2;
  377. dat2 = (FCELL) 0.00001;
  378. Rast_add_f_color_rule(&dat1, 200, 255, 200,
  379. &dat2, 255, 255, 0, &colors);
  380. dat1 = dat2;
  381. dat2 = (FCELL) 0.001;
  382. Rast_add_f_color_rule(&dat1, 255, 255, 0,
  383. &dat2, 255, 127, 0, &colors);
  384. dat1 = dat2;
  385. dat2 = (FCELL) 0.01;
  386. Rast_add_f_color_rule(&dat1, 255, 127, 0,
  387. &dat2, 255, 0, 0, &colors);
  388. dat1 = dat2;
  389. dat2 = (FCELL) amax1(c1max, c2max); /* for derivatives use max
  390. * dxx,dyy,dxy */
  391. Rast_add_f_color_rule(&dat1, 255, 0, 0,
  392. &dat2, 255, 0, 200, &colors);
  393. if (params->pcurv != NULL) {
  394. mapset = G_find_file("cell", params->pcurv, "");
  395. if (mapset == NULL) {
  396. G_warning(_("Raster map <%s> not found"), params->pcurv);
  397. return -1;
  398. }
  399. Rast_write_colors(params->pcurv, mapset, &colors);
  400. Rast_quantize_fp_map_range(params->pcurv, mapset, dat1, dat2,
  401. (CELL) (dat1 * MULT),
  402. (CELL) (dat2 * MULT));
  403. do_history(params->pcurv, vect, input, params);
  404. }
  405. if (params->tcurv != NULL) {
  406. mapset = G_find_file("cell", params->tcurv, "");
  407. if (mapset == NULL) {
  408. G_warning(_("Raster map <%s> not found"), params->tcurv);
  409. return -1;
  410. }
  411. Rast_write_colors(params->tcurv, mapset, &colors);
  412. Rast_quantize_fp_map_range(params->tcurv, mapset, dat1, dat2,
  413. (CELL) (dat1 * MULT),
  414. (CELL) (dat2 * MULT));
  415. do_history(params->tcurv, vect, input, params);
  416. }
  417. if (params->mcurv != NULL) {
  418. mapset = G_find_file("cell", params->mcurv, "");
  419. if (mapset == NULL) {
  420. G_warning(_("Raster map <%s> not found"), params->mcurv);
  421. return -1;
  422. }
  423. Rast_write_colors(params->mcurv, mapset, &colors);
  424. Rast_quantize_fp_map_range(params->mcurv, mapset, dat1, dat2,
  425. (CELL) (dat1 * MULT),
  426. (CELL) (dat2 * MULT));
  427. do_history(params->mcurv, vect, input, params);
  428. }
  429. }
  430. }
  431. if (params->elev != NULL) {
  432. mapset = G_find_file("cell", params->elev, "");
  433. if (mapset == NULL) {
  434. G_warning(_("Raster map <%s> not found"), params->elev);
  435. return -1;
  436. }
  437. type = "raster";
  438. Rast_short_history(params->elev, type, &hist);
  439. params->dmin = sqrt(params->dmin);
  440. /*
  441. * sprintf (hist.edhist[0], "tension=%f, smoothing=%f", params->fi *
  442. * dnorm / 1000., params->rsm);
  443. */
  444. if (dtens) {
  445. if (params->rsm == -1)
  446. Rast_append_format_history(
  447. &hist, "giventension=%f, smoothing att=%d",
  448. params->fi * 1000. / dnorm, params->smatt);
  449. else
  450. Rast_append_format_history(
  451. &hist, "giventension=%f, smoothing=%f",
  452. params->fi * 1000. / dnorm, params->rsm);
  453. }
  454. else {
  455. if (params->rsm == -1)
  456. Rast_append_format_history(
  457. &hist, "tension=%f, smoothing att=%d",
  458. params->fi * 1000. / dnorm, params->smatt);
  459. else
  460. Rast_append_format_history(
  461. &hist, "tension=%f, smoothing=%f",
  462. params->fi, params->rsm);
  463. }
  464. Rast_append_format_history(
  465. &hist, "dnorm=%f, dmin=%f, zmult=%f",
  466. dnorm, params->dmin, params->zmult);
  467. /*
  468. * sprintf(hist.edhist[2], "segmax=%d, npmin=%d, errtotal=%f",
  469. * params->kmax,params->kmin,ertot);
  470. */
  471. /*
  472. * sprintf (hist.edhist[2], "segmax=%d, npmin=%d, errtotal =%f",
  473. * params->kmax, params->kmin, sqrt (ertot) / n_points);
  474. */
  475. Rast_append_format_history(
  476. &hist, "segmax=%d, npmin=%d, rmsdevi=%f",
  477. params->kmax, params->kmin, sqrt(ertot / n_points));
  478. Rast_append_format_history(
  479. &hist, "zmin_data=%f, zmax_data=%f", zmin, zmax);
  480. Rast_append_format_history(
  481. &hist, "zmin_int=%f, zmax_int=%f", zminac, zmaxac);
  482. if ((params->theta) && (params->scalex))
  483. Rast_append_format_history(
  484. &hist, "theta=%f, scalex=%f", params->theta,
  485. params->scalex);
  486. Rast_format_history(&hist, HIST_DATSRC_1, "%s %s",
  487. vect ? "vector map" : "site file",
  488. input);
  489. Rast_command_history(&hist);
  490. Rast_write_history(params->elev, &hist);
  491. if (params->ts)
  492. G_write_raster_timestamp(params->elev, params->ts);
  493. Rast_free_history(&hist);
  494. }
  495. /*
  496. * if (title) Rast_put_cell_title (output, title);
  497. */
  498. return 1;
  499. }