interface.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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[24];
  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. G_add_keyword(_("terrain"));
  39. G_add_keyword(_("elevation"));
  40. G_add_keyword(_("landform"));
  41. module->label = _("Extracts terrain parameters from a DEM.");
  42. module->description = _("Uses a multi-scale approach"
  43. " by taking fitting quadratic parameters to any size window (via least squares).");
  44. rast_in = G_define_standard_option(G_OPT_R_INPUT);
  45. rast_out = G_define_standard_option(G_OPT_R_OUTPUT);
  46. tol1_val = G_define_option(); /* Request memory for each option. */
  47. tol2_val = G_define_option();
  48. win_size = G_define_option();
  49. parameter = G_define_option();
  50. expon = G_define_option();
  51. vert_sc = G_define_option();
  52. constr = G_define_flag();
  53. /* Each option has a 'key' (short descriptn), a 'description` (longer one) */
  54. /* a 'type' (eg int, or string), and an indication whether manditory or not */
  55. rast_out->description =
  56. _("Name for output raster map containing morphometric parameter");
  57. tol1_val->key = "slope_tolerance";
  58. tol1_val->description =
  59. _("Slope tolerance that defines a 'flat' surface (degrees)");
  60. tol1_val->type = TYPE_DOUBLE;
  61. tol1_val->required = NO;
  62. tol1_val->answer = "1.0";
  63. tol2_val->key = "curvature_tolerance";
  64. tol2_val->description =
  65. _("Curvature tolerance that defines 'planar' surface");
  66. tol2_val->type = TYPE_DOUBLE;
  67. tol2_val->required = NO;
  68. tol2_val->answer = "0.0001";
  69. sprintf(buf, "3-%i", MAX_WSIZE);
  70. win_size->key = "size";
  71. win_size->description = _("Size of processing window (odd number only)");
  72. win_size->type = TYPE_INTEGER;
  73. win_size->required = NO;
  74. win_size->options = G_store(buf);
  75. win_size->answer = "3";
  76. parameter->key = "method";
  77. parameter->description =
  78. _("Morphometric parameter in 'size' window to calculate");
  79. parameter->type = TYPE_STRING;
  80. parameter->required = NO;
  81. parameter->options =
  82. "elev,slope,aspect,profc,planc,longc,crosc,minic,maxic,feature";
  83. parameter->answer = "elev";
  84. expon->key = "exponent";
  85. expon->description = _("Exponent for distance weighting (0.0-4.0)");
  86. expon->type = TYPE_DOUBLE;
  87. expon->required = NO;
  88. expon->answer = "0.0";
  89. vert_sc->key = "zscale";
  90. vert_sc->description = _("Vertical scaling factor");
  91. vert_sc->type = TYPE_DOUBLE;
  92. vert_sc->required = NO;
  93. vert_sc->answer = "1.0";
  94. constr->key = 'c';
  95. constr->description = _("Constrain model through central window cell");
  96. if (G_parser(argc, argv)) /* Actually performs the prompting for */
  97. exit(EXIT_FAILURE); /* keyboard input. */
  98. rast_in_name = rast_in->answer; /* Now keyboard input has been parsed, */
  99. rast_out_name = rast_out->answer; /* can place the contents into strings */
  100. wsize = atoi(win_size->answer);
  101. constrained = constr->answer;
  102. sscanf(expon->answer, "%lf", &exponent);
  103. sscanf(vert_sc->answer, "%lf", &zscale);
  104. sscanf(tol1_val->answer, "%lf", &slope_tol);
  105. sscanf(tol2_val->answer, "%lf", &curve_tol);
  106. if ((exponent < 0.0) || (exponent > 4.0))
  107. exponent = 0.0;
  108. if (zscale == 0.0)
  109. zscale = 1;
  110. if (!strcmp(parameter->answer, "elev"))
  111. mparam = ELEV;
  112. else if (!strcmp(parameter->answer, "slope"))
  113. mparam = SLOPE;
  114. else if (!strcmp(parameter->answer, "aspect"))
  115. mparam = ASPECT;
  116. else if (!strcmp(parameter->answer, "profc"))
  117. mparam = PROFC;
  118. else if (!strcmp(parameter->answer, "planc"))
  119. mparam = PLANC;
  120. else if (!strcmp(parameter->answer, "crosc"))
  121. mparam = CROSC;
  122. else if (!strcmp(parameter->answer, "longc"))
  123. mparam = LONGC;
  124. else if (!strcmp(parameter->answer, "maxic"))
  125. mparam = MAXIC;
  126. else if (!strcmp(parameter->answer, "minic"))
  127. mparam = MINIC;
  128. else if (!strcmp(parameter->answer, "feature"))
  129. mparam = FEATURE;
  130. else {
  131. G_warning(_("Morphometric parameter not recognised. Assuming 'Elevation'"));
  132. mparam = ELEV;
  133. }
  134. /* make sure input and output names are valid */
  135. G_check_input_output_name(rast_in_name, rast_out_name, G_FATAL_EXIT);
  136. if ((wsize / 2 != (wsize - 1) / 2) || (wsize > MAX_WSIZE))
  137. G_fatal_error(_("Inappropriate window size (too big or even)"));
  138. }