00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include "hydroclimate.h"
00023 #include "hydroinout.h"
00024 #include "hydroparams.h"
00025 #include "hydroalloc_mem.h"
00026 #define MAXDIR (100)
00027
00028
00029
00030
00031 int hydroreadinput()
00032 {
00033
00034
00035
00036 char chs[120], dumchr[2];
00037 int jj, err, dumint,totyears, k;
00038 double dumdbl;
00039 char dummyx;
00040 err = 0;
00041 k=0;
00042 totyears=0;
00043
00044
00045
00046
00047 if (webflag == 0)
00048 if ( (fidinput = fopen(fnameinput,"r")) == NULL) {
00049 fprintf(stderr, " HydroReadInput.c ERROR: Unable to open the input file %s \n",fnameinput);
00050 fprintf(stderr, " Make sure the input file name is all in capitals\n");
00051 fprintf(stderr, " program aborted \n");
00052 exit(1);
00053 }
00054
00055
00056
00057
00058 fgets( title[0], 120, fidinput);
00059
00060
00061
00062
00063 for( jj=0; jj<MAXCHAR; jj++ ) {
00064 fscanf( fidinput, "%c", &asciioutput[jj]);
00065 asciioutput[jj]=toupper(asciioutput[jj]);
00066 if (asciioutput[jj] == ' ')
00067 jj = MAXCHAR;
00068 }
00069 fgets( chs, 120, fidinput );
00070
00071
00072
00073
00074 if (webflag == 0){
00075 for( jj=0; jj<MAXDIR; jj++ ) {
00076 fscanf( fidinput, "%c", &directory[jj]);
00077 if (directory[jj] == ' ' || directory[jj] == '\t'){
00078 if (directory[jj-1] != '/')
00079 directory[jj] = '/';
00080 else directory[jj] = '\0';
00081 jj = MAXDIR;
00082 }
00083 }
00084 }
00085 else strcpy(directory,OUTPUT_DIR);
00086 fgets( chs, 120, fidinput );
00087
00088
00089
00090
00091 fscanf( fidinput, "%d", &nepochs);
00092 fgets( chs, 120, fidinput );
00093
00094
00095
00096
00097
00098 for (ep=0; ep<nepochs; ep++ ){
00099
00100
00101
00102
00103
00104 if( ep != 0 ) strcpy(dumchr,timestep);
00105 fscanf( fidinput, "%d %d ", &syear[ep], &nyears[ep]);
00106 fgets( timestep, 2, fidinput );
00107 timestep[0]=tolower(timestep[0]);
00108 fgets( chs, 120, fidinput );
00109 if( ep != 0 && dumchr[0] != timestep[0] ) {
00110 fprintf( stderr," HydroReadInput.c WARNING: timestep changed between epochs. \n");
00111 fprintf( stderr," Hydrotrend will use the timestep from the begining epoch. \n");
00112 fprintf( stderr," Present epoch = %d \n", ep+1 );
00113 fprintf( stderr," epoch # 1 timestep = %s \n", dumchr);
00114 fprintf( stderr," epoch # %d timestep = %s \n", ep, timestep);
00115 fprintf( stderr," syear=%d, nyears=%d\n",syear[ep], nyears[ep]);
00116 strcpy(timestep,dumchr);
00117 }
00118
00119
00120
00121
00122 fscanf( fidinput, "%d %d ", &tblstart[ep], &tblend[ep]);
00123 fgets( chs, 120, fidinput );
00124
00125
00126
00127
00128
00129 if( ep == 0 ) {
00130 fscanf( fidinput, "%d ", &ngrain);
00131 fgets( chs, 120, fidinput );
00132 }
00133 else {
00134 fscanf( fidinput, "%d ", &dumint);
00135 fgets( chs, 120, fidinput );
00136 if( dumint != ngrain ) {
00137 fprintf( stderr, " HydroReadInput.c ERROR: ngrain must be constant for all epochs. \n");
00138 fprintf( stderr, " present epoch = %d \n", ep+1 );
00139 fprintf( stderr, " initial ngrain = %d \n", ngrain );
00140 fprintf( stderr, " present ngrain = %d \n", dumint );
00141 err = 1;
00142 }
00143 }
00144
00145
00146
00147
00148 for( jj=0; jj<ngrain; jj++ )
00149 fscanf( fidinput, "%lf ", &grainpct[jj][ep]);
00150 fgets( chs, 120, fidinput );
00151
00152
00153
00154
00155 fscanf( fidinput, "%lf %lf %lf ", &Tstart[ep], &Tchange[ep], &Tstd[ep]);
00156 fgets( chs, 120, fidinput );
00157
00158
00159
00160
00161 fscanf( fidinput, "%lf %lf %lf ", &Pstart[ep], &Pchange[ep], &Pstd[ep]);
00162 fgets( chs, 120, fidinput );
00163
00164
00165
00166
00167 fscanf( fidinput, "%lf %lf %lf ", &Pmassbal[ep], &Pexponent[ep], &Prange[ep]);
00168 fgets( chs, 120, fidinput );
00169
00170
00171
00172
00173 fscanf( fidinput, "%lf ", &baseflowtot[ep]);
00174 fgets( chs, 120, fidinput );
00175
00176
00177
00178
00179 for( jj=0; jj<12; jj++ ) {
00180 fscanf( fidinput, "%3s %lf %lf %lf %lf ", moname[jj], &Tnominal[jj][ep], &Tnomstd[jj][ep], &Pnominal[jj][ep], &Pnomstd[jj][ep]);
00181 fgets( chs, 120, fidinput );
00182 Pnominal[jj][ep] /= 1000;
00183 Pnomstd[jj][ep] /= 1000;
00184 }
00185
00186
00187
00188
00189 fscanf( fidinput, "%lf ", &lapserate[ep]);
00190 fgets( chs, 120, fidinput );
00191 lapserateflag = 0;
00192 if (lapserate[ep] == -9999){
00193 globalparflag++;
00194 lapserateflag = 1;
00195 }
00196 else
00197 lapserate[ep] /= 1000;
00198
00199
00200
00201
00202 fscanf( fidinput, "%lf %lf ", &ELAstart[ep], &ELAchange[ep]);
00203 fgets( chs, 120, fidinput );
00204
00205
00206
00207
00208 fscanf( fidinput, "%lf ", &dryevap[ep]);
00209 fgets( chs, 120, fidinput );
00210
00211
00212
00213
00214 fscanf( fidinput, "%lf ", &rslope[ep]);
00215 fgets( chs, 120, fidinput );
00216
00217
00218
00219
00220 fscanf( fidinput, "%lf ", &basinlength[ep]);
00221 fgets( chs, 120, fidinput );
00222 basinlength[ep] *= 1000;
00223
00224
00225
00226
00227 fscanf( fidinput, "%lf %c", &Rvol[ep], &Rparamcheck[ep]);
00228 if (Rparamcheck[ep] == 'a' || Rparamcheck[ep] == 'A')
00229 fscanf( fidinput, "%lf", &Ralt[ep]);
00230 if (Rparamcheck[ep] == 'd' || Rparamcheck[ep] == 'D')
00231 fscanf( fidinput, "%lf", &Rarea[ep]);
00232 fgets( chs, 120, fidinput );
00233
00234
00235
00236
00237 fscanf( fidinput, "%lf %lf ", &velcof[ep], &velpow[ep]);
00238 fgets( chs, 120, fidinput );
00239 if (velpow[ep] == -9999){
00240 globalparflag++;
00241 velpowflag = 1;
00242 }
00243
00244
00245
00246
00247
00248 fscanf( fidinput, "%lf %lf ", &widcof[ep], &widpow[ep]);
00249 fgets( chs, 120, fidinput );
00250 if (widpow[ep] == -9999){
00251 globalparflag++;
00252 widpowflag = 1;
00253 }
00254 if ( widpowflag != 1 && velpowflag != 1){
00255 depcof[ep] = 1/(velcof[ep]*widcof[ep]);
00256 deppow[ep] = 1 - velpow[ep] - widpow[ep];
00257 }
00258
00259
00260
00261
00262 fscanf( fidinput, "%lf ", &avgvel[ep]);
00263 fgets( chs, 120, fidinput );
00264
00265
00266
00267
00268 fscanf( fidinput, "%lf %lf ", &gwmax[ep], &gwmin[ep]);
00269 fgets( chs, 120, fidinput );
00270
00271
00272
00273
00274
00275 if( ep == 0 ) {
00276 fscanf( fidinput, "%lf ", &gwinitial);
00277 fgets( chs, 120, fidinput );
00278 }
00279 else {
00280 fscanf( fidinput, "%lf ", &dumdbl);
00281 fgets( chs, 120, fidinput );
00282 }
00283
00284
00285
00286
00287 fscanf( fidinput, "%lf %lf ", &alphass[ep], &betass[ep]);
00288 fgets( chs, 120, fidinput );
00289
00290
00291
00292
00293 fscanf( fidinput, "%lf ", &Ko[ep]);
00294 fgets( chs, 120, fidinput );
00295 Ko[ep] /= 1000;
00296
00297
00298
00299
00300 fscanf( fidinput, "%lf %lf", &lon, &lat);
00301 fgets( chs, 120, fidinput );
00302
00303
00304
00305
00306
00307 noutletflag = 0;
00308 outletmodelflag = 0;
00309 fscanf(fidinput, "%c", &dummyx);
00310 if (dummyx == 'u' || dummyx == 'U'){
00311 noutletflag = 1;
00312 outletmodelflag = 1;
00313 minnoutlet = 2;
00314 maxnoutlet = 9;
00315 }
00316 else if (dummyx == 'r' || dummyx == 'R'){
00317 noutletflag = 1;
00318 outletmodelflag = 1;
00319 fscanf(fidinput, "%d %d", &minnoutlet, &maxnoutlet);
00320 }
00321 else {
00322 fseek(fidinput,sizeof(char)*-1,SEEK_CUR);
00323 fscanf(fidinput, "%d ", &noutlet);
00324 minnoutlet = 2;
00325 maxnoutlet = noutlet;
00326 if ( noutlet == 1 ){
00327 minnoutlet = 0;
00328 maxnoutlet = 1;
00329 }
00330 if ( noutlet > 1 )
00331 outletmodelflag = 1;
00332 }
00333 fgets( chs, 120, fidinput );
00334
00335
00336
00337
00338 steadyoutletpctflag = 0;
00339 nooutletpctflag = 0;
00340 if (dummyx == 'r' || dummyx == 'R' || dummyx == 'u' || dummyx == 'U'){
00341 steadyoutletpctflag = 1;
00342 nooutletpctflag = 1;
00343 }
00344 if (noutletflag == 0){
00345 fscanf (fidinput, " %c", &dummyx);
00346 if (dummyx == 'u' || dummyx == 'U'){
00347 nooutletpctflag = 1;
00348 fscanf ( fidinput, " %c ", &dummyx);
00349 if (dummyx == 's' || dummyx == 'S')
00350 steadyoutletpctflag = 0;
00351 else if (dummyx == 'u' || dummyx == 'U')
00352 steadyoutletpctflag = 1;
00353 else err++;
00354 }
00355 else if ( dummyx == '0' || dummyx == '1' ){
00356 if ( ep == 0 )
00357 outletpctdummy = malloc2d( maxnoutlet, maxepoch, double );
00358 fseek(fidinput,sizeof(char)*-1,SEEK_CUR);
00359 for( jj=0; jj<noutlet; jj++ )
00360 fscanf( fidinput, "%lf ", &outletpctdummy[jj][ep]);
00361 }
00362 }
00363 fgets( chs, 120, fidinput );
00364
00365
00366
00367
00368 fscanf (fidinput, "%c", &dummyx);
00369 if (dummyx == 'n' || dummyx == 'N'){
00370 fscanf( fidinput, "%d ", &eventsnr[ep] );
00371 floodvalue[ep] = 0.0;
00372 eventnrflag = 0;
00373 if ( eventsnr[ep] == 0 )
00374 eventsnr[ep] = 1;
00375 if ( steadyoutletpctflag == 0 )
00376 eventsnr[ep] = 1;
00377 }
00378 if (dummyx =='q' || dummyx == 'Q' ){
00379 eventsnr[ep] = 1;
00380 eventnrflag = 1;
00381 fscanf( fidinput, "%lf", &floodvalue[ep] );
00382 }
00383 fgets( chs, 120, fidinput );
00384
00385
00386
00387
00388 fscanf( fidinput, "%lf ", &sedfilter[ep] );
00389 if ( noutlet == 1 )
00390 sedfilter[ep] = 0.0;
00391 fgets( chs, 120, fidinput );
00392
00393
00394
00395
00396 fscanf( fidinput, "%d ", &Qsbarformulaflag[ep] );
00397 fgets( chs, 120, fidinput );
00398
00399
00400
00401
00402 if ( webflag == 1 ){
00403 for ( ep=0; ep<nepochs; ep++ )
00404 totyears = nyears[ep];
00405 if (( totyears > 5000 ) && ( asciioutput[1] == 'N' )){
00406 strcpy(asciioutput,OFF);
00407 fprintf( stderr," HydroReadInput.c WARNING: Total years, %d > 5000. \n",totyears);
00408 fprintf( stderr," Hydrotrend for the web is set in such a way that it won't \n");
00409 fprintf( stderr," write ascii output as total years exceed 5000yr. \n");
00410 fprintf( stderr," So the ascii option in the inputfile will be ignored. \n ");
00411 }
00412 if ((totyears > 10000) && (timestep[0] != 'y')){
00413 timestep[0] = 'y';
00414 fprintf( stderr," HydroReadInput.c WARNING: only yearly output is generated. \n");
00415 fprintf( stderr," Hydrotrend for the web is set in such a way that it only \n");
00416 fprintf( stderr," writes yearly output when %d > 10000. \n",totyears);
00417 fprintf( stderr," So the output interval option of the inputfile is overruled. \n ");
00418 }
00419
00420 }
00421 }
00422 fclose(fidinput);
00423 return(err);
00424 }