file_io.c 12 KB

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