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

Go to the documentation of this file.
00001 /*
00002  *  Hydroreadclimate.c
00003  *
00004  *
00005  *  Author:   A.J. Kettner (August-October 2002) (February 2003)
00006  *
00007  *
00008  * Variable             Def.Location            Type            Units   Usage
00009  * --------             ------------            ----            -----   -----
00010  * err                  various                         int                     -               error flag, halts program
00011  * count                Read_Rainfall_Etc       int                     -
00012  * dummyT               Read_Rainfall_Etc       double          deg.C
00013  * dummyTtot    Read_Rainfall_Etc       double          deg.C
00014  * dummyT2              Read_Rainfall_Etc       double          deg.C
00015  * dummyR               Read_Rainfall_Etc       double          mm
00016  * dummyRtot    Read_Rainfall_Etc       double          mm
00017  * HOURSINDAY   Read_Rainfall_Etc       -                       hours   define the hours in a day
00018  * i                    Read_Rainfall_Etc       int                     -
00019  * k                    Read_Rainfall_Etc       int                     -
00020  * line[200]    Read_Rainfall_Etc       char            -
00021  * n                    Read_Rainfall_Etc       long            -               counter for the number of years
00022  */
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <math.h>
00028 #include "hydroalloc_mem.h"
00029 #include "hydroinout.h"
00030 #include "hydroparams.h"
00031 #include "hydroreadclimate.h"
00032 #include "hydrotimeser.h"
00033 #include "hydroclimate.h"
00034 
00035 /*-----------------------
00036  *  Function Definition
00037  *-----------------------*/
00038 void Read_Rainfall_Etc(gw_rainfall_etc* gw_rain);
00039 
00040 /*---------------------------
00041  *  Start of HydroReadInput
00042  *---------------------------*/
00043 int hydroreadclimate(gw_rainfall_etc* gw_rain) {
00044 
00045 /*-------------------
00046  *  Local Variables
00047  *-------------------*/
00048 int k,err;
00049 
00050 /*-----------------------
00051  *  Set local Variables
00052  *-----------------------*/
00053 err=0;
00054 ep=0;
00055 total_yr=0;
00056 for (k=0; k<nepochs; k++)
00057         total_yr += nyears[ep];
00058         
00059 /*-------------------------------
00060  * Read Rainfall input FOR NOW
00061  *-------------------------------*/
00062     Read_Rainfall_Etc(gw_rain);
00063     return(err);
00064 } /* end hydroreadclimate.c */
00065 
00066    
00067 /*-----------------------
00068  *  Function Prototypes
00069  *-----------------------*/
00070   
00071 /*------------------------------
00072  *  Function Read_Rainfall_Etc
00073  *------------------------------*/
00074 void Read_Rainfall_Etc(gw_rainfall_etc* gw_rain ) {
00075 
00076 /*-------------------
00077  *  Local Variables
00078  *-------------------*/
00079 #define HOURSINDAY  (24.0) 
00080 int k, count,i;
00081 long n;
00082 char line[200];
00083 double dummyT, dummyR, dummyT2, dummyTtot, dummyRtot, dummydouble;
00084 
00085 /*------------------------
00086  *  Open rain input file
00087  *------------------------*/
00088         if (webflag == 1){
00089                 if ((fidinputgw_r = fopen(ffnameinputgw_r, "r")) == NULL){
00090                 fprintf(stderr, "  Read_Rainfall_Etc MESSAGE: Unable to open input file %s \n",ffnameinputgw_r);
00091                 fprintf(stderr, "    Hydrotrend will generate it's own climate values based on\n");
00092                     fprintf(stderr, "    line 14-25 of the input values in the input file.\n\n");
00093                 raindatafile = 0;
00094             }
00095             else raindatafile = 1;
00096         }
00097         if (webflag != 1){
00098                 if ((fidinputgw_r = fopen(fnameinputgw_r, "r")) == NULL){
00099                 fprintf(stderr, "  Read_Rainfall_Etc MESSAGE: Unable to open input file %s \n",fnameinputgw_r);
00100                 fprintf(stderr, "    Hydrotrend will generate it's own climate values based on\n");
00101                     fprintf(stderr, "    line 14-25 of the input values in the input file.\n\n");
00102                 raindatafile = 0;
00103             }
00104         else raindatafile = 1;
00105         }
00106         if (raindatafile == 1){
00107                 
00108 /*--------------------
00109  *  Strip off header
00110  *--------------------*/
00111                 for (k=0; k < 6; k++)
00112                         fgets(line, sizeof(line), fidinputgw_r);
00113 
00114 /*---------------------------------
00115  *  Read number of timesteps & dt
00116  *---------------------------------*/
00117                 fgets(line, sizeof(line), fidinputgw_r);
00118                 sscanf(line, "%ld %ld", &gw_rain->n_steps, &gw_rain->dt);
00119 
00120 /*---------------------------------------------------
00121  *  Check number of years to run with actually rain
00122  *  data. If actually rain data is not equal with 
00123  *  the number of years to run, stop program.
00124  *---------------------------------------------------*/
00125                 if (gw_rain->n_steps/daysiy < total_yr){
00126                         dummydouble = gw_rain->n_steps/daysiy;
00127                         fprintf(stderr, "  HydroReadclimate.c ERROR: Unable to run hydrotrend because of\n"); 
00128                         fprintf(stderr, "    insufficient climate data\n");
00129                         fprintf(stderr, "    Number of model years in HYDRO.IN, total years from all epochs: %d (line 5).\n",total_yr);
00130                         fprintf(stderr, "    is not equal to number of years of climate data: %f.\n",dummydouble);
00131                         fprintf(stderr, "    program aborted \n");
00132                         exit(-1);
00133                 }       
00134 
00135 /*------------------------------
00136  *  Allocate memory for arrays
00137  *------------------------------*/
00138                 gw_rain->R = malloc2d(total_yr, daysiy, double);
00139                 gw_rain->T = malloc2d(total_yr, daysiy, double);
00140                 gw_rain->Tperyear = malloc1d(total_yr, double);
00141  
00142 /*----------------------------------------------
00143  *  Read Precipitation and Temperature values
00144  *  If values are not daily values 
00145  *  but for example hour values, then
00146  *  average the temperature and add
00147  *  all the precipitation values for that day.
00148  *----------------------------------------------*/
00149                 for (n=0; n < total_yr; n++){
00150                         dummyT2=0.0;
00151                         for (k=0; k<daysiy; k++){
00152                                 dummyRtot = 0.0;
00153                                 dummyTtot = 0.0;
00154                                 for (i=0; i<(HOURSINDAY/gw_rain->dt); i++){
00155                                         fgets(line, sizeof(line), fidinputgw_r);
00156                                         count = sscanf(line, "%lf %lf",&dummyR, &dummyT);
00157                                         if (count != 2){
00158                                                 n =(n+1)*(k+1)*(i+1);
00159                                                 fprintf(stderr, "  HydroReadclimate.c ERROR: Error occured when\n" );
00160                                                 fprintf(stderr, "    trying to read line %ld\n", n );
00161                                                 fprintf(stderr, "    of file: %s.\n",fnameinputgw_r );
00162                                                 fprintf(stderr, "    Precipitation or Temperature data is missing\n" );
00163                                         fprintf(stderr, "    program aborted \n" );
00164                                         exit(-1);
00165                                 }
00166                                         if (dummyR < 0.0 || dummyR > 1000.0){
00167                                                 fprintf(stderr, "  HydroReadclimate.c ERROR:\n" );
00168                                                 fprintf(stderr, "    Precipitation data out of range,\n");
00169                                                 fprintf(stderr, "    ppt is 1000.0 < %f < 0.0\n ",dummyR );
00170                                                 fprintf(stderr, "    program aborted \n" );             
00171                                                 exit(-1);
00172                                         }
00173                                         if (dummyT < -50.0 || dummyT > 50.0){
00174                                                 fprintf(stderr, "  HydroReadclimate.c ERROR:\n" );
00175                                                 fprintf(stderr, "    Precipitation data out of range,\n");
00176                                                 fprintf(stderr, "    ppt of 80.0 < %f < -80.0 ",dummyT );
00177                                                 fprintf(stderr, "    program aborted \n" );             
00178                                                 exit(-1);
00179                                         }               
00180                                         dummyRtot += dummyR;
00181                                         dummyTtot += dummyT;
00182                                 }
00183                                 dummyTtot = ((dummyTtot)/(HOURSINDAY/gw_rain->dt));
00184                                 gw_rain->R[n][k] = dummyRtot;
00185                                 gw_rain->T[n][k] = dummyTtot;
00186                                 gw_rain->R[n][k] *= (Pmassbal[ep]/1000.0);
00187                                 dummyT2 +=gw_rain->T[n][k];
00188                         }
00189                         gw_rain->Tperyear[n]=dummyT2/daysiy;
00190                 }
00191 /*--------------------
00192  *  Close input file
00193  *--------------------*/
00194                 fclose(fidinputgw_r);
00195         }
00196     return;
00197 }  /* end Read_Rainfall_Etc */   
00198 
00199 

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