main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. G_add_keyword(_("model"));
  49. module->description =
  50. _("Simulates TOPMODEL which is a physically based hydrologic model.");
  51. /* Parameter definitions */
  52. params.params = G_define_standard_option(G_OPT_F_INPUT);
  53. params.params->key = "parameters";
  54. params.params->description = _("Name of input TOPMODEL parameters file");
  55. params.topidxstats = G_define_standard_option(G_OPT_F_INPUT);
  56. params.topidxstats->key = "topidxstats";
  57. params.topidxstats->description =
  58. _("Name of input topographic index statistics file");
  59. params.input = G_define_standard_option(G_OPT_F_INPUT);
  60. params.input->description =
  61. _("Name of input rainfall and potential evapotranspiration data file");
  62. params.output = G_define_standard_option(G_OPT_F_OUTPUT);
  63. params.output->description = _("Name for output file");
  64. params.timestep = G_define_option();
  65. params.timestep->key = "timestep";
  66. params.timestep->label = _("Time step");
  67. params.timestep->description = _("Generate output for this time step");
  68. params.timestep->type = TYPE_INTEGER;
  69. params.timestep->required = NO;
  70. params.topidxclass = G_define_option();
  71. params.topidxclass->key = "topidxclass";
  72. params.topidxclass->label = _("Topographic index class");
  73. params.topidxclass->description =
  74. _("Generate output for this topographic index class");
  75. params.topidxclass->type = TYPE_INTEGER;
  76. params.topidxclass->required = NO;
  77. params.topidx = G_define_standard_option(G_OPT_R_INPUT);
  78. params.topidx->key = "topidx";
  79. params.topidx->label =
  80. _("Name of input topographic index raster map");
  81. params.topidx->description =
  82. _("Must be clipped to the catchment boundary. Used for generating outtopidxstats");
  83. params.topidx->required = NO;
  84. params.topidx->guisection = _("Preprocess");
  85. params.ntopidxclasses = G_define_option();
  86. params.ntopidxclasses->key = "ntopidxclasses";
  87. params.ntopidxclasses->label = _("Number of topographic index classes");
  88. params.ntopidxclasses->description =
  89. _("Used for generating outtopidxstats");
  90. params.ntopidxclasses->type = TYPE_INTEGER;
  91. params.ntopidxclasses->required = NO;
  92. params.ntopidxclasses->answer = "30";
  93. params.ntopidxclasses->guisection = _("Preprocess");
  94. params.outtopidxstats = G_define_standard_option(G_OPT_F_OUTPUT);
  95. params.outtopidxstats->key = "outtopidxstats";
  96. params.outtopidxstats->label =
  97. _("Name for output topographic index statistics file");
  98. params.outtopidxstats->description =
  99. _("Requires topidx and ntopidxclasses");
  100. params.outtopidxstats->required = NO;
  101. params.outtopidxstats->guisection = _("Preprocess");
  102. flags.preprocess = G_define_flag();
  103. flags.preprocess->key = 'p';
  104. flags.preprocess->description =
  105. _("Preprocess only and stop after generating outtopidxstats");
  106. flags.preprocess->suppress_required = YES;
  107. if (G_parser(argc, argv))
  108. exit(EXIT_FAILURE);
  109. /* Store given parameters */
  110. file.params = params.params->answer;
  111. file.topidxstats = params.topidxstats->answer;
  112. file.input = params.input->answer;
  113. file.output = params.output->answer;
  114. if (!params.timestep->answer)
  115. params.timestep->answer = "0";
  116. misc.timestep = atoi(params.timestep->answer);
  117. if (!params.topidxclass->answer)
  118. params.topidxclass->answer = "0";
  119. misc.topidxclass = atoi(params.topidxclass->answer);
  120. if (params.topidx->answer && params.outtopidxstats->answer) {
  121. char *topidx;
  122. int ntopidxclasses;
  123. char *outtopidxstats;
  124. topidx = params.topidx->answer;
  125. ntopidxclasses = atoi(params.ntopidxclasses->answer);
  126. outtopidxstats = params.outtopidxstats->answer;
  127. if (ntopidxclasses <= 1)
  128. G_fatal_error(_("%s must be greater than 1"), "ntopidxclasses");
  129. create_topidxstats(topidx, ntopidxclasses, outtopidxstats);
  130. } else if (params.topidx->answer) {
  131. G_warning(_("Ignoring %s because %s is not specified"),
  132. params.topidx->key, params.outtopidxstats->key);
  133. } else if (params.outtopidxstats->answer) {
  134. G_warning(_("Ignoring %s because %s is not specified"),
  135. params.outtopidxstats->key, params.topidx->key);
  136. }
  137. if (flags.preprocess->answer)
  138. exit(EXIT_SUCCESS);
  139. /* Read input */
  140. read_input();
  141. /* Run TOPMODEL */
  142. run_topmodel();
  143. /* Write output */
  144. write_output();
  145. exit(EXIT_SUCCESS);
  146. }