main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /****************************************************************************
  2. *
  3. * MODULE: r.topmodel
  4. *
  5. * AUTHOR(S): Huidae Cho <grass4u gmail.com>, Hydro Laboratory,
  6. * Kyungpook National University
  7. * Based on TMOD9502.FOR by Keith Beven <k.beven lancaster.ac.uk>
  8. *
  9. * PURPOSE: Simulates TOPMODEL.
  10. *
  11. * COPYRIGHT: (C) 2000-2014 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #define _MAIN_C_
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <grass/gis.h>
  22. #include <grass/glocale.h>
  23. #include "global.h"
  24. int main(int argc, char **argv)
  25. {
  26. struct GModule *module;
  27. struct
  28. {
  29. struct Option *params;
  30. struct Option *topidxstats;
  31. struct Option *input;
  32. struct Option *output;
  33. struct Option *timestep;
  34. struct Option *topidxclass;
  35. struct Option *topidx;
  36. struct Option *ntopidxclasses;
  37. struct Option *outtopidxstats;
  38. } params;
  39. struct
  40. {
  41. struct Flag *preprocess;
  42. } flags;
  43. /* Initialize GRASS and parse command line */
  44. G_gisinit(argv[0]);
  45. module = G_define_module();
  46. G_add_keyword(_("raster"));
  47. G_add_keyword(_("hydrology"));
  48. module->description =
  49. _("Simulates TOPMODEL which is a physically based hydrologic model.");
  50. /* Parameter definitions */
  51. params.params = G_define_standard_option(G_OPT_F_INPUT);
  52. params.params->key = "parameters";
  53. params.params->description = _("Name of TOPMODEL parameters file");
  54. params.topidxstats = G_define_standard_option(G_OPT_F_INPUT);
  55. params.topidxstats->key = "topidxstats";
  56. params.topidxstats->description =
  57. _("Name of topographic index statistics file");
  58. params.input = G_define_standard_option(G_OPT_F_INPUT);
  59. params.input->description =
  60. _("Name of rainfall and potential evapotranspiration data file");
  61. params.output = G_define_standard_option(G_OPT_F_OUTPUT);
  62. params.output->description = _("Name for output file");
  63. params.timestep = G_define_option();
  64. params.timestep->key = "timestep";
  65. params.timestep->label = _("Time step");
  66. params.timestep->description = _("Generate output for this time step");
  67. params.timestep->type = TYPE_INTEGER;
  68. params.timestep->required = NO;
  69. params.topidxclass = G_define_option();
  70. params.topidxclass->key = "topidxclass";
  71. params.topidxclass->label = _("Topographic index class");
  72. params.topidxclass->description =
  73. _("Generate output for this topographic index class");
  74. params.topidxclass->type = TYPE_INTEGER;
  75. params.topidxclass->required = NO;
  76. params.topidx = G_define_standard_option(G_OPT_R_INPUT);
  77. params.topidx->key = "topidx";
  78. params.topidx->label =
  79. _("Name of input topographic index raster map");
  80. params.topidx->description =
  81. _("Must be clipped to the catchment boundary. Used for generating outtopidxstats");
  82. params.topidx->required = NO;
  83. params.topidx->guisection = _("Preprocess");
  84. params.ntopidxclasses = G_define_option();
  85. params.ntopidxclasses->key = "ntopidxclasses";
  86. params.ntopidxclasses->label = _("Number of topographic index classes");
  87. params.ntopidxclasses->description =
  88. _("Used for generating outtopidxstats");
  89. params.ntopidxclasses->type = TYPE_INTEGER;
  90. params.ntopidxclasses->required = NO;
  91. params.ntopidxclasses->answer = "30";
  92. params.ntopidxclasses->guisection = _("Preprocess");
  93. params.outtopidxstats = G_define_standard_option(G_OPT_F_OUTPUT);
  94. params.outtopidxstats->key = "outtopidxstats";
  95. params.outtopidxstats->label =
  96. _("Name for output topographic index statistics file");
  97. params.outtopidxstats->description =
  98. _("Requires topidx and ntopidxclasses");
  99. params.outtopidxstats->required = NO;
  100. params.outtopidxstats->guisection = _("Preprocess");
  101. flags.preprocess = G_define_flag();
  102. flags.preprocess->key = 'p';
  103. flags.preprocess->description =
  104. _("Preprocess only and stop after generating outtopidxstats");
  105. flags.preprocess->suppress_required = YES;
  106. if (G_parser(argc, argv))
  107. exit(EXIT_FAILURE);
  108. /* Store given parameters */
  109. file.params = params.params->answer;
  110. file.topidxstats = params.topidxstats->answer;
  111. file.input = params.input->answer;
  112. file.output = params.output->answer;
  113. if (!params.timestep->answer)
  114. params.timestep->answer = "0";
  115. misc.timestep = atoi(params.timestep->answer);
  116. if (!params.topidxclass->answer)
  117. params.topidxclass->answer = "0";
  118. misc.topidxclass = atoi(params.topidxclass->answer);
  119. if (params.topidx->answer && params.outtopidxstats->answer) {
  120. char *topidx;
  121. int ntopidxclasses;
  122. char *outtopidxstats;
  123. topidx = params.topidx->answer;
  124. ntopidxclasses = atoi(params.ntopidxclasses->answer);
  125. outtopidxstats = params.outtopidxstats->answer;
  126. if (ntopidxclasses <= 1)
  127. G_fatal_error(_("%s must be greater than 1"), "ntopidxclasses");
  128. create_topidxstats(topidx, ntopidxclasses, outtopidxstats);
  129. } else if (params.topidx->answer) {
  130. G_warning(_("Ignoring %s because %s is not specified"),
  131. params.topidx->key, params.outtopidxstats->key);
  132. } else if (params.outtopidxstats->answer) {
  133. G_warning(_("Ignoring %s because %s is not specified"),
  134. params.outtopidxstats->key, params.topidx->key);
  135. }
  136. if (flags.preprocess->answer)
  137. exit(EXIT_SUCCESS);
  138. /* Read input */
  139. read_input();
  140. /* Run TOPMODEL */
  141. run_topmodel();
  142. /* Write output */
  143. write_output();
  144. exit(EXIT_SUCCESS);
  145. }