init_vars.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "Gwater.h"
  4. #include <grass/gis.h>
  5. #include <grass/raster.h>
  6. #include <grass/glocale.h>
  7. int ele_round(double);
  8. int init_vars(int argc, char *argv[])
  9. {
  10. int r, c;
  11. CELL *buf, alt_value, asp_value, block_value;
  12. DCELL dvalue, wat_value, *dbuf;
  13. void *elebuf, *ptr;
  14. int fd, ele_map_type;
  15. size_t ele_size;
  16. char MASK_flag;
  17. int seg_idx;
  18. G_gisinit(argv[0]);
  19. /* input */
  20. ele_flag = pit_flag = run_flag = ril_flag = 0;
  21. /* output */
  22. wat_flag = asp_flag = bas_flag = seg_flag = haf_flag = tci_flag = 0;
  23. bas_thres = 0;
  24. /* shed, unused */
  25. arm_flag = dis_flag = 0;
  26. /* RUSLE */
  27. ob_flag = st_flag = sl_flag = sg_flag = ls_flag = er_flag = 0;
  28. zero = 0;
  29. one = 1;
  30. nxt_avail_pt = 0;
  31. /* dep_flag = 0; */
  32. max_length = d_zero = 0.0;
  33. d_one = 1.0;
  34. ril_value = -1.0;
  35. /* dep_slope = 0.0; */
  36. sides = 8;
  37. mfd = 1;
  38. c_fac = 5;
  39. abs_acc = 0;
  40. flat_flag = 0;
  41. ele_scale = 1;
  42. for (r = 1; r < argc; r++) {
  43. if (sscanf(argv[r], "elevation=%s", ele_name) == 1)
  44. ele_flag++;
  45. else if (sscanf(argv[r], "accumulation=%s", wat_name) == 1)
  46. wat_flag++;
  47. else if (sscanf(argv[r], "tci=%s", tci_name) == 1)
  48. tci_flag++;
  49. else if (sscanf(argv[r], "drainage=%s", asp_name) == 1)
  50. asp_flag++;
  51. else if (sscanf(argv[r], "depression=%s", pit_name) == 1)
  52. pit_flag++;
  53. else if (sscanf(argv[r], "threshold=%d", &bas_thres) == 1) ;
  54. else if (sscanf(argv[r], "max_slope_length=%lf", &max_length) == 1) ;
  55. else if (sscanf(argv[r], "basin=%s", bas_name) == 1)
  56. bas_flag++;
  57. else if (sscanf(argv[r], "stream=%s", seg_name) == 1)
  58. seg_flag++;
  59. else if (sscanf(argv[r], "half_basin=%s", haf_name) == 1)
  60. haf_flag++;
  61. else if (sscanf(argv[r], "flow=%s", run_name) == 1)
  62. run_flag++;
  63. else if (sscanf(argv[r], "ar=%s", arm_name) == 1)
  64. arm_flag++;
  65. /* slope length
  66. else if (sscanf(argv[r], "sl=%[^\n]", sl_name) == 1)
  67. sl_flag++; */
  68. else if (sscanf(argv[r], "length_slope=%s", ls_name) == 1)
  69. ls_flag++;
  70. else if (sscanf(argv[r], "slope_steepness=%s", sg_name) == 1)
  71. sg_flag++;
  72. else if (sscanf(argv[r], "blocking=%s", ob_name) == 1)
  73. ob_flag++;
  74. else if (sscanf(argv[r], "disturbed_land=%s", ril_name) == 1) {
  75. if (sscanf(ril_name, "%lf", &ril_value) == 0) {
  76. ril_value = -1.0;
  77. ril_flag++;
  78. }
  79. }
  80. /* slope deposition
  81. else if (sscanf (argv[r], "sd=%[^\n]", dep_name) == 1) dep_flag++; */
  82. else if (sscanf(argv[r], "-%d", &sides) == 1) {
  83. if (sides != 4)
  84. usage(argv[0]);
  85. }
  86. else if (sscanf(argv[r], "convergence=%d", &c_fac) == 1) ;
  87. else if (strcmp(argv[r], "-s") == 0)
  88. mfd = 0;
  89. else if (strcmp(argv[r], "-a") == 0)
  90. abs_acc = 1;
  91. else if (strcmp(argv[r], "-b") == 0)
  92. flat_flag = 1;
  93. else
  94. usage(argv[0]);
  95. }
  96. if (mfd == 1 && (c_fac < 1 || c_fac > 10)) {
  97. G_fatal_error("Convergence factor must be between 1 and 10.");
  98. }
  99. if ((ele_flag != 1)
  100. ||
  101. ((arm_flag == 1) &&
  102. ((bas_thres <= 0) || ((haf_flag != 1) && (bas_flag != 1))))
  103. ||
  104. ((bas_thres <= 0) &&
  105. ((bas_flag == 1) || (seg_flag == 1) || (haf_flag == 1) ||
  106. (sl_flag == 1) || (sg_flag == 1) || (ls_flag == 1)))
  107. )
  108. usage(argv[0]);
  109. tot_parts = 4;
  110. if (ls_flag || sg_flag)
  111. tot_parts++;
  112. if (bas_thres > 0)
  113. tot_parts++;
  114. G_message(_("SECTION 1a (of %1d): Initiating Memory."), tot_parts);
  115. this_mapset = G_mapset();
  116. if (sl_flag || sg_flag || ls_flag)
  117. er_flag = 1;
  118. /* for sd factor
  119. if (dep_flag) {
  120. if (sscanf (dep_name, "%lf", &dep_slope) != 1) {
  121. dep_mapset = do_exist (dep_name);
  122. dep_flag = -1;
  123. }
  124. }
  125. */
  126. G_get_set_window(&window);
  127. nrows = Rast_window_rows();
  128. ncols = Rast_window_cols();
  129. total_cells = nrows * ncols;
  130. if (max_length <= d_zero)
  131. max_length = 10 * nrows * window.ns_res + 10 * ncols * window.ew_res;
  132. if (window.ew_res < window.ns_res)
  133. half_res = .5 * window.ew_res;
  134. else
  135. half_res = .5 * window.ns_res;
  136. diag = sqrt(window.ew_res * window.ew_res +
  137. window.ns_res * window.ns_res);
  138. if (sides == 4)
  139. diag *= 0.5;
  140. alt =
  141. (CELL *) G_malloc(sizeof(CELL) * size_array(&alt_seg, nrows, ncols));
  142. wat =
  143. (DCELL *) G_malloc(sizeof(DCELL) *
  144. size_array(&wat_seg, nrows, ncols));
  145. if (tci_flag)
  146. tci = (DCELL *) G_malloc(sizeof(DCELL) *
  147. size_array(&wat_seg, nrows, ncols));
  148. else
  149. tci = NULL;
  150. asp =
  151. (CELL *) G_malloc(size_array(&asp_seg, nrows, ncols) * sizeof(CELL));
  152. if (er_flag) {
  153. r_h =
  154. (CELL *) G_malloc(sizeof(CELL) * size_array(&r_h_seg, nrows, ncols));
  155. }
  156. swale = flag_create(nrows, ncols);
  157. in_list = flag_create(nrows, ncols);
  158. worked = flag_create(nrows, ncols);
  159. /* open elevation input */
  160. fd = Rast_open_old(ele_name, "");
  161. ele_map_type = Rast_get_map_type(fd);
  162. ele_size = Rast_cell_size(ele_map_type);
  163. elebuf = Rast_allocate_buf(ele_map_type);
  164. if (ele_map_type == FCELL_TYPE || ele_map_type == DCELL_TYPE)
  165. ele_scale = 1000; /* should be enough to do the trick */
  166. if (flat_flag)
  167. ele_scale = 10000;
  168. /* read elevation input and mark NULL/masked cells */
  169. /* intialize accumulation and drainage direction */
  170. MASK_flag = 0;
  171. do_points = nrows * ncols;
  172. for (r = 0; r < nrows; r++) {
  173. Rast_get_row(fd, elebuf, r, ele_map_type);
  174. ptr = elebuf;
  175. for (c = 0; c < ncols; c++) {
  176. seg_idx = SEG_INDEX(alt_seg, r, c);
  177. /* all flags need to be manually set to zero */
  178. flag_unset(swale, r, c);
  179. flag_unset(in_list, r, c);
  180. flag_unset(worked, r, c);
  181. /* check for masked and NULL cells */
  182. if (Rast_is_null_value(ptr, ele_map_type)) {
  183. FLAG_SET(worked, r, c);
  184. FLAG_SET(in_list, r, c);
  185. Rast_set_c_null_value(&alt_value, 1);
  186. Rast_set_d_null_value(&wat_value, 1);
  187. do_points--;
  188. }
  189. else {
  190. if (ele_map_type == CELL_TYPE) {
  191. alt_value = *((CELL *)ptr);
  192. alt_value *= ele_scale;
  193. }
  194. else if (ele_map_type == FCELL_TYPE) {
  195. dvalue = *((FCELL *)ptr);
  196. dvalue *= ele_scale;
  197. alt_value = ele_round(dvalue);
  198. }
  199. else if (ele_map_type == DCELL_TYPE) {
  200. dvalue = *((DCELL *)ptr);
  201. dvalue *= ele_scale;
  202. alt_value = ele_round(dvalue);
  203. }
  204. wat_value = 1.0;
  205. }
  206. alt[seg_idx] = alt_value;
  207. wat[seg_idx] = wat_value;
  208. asp[seg_idx] = 0;
  209. if (er_flag) {
  210. r_h[seg_idx] = alt_value;
  211. }
  212. ptr = G_incr_void_ptr(ptr, ele_size);
  213. }
  214. }
  215. Rast_close(fd);
  216. G_free(elebuf);
  217. if (do_points < nrows * ncols)
  218. MASK_flag = 1;
  219. /* read flow accumulation from input map flow: amount of overland flow per cell */
  220. if (run_flag) {
  221. dbuf = Rast_allocate_d_buf();
  222. fd = Rast_open_old(run_name, "");
  223. for (r = 0; r < nrows; r++) {
  224. Rast_get_d_row(fd, dbuf, r);
  225. for (c = 0; c < ncols; c++) {
  226. if (MASK_flag) {
  227. block_value = FLAG_GET(worked, r, c);
  228. if (!block_value)
  229. wat[SEG_INDEX(wat_seg, r, c)] = dbuf[c];
  230. else
  231. wat[SEG_INDEX(wat_seg, r, c)] = 0.0;
  232. }
  233. else
  234. wat[SEG_INDEX(wat_seg, r, c)] = dbuf[c];
  235. }
  236. }
  237. Rast_close(fd);
  238. G_free(dbuf);
  239. }
  240. /* overland blocking map; this is also creating streams... */
  241. if (ob_flag) {
  242. buf = Rast_allocate_c_buf();
  243. fd = Rast_open_old(ob_name, "");
  244. for (r = 0; r < nrows; r++) {
  245. Rast_get_c_row(fd, buf, r);
  246. for (c = 0; c < ncols; c++) {
  247. block_value = buf[c];
  248. if (!Rast_is_c_null_value(&block_value) && block_value)
  249. FLAG_SET(swale, r, c);
  250. }
  251. }
  252. Rast_close(fd);
  253. G_free(buf);
  254. }
  255. if (ril_flag)
  256. ril_fd = Rast_open_old(ril_name, "");
  257. /* RUSLE: LS and/or S factor */
  258. if (er_flag) {
  259. s_l =
  260. (double *)G_malloc(size_array(&s_l_seg, nrows, ncols) *
  261. sizeof(double));
  262. }
  263. if (sg_flag) {
  264. s_g =
  265. (double *)G_malloc(size_array(&s_g_seg, nrows, ncols) *
  266. sizeof(double));
  267. }
  268. if (ls_flag) {
  269. l_s =
  270. (double *)G_malloc(size_array(&l_s_seg, nrows, ncols) *
  271. sizeof(double));
  272. }
  273. astar_pts = (int *) G_malloc((do_points + 1) * sizeof(int));
  274. /* heap_index will track astar_pts in ternary min-heap */
  275. /* heap_index is one-based */
  276. heap_index = (int *)G_malloc((do_points + 1) * sizeof(int));
  277. G_message(_("SECTION 1b (of %1d): Determining Offmap Flow."), tot_parts);
  278. /* heap is empty */
  279. heap_size = 0;
  280. if (pit_flag) {
  281. buf = Rast_allocate_c_buf();
  282. fd = Rast_open_old(pit_name, "");
  283. }
  284. else
  285. buf = NULL;
  286. first_astar = first_cum = -1;
  287. for (r = 0; r < nrows; r++) {
  288. G_percent(r, nrows, 3);
  289. if (pit_flag)
  290. Rast_get_c_row(fd, buf, r);
  291. for (c = 0; c < ncols; c++) {
  292. seg_idx = SEG_INDEX(wat_seg, r, c);
  293. if (!(FLAG_GET(worked, r, c))) {
  294. asp_value = asp[seg_idx];
  295. if (er_flag)
  296. s_l[seg_idx] = half_res;
  297. if (r == 0 || c == 0 || r == nrows - 1 ||
  298. c == ncols - 1) {
  299. wat_value = wat[seg_idx];
  300. if (wat_value > 0)
  301. wat[seg_idx] = -wat_value;
  302. if (r == 0)
  303. asp_value = -2;
  304. else if (c == 0)
  305. asp_value = -4;
  306. else if (r == nrows - 1)
  307. asp_value = -6;
  308. else if (c == ncols - 1)
  309. asp_value = -8;
  310. asp[seg_idx] = asp_value;
  311. alt_value = alt[seg_idx];
  312. add_pt(r, c, alt_value);
  313. }
  314. else if (FLAG_GET(worked, r - 1, c)) {
  315. alt_value = alt[seg_idx];
  316. add_pt(r, c, alt_value);
  317. asp[seg_idx] = -2;
  318. wat_value = wat[seg_idx];
  319. if (wat_value > 0)
  320. wat[seg_idx] = -wat_value;
  321. }
  322. else if (FLAG_GET(worked, r + 1, c)) {
  323. alt_value = alt[seg_idx];
  324. add_pt(r, c, alt_value);
  325. asp[seg_idx] = -6;
  326. wat_value = wat[seg_idx];
  327. if (wat_value > 0)
  328. wat[seg_idx] = -wat_value;
  329. }
  330. else if (FLAG_GET(worked, r, c - 1)) {
  331. alt_value = alt[seg_idx];
  332. add_pt(r, c, alt_value);
  333. asp[seg_idx] = -4;
  334. wat_value = wat[seg_idx];
  335. if (wat_value > 0)
  336. wat[seg_idx] = -wat_value;
  337. }
  338. else if (FLAG_GET(worked, r, c + 1)) {
  339. alt_value = alt[seg_idx];
  340. add_pt(r, c, alt_value);
  341. asp[seg_idx] = -8;
  342. wat_value = wat[seg_idx];
  343. if (wat_value > 0)
  344. wat[seg_idx] = -wat_value;
  345. }
  346. else if (sides == 8 && FLAG_GET(worked, r - 1, c - 1)) {
  347. alt_value = alt[seg_idx];
  348. add_pt(r, c, alt_value);
  349. asp[seg_idx] = -3;
  350. wat_value = wat[seg_idx];
  351. if (wat_value > 0)
  352. wat[seg_idx] = -wat_value;
  353. }
  354. else if (sides == 8 && FLAG_GET(worked, r - 1, c + 1)) {
  355. alt_value = alt[seg_idx];
  356. add_pt(r, c, alt_value);
  357. asp[seg_idx] = -1;
  358. wat_value = wat[seg_idx];
  359. if (wat_value > 0)
  360. wat[seg_idx] = -wat_value;
  361. }
  362. else if (sides == 8 && FLAG_GET(worked, r + 1, c - 1)) {
  363. alt_value = alt[seg_idx];
  364. add_pt(r, c, alt_value);
  365. asp[seg_idx] = -5;
  366. wat_value = wat[seg_idx];
  367. if (wat_value > 0)
  368. wat[seg_idx] = -wat_value;
  369. }
  370. else if (sides == 8 && FLAG_GET(worked, r + 1, c + 1)) {
  371. alt_value = alt[seg_idx];
  372. add_pt(r, c, alt_value);
  373. asp[seg_idx] = -7;
  374. wat_value = wat[seg_idx];
  375. if (wat_value > 0)
  376. wat[seg_idx] = -wat_value;
  377. }
  378. /* real depression ? */
  379. if (pit_flag && asp[seg_idx] == 0) {
  380. if (!Rast_is_c_null_value(&buf[c]) && buf[c] != 0) {
  381. alt_value = alt[seg_idx];
  382. add_pt(r, c, alt_value);
  383. }
  384. }
  385. }
  386. }
  387. }
  388. G_percent(r, nrows, 1); /* finish it */
  389. if (pit_flag) {
  390. Rast_close(fd);
  391. G_free(buf);
  392. }
  393. return 0;
  394. }
  395. int ele_round(double x)
  396. {
  397. int n;
  398. if (x >= 0.0)
  399. n = x + .5;
  400. else {
  401. n = -x + .5;
  402. n = -n;
  403. }
  404. return n;
  405. }