-
Notifications
You must be signed in to change notification settings - Fork 23
/
DHSVMChannel.c
executable file
·318 lines (280 loc) · 10.6 KB
/
DHSVMChannel.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -------------------------------------------------------------
file: DHSVMChannel.c
------------------------------------------------------------- */
/* -------------------------------------------------------------
Battelle Memorial Institute
Pacific Northwest Laboratory
------------------------------------------------------------- */
/* -------------------------------------------------------------
Created August 30, 1996 by William A Perkins
$Id: DHSVMChannel.c,v 1.16 2004/08/18 01:01:26 colleen Exp $
------------------------------------------------------------- */
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "constants.h"
#include "getinit.h"
#include "DHSVMChannel.h"
#include "DHSVMerror.h"
#include "functions.h"
#include "settings.h"
#include "errorhandler.h"
#include "fileio.h"
/* -----------------------------------------------------------------------------
InitChannel
Reads stream and road files and builds the networks.
-------------------------------------------------------------------------- */
void
InitChannel(LISTPTR Input, MAPSIZE * Map, int deltat, CHANNEL * channel,
SOILPIX ** SoilMap, int *MaxStreamID, int *MaxRoadID, OPTIONSTRUCT *Options)
{
int i;
STRINIENTRY StrEnv[] = {
{"ROUTING", "STREAM NETWORK FILE", "", ""},
{"ROUTING", "STREAM MAP FILE", "", ""},
{"ROUTING", "STREAM CLASS FILE", "", ""},
{"ROUTING", "ROAD NETWORK FILE", "", "none"},
{"ROUTING", "ROAD MAP FILE", "", "none"},
{"ROUTING", "ROAD CLASS FILE", "", "none"},
{NULL, NULL, "", NULL}
};
printf("\nInitializing Road/Stream Networks\n");
/* Read the key-entry pairs from the ROUTING section in the input file */
for (i = 0; StrEnv[i].SectionName; i++) {
GetInitString(StrEnv[i].SectionName, StrEnv[i].KeyName, StrEnv[i].Default,
StrEnv[i].VarStr, (unsigned long) BUFSIZE, Input);
if (IsEmptyStr(StrEnv[i].VarStr))
ReportError(StrEnv[i].KeyName, 51);
}
channel->stream_class = NULL;
channel->road_class = NULL;
channel->streams = NULL;
channel->roads = NULL;
channel->stream_map = NULL;
channel->road_map = NULL;
channel_init();
channel_grid_init(Map->NX, Map->NY);
if (strncmp(StrEnv[stream_class].VarStr, "none", 4)) {
printf("\tReading Stream data\n");
if ((channel->stream_class =
channel_read_classes(StrEnv[stream_class].VarStr, stream_class,
FALSE)) == NULL) {
ReportError(StrEnv[stream_class].VarStr, 5);
}
if ((channel->streams =
channel_read_network(StrEnv[stream_network].VarStr,
channel->stream_class, MaxStreamID)) == NULL) {
ReportError(StrEnv[stream_network].VarStr, 5);
}
if ((channel->stream_map =
channel_grid_read_map(channel->streams,
StrEnv[stream_map].VarStr, SoilMap)) == NULL) {
ReportError(StrEnv[stream_map].VarStr, 5);
}
error_handler(ERRHDL_STATUS,
"InitChannel: computing stream network routing coefficients");
channel_routing_parameters(channel->streams, (double) deltat);
}
if (strncmp(StrEnv[road_class].VarStr, "none", 4)) {
printf("\tReading Road data\n");
if ((channel->road_class =
channel_read_classes(StrEnv[road_class].VarStr, road_class,
Options->Sediment)) == NULL) {
ReportError(StrEnv[road_class].VarStr, 5);
}
if ((channel->roads =
channel_read_network(StrEnv[road_network].VarStr,
channel->road_class, MaxRoadID)) == NULL) {
ReportError(StrEnv[road_network].VarStr, 5);
}
if ((channel->road_map =
channel_grid_read_map(channel->roads,
StrEnv[road_map].VarStr, SoilMap)) == NULL) {
ReportError(StrEnv[road_map].VarStr, 5);
}
error_handler(ERRHDL_STATUS,
"InitChannel: computing road network routing coefficients");
channel_routing_parameters(channel->roads, (double) deltat);
}
}
/* -------------------------------------------------------------
InitChannelDump
------------------------------------------------------------- */
void InitChannelDump(CHANNEL * channel, char *DumpPath)
{
char buffer[NAMESIZE];
if (channel->streams != NULL) {
sprintf(buffer, "%sStream.Flow", DumpPath);
OpenFile(&(channel->streamout), buffer, "w", TRUE);
sprintf(buffer, "%sStreamflow.Only", DumpPath);
OpenFile(&(channel->streamflowout), buffer, "w", TRUE);
}
if (channel->roads != NULL) {
sprintf(buffer, "%sRoad.Flow", DumpPath);
OpenFile(&(channel->roadout), buffer, "w", TRUE);
sprintf(buffer, "%sRoadflow.Only", DumpPath);
OpenFile(&(channel->roadflowout), buffer, "w", TRUE);
}
}
/* -------------------------------------------------------------
InitChannelSedimentDump
------------------------------------------------------------- */
void InitChannelSedimentDump(CHANNEL * channel, char *DumpPath,
int ChannelRouting)
{
char buffer[NAMESIZE];
if ((channel->streams != NULL) && (ChannelRouting == TRUE)) {
sprintf(buffer, "%sSed.Stream.Flow", DumpPath);
OpenFile(&(channel->sedstreamout), buffer, "w", TRUE);
sprintf(buffer, "%sSed.Streamflow.Only", DumpPath);
OpenFile(&(channel->sedstreamflowout), buffer, "w", TRUE);
}
else if ((channel->streams != NULL) && (ChannelRouting == FALSE)) {
sprintf(buffer, "%sSed.Streaminflow.Only", DumpPath);
OpenFile(&(channel->sedstreaminflow), buffer, "w", TRUE);
}
if ((channel->roads != NULL) && (ChannelRouting == TRUE)) {
sprintf(buffer, "%sSed.Road.Flow", DumpPath);
OpenFile(&(channel->sedroadout), buffer, "w", TRUE);
sprintf(buffer, "%sSed.Roadflow.Only", DumpPath);
OpenFile(&(channel->sedroadflowout), buffer, "w", TRUE);
}
else if ((channel->roads != NULL) && (ChannelRouting == FALSE)) {
sprintf(buffer, "%sSed.Roadinflow.Only", DumpPath);
OpenFile(&(channel->sedroadinflow), buffer, "w", TRUE);
}
}
/* -------------------------------------------------------------
ChannelCulvertFlow
computes outflow of channel/road network to a grid cell, if it
contains a sink
------------------------------------------------------------- */
double ChannelCulvertFlow(int y, int x, CHANNEL * ChannelData)
{
if (channel_grid_has_channel(ChannelData->road_map, x, y)) {
return channel_grid_outflow(ChannelData->road_map, x, y);
}
else {
return 0;
}
}
/* -------------------------------------------------------------
RouteChannel
------------------------------------------------------------- */
void
RouteChannel(CHANNEL * ChannelData, TIMESTRUCT * Time, MAPSIZE * Map,
TOPOPIX ** TopoMap, SOILPIX ** SoilMap, AGGREGATED * Total,
OPTIONSTRUCT *Options, ROADSTRUCT ** Network,
SOILTABLE * SType, PRECIPPIX ** PrecipMap, SEDPIX **SedMap,
float Tair, float Rh, float *SedDiams)
{
int x, y;
int flag;
char buffer[32];
float CulvertFlow;
/* give any surface water to roads w/o sinks */
for (y = 0; y < Map->NY; y++) {
for (x = 0; x < Map->NX; x++) {
if (INBASIN(TopoMap[y][x].Mask)) {
SoilMap[y][x].IExcessSed = SoilMap[y][x].IExcess;
if (channel_grid_has_channel(ChannelData->road_map, x, y) && !channel_grid_has_sink(ChannelData->road_map, x, y)) { /* road w/o sink */
SoilMap[y][x].RoadInt += SoilMap[y][x].IExcess;
channel_grid_inc_inflow(ChannelData->road_map, x, y,
SoilMap[y][x].IExcess * Map->DX * Map->DY);
SoilMap[y][x].IExcess = 0.0f;
}
}
}
}
if(Options->RoadRouting){
RouteRoad(Map, Time, TopoMap, SoilMap, Network, SType, ChannelData,
PrecipMap, SedMap, Tair, Rh, SedDiams);
}
/* route the road network and save results */
SPrintDate(&(Time->Current), buffer);
flag = IsEqualTime(&(Time->Current), &(Time->Start));
if (ChannelData->roads != NULL) {
channel_route_network(ChannelData->roads, Time->Dt);
channel_save_outflow_text(buffer, ChannelData->roads,
ChannelData->roadout, ChannelData->roadflowout,
flag);
}
/* add culvert outflow to surface water */
Total->CulvertReturnFlow = 0.0;
for (y = 0; y < Map->NY; y++) {
for (x = 0; x < Map->NX; x++) {
if (INBASIN(TopoMap[y][x].Mask)) {
CulvertFlow = ChannelCulvertFlow(y, x, ChannelData);
CulvertFlow /= Map->DX * Map->DY;
/* CulvertFlow = (CulvertFlow > 0.0) ? CulvertFlow : 0.0; */
if (channel_grid_has_channel(ChannelData->stream_map, x, y)) {
channel_grid_inc_inflow(ChannelData->stream_map, x, y,
(SoilMap[y][x].IExcess +
CulvertFlow) * Map->DX * Map->DY);
SoilMap[y][x].ChannelInt += SoilMap[y][x].IExcess;
Total->CulvertToChannel += CulvertFlow;
Total->RunoffToChannel += SoilMap[y][x].IExcess;
SoilMap[y][x].IExcess = 0.0f;
}
else {
SoilMap[y][x].IExcess += CulvertFlow;
Total->CulvertReturnFlow += CulvertFlow;
}
}
}
}
/* route stream channels */
if (ChannelData->streams != NULL) {
channel_route_network(ChannelData->streams, Time->Dt);
channel_save_outflow_text(buffer, ChannelData->streams,
ChannelData->streamout,
ChannelData->streamflowout, flag);
}
}
/* -------------------------------------------------------------
ChannelCut
computes necessary parameters for cell storage adjustment from
channel/road dimensions
------------------------------------------------------------- */
void ChannelCut(int y, int x, CHANNEL * ChannelData, ROADSTRUCT * Network)
{
float bank_height = 0.0;
float cut_area = 0.0;
if (channel_grid_has_channel(ChannelData->stream_map, x, y)) {
bank_height = channel_grid_cell_bankht(ChannelData->stream_map, x, y);
cut_area = channel_grid_cell_width(ChannelData->stream_map, x, y) *
channel_grid_cell_length(ChannelData->stream_map, x, y);
}
else if (channel_grid_has_channel(ChannelData->road_map, x, y)) {
bank_height = channel_grid_cell_bankht(ChannelData->road_map, x, y);
cut_area = channel_grid_cell_width(ChannelData->road_map, x, y) *
channel_grid_cell_length(ChannelData->road_map, x, y);
}
Network->Area = cut_area;
Network->BankHeight = bank_height;
}
/* -------------------------------------------------------------
ChannelFraction
This computes the (sub)surface flow fraction for a road
------------------------------------------------------------- */
uchar ChannelFraction(TOPOPIX * topo, ChannelMapRec * rds)
{
float effective_width = 0;
float total_width;
float sine, cosine;
ChannelMapRec *r;
float fract = 0.0;
if (rds == NULL) {
return 0;
}
cosine = cos(topo->Aspect);
sine = sin(topo->Aspect);
total_width = topo->FlowGrad / topo->Slope;
effective_width = 0.0;
for (r = rds; r != NULL; r = r->next) {
effective_width += r->length * sin(fabs(topo->Aspect - r->aspect));
}
fract = effective_width / total_width * 255.0;
fract = (fract > 255.0 ? 255.0 : floor(fract + 0.5));
return (uchar) fract;
}