file_io.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <grass/raster.h>
  5. #include <grass/glocale.h>
  6. #include "global.h"
  7. void get_line(FILE * fp, char *buffer)
  8. {
  9. char *str;
  10. buffer[0] = 0;
  11. fscanf(fp, "%[^\n]", buffer);
  12. getc(fp);
  13. if ((str = (char *)strchr(buffer, '#')))
  14. *str = 0;
  15. }
  16. void read_input(void)
  17. {
  18. char buf[1024];
  19. FILE *fp;
  20. int i;
  21. /* Read parameters file */
  22. if ((fp = fopen(file.params, "r")) == NULL)
  23. G_fatal_error(_("Unable to open input file <%s>"), file.params);
  24. for (; !feof(fp);) {
  25. get_line(fp, buf);
  26. i = strlen(buf) - 1;
  27. for (; i >= 0; i--) {
  28. if (buf[i] != ' ' && buf[i] != '\t') {
  29. buf[i + 1] = 0;
  30. break;
  31. }
  32. }
  33. if (i >= 0)
  34. break;
  35. }
  36. params.name = G_store(buf);
  37. for (; !feof(fp);) {
  38. get_line(fp, buf);
  39. if (sscanf(buf, "%lf", &(params.A)) == 1)
  40. break;
  41. }
  42. for (; !feof(fp);) {
  43. get_line(fp, buf);
  44. if (sscanf(buf, "%lf", &(params.qs0)) == 1)
  45. break;
  46. }
  47. if (params.qs0 == 0.0) {
  48. fclose(fp);
  49. G_fatal_error(_("%s cannot be 0"), "parameters.qs0");
  50. }
  51. for (; !feof(fp);) {
  52. get_line(fp, buf);
  53. if (sscanf(buf, "%lf", &(params.lnTe)) == 1)
  54. break;
  55. }
  56. for (; !feof(fp);) {
  57. get_line(fp, buf);
  58. if (sscanf(buf, "%lf", &(params.m)) == 1)
  59. break;
  60. }
  61. for (; !feof(fp);) {
  62. get_line(fp, buf);
  63. if (sscanf(buf, "%lf", &(params.Sr0)) == 1)
  64. break;
  65. }
  66. for (; !feof(fp);) {
  67. get_line(fp, buf);
  68. if (sscanf(buf, "%lf", &(params.Srmax)) == 1)
  69. break;
  70. }
  71. for (; !feof(fp);) {
  72. get_line(fp, buf);
  73. if (sscanf(buf, "%lf", &(params.td)) == 1)
  74. break;
  75. }
  76. for (; !feof(fp);) {
  77. get_line(fp, buf);
  78. if (sscanf(buf, "%lf", &(params.vch)) == 1)
  79. break;
  80. }
  81. for (; !feof(fp);) {
  82. get_line(fp, buf);
  83. if (sscanf(buf, "%lf", &(params.vr)) == 1)
  84. break;
  85. }
  86. for (; !feof(fp);) {
  87. get_line(fp, buf);
  88. if (sscanf(buf, "%d", &(params.infex)) == 1)
  89. break;
  90. }
  91. for (; !feof(fp);) {
  92. get_line(fp, buf);
  93. if (sscanf(buf, "%lf", &(params.K0)) == 1)
  94. break;
  95. }
  96. for (; !feof(fp);) {
  97. get_line(fp, buf);
  98. if (sscanf(buf, "%lf", &(params.psi)) == 1)
  99. break;
  100. }
  101. for (; !feof(fp);) {
  102. get_line(fp, buf);
  103. if (sscanf(buf, "%lf", &(params.dtheta)) == 1)
  104. break;
  105. }
  106. params.d = NULL;
  107. params.Ad = NULL;
  108. for (i = 0; !feof(fp);) {
  109. double d;
  110. double Ad_r;
  111. get_line(fp, buf);
  112. if (sscanf(buf, "%lf %lf", &d, &Ad_r) == 2) {
  113. params.d = (double *)G_realloc(params.d, (i + 1) * sizeof(double));
  114. params.Ad = (double *)G_realloc(params.Ad,
  115. (i + 1) * sizeof(double));
  116. params.d[i] = d;
  117. params.Ad[i++] = Ad_r * params.A;
  118. }
  119. }
  120. params.nch = i;
  121. fclose(fp);
  122. /* Read topographic index statistics file */
  123. if ((fp = fopen(file.topidxstats, "r")) == NULL)
  124. G_fatal_error(_("Unable to open input file <%s>"), file.topidxstats);
  125. topidxstats.atb = NULL;
  126. topidxstats.Aatb_r = NULL;
  127. for (i = 0; !feof(fp);) {
  128. double atb;
  129. double Aatb_r;
  130. get_line(fp, buf);
  131. if (sscanf(buf, "%lf %lf", &atb, &Aatb_r) == 2) {
  132. topidxstats.atb = (double *)G_realloc(topidxstats.atb,
  133. (i + 1) * sizeof(double));
  134. topidxstats.Aatb_r = (double *)G_realloc(topidxstats.Aatb_r,
  135. (i + 1) * sizeof(double));
  136. topidxstats.atb[i] = atb;
  137. topidxstats.Aatb_r[i++] = Aatb_r;
  138. }
  139. }
  140. misc.ntopidxclasses = i;
  141. fclose(fp);
  142. /* Read input file */
  143. if ((fp = fopen(file.input, "r")) == NULL)
  144. G_fatal_error(_("Unable to open input file <%s>"), file.input);
  145. for (; !feof(fp);) {
  146. get_line(fp, buf);
  147. if (sscanf(buf, "%lf", &(input.dt)) == 1)
  148. break;
  149. }
  150. input.R = NULL;
  151. input.Ep = NULL;
  152. for (i = 0; !feof(fp);) {
  153. double R;
  154. double Ep;
  155. get_line(fp, buf);
  156. if (sscanf(buf, "%lf %lf", &R, &Ep) == 2) {
  157. input.R = (double *)G_realloc(input.R, (i + 1) * sizeof(double));
  158. input.Ep = (double *)G_realloc(input.Ep, (i + 1) * sizeof(double));
  159. input.R[i] = R;
  160. input.Ep[i++] = Ep;
  161. }
  162. }
  163. input.ntimesteps = i;
  164. fclose(fp);
  165. if (!(misc.timestep > 0 && misc.timestep < input.ntimesteps + 1))
  166. misc.timestep = 0;
  167. if (!(misc.topidxclass > 0 && misc.topidxclass < misc.ntopidxclasses + 1))
  168. misc.topidxclass = 0;
  169. }
  170. void write_output(void)
  171. {
  172. FILE *fp;
  173. time_t tloc;
  174. struct tm *ltime;
  175. int st, et, si, ei;
  176. int i, j;
  177. time(&tloc);
  178. ltime = localtime(&tloc);
  179. ltime->tm_year += 1900;
  180. ltime->tm_mon++;
  181. if ((fp = fopen(file.output, "w")) == NULL)
  182. G_fatal_error(_("Unable to create output file <%s>"), file.output);
  183. fprintf(fp, "# r.topmodel output file for %s\n", params.name);
  184. fprintf(fp, "# Run time: %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n",
  185. ltime->tm_year, ltime->tm_mon, ltime->tm_mday,
  186. ltime->tm_hour, ltime->tm_min, ltime->tm_sec);
  187. fprintf(fp, "#\n");
  188. fprintf(fp, "# Qt_peak [m^3/timestep]: Peak total flow\n");
  189. fprintf(fp, "# tt_peak [timestep]: Peak time for total flow\n");
  190. fprintf(fp, "# Qt_mean [m^3/timestep]: Mean total flow\n");
  191. fprintf(fp, "# lnTe [ln(m^2/timestep)]: ln of the average transmissivity at the soil surface\n");
  192. fprintf(fp, "# vch [m/timestep]: Main channel routing velocity\n");
  193. fprintf(fp, "# vr [m/timestep]: Internal subcatchment routing velocity\n");
  194. fprintf(fp, "# lambda [ln(m^2)]: Average topographic index\n");
  195. fprintf(fp, "# qss [m/timestep]: Saturated subsurface flow per unit area\n");
  196. fprintf(fp, "# qs0 [m/timestep]: Initial subsurface flow per unit area\n");
  197. fprintf(fp, "# ntopidxclasses: Number of topographic index classes\n");
  198. fprintf(fp, "# dt [h]: Time step\n");
  199. fprintf(fp, "# ntimesteps: Number of time steps\n");
  200. fprintf(fp, "# nch: Number of channel segments\n");
  201. fprintf(fp, "# delay [timestep]: Routing delay in the main channel\n");
  202. fprintf(fp, "# tc [timestep]: Time of concentration\n");
  203. fprintf(fp, "# tcsub [timestep]: Time of concentration in the subcatchment\n");
  204. fprintf(fp, "#\n");
  205. fprintf(fp, "# tch [timestep]: Routing time to the catchment outlet\n");
  206. fprintf(fp, "# Ad [m^2]: Difference in the contribution area\n");
  207. fprintf(fp, "# Qt [m^3/timestep]: Total flow\n");
  208. fprintf(fp, "# qt [m/timestep]: Total flow per unit area\n");
  209. fprintf(fp, "# qo [m/timestep]: Saturated overland flow per unit area\n");
  210. fprintf(fp, "# qs [m/timestep]: Subsurface flow per unit area\n");
  211. fprintf(fp, "# qv [m/timestep]: Vertical drainage flux from unsaturated zone\n");
  212. fprintf(fp, "# S_mean [m]: Mean saturation deficit\n");
  213. if (params.infex) {
  214. fprintf(fp, "# f [m/timestep]: Infiltration rate\n");
  215. fprintf(fp, "# fex [m/timestep]: Infiltration excess runoff\n");
  216. }
  217. if (misc.timestep || misc.topidxclass) {
  218. fprintf(fp, "#\n");
  219. fprintf(fp, "# Srz [m]: Root zone storage deficit\n");
  220. fprintf(fp, "# Suz [m]: Unsaturated zone storage (gravity drainage)\n");
  221. fprintf(fp, "# S [m]: Local saturated zone deficit due to gravity drainage\n");
  222. fprintf(fp, "# Ea [m/timestep]: Actual evapotranspiration\n");
  223. fprintf(fp, "# ex [m/timestep]: Excess flow from fully saturated area per unit area\n");
  224. }
  225. fprintf(fp, "\n");
  226. fprintf(fp, "Qt_peak: %10.3e\n", misc.Qt_peak);
  227. fprintf(fp, "tt_peak: %10d\n", misc.tt_peak);
  228. fprintf(fp, "Qt_mean: %10.3e\n", misc.Qt_mean);
  229. fprintf(fp, "lnTe: %10.3e\n", misc.lnTe);
  230. fprintf(fp, "vch: %10.3e\n", misc.vch);
  231. fprintf(fp, "vr: %10.3e\n", misc.vr);
  232. fprintf(fp, "lambda: %10.3e\n", misc.lambda);
  233. fprintf(fp, "qss: %10.3e\n", misc.qss);
  234. fprintf(fp, "qs0: %10.3e\n", misc.qs0);
  235. fprintf(fp, "ntopidxclasses: %10d\n", misc.ntopidxclasses);
  236. fprintf(fp, "dt: %10.3e\n", input.dt);
  237. fprintf(fp, "ntimesteps: %10d\n", input.ntimesteps);
  238. fprintf(fp, "nch: %10d\n", params.nch);
  239. fprintf(fp, "delay: %10d\n", misc.delay);
  240. fprintf(fp, "tc: %10d\n", misc.tc);
  241. fprintf(fp, "tcsub: %10d\n", misc.tcsub);
  242. fprintf(fp, "\n");
  243. fprintf(fp, "%10s\n", "tch");
  244. for (i = 0; i < params.nch; i++)
  245. fprintf(fp, "%10.3e\n", misc.tch[i]);
  246. fprintf(fp, "\n");
  247. fprintf(fp, "%10s\n", "Ad");
  248. for (i = 0; i < misc.tcsub; i++)
  249. fprintf(fp, "%10.3e\n", misc.Ad[i]);
  250. fprintf(fp, "\n");
  251. st = et = si = ei = 0;
  252. if (misc.timestep || misc.topidxclass) {
  253. if (misc.timestep) {
  254. st = misc.timestep - 1;
  255. et = misc.timestep;
  256. }
  257. else {
  258. st = 0;
  259. et = input.ntimesteps;
  260. }
  261. if (misc.topidxclass) {
  262. si = misc.topidxclass - 1;
  263. ei = misc.topidxclass;
  264. }
  265. else {
  266. si = 0;
  267. ei = misc.ntopidxclasses;
  268. }
  269. }
  270. fprintf(fp, "%10s %10s %10s %10s %10s %10s %10s",
  271. "timestep", "Qt", "qt", "qo", "qs", "qv", "S_mean");
  272. if (params.infex)
  273. fprintf(fp, " %10s %10s", "f", "fex");
  274. fprintf(fp, "\n");
  275. for (i = 0; i < input.ntimesteps; i++) {
  276. fprintf(fp, "%10d %10.3e %10.3e %10.3e %10.3e %10.3e %10.3e",
  277. i + 1, misc.Qt[i],
  278. misc.qt[i][misc.ntopidxclasses],
  279. misc.qo[i][misc.ntopidxclasses], misc.qs[i],
  280. misc.qv[i][misc.ntopidxclasses], misc.S_mean[i]);
  281. if (params.infex)
  282. fprintf(fp, " %10.3e %10.3e", misc.f[i], misc.fex[i]);
  283. fprintf(fp, "\n");
  284. }
  285. if (misc.timestep || misc.topidxclass) {
  286. fprintf(fp, "\n");
  287. fprintf(fp, "For ");
  288. if (misc.timestep)
  289. fprintf(fp, "timestep: %5d", misc.timestep);
  290. if (misc.timestep && misc.topidxclass)
  291. fprintf(fp, ", ");
  292. if (misc.topidxclass)
  293. fprintf(fp, "topidxclass: %5d", misc.topidxclass);
  294. fprintf(fp, "\n");
  295. if (misc.timestep && !misc.topidxclass) {
  296. fprintf(fp, "%10s ", "topidxclass");
  297. }
  298. else if (misc.topidxclass && !misc.timestep) {
  299. fprintf(fp, "%10s ", "timestep");
  300. }
  301. fprintf(fp, "%10s %10s %10s %10s %10s %10s %10s %10s %10s\n",
  302. "qt", "qo", "qs", "qv", "Srz", "Suz", "S", "Ea", "ex");
  303. for (i = st; i < et; i++)
  304. for (j = si; j < ei; j++) {
  305. if (misc.timestep && !misc.topidxclass) {
  306. fprintf(fp, "%10d ", j + 1);
  307. }
  308. else if (misc.topidxclass && !misc.timestep) {
  309. fprintf(fp, "%10d ", i + 1);
  310. }
  311. fprintf(fp, "%10.3e %10.3e %10.3e "
  312. "%10.3e %10.3e %10.3e "
  313. "%10.3e %10.3e %10.3e\n",
  314. misc.qt[i][j], misc.qo[i][j],
  315. misc.qs[i], misc.qv[i][j],
  316. misc.Srz[i][j], misc.Suz[i][j],
  317. misc.S[i][j], misc.Ea[i][j], misc.ex[i][j]);
  318. }
  319. }
  320. fclose(fp);
  321. }