input.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. ************************************************************
  3. * MODULE: r.le.pixel/input.c *
  4. * Version 5.0 Nov. 1, 2001 *
  5. * *
  6. * AUTHOR: W.L. Baker, University of Wyoming *
  7. * BAKERWL@UWYO.EDU *
  8. * *
  9. * PURPOSE: To analyze pixel-scale landscape properties *
  10. * input.c uses the GRASS parser to get user input *
  11. * *
  12. * COPYRIGHT: (C) 2001 by W.L. Baker *
  13. * *
  14. * This program is free software under the GNU General *
  15. * Public License(>=v2). Read the file COPYING that comes *
  16. * with GRASS for details *
  17. * *
  18. ************************************************************/
  19. #include <stdlib.h>
  20. #include <grass/config.h>
  21. #include "pixel.h"
  22. extern struct CHOICE *choice;
  23. void user_input(int argc, char **argv)
  24. {
  25. int i;
  26. /* setup the GRASS parsing routine
  27. structures to be used to read
  28. in the user's parameter choices */
  29. struct Flag *units;
  30. struct Flag *zscore;
  31. struct Flag *edgemap;
  32. struct Option *name;
  33. struct Option *sampling_method;
  34. struct Option *region;
  35. struct Option *att;
  36. struct Option *diversity;
  37. struct Option *measure_code;
  38. struct Option *method_code;
  39. struct Option *juxtaposition;
  40. struct Option *edge;
  41. /* use the GRASS parsing routines to read in the user's parameter choices */
  42. edgemap = G_define_flag();
  43. edgemap->key = 'e';
  44. edgemap->description =
  45. "Output map 'edge' of edges given a '1' in r.le.para/edge file";
  46. units = G_define_flag();
  47. units->key = 'u';
  48. units->description =
  49. "Output maps 'units_x' with sampling units for each scale x ";
  50. zscore = G_define_flag();
  51. zscore->key = 'z';
  52. zscore->description = "Output map 'zscores' with standardized scores";
  53. name = G_define_option();
  54. name->key = "map";
  55. name->description = "Raster map to be analyzed";
  56. name->type = TYPE_STRING;
  57. name->gisprompt = "old,cell,raster";
  58. name->required = YES;
  59. sampling_method = G_define_option();
  60. sampling_method->answer = "w";
  61. sampling_method->key = "sam";
  62. sampling_method->description = "Sampling method (choose only 1 method):\n"
  63. "\tw = whole map u = units m = moving window r = regions";
  64. sampling_method->type = TYPE_STRING;
  65. sampling_method->multiple = NO;
  66. sampling_method->required = NO;
  67. region = G_define_option();
  68. region->key = "reg";
  69. region->description =
  70. "Name of regions map, only when sam = r; omit otherwise";
  71. region->type = TYPE_STRING;
  72. region->gisprompt = "old,cell,raster";
  73. region->required = NO;
  74. att = G_define_option();
  75. att->key = "att";
  76. att->description =
  77. "b1 = mn. pixel att. b2 = s.d. pixel att.\n"
  78. "\tb3 = min. pixel att. b4 = max. pixel att.";
  79. att->options = "b1,b2,b3,b4";
  80. att->type = TYPE_STRING;
  81. att->multiple = YES;
  82. att->required = NO;
  83. diversity = G_define_option();
  84. diversity->key = "div";
  85. diversity->description =
  86. "d1 = richness d2 = Shannon d3 = dominance d4 = inv. Simpson";
  87. diversity->options = "d1,d2,d3,d4";
  88. diversity->type = TYPE_STRING;
  89. diversity->multiple = YES;
  90. diversity->required = NO;
  91. method_code = G_define_option();
  92. method_code->key = "te1";
  93. method_code->description = "Texture method (choose only 1 method):\n"
  94. "\tm1 = 2N-H m2 = 2N-45 m3 = 2N-V m4 = 2N-135\n"
  95. "\tm5 = 4N-HV m6 = 4N-DIAG m7 = 8N";
  96. method_code->options = "m1,m2,m3,m4,m5,m6,m7";
  97. method_code->type = TYPE_STRING;
  98. method_code->multiple = NO;
  99. method_code->required = NO;
  100. measure_code = G_define_option();
  101. measure_code->key = "te2";
  102. measure_code->description =
  103. "Texture measures (required if te1 was specified):\n"
  104. "\tt1 = contagion t2 = ang. sec. mom. t3 = inv. diff. mom.\n"
  105. "\tt4 = entropy t5 = contrast";
  106. measure_code->options = "t1,t2,t3,t4,t5";
  107. measure_code->type = TYPE_STRING;
  108. measure_code->multiple = YES;
  109. measure_code->required = NO;
  110. juxtaposition = G_define_option();
  111. juxtaposition->key = "jux";
  112. juxtaposition->description =
  113. "Juxtaposition measures (weight file in r.le.para needed):\n"
  114. "\tj1 = mn. juxtaposition j2 = s.d. juxtaposition";
  115. juxtaposition->options = "j1,j2";
  116. juxtaposition->type = TYPE_STRING;
  117. juxtaposition->multiple = YES;
  118. juxtaposition->required = NO;
  119. edge = G_define_option();
  120. edge->key = "edg";
  121. edge->description =
  122. "e1 = sum of edges e2 = sum of edges by type (need edge file: r.le.para)";
  123. edge->options = "e1,e2";
  124. edge->type = TYPE_STRING;
  125. edge->multiple = YES;
  126. edge->required = NO;
  127. if (G_parser(argc, argv))
  128. exit(EXIT_FAILURE);
  129. /* record the user inputs for map,
  130. sam, run, and out parameters */
  131. strcpy(choice->fn, name->answer);
  132. choice->wrum = sampling_method->answer[0];
  133. /* check for unacceptable values for
  134. input parameters */
  135. if (strcmp(sampling_method->answer, "w") &&
  136. strcmp(sampling_method->answer, "u") &&
  137. strcmp(sampling_method->answer, "m") &&
  138. strcmp(sampling_method->answer, "r")) {
  139. fprintf(stdout, "\n");
  140. fprintf(stdout,
  141. " ***************************************************\n");
  142. fprintf(stdout,
  143. " You input an unacceptable value for parameter sam \n");
  144. fprintf(stdout,
  145. " ***************************************************\n");
  146. exit(EXIT_FAILURE);
  147. }
  148. /* check for multiple values for te1 */
  149. if (method_code->answer)
  150. if (method_code->answers[1]) {
  151. fprintf(stdout, "\n");
  152. fprintf(stdout,
  153. " **********************************************\n");
  154. fprintf(stdout,
  155. " You input multiple values for parameter te1, \n");
  156. fprintf(stdout,
  157. " but only one is allowed \n");
  158. fprintf(stdout,
  159. " **********************************************\n");
  160. exit(EXIT_FAILURE);
  161. }
  162. /* if the -u flag is specified, then
  163. set the choice->units flag to 1 */
  164. choice->units = 0;
  165. if (!strcmp(sampling_method->answer, "u") && units->answer)
  166. choice->units = 1;
  167. else if (strcmp(sampling_method->answer, "u") && units->answer) {
  168. fprintf(stdout, "\n");
  169. fprintf(stdout,
  170. " ***************************************************\n");
  171. fprintf(stdout,
  172. " You requested output of map 'units' with sampling \n");
  173. fprintf(stdout,
  174. " units, by using flag -u, but this option is only \n");
  175. fprintf(stdout,
  176. " available when sam=u \n");
  177. fprintf(stdout,
  178. " ***************************************************\n");
  179. exit(EXIT_FAILURE);
  180. }
  181. /* if sampling_method is by REGION
  182. get region file name. Check to see
  183. that the name was input */
  184. if (!strcmp(sampling_method->answer, "r")) {
  185. if (region->answer)
  186. strcpy(choice->reg, region->answer);
  187. else {
  188. fprintf(stdout, "\n");
  189. fprintf(stdout,
  190. " ***********************************************\n");
  191. fprintf(stdout,
  192. " You requested sampling by region, but did not \n");
  193. fprintf(stdout,
  194. " input the name of the region using the reg= \n");
  195. fprintf(stdout,
  196. " parameter \n");
  197. fprintf(stdout,
  198. " ***********************************************\n");
  199. exit(EXIT_FAILURE);
  200. }
  201. }
  202. if (region->answer)
  203. if (strcmp(sampling_method->answer, "r")) {
  204. fprintf(stdout, "\n");
  205. fprintf(stdout,
  206. " ***********************************************\n");
  207. fprintf(stdout,
  208. " You requested sampling by region, by using \n");
  209. fprintf(stdout,
  210. " the reg= parameter, but did not input the \n");
  211. fprintf(stdout,
  212. " sam=r parameter \n");
  213. fprintf(stdout,
  214. " ***********************************************\n");
  215. exit(EXIT_FAILURE);
  216. }
  217. /* initialize flag arrays in choice
  218. data structure. */
  219. for (i = 0; i < 5; i++)
  220. choice->att[i] = 0;
  221. for (i = 0; i < 5; i++)
  222. choice->div[i] = 0;
  223. for (i = 0; i < 6; i++)
  224. choice->te2[i] = 0;
  225. for (i = 0; i < 3; i++)
  226. choice->jux[i] = 0;
  227. for (i = 0; i < 3; i++)
  228. choice->edg[i] = 0;
  229. /* fill measure_code and method
  230. code arrays */
  231. if (att->answer) {
  232. choice->att[0] = 1;
  233. for (i = 0; att->answers[i] != NULL; i++) {
  234. if (!strcmp(att->answers[i], "b1"))
  235. choice->att[1] = 1;
  236. else if (!strcmp(att->answers[i], "b2"))
  237. choice->att[2] = 1;
  238. else if (!strcmp(att->answers[i], "b3"))
  239. choice->att[3] = 1;
  240. else if (!strcmp(att->answers[i], "b4"))
  241. choice->att[4] = 1;
  242. }
  243. }
  244. if (edgemap->answer && choice->wrum == 'w') {
  245. choice->edgemap = edgemap->answer;
  246. choice->edg[0] = 1;
  247. choice->edg[2] = 1;
  248. }
  249. else if (edgemap->answer && choice->wrum != 'w') {
  250. fprintf(stdout, "\n");
  251. fprintf(stdout,
  252. " ****************************************************\n");
  253. fprintf(stdout,
  254. " An edge map (flag is -e) is not available unless \n");
  255. fprintf(stdout,
  256. " sam=w \n");
  257. fprintf(stdout,
  258. " ****************************************************\n");
  259. exit(EXIT_FAILURE);
  260. }
  261. if (zscore->answer && choice->wrum == 'w') {
  262. choice->z = zscore->answer;
  263. choice->att[0] = 1;
  264. choice->att[1] = 1;
  265. choice->att[2] = 1;
  266. }
  267. else if (zscore->answer && choice->wrum != 'w') {
  268. fprintf(stdout, "\n");
  269. fprintf(stdout,
  270. " ****************************************************\n");
  271. fprintf(stdout,
  272. " A zscores map (flag is -z) is not available unless \n");
  273. fprintf(stdout,
  274. " sam=w \n");
  275. fprintf(stdout,
  276. " ****************************************************\n");
  277. exit(EXIT_FAILURE);
  278. }
  279. if (diversity->answer) {
  280. choice->div[0] = 1;
  281. for (i = 0; diversity->answers[i] != NULL; i++) {
  282. if (!strcmp(diversity->answers[i], "d1"))
  283. choice->div[1] = 1;
  284. else if (!strcmp(diversity->answers[i], "d2"))
  285. choice->div[2] = 1;
  286. else if (!strcmp(diversity->answers[i], "d3"))
  287. choice->div[3] = 1;
  288. else if (!strcmp(diversity->answers[i], "d4"))
  289. choice->div[4] = 1;
  290. }
  291. }
  292. choice->tex = 0;
  293. if (measure_code->answer || method_code->answer) {
  294. if (measure_code->answer && method_code->answer) {
  295. choice->te2[0] = 1;
  296. for (i = 0; measure_code->answers[i] != NULL; i++) {
  297. if (!strcmp(measure_code->answers[i], "t1"))
  298. choice->te2[1] = 1;
  299. else if (!strcmp(measure_code->answers[i], "t2"))
  300. choice->te2[2] = 1;
  301. else if (!strcmp(measure_code->answers[i], "t3"))
  302. choice->te2[3] = 1;
  303. else if (!strcmp(measure_code->answers[i], "t4"))
  304. choice->te2[4] = 1;
  305. else if (!strcmp(measure_code->answers[i], "t5"))
  306. choice->te2[5] = 1;
  307. }
  308. if (!strcmp(method_code->answer, "m1"))
  309. choice->tex = 1;
  310. else if (!strcmp(method_code->answer, "m2"))
  311. choice->tex = 2;
  312. else if (!strcmp(method_code->answer, "m3"))
  313. choice->tex = 3;
  314. else if (!strcmp(method_code->answer, "m4"))
  315. choice->tex = 4;
  316. else if (!strcmp(method_code->answer, "m5"))
  317. choice->tex = 5;
  318. else if (!strcmp(method_code->answer, "m6"))
  319. choice->tex = 6;
  320. else if (!strcmp(method_code->answer, "m7"))
  321. choice->tex = 7;
  322. }
  323. else {
  324. fprintf(stdout, "\n");
  325. fprintf(stdout,
  326. " ************************************************\n");
  327. fprintf(stdout,
  328. " You requested texture measurement, but did not \n");
  329. fprintf(stdout,
  330. " input both parameter te1 and te2 \n");
  331. fprintf(stdout,
  332. " ************************************************\n");
  333. exit(EXIT_FAILURE);
  334. }
  335. }
  336. if (juxtaposition->answer) {
  337. choice->jux[0] = 1;
  338. for (i = 0; juxtaposition->answers[i] != NULL; i++) {
  339. if (!strcmp(juxtaposition->answers[i], "j1"))
  340. choice->jux[1] = 1;
  341. else if (!strcmp(juxtaposition->answers[i], "j2"))
  342. choice->jux[2] = 1;
  343. }
  344. }
  345. if (edge->answer) {
  346. choice->edg[0] = 1;
  347. for (i = 0; edge->answers[i] != NULL; i++) {
  348. if (!strcmp(edge->answers[i], "e1"))
  349. choice->edg[1] = 1;
  350. else if (!strcmp(edge->answers[i], "e2"))
  351. choice->edg[2] = 1;
  352. }
  353. }
  354. if (!att->answer && !diversity->answer && !measure_code->answer &&
  355. !juxtaposition->answer && !edge->answer && !zscore->answer &&
  356. !edgemap->answer) {
  357. fprintf(stdout, "\n");
  358. fprintf(stdout,
  359. " **************************************************\n");
  360. fprintf(stdout,
  361. " You did not select any measures to be calculated \n");
  362. fprintf(stdout,
  363. " **************************************************\n");
  364. exit(EXIT_FAILURE);
  365. }
  366. return;
  367. }