close_maps.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include "Gwater.h"
  2. #include <unistd.h>
  3. #include <grass/gis.h>
  4. #include <grass/raster.h>
  5. #include <grass/glocale.h>
  6. int close_maps(void)
  7. {
  8. struct Colors colors;
  9. int r, c, fd;
  10. CELL *buf = NULL;
  11. DCELL *dbuf = NULL;
  12. struct FPRange accRange;
  13. DCELL min, max;
  14. DCELL clr_min, clr_max;
  15. DCELL sum, sum_sqr, stddev, lstddev, dvalue;
  16. if (asp_flag || dis_flag)
  17. buf = Rast_allocate_c_buf();
  18. if (wat_flag || ls_flag || sl_flag || sg_flag || tci_flag)
  19. dbuf = Rast_allocate_d_buf();
  20. G_free(alt);
  21. if (ls_flag || sg_flag)
  22. G_free(r_h);
  23. sum = sum_sqr = stddev = 0.0;
  24. if (wat_flag) {
  25. fd = Rast_open_new(wat_name, DCELL_TYPE);
  26. if (abs_acc) {
  27. G_warning("Writing out only positive flow accumulation values.");
  28. G_warning("Cells with a likely underestimate for flow accumulation can no longer be identified.");
  29. for (r = 0; r < nrows; r++) {
  30. Rast_set_d_null_value(dbuf, ncols); /* reset row to all NULL */
  31. for (c = 0; c < ncols; c++) {
  32. dvalue = wat[SEG_INDEX(wat_seg, r, c)];
  33. if (Rast_is_d_null_value(&dvalue) == 0 && dvalue) {
  34. dvalue = ABS(dvalue);
  35. dbuf[c] = dvalue;
  36. sum += dvalue;
  37. sum_sqr += dvalue * dvalue;
  38. }
  39. }
  40. Rast_put_row(fd, dbuf, DCELL_TYPE);
  41. }
  42. }
  43. else {
  44. for (r = 0; r < nrows; r++) {
  45. Rast_set_d_null_value(dbuf, ncols); /* reset row to all NULL */
  46. for (c = 0; c < ncols; c++) {
  47. dvalue = wat[SEG_INDEX(wat_seg, r, c)];
  48. if (!Rast_is_d_null_value(&dvalue)) {
  49. dbuf[c] = dvalue;
  50. dvalue = ABS(dvalue);
  51. sum += dvalue;
  52. sum_sqr += dvalue * dvalue;
  53. }
  54. }
  55. Rast_put_row(fd, dbuf, DCELL_TYPE);
  56. }
  57. }
  58. Rast_close(fd);
  59. stddev =
  60. sqrt((sum_sqr - (sum + sum / do_points)) / (do_points - 1));
  61. G_debug(1, "stddev: %f", stddev);
  62. /* set nice color rules: yellow, green, cyan, blue, black */
  63. lstddev = log(stddev);
  64. Rast_read_fp_range(wat_name, this_mapset, &accRange);
  65. min = max = 0;
  66. Rast_get_fp_range_min_max(&accRange, &min, &max);
  67. Rast_init_colors(&colors);
  68. if (min < 0) {
  69. if (min < (-stddev - 1)) {
  70. clr_min = min - 1;
  71. clr_max = -stddev - 1;
  72. Rast_add_d_color_rule(&clr_min, 0, 0, 0, &clr_max, 0,
  73. 0, 0, &colors);
  74. }
  75. clr_min = -stddev - 1.;
  76. clr_max = -1. * exp(lstddev * 0.75);
  77. Rast_add_d_color_rule(&clr_min, 0, 0, 0, &clr_max, 0,
  78. 0, 255, &colors);
  79. clr_min = clr_max;
  80. clr_max = -1. * exp(lstddev * 0.5);
  81. Rast_add_d_color_rule(&clr_min, 0, 0, 255, &clr_max, 0,
  82. 255, 255, &colors);
  83. clr_min = clr_max;
  84. clr_max = -1. * exp(lstddev * 0.35);
  85. Rast_add_d_color_rule(&clr_min, 0, 255, 255, &clr_max, 0,
  86. 255, 0, &colors);
  87. clr_min = clr_max;
  88. clr_max = -1.;
  89. Rast_add_d_color_rule(&clr_min, 0, 255, 0, &clr_max, 255,
  90. 255, 0, &colors);
  91. }
  92. clr_min = -1.;
  93. clr_max = 1.;
  94. Rast_add_d_color_rule(&clr_min, 255, 255, 0, &clr_max, 255,
  95. 255, 0, &colors);
  96. clr_min = 1;
  97. clr_max = exp(lstddev * 0.35);
  98. Rast_add_d_color_rule(&clr_min, 255, 255, 0, &clr_max, 0,
  99. 255, 0, &colors);
  100. clr_min = clr_max;
  101. clr_max = exp(lstddev * 0.5);
  102. Rast_add_d_color_rule(&clr_min, 0, 255, 0, &clr_max, 0,
  103. 255, 255, &colors);
  104. clr_min = clr_max;
  105. clr_max = exp(lstddev * 0.75);
  106. Rast_add_d_color_rule(&clr_min, 0, 255, 255, &clr_max, 0,
  107. 0, 255, &colors);
  108. clr_min = clr_max;
  109. clr_max = stddev + 1.;
  110. Rast_add_d_color_rule(&clr_min, 0, 0, 255, &clr_max, 0, 0,
  111. 0, &colors);
  112. if (max > 0 && max > stddev + 1) {
  113. clr_min = stddev + 1;
  114. clr_max = max + 1;
  115. Rast_add_d_color_rule(&clr_min, 0, 0, 0, &clr_max, 0, 0,
  116. 0, &colors);
  117. }
  118. Rast_write_colors(wat_name, this_mapset, &colors);
  119. }
  120. /* TCI */
  121. if (tci_flag) {
  122. DCELL watvalue;
  123. double mean;
  124. sum = sum_sqr = stddev = 0.0;
  125. fd = Rast_open_new(tci_name, DCELL_TYPE);
  126. for (r = 0; r < nrows; r++) {
  127. Rast_set_d_null_value(dbuf, ncols); /* reset row to all NULL */
  128. for (c = 0; c < ncols; c++) {
  129. dvalue = tci[SEG_INDEX(wat_seg, r, c)];
  130. watvalue = wat[SEG_INDEX(wat_seg, r, c)];
  131. if (!Rast_is_d_null_value(&watvalue)) {
  132. dbuf[c] = dvalue;
  133. sum += dvalue;
  134. sum_sqr += dvalue * dvalue;
  135. }
  136. }
  137. Rast_put_row(fd, dbuf, DCELL_TYPE);
  138. }
  139. Rast_close(fd);
  140. mean = sum / do_points;
  141. stddev =
  142. sqrt((sum_sqr - (sum + sum / do_points)) / (do_points - 1));
  143. G_debug(1, "stddev: %f", stddev);
  144. /* set nice color rules: yellow, green, cyan, blue, black */
  145. lstddev = log(stddev);
  146. Rast_read_fp_range(tci_name, this_mapset, &accRange);
  147. min = max = 0;
  148. Rast_get_fp_range_min_max(&accRange, &min, &max);
  149. Rast_init_colors(&colors);
  150. if (min - 1 < mean - 0.5 * stddev) {
  151. clr_min = min - 1;
  152. clr_max = mean - 0.5 * stddev;
  153. Rast_add_d_color_rule(&clr_min, 255, 255, 0, &clr_max, 255,
  154. 255, 0, &colors);
  155. }
  156. clr_min = mean - 0.5 * stddev;
  157. clr_max = mean - 0.2 * stddev;
  158. Rast_add_d_color_rule(&clr_min, 255, 255, 0, &clr_max, 0,
  159. 255, 0, &colors);
  160. clr_min = clr_max;
  161. clr_max = mean + 0.2 * stddev;
  162. Rast_add_d_color_rule(&clr_min, 0, 255, 0, &clr_max, 0,
  163. 255, 255, &colors);
  164. clr_min = clr_max;
  165. clr_max = mean + 0.6 * stddev;
  166. Rast_add_d_color_rule(&clr_min, 0, 255, 255, &clr_max, 0,
  167. 0, 255, &colors);
  168. clr_min = clr_max;
  169. clr_max = mean + 1. * stddev;
  170. Rast_add_d_color_rule(&clr_min, 0, 0, 255, &clr_max, 0, 0,
  171. 0, &colors);
  172. if (max > 0 && max > clr_max) {
  173. clr_min = clr_max;
  174. clr_max = max + 1;
  175. Rast_add_d_color_rule(&clr_min, 0, 0, 0, &clr_max, 0, 0,
  176. 0, &colors);
  177. }
  178. Rast_write_colors(tci_name, this_mapset, &colors);
  179. }
  180. /* TODO: elevation == NULL -> drainage direction == NULL (wat == 0 where ele == NULL) */
  181. /* keep drainage direction == 0 for real depressions */
  182. if (asp_flag) {
  183. fd = Rast_open_c_new(asp_name);
  184. for (r = 0; r < nrows; r++) {
  185. Rast_set_c_null_value(buf, ncols); /* reset row to all NULL */
  186. for (c = 0; c < ncols; c++) {
  187. dvalue = wat[SEG_INDEX(wat_seg, r, c)];
  188. if (!Rast_is_d_null_value(&dvalue))
  189. buf[c] = asp[SEG_INDEX(asp_seg, r, c)];
  190. }
  191. Rast_put_row(fd, buf, CELL_TYPE);
  192. }
  193. Rast_close(fd);
  194. Rast_init_colors(&colors);
  195. Rast_make_aspect_colors(&colors, -8, 8);
  196. Rast_write_colors(asp_name, this_mapset, &colors);
  197. }
  198. G_free(asp);
  199. flag_destroy(swale);
  200. /*
  201. Rast_free_colors(&colors);
  202. */
  203. G_free(wat);
  204. if (ls_flag) {
  205. fd = Rast_open_new(ls_name, DCELL_TYPE);
  206. for (r = 0; r < nrows; r++) {
  207. for (c = 0; c < ncols; c++) {
  208. dbuf[c] = l_s[SEG_INDEX(l_s_seg, r, c)];
  209. }
  210. Rast_put_row(fd, dbuf, DCELL_TYPE);
  211. }
  212. Rast_close(fd);
  213. G_free(l_s);
  214. }
  215. if (sl_flag) {
  216. fd = Rast_open_new(sl_name, DCELL_TYPE);
  217. for (r = 0; r < nrows; r++) {
  218. for (c = 0; c < ncols; c++) {
  219. dbuf[c] = s_l[SEG_INDEX(s_l_seg, r, c)];
  220. if (dbuf[c] > max_length)
  221. dbuf[c] = max_length;
  222. }
  223. Rast_put_row(fd, dbuf, DCELL_TYPE);
  224. }
  225. Rast_close(fd);
  226. }
  227. if (sl_flag || ls_flag || sg_flag)
  228. G_free(s_l);
  229. if (sg_flag) {
  230. fd = Rast_open_new(sg_name, DCELL_TYPE);
  231. for (r = 0; r < nrows; r++) {
  232. for (c = 0; c < ncols; c++) {
  233. dbuf[c] = s_g[SEG_INDEX(s_g_seg, r, c)];
  234. }
  235. Rast_put_row(fd, dbuf, DCELL_TYPE);
  236. }
  237. Rast_close(fd);
  238. G_free(s_g);
  239. }
  240. return 0;
  241. }