interface.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*********************************************************************************/
  2. /*** interface() ***/
  3. /*** Function to get input from user and check files can be opened ***/
  4. /*** ***/
  5. /*** Jo Wood, Department of Geography, V1.2, 7th February 1992 ***/
  6. /*********************************************************************************/
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <grass/gis.h>
  10. #include <grass/glocale.h>
  11. #include "param.h"
  12. void interface(int argc, char **argv)
  13. {
  14. /*--------------------------------------------------------------------------*/
  15. /* INITIALISE */
  16. /*--------------------------------------------------------------------------*/
  17. struct Option *rast_in, /* Name of input file from command line. */
  18. *rast_out, /* Holds name of output file. */
  19. *tol1_val, /* Tolerance values for feature */
  20. *tol2_val, /* detection (slope and curvature). */
  21. *win_size, /* Size of side of local window. */
  22. *parameter, /* Morphometric parameter to calculate. */
  23. *expon, /* Inverse distance exponent for weight. */
  24. *vert_sc; /* Vertical scaling factor. */
  25. struct Flag *constr; /* Forces quadratic through the central */
  26. /* cell of local window if selected. */
  27. struct GModule *module; /* GRASS module description */
  28. char buf[4];
  29. G_gisinit(argv[0]); /* GRASS function which MUST be called */
  30. /* first to check for valid database */
  31. /* and mapset and prompt user for input. */
  32. /*--------------------------------------------------------------------------*/
  33. /* SET PARSER OPTIONS */
  34. /*--------------------------------------------------------------------------*/
  35. module = G_define_module();
  36. G_add_keyword(_("raster"));
  37. G_add_keyword(_("geomorphology"));
  38. module->label = _("Extracts terrain parameters from a DEM.");
  39. module->description = _("Uses a multi-scale approach"
  40. " by taking fitting quadratic parameters to any size window (via least squares).");
  41. rast_in = G_define_standard_option(G_OPT_R_INPUT);
  42. rast_out = G_define_standard_option(G_OPT_R_OUTPUT);
  43. tol1_val = G_define_option(); /* Request memory for each option. */
  44. tol2_val = G_define_option();
  45. win_size = G_define_option();
  46. parameter = G_define_option();
  47. expon = G_define_option();
  48. vert_sc = G_define_option();
  49. constr = G_define_flag();
  50. /* Each option has a 'key' (short descriptn), a 'description` (longer one) */
  51. /* a 'type' (eg int, or string), and an indication whether manditory or not */
  52. rast_out->description =
  53. _("Output raster layer containing morphometric parameter");
  54. tol1_val->key = "s_tol";
  55. tol1_val->description =
  56. _("Slope tolerance that defines a 'flat' surface (degrees)");
  57. tol1_val->type = TYPE_DOUBLE;
  58. tol1_val->required = NO;
  59. tol1_val->answer = "1.0";
  60. tol2_val->key = "c_tol";
  61. tol2_val->description =
  62. _("Curvature tolerance that defines 'planar' surface");
  63. tol2_val->type = TYPE_DOUBLE;
  64. tol2_val->required = NO;
  65. tol2_val->answer = "0.0001";
  66. sprintf(buf, "3-%i", MAX_WSIZE);
  67. win_size->key = "size";
  68. win_size->description = _("Size of processing window (odd number only)");
  69. win_size->type = TYPE_INTEGER;
  70. win_size->required = NO;
  71. win_size->options = G_store(buf);
  72. win_size->answer = "3";
  73. parameter->key = "param";
  74. parameter->description =
  75. _("Morphometric parameter in 'size' window to calculate");
  76. parameter->type = TYPE_STRING;
  77. parameter->required = NO;
  78. parameter->options =
  79. "elev,slope,aspect,profc,planc,longc,crosc,minic,maxic,feature";
  80. parameter->answer = "elev";
  81. expon->key = "exp";
  82. expon->description = _("Exponent for distance weighting (0.0-4.0)");
  83. expon->type = TYPE_DOUBLE;
  84. expon->required = NO;
  85. expon->answer = "0.0";
  86. vert_sc->key = "zscale";
  87. vert_sc->description = _("Vertical scaling factor");
  88. vert_sc->type = TYPE_DOUBLE;
  89. vert_sc->required = NO;
  90. vert_sc->answer = "1.0";
  91. constr->key = 'c';
  92. constr->description = _("Constrain model through central window cell");
  93. if (G_parser(argc, argv)) /* Actually performs the prompting for */
  94. exit(EXIT_FAILURE); /* keyboard input. */
  95. rast_in_name = rast_in->answer; /* Now keyboard input has been parsed, */
  96. rast_out_name = rast_out->answer; /* can place the contents into strings */
  97. wsize = atoi(win_size->answer);
  98. constrained = constr->answer;
  99. sscanf(expon->answer, "%lf", &exponent);
  100. sscanf(vert_sc->answer, "%lf", &zscale);
  101. sscanf(tol1_val->answer, "%lf", &slope_tol);
  102. sscanf(tol2_val->answer, "%lf", &curve_tol);
  103. if ((exponent < 0.0) || (exponent > 4.0))
  104. exponent = 0.0;
  105. if (zscale == 0.0)
  106. zscale = 1;
  107. if (!strcmp(parameter->answer, "elev"))
  108. mparam = ELEV;
  109. else if (!strcmp(parameter->answer, "slope"))
  110. mparam = SLOPE;
  111. else if (!strcmp(parameter->answer, "aspect"))
  112. mparam = ASPECT;
  113. else if (!strcmp(parameter->answer, "profc"))
  114. mparam = PROFC;
  115. else if (!strcmp(parameter->answer, "planc"))
  116. mparam = PLANC;
  117. else if (!strcmp(parameter->answer, "crosc"))
  118. mparam = CROSC;
  119. else if (!strcmp(parameter->answer, "longc"))
  120. mparam = LONGC;
  121. else if (!strcmp(parameter->answer, "maxic"))
  122. mparam = MAXIC;
  123. else if (!strcmp(parameter->answer, "minic"))
  124. mparam = MINIC;
  125. else if (!strcmp(parameter->answer, "feature"))
  126. mparam = FEATURE;
  127. else {
  128. G_warning(_("Morphometric parameter not recognised. Assuming 'Elevation'"));
  129. mparam = ELEV;
  130. }
  131. /* make sure input and output names are valid */
  132. G_check_input_output_name(rast_in_name, rast_out_name, G_FATAL_EXIT);
  133. if ((wsize / 2 != (wsize - 1) / 2) || (wsize > MAX_WSIZE))
  134. G_fatal_error(_("Inappropriate window size (too big or even)"));
  135. }