com_line.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/glocale.h>
  4. #include "watershed.h"
  5. #include "string.h"
  6. /* make sure any useful info is transfered to the man page before ripping out the interactive help messages */
  7. /* in addition there seem to be some useful user options here which are not currently available from the main parser */
  8. int com_line_Gwater(INPUT * input, OUTPUT * output)
  9. {
  10. struct Cell_head *window;
  11. char map_layer[48], buf[100], *prog_name, *mapset;
  12. double d;
  13. int i;
  14. window = &(output->window);
  15. if (0 == G_yes("Continue?", 1))
  16. exit(EXIT_SUCCESS);
  17. input->haf_name = (char *)G_calloc(40, sizeof(char));
  18. input->accum_name = (char *)G_calloc(40, sizeof(char));
  19. G_message(_("\nThis set of questions will organize the command line for the"));
  20. G_message(_("%s program to run properly for your application."),
  21. NON_NAME);
  22. G_message(_("The first question is whether you want %s to run"),
  23. NON_NAME);
  24. G_message(_("in its fast mode or its slow mode. If you run %s"),
  25. NON_NAME);
  26. G_message(_("in the fast mode, the computer will finish about 10 times faster"));
  27. G_message(_("than in the slow mode, but will not allow other programs to run"));
  28. G_message(_("at the same time. The fast mode also places all of the data into"));
  29. G_message(_("RAM, which limits the size of window that can be run. The slow"));
  30. G_message(_("mode uses disk space in the same hard disk partition as where GRASS is"));
  31. G_message(_("stored. Thus, if the program does not work in the slow mode, you will"));
  32. G_message(_("need to remove unnecessary files from that partition. The slow mode"));
  33. G_message(_("will allow other processes to run concurrently with %s.\n"),
  34. NON_NAME);
  35. sprintf(buf, "Do you want to use the fast mode of %s?", NON_NAME);
  36. input->com_line_ram = input->com_line_seg = NULL;
  37. input->fast = 0;
  38. input->slow = 0;
  39. if (G_yes(buf, 1)) {
  40. input->fast = 1;
  41. input->com_line_ram = (char *)G_calloc(400, sizeof(char));
  42. prog_name = G_store(RAM_NAME);
  43. sprintf(input->com_line_ram,
  44. "\"%s/etc/water/%s\"", G_gisbase(), RAM_NAME);
  45. fprintf(stderr,
  46. "\nIf there is not enough ram for the fast mode (%s) to run,\n",
  47. RAM_NAME);
  48. sprintf(buf, "should the slow mode (%s) be run instead?", SEG_NAME);
  49. if (G_yes(buf, 1)) {
  50. input->slow = 1;
  51. input->com_line_seg = (char *)G_calloc(400, sizeof(char));
  52. sprintf(input->com_line_seg,
  53. "\"%s/etc/water/%s\"", G_gisbase(), SEG_NAME);
  54. }
  55. }
  56. else {
  57. input->slow = 1;
  58. prog_name = G_store(SEG_NAME);
  59. input->com_line_seg = (char *)G_calloc(400, sizeof(char));
  60. sprintf(input->com_line_seg,
  61. "\"%s/etc/water/%s\"", G_gisbase(), SEG_NAME);
  62. }
  63. G_message(_("\nIf you hit <return> by itself for the next question, this"));
  64. G_message(_("program will terminate."));
  65. mapset = G_ask_old("What is the name of the elevation map layer?",
  66. map_layer, "cell", "cell");
  67. if (!mapset)
  68. exit(EXIT_FAILURE);
  69. if (input->fast)
  70. com_line_add(&(input->com_line_ram), " el=", map_layer, mapset);
  71. if (input->slow)
  72. com_line_add(&(input->com_line_seg), " el=", map_layer, mapset);
  73. G_message(_("\nOne of the options for %s is a `depression map'. A"),
  74. prog_name);
  75. G_message(_("depression map indicates all the locations in the current map window where"));
  76. G_message(_("water accumulates and does not leave by the edge of the map. Lakes without"));
  77. G_message(_("outlet streams and sinkholes are examples of `depressions'. If you wish to"));
  78. G_message(_("have a depression map, prepare a map where non-zero values indicate the"));
  79. G_message(_("locations where depressions occur.\n"));
  80. G_message(_("Hit <return> by itself for the next question if there is no depression map."));
  81. mapset = G_ask_old("What is the name of the depression map layer?",
  82. map_layer, "cell", "cell");
  83. if (mapset) {
  84. if (input->fast)
  85. com_line_add(&(input->com_line_ram), " de=", map_layer, mapset);
  86. if (input->slow)
  87. com_line_add(&(input->com_line_seg), " de=", map_layer, mapset);
  88. }
  89. G_message(_("\nThe %s program will divide the elevation map into a number of"),
  90. prog_name);
  91. G_message(_("watershed basins. The number of watershed basins is indirectly determined"));
  92. G_message(_("by the `basin threshold' value. The basin threshold is the area necessary for"));
  93. G_message(_("%s to define a unique watershed basin. This area only applies to"),
  94. prog_name);
  95. G_message(_("`exterior drainage basins'. An exterior drainage basin does not have any"));
  96. G_message(_("drainage basins flowing into it. Interior drainage basin size is determined"));
  97. G_message(_("by the surface flow going into stream segments between stream interceptions."));
  98. G_message(_("Thus interior drainage basins can be of any size. The %s program"),
  99. prog_name);
  100. G_message(_("also allows the user to relate basin size to potential overland flow"));
  101. G_message(_("(i.e., areas with low infiltration capacities will need smaller areas to"));
  102. G_message(_("develop stream channels than neighboring areas with high infiltration rates)."));
  103. G_message(_("The user can create a map layer with potential overland flow values, and"));
  104. G_message(_("%s will accumulate those values instead of area.\n"),
  105. prog_name);
  106. G_message(_("What unit of measure will you use for the basin threshold:"));
  107. do {
  108. G_message(_(" 1) acres, 2) meters sq., 3) miles sq., 4) hectares,"));
  109. G_message(_(" 5) kilometers sq., 6) map cells, 7) overland flow units"));
  110. fprintf(stderr, _("Choose 1-7 or 0 to exit this program: "));
  111. G_gets(map_layer);
  112. sscanf(map_layer, "%d", &i);
  113. } while (i > 7 || i < 0);
  114. if (!i)
  115. exit(EXIT_SUCCESS);
  116. output->type_area = (char)i;
  117. G_message(_("\nHow large an area (or how many overland flow units) must a drainage basin"));
  118. fprintf(stderr, _("be for it to be an exterior drainage basin: "));
  119. G_gets(map_layer);
  120. sscanf(map_layer, "%lf", &d);
  121. switch (i) {
  122. case 1:
  123. if (input->fast)
  124. basin_com_add(&(input->com_line_ram), d, ACRE_TO_METERSQ, window);
  125. if (input->slow)
  126. basin_com_add(&(input->com_line_seg), d, ACRE_TO_METERSQ, window);
  127. break;
  128. case 2:
  129. if (input->fast)
  130. basin_com_add(&(input->com_line_ram), d, 1.0, window);
  131. if (input->slow)
  132. basin_com_add(&(input->com_line_seg), d, 1.0, window);
  133. break;
  134. case 3:
  135. if (input->fast)
  136. basin_com_add(&(input->com_line_ram), d, MILESQ_TO_METERSQ,
  137. window);
  138. if (input->slow)
  139. basin_com_add(&(input->com_line_seg), d, MILESQ_TO_METERSQ,
  140. window);
  141. break;
  142. case 4:
  143. if (input->fast)
  144. basin_com_add(&(input->com_line_ram), d, HECTACRE_TO_METERSQ,
  145. window);
  146. if (input->slow)
  147. basin_com_add(&(input->com_line_seg), d, HECTACRE_TO_METERSQ,
  148. window);
  149. break;
  150. case 5:
  151. if (input->fast)
  152. basin_com_add(&(input->com_line_ram), d, KILOSQ_TO_METERSQ,
  153. window);
  154. if (input->slow)
  155. basin_com_add(&(input->com_line_seg), d, KILOSQ_TO_METERSQ,
  156. window);
  157. break;
  158. case 6:
  159. if (input->fast)
  160. basin_com_add(&(input->com_line_ram), d,
  161. (window->ns_res * window->ew_res), window);
  162. if (input->slow)
  163. basin_com_add(&(input->com_line_seg), d,
  164. (window->ns_res * window->ew_res), window);
  165. break;
  166. case 7: /* needs an overland flow map */
  167. G_message(_("\nIf you hit <return> by itself for the next question, this"));
  168. G_message(_("program will terminate."));
  169. mapset = G_ask_old("What is the name of the overland flow map layer?",
  170. map_layer, "cell", "cell");
  171. if (!mapset)
  172. exit(EXIT_FAILURE);
  173. if (input->fast) {
  174. com_line_add(&(input->com_line_ram), " ov=", map_layer, mapset);
  175. basin_com_add(&(input->com_line_ram), d,
  176. (window->ns_res * window->ew_res), window);
  177. }
  178. if (input->slow) {
  179. com_line_add(&(input->com_line_seg), " ov=", map_layer, mapset);
  180. basin_com_add(&(input->com_line_seg), d,
  181. (window->ns_res * window->ew_res), window);
  182. }
  183. break;
  184. }
  185. G_message(_("\n%s must create a map layer of watershed basins"),
  186. prog_name);
  187. G_message(_("before %s can run properly."), G_program_name());
  188. strcpy(buf, "Please name the output watershed basin map:");
  189. do {
  190. mapset = G_ask_new(buf, input->haf_name, "cell", "");
  191. } while (NULL == mapset);
  192. if (input->fast)
  193. com_line_add(&(input->com_line_ram), " ba=", input->haf_name, NULL);
  194. if (input->slow)
  195. com_line_add(&(input->com_line_seg), " ba=", input->haf_name, NULL);
  196. /*
  197. This section queries the user about the armsed file input. If
  198. you want to make this an option, the code below "COMMENT2" needs to be
  199. modified.
  200. */
  201. #ifdef ARMSED
  202. G_message(_("\n%s must create a file of watershed basin relationships"),
  203. prog_name);
  204. G_message(_("before %s can run properly."), G_program_name());
  205. input->ar_file_name = NULL;
  206. while (input->ar_file_name == NULL) {
  207. fprintf(stderr, _("\nPlease name this file:"));
  208. G_gets(char_input);
  209. if (1 != G_legal_filename(char_input)) {
  210. G_message(_("<%s> is an illegal file name"), char_input);
  211. }
  212. else
  213. input->ar_file_name = G_store(char_input);
  214. }
  215. if (input->fast)
  216. com_line_add(&(input->com_line_ram), " ar=", input->ar_file_name,
  217. NULL);
  218. if (input->slow)
  219. com_line_add(&(input->com_line_seg), " ar=", input->ar_file_name,
  220. NULL);
  221. /*
  222. end of ARMSED comment code
  223. */
  224. /*
  225. COMMENT2 This section of code tells the program where to place the statistics
  226. about the watershed basin. GRASS users don't need this (w/ r.stats), but the
  227. format is suppossed to be "user-friendly" to hydrologists. For the stats to be
  228. created, the armsed file output needs to exist. For the stats to be an option
  229. in this program: 1) it should be querried before the armsed file query, and 2)
  230. make the armsed file query manditory if this option is invoked.
  231. */
  232. G_message(_("\n%s will generate a lot of output. Indicate a file"),
  233. G_program_name());
  234. G_message(_("name for %s to send the output to."), G_program_name());
  235. output->file_name = NULL;
  236. while (output->file_name == NULL) {
  237. fprintf(stderr, _("\nPlease name this file:"));
  238. G_gets(char_input);
  239. if (1 != G_legal_filename(char_input)) {
  240. G_message(_("<%s> is an illegal file name"), char_input);
  241. }
  242. else
  243. output->file_name = G_store(char_input);
  244. }
  245. /*
  246. end of COMMENT2
  247. */
  248. #endif
  249. G_message(_("\nThe accumulation map from %s must be present for"),
  250. prog_name);
  251. G_message(_("%s to work properly."), G_program_name());
  252. strcpy(buf, "Please name the accumulation map:");
  253. do {
  254. mapset = G_ask_new(buf, input->accum_name, "cell", "");
  255. } while (NULL == mapset);
  256. if (input->fast)
  257. com_line_add(&(input->com_line_ram), " ac=", input->accum_name, NULL);
  258. if (input->slow)
  259. com_line_add(&(input->com_line_seg), " ac=", input->accum_name, NULL);
  260. G_message(_("\n%s can produce several maps not necessary for"),
  261. prog_name);
  262. G_message(_("%s to function (stream channels, overland flow aspect, and"),
  263. G_program_name());
  264. G_message(_("a display version of the accumulation map). %s also has the"),
  265. prog_name);
  266. G_message(_("ability to generate several variables in the Revised Universal Soil Loss"));
  267. G_message(_("Equation (Rusle): Slope Length (LS), and Slope Steepness (S).\n"));
  268. sprintf(buf, "Would you like any of these maps to be created?");
  269. if (G_yes(buf, 1)) {
  270. mapset = G_ask_new("", map_layer, "cell", "stream channel");
  271. if (mapset != NULL) {
  272. if (input->fast)
  273. com_line_add(&(input->com_line_ram), " se=", map_layer, NULL);
  274. if (input->slow)
  275. com_line_add(&(input->com_line_seg), " se=", map_layer, NULL);
  276. }
  277. mapset = G_ask_new("", map_layer, "cell", "half basin");
  278. if (mapset != NULL) {
  279. if (input->fast)
  280. com_line_add(&(input->com_line_ram), " ha=", map_layer, NULL);
  281. if (input->slow)
  282. com_line_add(&(input->com_line_seg), " ha=", map_layer, NULL);
  283. }
  284. mapset = G_ask_new("", map_layer, "cell", "overland aspect");
  285. if (mapset != NULL) {
  286. if (input->fast)
  287. com_line_add(&(input->com_line_ram), " dr=", map_layer, NULL);
  288. if (input->slow)
  289. com_line_add(&(input->com_line_seg), " dr=", map_layer, NULL);
  290. }
  291. mapset = G_ask_new("", map_layer, "cell", "display");
  292. if (mapset != NULL) {
  293. if (input->fast)
  294. com_line_add(&(input->com_line_ram), " di=", map_layer, NULL);
  295. if (input->slow)
  296. com_line_add(&(input->com_line_seg), " di=", map_layer, NULL);
  297. }
  298. i = 0;
  299. mapset = G_ask_new("", map_layer, "cell", "Slope Length");
  300. if (mapset != NULL) {
  301. i = 1;
  302. if (input->fast)
  303. com_line_add(&(input->com_line_ram), " LS=", map_layer, NULL);
  304. if (input->slow)
  305. com_line_add(&(input->com_line_seg), " LS=", map_layer, NULL);
  306. }
  307. mapset = G_ask_new("", map_layer, "cell", "Slope Steepness");
  308. if (mapset != NULL) {
  309. i = 1;
  310. if (input->fast)
  311. com_line_add(&(input->com_line_ram), " S=", map_layer, NULL);
  312. if (input->slow)
  313. com_line_add(&(input->com_line_seg), " S=", map_layer, NULL);
  314. }
  315. if (i) {
  316. G_message(_("\nThe Slope Length factor (LS) and Slope Steepness (S) are influenced by"));
  317. G_message(_("disturbed land. %s reflects this with an optional map layer or value"),
  318. prog_name);
  319. G_message(_("where the value indicates the percent of disturbed (barren) land in that cell."));
  320. G_message(_("Type <return> if you do not have a disturbed land map layer."));
  321. mapset = G_ask_old("", map_layer, "cell", "disturbed land");
  322. if (mapset != NULL) {
  323. if (input->fast)
  324. com_line_add(&(input->com_line_ram), " r=", map_layer,
  325. NULL);
  326. if (input->slow)
  327. com_line_add(&(input->com_line_seg), " r=", map_layer,
  328. NULL);
  329. }
  330. else {
  331. G_message(_("\nType the value indicating the percent of disturbed land. This value will"));
  332. G_message(_("be used for every cell in the current region."));
  333. i = -6;
  334. while (i < 0 || i > 100) {
  335. fprintf(stderr, _("\nInput value here [0-100]: "));
  336. fgets(buf, 80, stdin);
  337. sscanf(buf, "%d", &i);
  338. }
  339. if (input->fast)
  340. com_add(&(input->com_line_ram), " r=", i);
  341. if (input->slow)
  342. com_add(&(input->com_line_seg), " r=", i);
  343. }
  344. /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
  345. G_message(_("\nOverland surface flow only occurs for a set distance before swales form."));
  346. G_message(_("Because of digital terrain model limitations, %s cannot pick up"),
  347. prog_name);
  348. G_message(_("these swales. %s allows for an input (warning: kludge factor)"),
  349. prog_name);
  350. G_message(_("that prevents the surface flow distance from getting too long. Normally,"));
  351. G_message(_("maximum slope length is around 600 feet (about 183 meters)."));
  352. i = -1;
  353. while (i < 0) {
  354. fprintf(stdout,
  355. "\nInput maximum slope length here (in meters): ");
  356. fgets(buf, 80, stdin);
  357. sscanf(buf, "%d", &i);
  358. }
  359. if (input->fast)
  360. com_add(&(input->com_line_ram), " ms=", i);
  361. if (input->slow)
  362. com_add(&(input->com_line_seg), " ms=", i);
  363. /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */
  364. G_message(_("\nRoads, ditches, changes in ground cover, and other factors will stop"));
  365. G_message(_("slope length. You may input a raster map indicating the locations of these"));
  366. G_message(_("blocking factors.\n"));
  367. G_message(_("Hit <return> by itself for the next question if there is no blocking map."));
  368. mapset = G_ask_old("What is the name of the blocking map layer?",
  369. map_layer, "cell", "cell");
  370. if (mapset) {
  371. if (input->fast)
  372. com_line_add(&(input->com_line_ram), " ob=", map_layer,
  373. mapset);
  374. if (input->slow)
  375. com_line_add(&(input->com_line_seg), " ob=", map_layer,
  376. mapset);
  377. }
  378. }
  379. }
  380. return 0;
  381. }
  382. int com_line_add(char **com_line, char *prompt, char *map_layer, char *mapset)
  383. {
  384. strcat(*com_line, prompt);
  385. strcat(*com_line, "\"");
  386. strcat(*com_line, map_layer);
  387. if (mapset) {
  388. strcat(*com_line, "@");
  389. strcat(*com_line, mapset);
  390. }
  391. strcat(*com_line, "\"");
  392. return 0;
  393. }
  394. int basin_com_add(char **com_line, double d, double modifier,
  395. struct Cell_head *window)
  396. {
  397. int i;
  398. char buf[20];
  399. i = (int)(.5 + modifier * d / window->ns_res / window->ew_res);
  400. if (i < 1)
  401. i = 1;
  402. sprintf(buf, " t=%d", i);
  403. strcat(*com_line, buf);
  404. return 0;
  405. }
  406. int com_add(char **com_line, char *prompt, int ril_value)
  407. {
  408. char buf[20];
  409. strcat(*com_line, prompt);
  410. sprintf(buf, "%d", ril_value);
  411. strcat(*com_line, buf);
  412. return 0;
  413. }