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

Go to the documentation of this file.
00001 /*
00002  *  HydroOutput.c
00003  *
00004  *  Writes the discharge (v,w,d) and sediment load
00005  *  data to a binary file.  After averaging the data
00006  *  to the user requested timestep (d,m,s,y).
00007  *
00008  *  Author1:   M.D. Morehead  (June 1998)
00009  *  Author2:   S.D. Peckham   (January 2002)
00010  *  Author3:   A.J. Kettner   (September 2002) (February 2003)
00011  *
00012  * Variable             Def.Location    Type    Units   Usage
00013  * --------             ------------    ----    -----   -----
00014  * comLen               HydroOutput.c   int             -               length of the title string
00015  * Cs                   HydroOutput.c   float   kg/m^3  averaged suspended concentration per grain class
00016  * Cstot                HydroOutput.c   float   kg/m^3  averaged suspended concentration total
00017  * dep[]                HydroOutput.c   float   m               river depth
00018  * err                  various                 int             -               error flag, halts program
00019  * ii                   various                 int             -               temporary loop counter
00020  * jj                   various                 int             -               temporary loop counter
00021  * kk                   various                 int             -               temporary loop counter
00022  * nrecords             HydroOutput.c   int             -               total # of output records
00023  * nYears               HydroOutput.c   int             -               total # of output years
00024  * recperyear   HydroOutput.c   int             -               # of output records per year
00025  * Qavg[]               HydroOutput.c   float   m^3/s   average river discharge
00026  * Qbavg[]              HydroOutput.c   float   kg/s    averaged bedload discharge
00027  * Qsavg[]              HydroOutput.c   float   kg/s    averaged suspended load flux
00028  * vel[]                HydroOutput.c   float   m/s             river velocity
00029  * wid[]                HydroOutput.c   float   m               river width
00030  *
00031  */
00032 
00033 #include <string.h>
00034 #include "hydroclimate.h"
00035 #include "hydroparams.h"
00036 #include "hydrotimeser.h"
00037 #include "hydroinout.h"
00038 #include "hydroreadclimate.h"
00039 #include "hydroalloc_mem.h"
00040 #include "hydrofree_mem.h"
00041 #include "hydrodaysmonths.h"
00042 
00043 /*------------------------
00044  *  Start of HydroOutput
00045  *------------------------*/
00046 int hydrooutput()
00047 {
00048 static int daystrs[4] = { 1,  91, 182, 274};
00049 static int dayends[4] = {90, 181, 273, 365};
00050 static int daysis[4]  = {90,  91,  92,  92};
00051         
00052 /*-------------------
00053  *  Local Variables
00054  *-------------------*/
00055 int     ii, jj, kk,p;
00056 int     err, comLen, nrecords, nYears;
00057 
00058 static int recperyear;
00059 
00060 float   vel[daysiy], wid[daysiy], dep[daysiy];
00061 float   **veloutlet, **widoutlet, **depoutlet;
00062 float   Qavg[daysiy], Qbavg[daysiy], **Qavgoutlet, **Qbavgoutlet;
00063 float   Qsavg[daysiy], **Qsavgoutlet;
00064 float   Cs, Cstot, *Csoutlet, *Cstotoutlet;
00065 Cstot = 0.0;
00066 err = 0;
00067 
00068 /*---------------------------------------
00069  *  Allocate memory for multiple outlet
00070  *---------------------------------------*/
00071         veloutlet                       = malloc2d( daysiy, maxnoutlet, float );
00072         widoutlet                       = malloc2d( daysiy, maxnoutlet, float );
00073         depoutlet                       = malloc2d( daysiy, maxnoutlet, float );
00074         Qavgoutlet                      = malloc2d( daysiy, maxnoutlet, float );
00075         Qbavgoutlet                     = malloc2d( daysiy, maxnoutlet, float );
00076         Qsavgoutlet                     = malloc2d( daysiy, maxnoutlet, float );
00077         Csoutlet                        = malloc1d( maxnoutlet, float );
00078         Cstotoutlet                     = malloc1d( maxnoutlet, float );
00079 
00080 /*------------------------------------------------
00081  *  Print the header to the binary file (fiddis)
00082  *  Opened in HydroOpenFiles.c
00083  *  Closed in HydroTrend.c
00084  *------------------------------------------------*/
00085 if ( yr == syear[0] ) {
00086    if(      timestep[0] == 'd' ) recperyear = 365;
00087    else if( timestep[0] == 'm' ) recperyear = 12;
00088    else if( timestep[0] == 's' ) recperyear = 4;
00089    else recperyear = 1;
00090 
00091    nYears       = syear[nepochs-1]+nyears[nepochs-1]-syear[0];
00092    comLen       = strlen(title[0])-1;
00093    nrecords     = nYears*recperyear;
00094         fwrite(     &comLen,  sizeof(int),      1, fiddistot);
00095         fwrite(    title[0], sizeof(char), comLen, fiddistot);
00096         fwrite(     &ngrain,  sizeof(int),      1, fiddistot);
00097         fwrite( &recperyear,  sizeof(int),      1, fiddistot);
00098         fwrite(   &nrecords,  sizeof(int),      1, fiddistot);
00099    if (outletmodelflag == 1)
00100         for (p=0; p<maxnoutlet; p++){
00101                 fwrite(     &comLen,  sizeof(int),      1, fiddis[p]);
00102                 fwrite(    title[0], sizeof(char), comLen, fiddis[p]);
00103                 fwrite(     &ngrain,  sizeof(int),      1, fiddis[p]);
00104                 fwrite( &recperyear,  sizeof(int),      1, fiddis[p]);
00105                 fwrite(   &nrecords,  sizeof(int),      1, fiddis[p]);
00106         }
00107 }
00108 
00109 /*-------------------------------------
00110  *  Average over the desired interval
00111  *-------------------------------------*/
00112 if (timestep[0] == 'd') {
00113 
00114    /*------------------------------------------
00115     *  Calculate V,W,D.
00116     *  Daily Qb and Cs are already calculated
00117     *------------------------------------------*/
00118         for( jj=0; jj<recperyear; jj++ ){
00119                 Qavg[jj]        = (float)(Qsumtot[jj]);
00120                 vel[jj]         = (float)(velcof[ep] * pow( Qavg[jj], velpow[ep] ));
00121                 wid[jj]         = (float)(widcof[ep] * pow( Qavg[jj], widpow[ep] ));
00122                 dep[jj]         = (float)(depcof[ep] * pow( Qavg[jj], deppow[ep] ));
00123                 Qbavg[jj]       = (float)(Qb[jj]);
00124                 Qsavg[jj]       = (float)(Qs[jj]);
00125                 if (outletmodelflag == 1)
00126                         for (p=0; p<maxnoutlet; p++){
00127                                 Qavgoutlet[jj][p]               = (float)(Qsum[jj][p]);
00128                                 veloutlet[jj][p]                = (float)(velcof[ep] * pow( Qavgoutlet[jj][p], velpow[ep] ));
00129                                 widoutlet[jj][p]                = (float)(widcof[ep] * pow( Qavgoutlet[jj][p], widpow[ep] ));
00130                                 depoutlet[jj][p]                = (float)(depcof[ep] * pow( Qavgoutlet[jj][p], deppow[ep] ));
00131                                 Qbavgoutlet[jj][p]      = (float)(Qboutlet[jj][p]);
00132                                 Qsavgoutlet[jj][p]      = (float)(Qsoutlet[jj][p]);
00133                         }
00134    }
00135 }
00136 else if( timestep[0] == 'm' ) {
00137    /*-----------------------------------------------------------
00138     *  Average over each month and then compute monthly values
00139     *-----------------------------------------------------------*/
00140 //      for (jj=0; jj<recperyear; jj++)
00141         for (jj=Jan; jj<=Dec; jj++)
00142         {
00143                 Qavg[jj] = 0.0;
00144                 Qbavg[jj] = 0.0;
00145                 Qsavg[jj] = 0.0;
00146                 if (outletmodelflag == 1)
00147                         for (p=0; p<maxnoutlet; p++){
00148                                 Qavgoutlet[jj][p]       = 0.0;
00149                                 Qbavgoutlet[jj][p]      = 0.0;
00150                                 Qsavgoutlet[jj][p]      = 0.0;
00151                         }
00152 //              for( ii=daystrm[jj]; ii<dayendm[jj]; ii++ )
00153                 for( ii=start_of(jj); ii<end_of(jj); ii++ )
00154                 {
00155                         Qavg[jj]        += (float)(Qsumtot[ii]);
00156                         Qbavg[jj]       += (float)(Qb[ii]);
00157                         Qsavg[jj]       += (float)(Qs[ii]);
00158                         if (outletmodelflag == 1)
00159                                 for (p=0; p<maxnoutlet; p++){
00160                                         Qavgoutlet[jj][p]       += (float)(Qsum[ii][p]);
00161                                         Qbavgoutlet[jj][p]      += (float)(Qboutlet[ii][p]);
00162                                         Qsavgoutlet[jj][p]      += (float)(Qsoutlet[ii][p]);
00163                                 }
00164                 }
00165 //              Qavg[jj]        = Qavg[jj]/daysim[jj];
00166                 Qavg[jj]        = Qavg[jj]/days_in_month(jj);
00167                 vel[jj] = (float)(velcof[ep] * pow( Qavg[jj], velpow[ep] ));
00168                 wid[jj] = (float)(widcof[ep] * pow( Qavg[jj], widpow[ep] ));
00169                 dep[jj] = (float)(depcof[ep] * pow( Qavg[jj], deppow[ep] ));
00170 //              Qbavg[jj]= Qbavg[jj]/daysim[jj];
00171                 Qbavg[jj]= Qbavg[jj]/days_in_month(jj);
00172 //              Qsavg[jj]= Qsavg[jj]/daysim[jj];
00173                 Qsavg[jj]= Qsavg[jj]/days_in_month(jj);
00174                 if (outletmodelflag == 1)
00175                         for (p=0; p<maxnoutlet; p++){
00176 //                              Qavgoutlet[jj][p]       = Qavgoutlet[jj][p]/daysim[jj];
00177                                 Qavgoutlet[jj][p]       = Qavgoutlet[jj][p]/days_in_month(jj);
00178                                 veloutlet[jj][p]        = (float)(velcof[ep] * pow( Qavgoutlet[jj][p], velpow[ep] ));
00179                                 widoutlet[jj][p]        = (float)(widcof[ep] * pow( Qavgoutlet[jj][p], widpow[ep] ));
00180                                 depoutlet[jj][p]        = (float)(depcof[ep] * pow( Qavgoutlet[jj][p], deppow[ep] ));
00181 //                              Qbavgoutlet[jj][p]      = Qbavgoutlet[jj][p]/daysim[jj];
00182                                 Qbavgoutlet[jj][p]      = Qbavgoutlet[jj][p]/days_in_month(jj);
00183 //                              Qsavgoutlet[jj][p]      = Qsavgoutlet[jj][p]/daysim[jj];
00184                                 Qsavgoutlet[jj][p]      = Qsavgoutlet[jj][p]/days_in_month(jj);
00185                         }
00186         }
00187 }
00188 else if( timestep[0] == 's' ) {
00189 
00190    /*-------------------------------------------------------------
00191     *  Average over each season and then compute seasonal values
00192     *-------------------------------------------------------------*/
00193         for( jj=0; jj<recperyear; jj++ ) {
00194                 Qavg[jj]        = 0.0;
00195                 Qbavg[jj]       = 0.0;
00196                 Qsavg[jj]       = 0.0;
00197                 if (outletmodelflag == 1)
00198                         for (p=0; p<maxnoutlet; p++){           
00199                                 Qavgoutlet[jj][p]       = 0.0;
00200                                 Qbavgoutlet[jj][p]      = 0.0;
00201                                 Qsavgoutlet[jj][p]      = 0.0;
00202                         }
00203                 for( ii=daystrs[jj]; ii<dayends[jj]; ii++ ){
00204                         Qavg[jj]        += (float)(Qsumtot[ii]);
00205                         Qbavg[jj]       += (float)(Qb[ii]);
00206                         Qsavg[jj]       += (float)(Qs[ii]);
00207                         if (outletmodelflag == 1)
00208                                 for (p=0; p<maxnoutlet; p++){
00209                                         Qavgoutlet[jj][p]       += (float)(Qsum[ii][p]);
00210                                         Qbavgoutlet[jj][p]      += (float)(Qboutlet[ii][p]);
00211                                         Qsavgoutlet[jj][p]      += (float)(Qsoutlet[ii][p]);
00212                                 }
00213                 }
00214                 Qavg[jj]        = Qavg[jj]/daysis[jj];
00215                 vel[jj]         = (float)(velcof[ep] * pow( Qavg[jj], velpow[ep] ));
00216                 wid[jj]         = (float)(widcof[ep] * pow( Qavg[jj], widpow[ep] ));
00217                 dep[jj]         = (float)(depcof[ep] * pow( Qavg[jj], deppow[ep] ));
00218                 Qbavg[jj]       = Qbavg[jj]/daysis[jj];
00219                 Qsavg[jj]       = Qsavg[jj]/daysis[jj];
00220                 if (outletmodelflag == 1)       
00221                         for (p=0; p<maxnoutlet; p++){
00222                                 Qavgoutlet[jj][p]       = Qavgoutlet[jj][p]/daysis[jj];
00223                                 veloutlet[jj][p]        = (float)(velcof[ep] * pow( Qavgoutlet[jj][p], velpow[ep] ));
00224                                 widoutlet[jj][p]        = (float)(widcof[ep] * pow( Qavgoutlet[jj][p], widpow[ep] ));
00225                                 depoutlet[jj][p]        = (float)(depcof[ep] * pow( Qavgoutlet[jj][p], deppow[ep] ));
00226                                 Qbavgoutlet[jj][p]= Qbavgoutlet[jj][p]/daysis[jj];
00227                                 Qsavgoutlet[jj][p]= Qsavgoutlet[jj][p]/daysis[jj];
00228                         }
00229         }
00230 }
00231 else {
00232 
00233    /*---------------------------------------------------------
00234     *  Average over each year and then compute annual values
00235     *---------------------------------------------------------*/
00236         jj = 0;
00237         Qavg[jj]        = 0.0;
00238         Qbavg[jj]       = 0.0;
00239         Qsavg[jj]       = 0.0;
00240         if (outletmodelflag == 1)  
00241                 for (p=0; p<maxnoutlet; p++){
00242                         jj = 0;
00243                         Qavgoutlet[jj][p]       = 0.0;
00244                         Qbavgoutlet[jj][p]      = 0.0;
00245                         Qsavgoutlet[jj][p]      = 0.0;
00246                 }
00247         for( ii=0; ii<daysiy; ii++ ){
00248                 Qavg[jj]        += (float)(Qsumtot[ii]);
00249                 Qbavg[jj]       += (float)(Qb[ii]);
00250                 Qsavg[jj]       += (float)(Qs[ii]);
00251                 if (outletmodelflag == 1)
00252                         for (p=0; p<maxnoutlet; p++){
00253                                 Qavgoutlet[jj][p]               += (float)(Qsum[ii][p]);
00254                                 Qbavgoutlet[jj][p]              += (float)(Qboutlet[ii][p]);
00255                                 Qsavgoutlet[jj][p]              += (float)(Qsoutlet[ii][p]);
00256                         }
00257         }
00258         Qavg[jj]        = Qavg[jj]/daysiy;
00259         vel[jj]         = (float)(velcof[ep] * pow( Qavg[jj], velpow[ep] ));
00260         wid[jj]         = (float)(widcof[ep] * pow( Qavg[jj], widpow[ep] ));
00261         dep[jj]         = (float)(depcof[ep] * pow( Qavg[jj], deppow[ep] ));
00262         Qbavg[jj]       = Qbavg[jj]/daysiy;
00263         Qsavg[jj]       = Qsavg[jj]/daysiy;     
00264         if (outletmodelflag == 1)
00265                 for (p=0; p<maxnoutlet; p++){
00266                         Qavgoutlet[jj][p]       = Qavgoutlet[jj][p]/daysiy;
00267                         veloutlet[jj][p]        = (float)(velcof[ep] * pow( Qavgoutlet[jj][p], velpow[ep] ));
00268                         widoutlet[jj][p]        = (float)(widcof[ep] * pow( Qavgoutlet[jj][p], widpow[ep] ));
00269                         depoutlet[jj][p]        = (float)(depcof[ep] * pow( Qavgoutlet[jj][p], deppow[ep] ));
00270                         Qbavgoutlet[jj][p]      = Qbavgoutlet[jj][p] / daysiy;
00271                         Qsavgoutlet[jj][p]      = Qsavgoutlet[jj][p] / daysiy;
00272                 }
00273 }  /* end if-else for time interval */
00274 
00275 /*
00276  *      Print the data for each year to the binary file
00277  *              velocity
00278  *              width
00279  *              depth
00280  *              bedload
00281  *              *conc[ii]
00282  */
00283 for (jj=0; jj<recperyear; jj++){
00284         fwrite(   &vel[jj], sizeof(float), 1, fiddistot);
00285         fwrite(   &wid[jj], sizeof(float), 1, fiddistot);
00286         fwrite(   &dep[jj], sizeof(float), 1, fiddistot);
00287         fwrite( &Qbavg[jj], sizeof(float), 1, fiddistot);
00288         for (kk=0; kk<ngrain; kk++) {
00289                 Cs = (float)(grainpct[kk][ep]*Qsavg[jj]/Qavg[jj]);
00290                 if (Qavg[jj] == 0.0)
00291                         Cs = 0.0;
00292                 fwrite( &Cs, sizeof(float), 1, fiddistot);
00293         }
00294         if (outletmodelflag == 1)
00295                 for (p=0; p<maxnoutlet; p++){
00296                         fwrite(   &veloutlet[jj][p], sizeof(float), 1, fiddis[p]);                      
00297                         fwrite(   &widoutlet[jj][p], sizeof(float), 1, fiddis[p]);
00298                         fwrite(   &depoutlet[jj][p], sizeof(float), 1, fiddis[p]);
00299                         fwrite( &Qbavgoutlet[jj][p], sizeof(float), 1, fiddis[p]);                      
00300                         for (kk=0; kk<ngrain; kk++) {
00301                                 Csoutlet[p] = (float)(grainpct[kk][ep]*Qsavgoutlet[jj][p]/Qavgoutlet[jj][p]);
00302                                 if (Qavgoutlet[jj][p] == 0.0)
00303                                         Csoutlet[p] = 0.0;
00304                                 fwrite( &Csoutlet[p], sizeof(float), 1, fiddis[p]);
00305                         }
00306                 }
00307 }
00308 
00309 /*----------------------------------------------
00310  *ascii routine to write the dis file in ascii
00311  *----------------------------------------------*/ 
00312 if( strncmp(asciioutput,ON,2) == 0){
00313     if (yr == syear[0]){
00314         fprintf(outp, "vel(m/s) wid(m) dep(m) ");
00315         fprintf(outp1,"Q(m^3/s) ");
00316             fprintf(outp2,"Cs(kg/m^3) Qs(kg/s) ");
00317         fprintf(outp3,"Qbavg() ");
00318         fprintf(outp4,"Cs_per_grainsize(kg/m^3) ");
00319             fprintf(outp5,"Qsavg() ");          
00320         if (outletmodelflag == 1)
00321                         for (p=0; p<maxnoutlet; p++){           
00322                         fprintf(outp, "vel(m/s) wid(m) dep(m) ");
00323                         fprintf(outp1,"Q(m^3/s) ");
00324                 fprintf(outp2,"Cs(kg/m^3) Qs(kg/s) ");
00325                         fprintf(outp3,"Qbavg() ");
00326                         fprintf(outp4,"Cs_per_grainsize(kg/m^3) ");
00327                 fprintf(outp5,"Qsavg(kg/s) ");
00328                         }
00329                 fprintf(outp, "\n");
00330                 fprintf(outp1,"\n");
00331                 fprintf(outp2,"\n");
00332                 fprintf(outp3,"\n");
00333                 fprintf(outp4,"\n");
00334                 fprintf(outp5,"\n");
00335             fprintf(outp, "--------  ------- ------- ");
00336         fprintf(outp1,"--------- ");                
00337         fprintf(outp2,"----------  --------- ");
00338             fprintf(outp3,"-------- ");
00339         fprintf(outp4,"------------------------ ");
00340         fprintf(outp5,"-------- ");
00341                 if (outletmodelflag == 1)       
00342                         for (p=0; p<maxnoutlet; p++){           
00343                     fprintf(outp, "--------  ------- ------- ");
00344                     fprintf(outp1,"--------- ");            
00345                         fprintf(outp2,"----------  --------- ");
00346                     fprintf(outp3,"-------- ");
00347                         fprintf(outp4,"------------------------ ");
00348                         fprintf(outp5,"-------- ");
00349                         }
00350                 fprintf(outp, "\n");
00351                 fprintf(outp1,"\n");
00352                 fprintf(outp2,"\n");
00353                 fprintf(outp3,"\n");
00354                 fprintf(outp4,"\n");
00355                 fprintf(outp5,"\n");                                                     
00356         }
00357         for (jj=0; jj<recperyear; jj++) {
00358         fprintf(outp, "%.3f\t  %.3f\t  %.3f\t  ",vel[jj],wid[jj],dep[jj]);
00359             fprintf(outp1,"%.3f ",vel[jj]*wid[jj]*dep[jj]);
00360         fprintf(outp3,"%.3f ",Qbavg[jj]);
00361                 for (kk=0; kk<ngrain; kk++) {
00362                         if (kk==0)
00363                                 Cstot = 0.0;
00364                         Cs = (float)(grainpct[kk][ep]*Qsavg[jj]/Qavg[jj]);
00365                         if (Qavg[jj] == 0)
00366                                 Cs = 0.0;
00367                         Cstot += Cs;
00368                         fprintf(outp4,"%.3f ",Cs);
00369                         if (kk == ngrain-1)
00370                                 fprintf(outp2,"%.3f\t %.3f ",Cstot,Cstot*vel[jj]*wid[jj]*dep[jj]);                              
00371                 }
00372                 fprintf(outp5,"%.3f ",Qsavg[jj]);       
00373                 if (outletmodelflag == 1)
00374                         for (p=0; p<maxnoutlet; p++){           
00375                         fprintf(outp, "%.3f\t  %.3f\t  %.3f\t  ",veloutlet[jj][p],widoutlet[jj][p],depoutlet[jj][p]);
00376                     fprintf(outp1,"%.3f ",veloutlet[jj][p]*widoutlet[jj][p]*depoutlet[jj][p]);
00377                         fprintf(outp3,"%.3f ",Qbavgoutlet[jj][p]);
00378                                 for (kk=0; kk<ngrain; kk++) {
00379                                         if (kk==0)
00380                                                 Cstotoutlet[p] = 0.0;
00381                                         Csoutlet[p] = (float)(grainpct[kk][ep]*Qsavgoutlet[jj][p]/Qavgoutlet[jj][p]);
00382                                         if (Qavgoutlet[jj][p] == 0)
00383                                                 Csoutlet[p] = 0.0;
00384                                         Cstotoutlet[p] += Csoutlet[p];
00385                                         fprintf(outp4,"%.3f ",Csoutlet[p]);
00386                                         if (kk == ngrain-1)
00387                                                 fprintf(outp2,"%.3f %.3f ",Cstotoutlet[p],Cstotoutlet[p]*veloutlet[jj][p]*widoutlet[jj][p]*depoutlet[jj][p]);
00388                                 }
00389                                 fprintf(outp5,"%.3f ",Qsavgoutlet[jj][p]);
00390                         }
00391                 fprintf(outp, "\n");
00392                 fprintf(outp1,"\n");
00393                 fprintf(outp2,"\n");
00394                 fprintf(outp3,"\n");
00395                 fprintf(outp4,"\n");
00396                 fprintf(outp5,"\n");        
00397         }       
00398 }
00399 
00400 /*---------------
00401  *  Free memory
00402  *---------------*/
00403         freematrix2D( (void**)veloutlet, daysiy ); 
00404         freematrix2D( (void**)widoutlet, daysiy );
00405         freematrix2D( (void**)depoutlet, daysiy );
00406         freematrix2D( (void**)Qavgoutlet, daysiy );
00407         freematrix2D( (void**)Qbavgoutlet, daysiy );
00408         freematrix2D( (void**)Qsavgoutlet, daysiy );
00409         freematrix1D( (void*)Csoutlet );
00410         freematrix1D( (void*)Cstotoutlet );
00411         
00412 return(err);
00413 }  /* end of HydroOutput.c */
00414 

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