-
Notifications
You must be signed in to change notification settings - Fork 6
/
hydroreadinput.c
488 lines (432 loc) · 17.5 KB
/
hydroreadinput.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*-------------------------------------------------------------------------------------------
* hydroreadinput.c
*
* Author: Albert Kettner, March 2006
*
* Reads the main ASCII input file for HYDROTREND (HYDRO.IN)
*
*
* Variable Def.Location Type Units Usage
* -------- ------------ ---- ----- -----
* chs[150] HydroReadInput.c char - temporary character string
* dumchr[2] HydroReadInput.c char - temporary character string
* dumdbl HydroReadInput.c double - temporary double
* dumint HydroReadInput.c int - temporary integer
* err various int - error flag, halts program
* jj various int - temporary loop counter
*
*-------------------------------------------------------------------------------------------*/
#include <string.h>
#include "hydroclimate.h"
#include "hydroinout.h"
#include "hydroparams.h"
#include "hydroalloc_mem.h"
#include "hydrotrend.h"
#define MAXDIR (100)
/*---------------------------
* Start of HydroReadInput
*---------------------------*/
int
hydroreadinput (char* file)
{
/*-------------------
* Local Variables
*-------------------*/
char chs[150], dumchr[2];
int jj, err, dumint, totyears, k;
double dumdbl, evapotranspiration;
char dummyx;
/*------------------------
* Initialize Variables
*------------------------*/
err = 0;
k = 0;
totyears = 0;
/*-----------------------
* Open the input file
*-----------------------*/
if ((fidinput = fopen (file, "r")) == NULL)
{
fprintf (stderr,
" HydroReadInput.c ERROR: Unable to open the input file %s \n",
file);
fprintf (stderr, " Make sure the input file name is HYDRO.IN\n");
fprintf (stderr, " program aborted \n");
// exit(1);
}
/*---------------------------------------
* 1) Read in title of first epoch (-)
*---------------------------------------*/
fgets (title, 150, fidinput);
/*-----------------------------------------------------------------
* 2) Read the option of writing output yes or no to ascii file
*-----------------------------------------------------------------*/
for (jj = 0; jj < MAXCHAR; jj++)
{
fscanf (fidinput, "%c", &asciioutput[jj]);
asciioutput[jj] = toupper (asciioutput[jj]);
if (asciioutput[jj] == ' ')
jj = MAXCHAR;
}
fgets (chs, 150, fidinput);
/*------------------------------------
* 3) Reed the output directory in
*------------------------------------*/
for (jj = 0; jj < MAXDIR; jj++)
{
fscanf (fidinput, "%c", &directory[jj]);
if (directory[jj] == ' ' || directory[jj] == '\t')
{
if (directory[jj - 1] != '/')
directory[jj] = '/';
else
directory[jj] = '\0';
jj = MAXDIR;
}
}
fgets (chs, 150, fidinput);
/*-----------------------------------
* 4) Read in number of epochs (-)
*-----------------------------------*/
fscanf (fidinput, "%d", &nepochs);
fgets (chs, 150, fidinput);
/*-------------------------------------------------
* Dynamic allocation off input variables
*-------------------------------------------------*/
hydroinputalloc (nepochs);
/*-----------------------------------
* Loop through number of epochs
* specified and retrieve the data
*-----------------------------------*/
for (ep = 0; ep < nepochs; ep++)
{
/*---------------------------------------------------------------
* 5) Read start year, number of years and timestep (a,-,char)
* Keep timestep from first epoch for all subsequent epochs
*---------------------------------------------------------------*/
if (ep != 0)
strcpy (dumchr, timestep);
fscanf (fidinput, "%d %d ", &syear[ep], &nyears[ep]);
fgets (timestep, 2, fidinput);
timestep[0] = tolower (timestep[0]);
fgets (chs, 150, fidinput);
if (ep != 0 && dumchr[0] != timestep[0])
{
fprintf (stderr,
" HydroReadInput.c WARNING: timestep changed between epochs. \n");
fprintf (stderr,
" Hydrotrend will use the timestep from the begining epoch. \n");
fprintf (stderr, " Present epoch = %d \n", ep + 1);
fprintf (stderr, " epoch # 1 timestep = %s \n", dumchr);
fprintf (stderr, " epoch # %d timestep = %s \n", ep, timestep);
fprintf (stderr, " syear=%d, nyears=%d\n", syear[ep],
nyears[ep]);
strcpy (timestep, dumchr);
}
/*---------------------------------------------------
* 7) Read number of grain sizes to simulate (int)
* Must be constant for all epochs
*---------------------------------------------------*/
if (ep == 0)
{
fscanf (fidinput, "%d ", &ngrain);
fgets (chs, 150, fidinput);
}
else
{
fscanf (fidinput, "%d ", &dumint);
fgets (chs, 150, fidinput);
if (dumint != ngrain)
{
fprintf (stderr,
" HydroReadInput.c ERROR: ngrain must be constant for all epochs. \n");
fprintf (stderr, " present epoch = %d \n", ep + 1);
fprintf (stderr, " initial ngrain = %d \n", ngrain);
fprintf (stderr, " present ngrain = %d \n", dumint);
err = 1;
}
}
/*---------------------------------------------
* 8) Read percentage of each grain size (%)
*---------------------------------------------*/
for (jj = 0; jj < ngrain; jj++)
fscanf (fidinput, "%lf ", &grainpct[jj][ep]);
fgets (chs, 150, fidinput);
/*--------------------------------------------------------------
* 9) Read temperature trend parameters ( degC, degC/a, degC)
*--------------------------------------------------------------*/
fscanf (fidinput, "%lf %lf %lf ", &Tstart[ep], &Tchange[ep], &Tstd[ep]);
fgets (chs, 150, fidinput);
/*--------------------------------------------------------------
* 10) Read precipitation trend parameters (m/a, (m/a)/a, m/a)
*--------------------------------------------------------------*/
fscanf (fidinput, "%lf %lf %lf ", &Pstart[ep], &Pchange[ep], &Pstd[ep]);
fgets (chs, 150, fidinput);
/*-------------------------------------------------------
* 11) Read rainfall mass balance parameters ( %, -, -)
*-------------------------------------------------------*/
fscanf (fidinput, "%lf %lf %lf ", &Pmassbal[ep], &Pexponent[ep],
&Prange[ep]);
fgets (chs, 150, fidinput);
/*-------------------------------------------------
* 12) Read constant base flow discharge (m^3/s)
*-------------------------------------------------*/
fscanf (fidinput, "%lf ", &baseflowtot[ep]);
fgets (chs, 150, fidinput);
/*--------------------------------------------------------------------------
* 13-24) Read climate statistics ( month, degC, degC, mm -> m, mm -> m )
*--------------------------------------------------------------------------*/
for (jj = 0; jj < 12; jj++)
{
fscanf (fidinput, "%3s %lf %lf %lf %lf ", moname[jj],
&Tnominal[jj][ep], &Tnomstd[jj][ep], &Pnominal[jj][ep],
&Pnomstd[jj][ep]);
fgets (chs, 150, fidinput);
Pnominal[jj][ep] /= 1000;
Pnomstd[jj][ep] /= 1000;
}
/*---------------------------------------------
* 25) Read lapse rate ( degC/km -> degC/m )
*---------------------------------------------*/
fscanf (fidinput, "%lf ", &lapserate[ep]);
fgets (chs, 150, fidinput);
lapserateflag = 0;
if (lapserate[ep] == -9999)
{
globalparflag++;
lapserateflag = 1;
}
else
lapserate[ep] /= 1000;
/*-------------------------------------------------------------
* 26) Read ELA start altitude and change per year ( m, m/a)
*-------------------------------------------------------------*/
fscanf (fidinput, "%lf %lf ", &ELAstart[ep], &ELAchange[ep]);
fgets (chs, 150, fidinput);
/*-------------------------------------------------------------------
* 27) Read percentage of nival/ice runoff lost to evaporation (%)
*-------------------------------------------------------------------*/
fscanf (fidinput, "%lf ", &dryevap[ep]);
fgets (chs, 150, fidinput);
/*-------------------------------------------------------------------
* 27a) Read the canopy interception coefficients (mm/d)(-)
*-------------------------------------------------------------------*/
fscanf (fidinput, "%lf %lf ", &alphag[ep], &betag[ep]);
alphag[ep] /= 1000;
fgets (chs, 150, fidinput);
/*-------------------------------------------------------------------
* 27b) Read the evapotranspiration coefficient(alphagwe[ep](mm/day))
* Read the evapotransiration coefficient (betagwe[ep](-))
*-------------------------------------------------------------------*/
fscanf (fidinput, "%lf %lf ", &alphagwe[ep], &betagwe[ep]);
alphagwe[ep] /= 1000;
fgets (chs, 150, fidinput);
/*----------------------------------------------
* 28) Read river bed average slope (m/m)
*----------------------------------------------*/
fscanf (fidinput, "%lf ", &rslope[ep]);
fgets (chs, 150, fidinput);
/*----------------------------------------------
* 28a) Read bedload rating term (-)
*----------------------------------------------*/
fscanf (fidinput, "%lf ", &alphabed[ep]);
fgets (chs, 150, fidinput);
if (alphabed[ep] == -9999)
{
alphabed[ep] = 1.0;
}
/*-------------------------------------
* 29) Read basin length ( km -> m )
*-------------------------------------*/
fscanf (fidinput, "%lf ", &basinlength[ep]);
fgets (chs, 150, fidinput);
basinlength[ep] *= 1000;
/*-----------------------------------------------------
* 30) Read percentage of basin covered by lakes (%)
*-----------------------------------------------------*/
fscanf (fidinput, "%lf %c", &Rvol[ep], &Rparamcheck[ep]);
if (Rparamcheck[ep] == 'a' || Rparamcheck[ep] == 'A')
{
fscanf (fidinput, "%lf", &Ralt[ep]);
Rarea[ep] = 0.0;
}
if (Rparamcheck[ep] == 'd' || Rparamcheck[ep] == 'D')
{
fscanf (fidinput, "%lf", &Rarea[ep]);
Ralt[ep] = 0.0;
}
fgets (chs, 150, fidinput);
/*------------------------------------------------------------
* 31) Read river mouth velocity coeff and exponent ( -, -)
*------------------------------------------------------------*/
fscanf (fidinput, "%lf %lf ", &velcof[ep], &velpow[ep]);
fgets (chs, 150, fidinput);
/*------------------------------------------------------------
* 32) Read river mouth width coeff and exponent ( -, -)
*------------------------------------------------------------*/
fscanf (fidinput, "%lf %lf ", &widcof[ep], &widpow[ep]);
fgets (chs, 150, fidinput);
depcof[ep] = 1 / (velcof[ep] * widcof[ep]);
deppow[ep] = 1 - velpow[ep] - widpow[ep];
/*-----------------------------------------
* 33) Read average river velocity (m/s)
*-----------------------------------------*/
fscanf (fidinput, "%lf ", &avgvel[ep]);
fgets (chs, 150, fidinput);
/*-------------------------------------------------------------
* 34) Read max and min size of groundwater pool ( m^3, m^3)
*-------------------------------------------------------------*/
fscanf (fidinput, "%lf %lf ", &gwmax[ep], &gwmin[ep]);
fgets (chs, 150, fidinput);
/*---------------------------------------------------
* 35) Read initial size of groundwater pool (m^3)
* for first epoch only
*---------------------------------------------------*/
if (ep == 0)
{
fscanf (fidinput, "%lf ", &gwinitial);
fgets (chs, 150, fidinput);
}
else
{
fscanf (fidinput, "%lf ", &dumdbl);
fgets (chs, 150, fidinput);
}
/*-----------------------------------------------------------------
* 36) Read subsurface storm flow coeff and exponent ( m^3/s, -)
*-----------------------------------------------------------------*/
fscanf (fidinput, "%lf %lf ", &alphass[ep], &betass[ep]);
fgets (chs, 150, fidinput);
/*-----------------------------------------------------------------
* 37) Read saturated hydraulic conductivity ( mm/day -> m/day )
*-----------------------------------------------------------------*/
fscanf (fidinput, "%lf ", &Ko[ep]);
fgets (chs, 150, fidinput);
Ko[ep] /= 1000;
/*------------------------------------------------------------------------
* 38) Read latitude (Geographic position of rivermouth) ( in degrees )
*------------------------------------------------------------------------*/
fscanf (fidinput, "%lf %lf", &lon, &lat);
fgets (chs, 150, fidinput);
/*------------------------------------------------
* 39) Read number of outlets to simulate (int)
* Must be constant for all epochs
*------------------------------------------------*/
noutletflag = 0;
outletmodelflag = 0;
fscanf (fidinput, "%c", &dummyx);
if (dummyx == 'u' || dummyx == 'U')
{
noutletflag = 1;
outletmodelflag = 1;
minnoutlet = 2;
maxnoutlet = 9;
}
else if (dummyx == 'r' || dummyx == 'R')
{
noutletflag = 1;
outletmodelflag = 1;
fscanf (fidinput, "%d %d", &minnoutlet, &maxnoutlet);
}
else
{
fseek (fidinput, sizeof (char) * -1, SEEK_CUR);
fscanf (fidinput, "%d ", &noutlet);
minnoutlet = 2;
maxnoutlet = noutlet;
if (noutlet == 1)
{
minnoutlet = 0;
maxnoutlet = 1;
}
if (noutlet > 1)
{
outletmodelflag = 1;
}
}
fgets (chs, 150, fidinput);
/*--------------------------------------
* 40) Read percentage of each outlet
*--------------------------------------*/
steadyoutletpctflag = 0;
nooutletpctflag = 0;
if (dummyx == 'r' || dummyx == 'R' || dummyx == 'u' || dummyx == 'U')
{
steadyoutletpctflag = 1;
nooutletpctflag = 1;
}
if (noutletflag == 0)
{
fscanf (fidinput, " %c", &dummyx);
if (dummyx == 'u' || dummyx == 'U')
{
nooutletpctflag = 1;
fscanf (fidinput, " %c ", &dummyx);
if (dummyx == 's' || dummyx == 'S')
steadyoutletpctflag = 0;
else if (dummyx == 'u' || dummyx == 'U')
steadyoutletpctflag = 1;
else
err++;
}
else if (dummyx == '0' || dummyx == '1')
{
if (ep == 0)
outletpctdummy = malloc2d (maxnoutlet, nepochs, double);
fseek (fidinput, sizeof (char) * -1, SEEK_CUR);
for (jj = 0; jj < noutlet; jj++)
{
fscanf (fidinput, " %lf ", &outletpctdummy[jj][ep]);
}
}
}
fgets (chs, 150, fidinput);
/*-----------------------------
* 41) Read number of events
*-----------------------------*/
fscanf (fidinput, "%c", &dummyx);
if (dummyx == 'n' || dummyx == 'N')
{
fscanf (fidinput, "%d ", &eventsnr[ep]);
floodvalue[ep] = 0.0;
eventnrflag = 0;
if (eventsnr[ep] == 0)
eventsnr[ep] = 1;
if (steadyoutletpctflag == 0)
eventsnr[ep] = 1;
}
if (dummyx == 'q' || dummyx == 'Q')
{
eventsnr[ep] = 1;
eventnrflag = 1;
fscanf (fidinput, "%lf", &floodvalue[ep]);
}
fgets (chs, 150, fidinput);
/*-----------------------------------------
* 42) Read the demping factor for Qsbar
*-----------------------------------------*/
fscanf (fidinput, "%lf ", &sedfilter[ep]);
if (noutlet == 1)
sedfilter[ep] = 0.0;
fgets (chs, 150, fidinput);
/*-----------------------------------------------------
* 43) Check which formula to use to calculate Qsbar
*-----------------------------------------------------*/
fscanf (fidinput, "%d ", &Qsbarformulaflag[ep]);
fgets (chs, 150, fidinput);
/*--------------------------------------------------------
* 44) If BQART is used -> get extra parameters
* read the Lithology factor
*--------------------------------------------------------*/
fscanf (fidinput, "%lf ", &lithology[ep]);
fgets (chs, 150, fidinput);
/*--------------------------------------------------------
* 45) If BQART is used -> get extra parameters
* read the anthropogenic factor
*--------------------------------------------------------*/
fscanf (fidinput, "%lf ", &anthro[ep]);
fgets (chs, 150, fidinput);
} /* end epoch loop */
fclose (fidinput);
return (err);
} /* end of HydroReadInput */