-
Notifications
You must be signed in to change notification settings - Fork 6
/
hydroreadclimate.c
201 lines (180 loc) · 7.27 KB
/
hydroreadclimate.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*-------------------------------------------------------------------------------------------
* hydroreadclimate.c
*
* Author: Albert Kettner, March 2006
*
* This subroutine will become active, if the option is used to generate discharge and sediment
* load based on sequential climate input. (Input file used: HYDRO.CLIMATE).
*
* Variable Def.Location Type Units Usage
* -------- ------------ ---- ----- -----
* err various int - error flag, halts program
* count Read_Rainfall_Etc int -
* dummyT Read_Rainfall_Etc double deg.C
* dummyTtot Read_Rainfall_Etc double deg.C
* dummyT2 Read_Rainfall_Etc double deg.C
* dummyR Read_Rainfall_Etc double mm
* dummyRtot Read_Rainfall_Etc double mm
* HOURSINDAY Read_Rainfall_Etc - hours define the hours in a day
* i Read_Rainfall_Etc int -
* k Read_Rainfall_Etc int -
* line[200] Read_Rainfall_Etc char -
* n Read_Rainfall_Etc long - counter for the number of years
*
*-------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "hydroalloc_mem.h"
#include "hydroinout.h"
#include "hydroparams.h"
#include "hydroreadclimate.h"
#include "hydrotimeser.h"
#include "hydroclimate.h"
/*-----------------------
* Function Definition
*-----------------------*/
void Read_Rainfall_Etc (gw_rainfall_etc * gw_rain, char* in_dir, char* in_file_prefix);
/*---------------------------
* Start of HydroReadInput
*---------------------------*/
int hydroreadclimate (gw_rainfall_etc * gw_rain, char* in_dir, char* in_file_prefix){
/*-------------------
* Local Variables
*-------------------*/
int k, err;
/*-----------------------
* Set local Variables
*-----------------------*/
err = 0;
ep = 0;
total_yr = 0;
for (k = 0; k < nepochs; k++)
total_yr += nyears[ep];
/*-------------------------------
* Read Rainfall input FOR NOW
*-------------------------------*/
Read_Rainfall_Etc (gw_rain, in_dir, in_file_prefix);
return (err);
} /* end hydroreadclimate.c */
/*-----------------------
* Function Prototypes
*-----------------------*/
/*------------------------------
* Function Read_Rainfall_Etc
*------------------------------*/
void Read_Rainfall_Etc (gw_rainfall_etc * gw_rain, char* in_dir, char* in_file_prefix){
/*-------------------
* Local Variables
*-------------------*/
#define HOURSINDAY (24.0)
int k, count, i;
long n;
char line[200], fnameinputgw_r[400], dummystring[200];
double dummyT, dummyR, dummyT2, dummyTtot, dummyRtot, dummydouble;
/*------------------------
* Open rain input file
*------------------------*/
sprintf (dummystring, "%s/%s", in_dir, in_file_prefix);
strcpy (fnameinputgw_r, dummystring);
strcat (fnameinputgw_r, fnameclimateext);
if ((fidinputgw_r = fopen (fnameinputgw_r, "r")) == NULL){
fprintf (stderr, " Read_Rainfall_Etc MESSAGE: Unable to open input file %s \n", fnameinputgw_r);
fprintf (stderr, " Hydrotrend will generate it's own climate values based on\n");
fprintf (stderr, " line 12-23 of the input values in the input file.\n\n");
raindatafile = 0;
}
else
raindatafile = 1;
if (raindatafile == 1){
/*--------------------
* Strip off header
*--------------------*/
for (k = 0; k < 6; k++){
fgets (line, sizeof (line), fidinputgw_r);
}
/*---------------------------------
* Read number of timesteps & dt
*---------------------------------*/
fgets (line, sizeof (line), fidinputgw_r);
sscanf (line, "%ld %ld", &gw_rain->n_steps, &gw_rain->dt);
//printf ("%ld, %ld\n",gw_rain->n_steps, gw_rain->dt);
/*---------------------------------------------------
* Check number of years to run with actually rain
* data. If actually rain data is not equal with
* the number of years to run, stop program.
*---------------------------------------------------*/
if (gw_rain->n_steps / daysiy < total_yr){
dummydouble = gw_rain->n_steps / daysiy;
fprintf (stderr, " HydroReadclimate.c ERROR: Unable to run hydrotrend because of\n");
fprintf (stderr, " insufficient climate data\n");
fprintf (stderr, " Number of model years in HYDRO.IN, total years from all epochs: %d (line 5).\n", total_yr);
fprintf (stderr, " is not equal to number of years of climate data: %f.\n", dummydouble);
fprintf (stderr, " program aborted \n");
exit (-1);
}
/*------------------------------
* Allocate memory for arrays
*------------------------------*/
gw_rain->R = malloc2d (total_yr, daysiy, double);
gw_rain->T = malloc2d (total_yr, daysiy, double);
gw_rain->Tperyear = malloc1d (total_yr, double);
/*----------------------------------------------
* Read Precipitation and Temperature values
* If values are not daily values
* but for example hour values, then
* average the temperature and add
* all the precipitation values for that day.
*----------------------------------------------*/
for (n = 0; n < total_yr; n++){
dummyT2 = 0.0;
for (k = 0; k < daysiy; k++){
dummyRtot = 0.0;
dummyTtot = 0.0;
for (i = 0; i < (HOURSINDAY / gw_rain->dt); i++){
fgets (line, sizeof (line), fidinputgw_r);
count = sscanf (line, "%lf %lf", &dummyR, &dummyT);
//printf ("%lf, %lf\n", dummyR, dummyT);
if (count != 2){
n = (n + 1) * (k + 1) * (i + 1);
fprintf (stderr, " HydroReadclimate.c ERROR: Error occured when\n");
fprintf (stderr, " trying to read line %ld\n", n);
fprintf (stderr, " of file: %s.\n", fnameinputgw_r);
fprintf (stderr, " Precipitation or Temperature data is missing\n");
fprintf (stderr, " Precipitation = %lf, Temp.= %lf\n", dummyR, dummyT);
fprintf (stderr, " program aborted \n");
exit (-1);
}
if (dummyR < 0.0 || dummyR > 2000.0){
fprintf (stderr, " HydroReadclimate.c ERROR:\n");
fprintf (stderr, " Precipitation data out of range,\n");
fprintf (stderr, " ppt is 2000.0 < %f < 0.0\n ", dummyR);
fprintf (stderr, " program aborted \n");
exit (-1);
}
if (dummyT < -80.0 || dummyT > 80.0){
fprintf (stderr, " HydroReadclimate.c ERROR:\n");
fprintf (stderr, " Temperature data out of range,\n");
fprintf (stderr, " Temp of 80.0 < %f < -80.0 ", dummyT);
fprintf (stderr, " program aborted \n");
exit (-1);
}
dummyRtot += dummyR;
dummyTtot += dummyT;
}
dummyTtot = ((dummyTtot) / (HOURSINDAY / gw_rain->dt));
gw_rain->R[n][k] = dummyRtot;
gw_rain->T[n][k] = dummyTtot;
gw_rain->R[n][k] *= (Pmassbal[ep] / 1000.0);
dummyT2 += gw_rain->T[n][k];
}
gw_rain->Tperyear[n] = dummyT2 / daysiy;
}
/*--------------------
* Close input file
*--------------------*/
fclose (fidinputgw_r);
}
return;
} /* end Read_Rainfall_Etc */