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