interface.c 6.9 KB

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