forked from kettner/hydrotrend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhydroshuffle.c
74 lines (66 loc) · 2.24 KB
/
hydroshuffle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*-------------------------------------------------------------------------------------------
* hydroshuffle.c
*
* Author: Albert Kettner, March 2006
*
* Randomly shuffles the days of the month for climate simulations
*
* Variable Def.Location Type Units Usage
* -------- ------------ ---- ----- -----
* dumflt HydroShuffle.c float - temporary float
* dvals[31] HydroShuffle.c int - shuffled array of daily index values
* err HydroShuffle.c int - error flag, halts program
* ii HydroShuffle.c int - temporary loop counter
* mnth HydroShuffle.c int - month of the year
* yy HydroShuffle.c int - temporary integer
* zz HydroShuffle.c int - temporary integer
*
*-------------------------------------------------------------------------------------------*/
#include "hydroclimate.h"
#include "hydroparams.h"
#include "hydrodaysmonths.h"
#include "hydrornseeds.h"
#define ntot (93)
/*---------------------------
* Start of HydroShuffle.c
*---------------------------*/
int
hydroshuffle (int dvals[31], int mnth)
{
float hydroran3 (long *idum);
float dumflt;
double dummy_double;
int yy, zz, ii, err;
/*------------------------
* Initialize variables
*------------------------*/
err = 0;
for (ii = 0; ii < daysim[mnth]; ii++)
dvals[ii] = ii + 1;
/*---------------------------------
* shuffle the days of the month
*---------------------------------*/
if (yr == syear[ep] && mnth == 0)
rnseed3 = -INIT_RAN_NUM_SEED;
for (ii = 0; ii < daysim[mnth]; ii++)
{
dumflt = hydroran3 (&rnseed3); /* get a uniform random number [0:1] */
if (0 > dumflt || dumflt > 1)
{
fprintf (stderr,
"A function in HydroRan2 failed in HydroShuffle.c \n");
fprintf (stderr,
" \t dumflt = %f: \t setting value to 0.5, ii = %d \n",
dumflt, ii);
dumflt = 0.5;
}
dummy_double = dumflt * (float) daysim[mnth];
yy = (int) rnd (dummy_double); /* scale to a random day */
if (yy == 0)
yy += 1;
zz = dvals[yy - 1]; /* swap random day with day ii */
dvals[yy - 1] = dvals[ii];
dvals[ii] = zz;
}
return (err);
} /* end of HydroShuffle.c */