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

Go to the documentation of this file.
00001 /*
00002  * HydroShuffle.c       randomly shuffles the days of the month for climate
00003  *                      simulations
00004  *
00005  *  Author:    M.D. Morehead    (June 1998)
00006  *  Author2:   A.J. Kettner     (August 2002)
00007  *
00008  * Variable             Def.Location    Type    Units   Usage
00009  * --------             ------------    ----    -----   -----
00010  * dumflt               HydroShuffle.c  float   -               temporary float
00011  * dvals[31]    HydroShuffle.c  int             -               shuffled array of daily index values
00012  * err                  HydroShuffle.c  int             -               error flag, halts program
00013  * ii                   HydroShuffle.c  int             -               temporary loop counter
00014  * mnth                 HydroShuffle.c  int             -               month of the year
00015  * yy                   HydroShuffle.c  int             -               temporary integer
00016  * zz                   HydroShuffle.c  int             -               temporary integer
00017  *
00018  */
00019 
00020 #include "hydroclimate.h"
00021 #include "hydroparams.h"
00022 #include "hydrodaysmonths.h"
00023 #include "hydrornseeds.h"
00024 
00025 
00026 #ifdef DBG
00027 #include "hydroinout.h"
00028 #endif
00029 
00030 #define ntot (93)
00031 
00032 /*---------------------------
00033  *  Start of HydroShuffle.c
00034  *---------------------------*/
00035 int hydroshuffle(int dvals[31],int mnth)
00036 {
00037 
00038 float hydroran3(long *idum);
00039 float dumflt;
00040 double dummy_double;
00041 int yy, zz, ii, err;
00042 
00043 /*------------------------
00044  *  Initialize variables
00045  *------------------------*/
00046 err   = 0;
00047 //for( ii=0; ii<daysim[mnth]; ii++ )
00048 for( ii=0; ii<days_in_month(mnth); ii++ )
00049       dvals[ii] = ii+1;
00050 
00051 /*---------------------------------
00052  *  shuffle the days of the month
00053  *---------------------------------*/
00054 if ( yr == syear[ep] && mnth == 0)
00055    rnseed3 = -INIT_RAN_NUM_SEED; 
00056 
00057 //for( ii=0; ii<daysim[mnth]; ii++ ) {
00058 for( ii=0; ii<days_in_month(mnth) ; ii++ ) {
00059    dumflt = hydroran3(&rnseed3);                                /* get a uniform random number [0:1] */
00060    if( 0 > dumflt || dumflt > 1 ){
00061       fprintf( stderr,"A function in HydroRan2 failed in HydroShuffle.c \n");
00062       fprintf( stderr," \t dumflt = %f: \t setting value to 0.5, ii = %d \n", dumflt, ii);
00063       dumflt=0.5;
00064    }
00065 
00066 //   dummy_double = dumflt*(float)daysim[mnth];
00067    dummy_double = dumflt*(float)days_in_month(mnth);
00068    yy = (int)rnd(dummy_double);                 /* scale to a random day */
00069    if( yy == 0 ) yy += 1;
00070    zz = dvals[yy-1];                            /* swap random day with day ii */
00071    dvals[yy-1] = dvals[ii];
00072    dvals[ii] = zz;
00073 }
00074 
00075 #ifdef DBG
00076   if( tblstart[ep] <= yr && yr <= tblend[ep] && mnth == 0 ) {
00077    fprintf( fidlog, "\n HydroShuffle.c:\n");
00078 //   fprintf( fidlog, "    epoch = %d, year = %d, month = %d, daysim = %d \n",ep+1,yr,mnth+1,daysim[mnth]);
00079    fprintf( fidlog, "    epoch = %d, year = %d, month = %d, daysim = %d \n",ep+1,yr,mnth+1,days_in_month(mnth));
00080 //   for( ii=0; ii<daysim[mnth]; ii++)
00081    for( ii=0; ii<days_in_month(mnth); ii++)
00082       fprintf( fidlog, "    ii = %d, \t dvals[ii] = %d \n", ii, dvals[ii] );
00083    fprintf( fidlog, "\n");
00084    fflush( fidlog );
00085   }
00086 #endif
00087 
00088 return(err);
00089 } /* end of HydroShuffle.c */

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