-
Notifications
You must be signed in to change notification settings - Fork 11
/
uebinputs.cpp
executable file
·423 lines (388 loc) · 13.8 KB
/
uebinputs.cpp
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
//#include "uebpg.h"
#include "uebpgdecls.h"
#pragma warning(disable : 4996)
//overload functions to read params
//this one would read params as array of floats
void readParams(const char* inpFile, float* &parArray, const int nParams)
{
parArray = new float[nParams];
ifstream pinFile(inpFile);
char headerLine[256];
pinFile.getline(headerLine,256,'\n'); //skip header
for (int i=0;i<nParams; i++)
{
pinFile.getline(headerLine,256,'\n');
pinFile.getline(headerLine,256,'\n');
sscanf(headerLine,"%f ",&parArray[i]);
}
pinFile.close();
return;
}
//function to read parameters into array of param struct with param name value pair
//Brute force! have to get better ways
void readParams(const char* inpFile, params strParamValues)
{
FILE* pinFile = fopen(inpFile,"rt");
char headerLine[256];
// char* parID;
fgets(headerLine,256,pinFile); //skip header line
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.irad);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.ireadalb);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.cg);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.z);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.z);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.rhog);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.lc);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.ks);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.de);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.avo);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.anir0);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.lans);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.lang);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.wlf);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.rd1);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.dnews);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.emc);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.alpha);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.alpha);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.gpar);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.uc);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.as);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.Bs);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.lambda);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.rimax);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.wcoeff);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.apar);
fgets(headerLine,256,pinFile); //skip id line
fscanf(pinFile,"%f\n",&strParamValues.cpar);
fclose(pinFile);
return;
}
void readSiteVars(const char* inpFile, sitevar *&svArr)
{
ifstream pinFile(inpFile);
char headerLine[256];
//istringstream valueLine;
pinFile.getline(headerLine,256); //skip header
float vardefaults[32] = { 0.0, 0.0, 0.0, 0.0, 1.0, 100000.0, 0.1, 0.0, 0.0,
0.0, 6.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.98, 5.712903,
4.350000, 6.890322, 8.660001, 8.938710, 10.010000, 9.541936,
9.038710, 7.160001, 8.106450, 5.923332, 5.058064, -9999.0, 111.00 };
for(int i=0; i<32;i++)
{
pinFile.getline(headerLine,256,':');
sscanf(headerLine,"%s ",&svArr[i].svName);
pinFile.getline(headerLine,256,'\n');
pinFile.getline(headerLine,256,'\n');
sscanf(headerLine,"%d ",&svArr[i].svType);
//headerLine[0] = 0;
//fscanf(pinFile,"%d\n",&svArr[i].svType);
switch (svArr[i].svType)
{
case -1:
svArr[i].svdefValue = vardefaults[i];
break;
case 0:
pinFile.getline(headerLine,256,'\n');
sscanf(headerLine,"%f ",&svArr[i].svdefValue);
break;
case 1:
pinFile.getline(headerLine,256,'\n');
sscanf(headerLine,"%s %s ",&svArr[i].svFile, &svArr[i].svVarName);
break;
default:
cout<<"Wrong site variable type; has to be -1 (Use default values), 0 (single value) or 1 (2D netcdf)"<<endl;
cout<<"Using default value..."<<endl;
svArr[i].svdefValue = vardefaults[i];
}
//i++;
//headerLine[0] = 0;
//}
}
pinFile.close();
return;
}
//function to read site variables (and initial contitions??)
//#_15 ics should come separate?
void readSiteVars(const char* inpFile, float svSValue[], char* svFile[], char* svVarName[], int svType[] )
{
FILE* pinFile = fopen(inpFile,"rt");
char headerLine[256];
fgets(headerLine,256,pinFile); //skip header line
int i = 0; //, svtest;
while (!feof(pinFile))
{
fgets(headerLine,256,pinFile);
if(headerLine[0] != ' ')
{
sscanf(headerLine,"%[^':'] %s",&svVarName[i]);
fgets(headerLine,256,pinFile);
sscanf(headerLine,"%d ",&svType[i]);
//fscanf(pinFile,"%d\n",&svArr[i].svType);
switch (svType[i])
{
case -1:
//svArr[i].svdefValue = vardefaults[i];
break;
case 0:
fgets(headerLine,256,pinFile);
sscanf(headerLine,"%f ",&svSValue[i]);
break;
case 1:
fgets(headerLine,256,pinFile);
sscanf(headerLine,"%s %s",&svFile[i],&svVarName[i]);
break;
default:
cout<<"Wrong site variable type; has to be -1 (Use default values), 0 (single value) or 1 (2D netcdf)"<<endl;
cout<<"Using default value..."<<endl;
break; //exit
}
i++; //increment i
headerLine[0] = ' ';
}
}
fclose(pinFile);
return;
}
void readSiteVars(const char* inpFile, sitevar svArr[], int indx)
{
FILE* pinFile = fopen(inpFile,"r");
char headerLine[256];
fgets(headerLine,256,pinFile); //skip header line
float vardefaults[32] = { 0.0, 0.0, 0.0, 0.0, 1.0, 100000.0, 0.1, 0.0, 0.0,
0.0, 6.6, 1.0, 0.0, 0.0, 0.0, 0.0, 0.25, 0.98, 5.712903,
4.350000, 6.890322, 8.660001, 8.938710, 10.010000, 9.541936,
9.038710, 7.160001, 8.106450, 5.923332, 5.058064, -9999.0, 111.00 };
for(int i=0; i<32;i++)
{
fgets(headerLine,256,pinFile);
if (headerLine[0] != ' ') //while(fgets(headerLine,256,pinFile) != NULL) //
{
sscanf(headerLine,"%[^':'] %s",&svArr[i].svName);
fgets(headerLine,256,pinFile);
sscanf(headerLine,"%d ",&svArr[i].svType);
headerLine[0] = 0;
//fscanf(pinFile,"%d\n",&svArr[i].svType);
switch (svArr[i].svType)
{
case -1:
svArr[i].svdefValue = vardefaults[i];
break;
case 0:
fgets(headerLine,256,pinFile);
sscanf(headerLine,"%f ",&svArr[i].svdefValue);
break;
case 1:
fgets(headerLine,256,pinFile);
sscanf(headerLine,"%s %s",&svArr[i].svFile,&svArr[i].svVarName);
break;
default:
cout<<"Wrong site variable type; has to be -1 (Use default values), 0 (single value) or 1 (2D netcdf)\n"<<endl;
cout<<"Using default value..."<<endl;
svArr[i].svdefValue = vardefaults[i];
//exit;
}
//i++;
headerLine[0] = 0;
}
}
fclose(pinFile);
return;
}
//function to read forcing/weather variables
void readInputForcVars(const char* inputconFile, inpforcvar *frArr)
{
ifstream pinFile(inputconFile);
char headerLine[256];
//istringstream valueLine;
pinFile.getline(headerLine,256); //skip header
for(int i=0; i<13;i++)
{
pinFile.getline(headerLine,256,':');
sscanf(headerLine,"%s ",&frArr[i].infName);
pinFile.getline(headerLine,256,'\n');
pinFile.getline(headerLine,256,'\n');
sscanf(headerLine,"%d ",&frArr[i].infType);
//headerLine[0] = 0;
//fscanf(pinFile,"%d\n",&svArr[i].svType);
switch (frArr[i].infType)
{
case -1:
pinFile.getline(headerLine, 256, '\n');
sscanf(headerLine, "%f ", &frArr[i].infdefValue);
break;
case 0:
pinFile.getline(headerLine,256,'\n');
sscanf(headerLine,"%s ",&frArr[i].infFile);
break;
case 1:
pinFile.getline(headerLine,256,'\n');
sscanf(headerLine,"%s %s %s %d",&frArr[i].infFile,&frArr[i].infvarName,&frArr[i].inftimeVar,&frArr[i].numNcfiles);
break;
case 2:
pinFile.getline(headerLine,256,'\n');
sscanf(headerLine,"%f ",&frArr[i].infdefValue);
break;
default:
cout<<"Wrong input/forcing type; has to be -1 (compute by the model), 2 (single value) , 0 (time-series text file) or 1 (3D netcdf)"<<endl;
cout<<"Using default value..."<<endl;
break; //exit(1);
}
//i++;
//headerLine[0] = 0;
//}
}
pinFile.close();
return;
}
//output control file: details of point, distributed netcdf, aggregated netcdf outputs
void readOutputControl(const char* outputconFile, pointOutput* &pOut, ncOutput* &ncOut, aggOutput* &aggOut, int &npout, int &nncout, int &naggOut)
{
ifstream poutFile(outputconFile);
char headerLine[256];
int nout;
//istringstream valueLine;
poutFile.getline(headerLine, 256); //skip header
//point outputs
poutFile.getline(headerLine, 256);
sscanf(headerLine, "%d ", &nout);
npout = nout;
pOut = new pointOutput[nout];
for (int i = 0; i < nout; i++)
{
poutFile.getline(headerLine, 256);
sscanf(headerLine, "%d %d %s ", &pOut[i].ycoord, &pOut[i].xcoord, &pOut[i].outfName);
}
//distributed netcdf output
poutFile.getline(headerLine, 256);
sscanf(headerLine, "%d ", &nout);
nncout = nout;
ncOut = new ncOutput[nout];
for (int i = 0; i < nout; i++)
{
poutFile.getline(headerLine, 256);
sscanf(headerLine, "%s %s %s ", &ncOut[i].symbol, &ncOut[i].outfName, &ncOut[i].units);
}
//poutFile.close();
//aggregated outputs
//ifstream paoutFile(aggoutputconFile);
//paoutFile.getline(headerLine, 256); //skip header
poutFile.getline(headerLine, 256);
sscanf(headerLine, "%d ", &nout);
naggOut = nout;
aggOut = new aggOutput[nout];
for (int i = 0; i < nout; i++)
{
poutFile.getline(headerLine, 256);
sscanf(headerLine, "%s %s %s ", &aggOut[i].symbol, &aggOut[i].units, &aggOut[i].aggop);
}
poutFile.close();
//paoutFile.close();
return;
}
//function to read input forcing time series text file
void readTextData(const char* inforcFile, float *&tcor_var, float *&tvar_in, int &nrecords)
{
FILE* inputFile = fopen(inforcFile,"r");
nrecords = 0;
char commentLine[256]; //string to read header line
while(!feof(inputFile))
{
commentLine[0] = ' ';
fgets(commentLine,256,inputFile);
if (commentLine[0] != ' ') //condition to make sure empty line is not read;
++nrecords;
}//while(!feof(inputfile))
//strinpts = new inptimeseries[nrecords]; //assign memory to store data records
tcor_var = new float[nrecords];
tvar_in = new float[nrecords];
rewind(inputFile);
fgets(commentLine,256,inputFile); //skip the comment lines
for (int i=0; i<nrecords; i++)
//#_15 check format of dtime here
fscanf(inputFile,"%f %f \n",&tcor_var[i],&tvar_in[i]);
fclose(inputFile);
}//void readTextData
// overload function with only the variable reading (input forcing time series text file)
void readTextData(const char* inforcFile, float *&tvar_in, int &nrecords)
{
ifstream inputFile(inforcFile,ios::in);
nrecords = 0;
char commentLine[256]; //string to read header line
if(!inputFile)
{
cout<<"Error opening file: "<<inforcFile<<endl;
return;
}
inputFile.getline(commentLine,256,'\n'); //skip first header line
//inputFile.getline(commentLine,256,'\n'); //first data line
while(!inputFile.eof())
{
commentLine[0] = ' ';
inputFile.getline(commentLine,256,'\n');
if (commentLine[0] != ' ') //condition to make sure empty line is not read;
++nrecords;
}//while
//cout<<"number of records in file: "<<inforcFile<<" "<<nrecords<<endl;
tvar_in = new float[nrecords];
inputFile.seekg(0L,ios::beg);
inputFile.getline(commentLine,256,'\n'); //skip first header line
for (int i=0; i<nrecords-1; i++)
{
inputFile.getline(commentLine,256,'\n');
sscanf(commentLine,"%*d %*d %*d %*d %f \n",&tvar_in[i]); //&tcor_var[i],&tvar_in[i]);
}
inputFile.close();
//for(int i=0; i< 100; i++)
// printf(" %f ", tvar_in[i]);
//printf(" \n");
}// __host__ __device__ void readTextData
//function to read input forcing time series text file
void readTStextFile(const char* inforcFile, float *&tvar_in, int &nrecords)
{
FILE* inputFile = fopen(inforcFile, "r");
nrecords = 0;
char commentLine[256]; //string to read header line
while (!feof(inputFile))
{
commentLine[0] = ' ';
fgets(commentLine, 256, inputFile);
if (commentLine[0] != ' ') //condition to make sure empty line is not read;
++nrecords;
}//while(!feof(inputfile))
//strinpts = new inptimeseries[nrecords]; //assign memory to store data records
tvar_in = new float[nrecords];
rewind(inputFile);
fgets(commentLine, 256, inputFile); //skip the comment lines
for (int i = 0; i<nrecords; i++)
//#_15 check format of dtime here
fscanf(inputFile, "%*d %*d %*d %*d %f \n", &tvar_in[i]);
fclose(inputFile);
}// readTextData