main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * r.topmodel: simulates TOPMODEL based on TMOD9502.FOR.
  3. *
  4. * TMOD9502.FOR Author: Keith Beven <k.beven@lancaster.ac.uk>
  5. * http://www.es.lancs.ac.uk/hfdg/topmodel.html
  6. *
  7. * Copyright (C) 2000, 2010 by the GRASS Development Team
  8. * Author: Huidae Cho <grass4u@gmail.com>
  9. * Hydro Laboratory, Kyungpook National University
  10. * South Korea
  11. *
  12. * This program is free software under the GPL (>=v2)
  13. * Read the file COPYING coming with GRASS for details.
  14. *
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <grass/glocale.h>
  19. #include "global.h"
  20. struct idxstats idxstats;
  21. struct params params;
  22. struct input input;
  23. struct map map;
  24. struct file file;
  25. struct misc misc;
  26. struct flg flg;
  27. const char *mapset;
  28. int main(int argc, char **argv)
  29. {
  30. struct GModule *module;
  31. struct
  32. {
  33. struct Option *basin;
  34. struct Option *elev;
  35. struct Option *fill;
  36. struct Option *dir;
  37. struct Option *belev;
  38. struct Option *topidx;
  39. struct Option *nidxclass;
  40. struct Option *idxstats;
  41. struct Option *params;
  42. struct Option *input;
  43. struct Option *output;
  44. struct Option *Qobs;
  45. struct Option *timestep;
  46. struct Option *idxclass;
  47. } param;
  48. struct
  49. {
  50. struct Flag *input;
  51. } flag;
  52. /* Initialize GRASS and parse command line */
  53. G_gisinit(argv[0]);
  54. module = G_define_module();
  55. G_add_keyword(_("raster"));
  56. G_add_keyword(_("hydrology"));
  57. module->description =
  58. _("Simulates TOPMODEL which is a physically based hydrologic model.");
  59. /* Parameter definitions */
  60. param.elev = G_define_standard_option(G_OPT_R_ELEV);
  61. param.elev->required = NO;
  62. param.elev->guisection = _("Input");
  63. param.basin = G_define_standard_option(G_OPT_R_INPUT);
  64. param.basin->key = "basin";
  65. param.basin->label =
  66. _("Name of input basin raster map");
  67. param.basin->description = _("Created by r.water.outlet (MASK)");
  68. param.basin->required = NO;
  69. param.basin->guisection = _("Input");
  70. param.fill = G_define_standard_option(G_OPT_R_OUTPUT);
  71. param.fill->key = "depressionless";
  72. param.fill->description = _("Name for output depressionless elevation raster map");
  73. param.fill->required = NO;
  74. param.fill->guisection = _("Output");
  75. param.dir = G_define_standard_option(G_OPT_R_OUTPUT);
  76. param.dir->key = "direction";
  77. param.dir->description =
  78. _("Name for output flow direction map for depressionless elevation raster map");
  79. param.dir->required = NO;
  80. param.dir->guisection = _("Output");
  81. param.belev = G_define_standard_option(G_OPT_R_OUTPUT);
  82. param.belev->key = "basin_elevation";
  83. param.belev->label = _("Name for output basin elevation raster map (o/i)");
  84. param.belev->description = _("MASK applied");
  85. param.belev->required = NO;
  86. param.belev->guisection = _("Output");
  87. param.topidx = G_define_standard_option(G_OPT_R_OUTPUT);
  88. param.topidx->key = "topidx";
  89. param.topidx->label =
  90. _("Name for output topographic index ln(a/tanB) raster map");
  91. param.topidx->description = _("MASK applied");
  92. param.topidx->required = NO;
  93. param.topidx->guisection = _("Output");
  94. param.nidxclass = G_define_option();
  95. param.nidxclass->key = "nidxclass";
  96. param.nidxclass->description =
  97. _("Number of topographic index classes");
  98. param.nidxclass->type = TYPE_INTEGER;
  99. param.nidxclass->required = NO;
  100. param.nidxclass->answer = "30";
  101. param.nidxclass->guisection = _("Parameters");
  102. param.idxstats = G_define_standard_option(G_OPT_F_INPUT);
  103. param.idxstats->key = "idxstats";
  104. param.idxstats->description =
  105. _("Name of topographic index statistics file (o/i)");
  106. param.params = G_define_standard_option(G_OPT_F_INPUT);
  107. param.params->key = "parameters";
  108. param.params->description = _("Name of TOPMODEL parameters file");
  109. param.input = G_define_standard_option(G_OPT_F_INPUT);
  110. param.input->description =
  111. _("Name of rainfall and potential evapotranspiration data file");
  112. param.output = G_define_standard_option(G_OPT_F_OUTPUT);
  113. param.output->description = _("Name for output file");
  114. param.Qobs = G_define_standard_option(G_OPT_F_OUTPUT);
  115. param.Qobs->key = "qobs";
  116. param.Qobs->description = _("Name for observed flow file");
  117. param.Qobs->required = NO;
  118. param.Qobs->guisection = _("Output");
  119. param.timestep = G_define_option();
  120. param.timestep->key = "timestep";
  121. param.timestep->description =
  122. _("Time step");
  123. param.timestep->type = TYPE_INTEGER;
  124. param.timestep->required = NO;
  125. param.timestep->guisection = _("Parameters");
  126. param.idxclass = G_define_option();
  127. param.idxclass->key = "idxclass";
  128. param.idxclass->description =
  129. _("Topographic index class");
  130. param.idxclass->type = TYPE_INTEGER;
  131. param.idxclass->required = NO;
  132. param.idxclass->guisection = _("Parameters");
  133. /* Flag definitions */
  134. flag.input = G_define_flag();
  135. flag.input->key = 'i';
  136. flag.input->description = _("Input data given for (o/i)");
  137. if (G_parser(argc, argv))
  138. exit(EXIT_FAILURE);
  139. /* Store given parameters and flags */
  140. map.basin = param.basin->answer;
  141. map.elev = param.elev->answer;
  142. map.belev = param.belev->answer;
  143. map.fill = param.fill->answer;
  144. map.dir = param.dir->answer;
  145. map.topidx = param.topidx->answer;
  146. file.idxstats = param.idxstats->answer;
  147. file.params = param.params->answer;
  148. file.input = param.input->answer;
  149. file.output = param.output->answer;
  150. file.Qobs = param.Qobs->answer;
  151. misc.nidxclass = atoi(param.nidxclass->answer);
  152. if (!param.timestep->answer)
  153. param.timestep->answer = "0";
  154. if (!param.idxclass->answer)
  155. param.idxclass->answer = "0";
  156. misc.timestep = atoi(param.timestep->answer);
  157. misc.idxclass = atoi(param.idxclass->answer);
  158. flg.input = flag.input->answer;
  159. flg.overwr = module->overwrite;
  160. mapset = G_mapset();
  161. /* Check run conditions */
  162. if (check_ready())
  163. exit(1);
  164. /* Adjust cell header */
  165. gregion();
  166. /* Create required maps */
  167. if (!flg.input) {
  168. if (map.fill)
  169. depressionless();
  170. basin_elevation();
  171. }
  172. top_index();
  173. /* Read required files */
  174. read_inputs();
  175. /* Implement TOPMODEL */
  176. topmodel();
  177. /* Write outputs */
  178. write_outputs();
  179. exit(0);
  180. }