/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/hydrotrend/hydroreadinput.c

Go to the documentation of this file.
00001 /*
00002  *      HydroReadInput.c
00003  *
00004  *      Reads the ASCII input file for HYDROTREND
00005  *
00006  *      Author:         M.D. Morehead (June 1998)
00007  *      Author2:        S.D. Peckham (September 2001)
00008  *  Author3:        A.J. Kettner (August - October 2002, april 2003)
00009  *
00010  *      Variable        Def.Location            Type    Units   Usage
00011  *      --------        ------------            ----    -----   -----
00012  *      chs[120]        HydroReadInput.c        char    -               temporary character string
00013  *      dumchr[2]       HydroReadInput.c        char    -               temporary character string
00014  *      dumdbl          HydroReadInput.c        double  -               temporary double
00015  *      dumint          HydroReadInput.c        int             -               temporary integer
00016  *      err                     various                         int             -               error flag, halts program
00017  *      jj                      various                         int             -               temporary loop counter
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  *  Start of HydroReadInput
00030  *---------------------------*/
00031 int hydroreadinput()
00032 {
00033 /*-------------------
00034  *  Local Variables
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  *  Open the input file
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  *  1) Read in title of first epoch (-)
00057  *---------------------------------------*/
00058 fgets( title[0], 120, fidinput);
00059 
00060 /*-----------------------------------------------------------------
00061  *  2) Read the option of writing output yes or no to ascii file
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  *  3) Reed the output directory in
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  *  4) Read in number of epochs (-)
00090  *-----------------------------------*/
00091 fscanf( fidinput, "%d", &nepochs);
00092 fgets( chs, 120, fidinput );
00093 
00094 /*-----------------------------------
00095  *  Loop through number of epochs
00096  *  specified and retrieve the data
00097  *-----------------------------------*/
00098 for (ep=0; ep<nepochs; ep++ ){
00099 
00100    /*---------------------------------------------------------------
00101     *  5) Read start year, number of years and timestep (a,-,char)
00102     *    Keep timestep from first epoch for all subsequent epochs
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     *  6) Read start and end years for the Table output (a,a)
00121     *----------------------------------------------------------*/
00122    fscanf( fidinput, "%d %d ", &tblstart[ep], &tblend[ep]);
00123    fgets( chs, 120, fidinput );
00124 
00125    /*---------------------------------------------------
00126     *  7) Read number of grain sizes to simulate (int)
00127     *   Must be constant for all epochs
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     *  8) Read percentage of each grain size (%)
00147     *---------------------------------------------*/
00148    for( jj=0; jj<ngrain; jj++ )
00149       fscanf( fidinput, "%lf  ", &grainpct[jj][ep]);
00150    fgets( chs, 120, fidinput );
00151 
00152    /*--------------------------------------------------------------
00153     *  9) Read temperature trend parameters ( degC, degC/a, degC)
00154     *--------------------------------------------------------------*/
00155    fscanf( fidinput, "%lf %lf %lf ", &Tstart[ep], &Tchange[ep], &Tstd[ep]);
00156    fgets( chs, 120, fidinput );
00157 
00158    /*--------------------------------------------------------------
00159     *  10) Read precipitation trend parameters (m/a, (m/a)/a, m/a)
00160     *--------------------------------------------------------------*/
00161    fscanf( fidinput, "%lf %lf %lf ", &Pstart[ep], &Pchange[ep], &Pstd[ep]);
00162    fgets( chs, 120, fidinput );
00163 
00164    /*-------------------------------------------------------
00165     *  11) Read rainfall mass balance parameters ( %, -, -)
00166     *-------------------------------------------------------*/
00167    fscanf( fidinput, "%lf %lf %lf ", &Pmassbal[ep], &Pexponent[ep], &Prange[ep]);
00168    fgets( chs, 120, fidinput );
00169 
00170    /*-------------------------------------------------
00171     *  12) Read constant base flow discharge (m^3/s)
00172     *-------------------------------------------------*/
00173    fscanf( fidinput, "%lf ", &baseflowtot[ep]);
00174    fgets( chs, 120, fidinput );
00175 
00176    /*--------------------------------------------------------------------------
00177     *  13-24) Read climate statistics ( month, degC, degC, mm -> m, mm -> m )
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     *  25) Read lapse rate ( degC/km -> degC/m )
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     *  26) Read ELA start altitude and change per year ( m, m/a)
00201     *-------------------------------------------------------------*/
00202    fscanf( fidinput, "%lf %lf ", &ELAstart[ep], &ELAchange[ep]);
00203    fgets( chs, 120, fidinput );
00204 
00205    /*-------------------------------------------------------------------
00206     *  27) Read percentage of nival/ice runoff lost to evaporation (%)
00207     *-------------------------------------------------------------------*/
00208    fscanf( fidinput, "%lf ", &dryevap[ep]);
00209    fgets( chs, 120, fidinput );
00210 
00211    /*----------------------------------------------
00212     *  28) Read river bed average slope (m/m)
00213     *----------------------------------------------*/
00214    fscanf( fidinput, "%lf ", &rslope[ep]);
00215    fgets( chs, 120, fidinput );
00216 
00217    /*-------------------------------------
00218     *  29) Read basin length ( km -> m )
00219     *-------------------------------------*/
00220    fscanf( fidinput, "%lf ", &basinlength[ep]);
00221    fgets( chs, 120, fidinput );
00222    basinlength[ep] *= 1000;
00223 
00224    /*-----------------------------------------------------
00225     *  30) Read percentage of basin covered by lakes (%)
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     *  31) Read river mouth velocity coeff and exponent ( -, -)
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     *  32) Read river mouth width coeff and exponent ( -, -)
00246     *    Calculate river mouth depth coeff and exponent ( -, -)
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     *  33) Read average river velocity (m/s)
00261     *-----------------------------------------*/
00262    fscanf( fidinput, "%lf ", &avgvel[ep]);
00263    fgets( chs, 120, fidinput );
00264 
00265    /*-------------------------------------------------------------
00266     *  34) Read max and min size of groundwater pool ( m^3, m^3)
00267     *-------------------------------------------------------------*/
00268    fscanf( fidinput, "%lf %lf ", &gwmax[ep], &gwmin[ep]);
00269    fgets( chs, 120, fidinput );
00270 
00271    /*---------------------------------------------------
00272     *  35) Read initial size of groundwater pool (m^3)
00273     *    for first epoch only
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     *  36) Read subsurface storm flow coeff and exponent ( m^3/s, -)
00286     *-----------------------------------------------------------------*/
00287    fscanf( fidinput, "%lf %lf ", &alphass[ep], &betass[ep]);
00288    fgets( chs, 120, fidinput );
00289 
00290    /*-----------------------------------------------------------------
00291     *  37) Read saturated hydraulic conductivity ( mm/day -> m/day )
00292     *-----------------------------------------------------------------*/
00293    fscanf( fidinput, "%lf ", &Ko[ep]);
00294    fgets( chs, 120, fidinput );
00295    Ko[ep] /= 1000;
00296 
00297    /*------------------------------------------------------------------------
00298     *  38) Read latitude (Geographic position of rivermouth) ( in degrees )
00299     *------------------------------------------------------------------------*/
00300    fscanf( fidinput, "%lf %lf", &lon, &lat); 
00301    fgets( chs, 120, fidinput );
00302 
00303    /*------------------------------------------------
00304     *  39) Read number of outlets to simulate (int)
00305     *   Must be constant for all epochs
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     *  40) Read percentage of each outlet 
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     *  41) Read number of events
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     *  42) Read the demping factor for Qsbar 
00387     *-----------------------------------------*/
00388    fscanf( fidinput, "%lf  ", &sedfilter[ep] );
00389    if ( noutlet == 1 )
00390                 sedfilter[ep] = 0.0;
00391    fgets( chs, 120, fidinput );
00392 
00393    /*-----------------------------------------------------
00394     *  43) Check which formula to use to calculate Qsbar 
00395     *-----------------------------------------------------*/
00396    fscanf( fidinput, "%d  ", &Qsbarformulaflag[ep] );
00397    fgets( chs, 120, fidinput );
00398                 
00399    /*-------------------------------------------------------
00400     *  Set limitations if the model is run-ed from the web
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  } /* end epoch loop */
00422  fclose(fidinput);
00423  return(err);
00424 }       /* end of HydroReadInput */

Generated on Fri Jan 4 18:04:14 2008 for sedflux by  doxygen 1.5.2