-
Notifications
You must be signed in to change notification settings - Fork 23
/
InitNetwork.c
executable file
·195 lines (181 loc) · 6.35 KB
/
InitNetwork.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
/*
* SUMMARY: InitNetwork.c - Initialize road/channel work
* USAGE:
*
* AUTHOR: DHSVM Project (Bart Nijssen)
* ORG: University of Washington, Department of Civil Engineering
* E-MAIL: [email protected]
* ORIG-DATE: 27-Aug-1996 at 18:34:01
* DESCRIPTION: Initialize road/channel work. Memory is allocated, and the
* necessary adjustments for the soil profile are calculated
* DESCRIP-END.
* FUNCTIONS: InitNetwork()
* COMMENTS:
* $Id: InitNetwork.c,v 1.8 2004/05/03 03:28:45 colleen Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include "constants.h"
#include "settings.h"
#include "data.h"
#include "DHSVMerror.h"
#include "functions.h"
#include "settings.h"
#include "soilmoisture.h"
#include "DHSVMChannel.h"
/*****************************************************************************
Function name: InitNetwork()
Purpose : Initialize road/channel work. Memory is allocated, and the
necessary adjustments for the soil profile are calculated
Comments :
*****************************************************************************/
void InitNetwork(int NY, int NX, float DX, float DY, TOPOPIX **TopoMap,
SOILPIX **SoilMap, VEGPIX **VegMap, VEGTABLE *VType,
ROADSTRUCT ***Network, CHANNEL *ChannelData,
LAYER Veg, OPTIONSTRUCT *Options)
{
const char *Routine = "InitNetwork";
int i; /* counter */
int x; /* column counter */
int y; /* row counter */
int sx, sy;
int minx, miny;
int doimpervious;
int numroads; /* Counter of number of pixels
with a road and channel */
int numroadschan; /* Counter of number of pixels
with a road */
FILE *inputfile;
/* Allocate memory for network structure */
if (!(*Network = (ROADSTRUCT **) calloc(NY, sizeof(ROADSTRUCT *))))
ReportError((char *) Routine, 1);
for (y = 0; y < NY; y++) {
if (!((*Network)[y] = (ROADSTRUCT *) calloc(NX, sizeof(ROADSTRUCT))))
ReportError((char *) Routine, 1);
}
for (y = 0; y < NY; y++) {
for (x = 0; x < NX; x++) {
if (INBASIN(TopoMap[y][x].Mask)) {
if (!((*Network)[y][x].Adjust =
(float *) calloc((VType[VegMap[y][x].Veg - 1].NSoilLayers + 1), sizeof(float))))
ReportError((char *) Routine, 1);
if (!((*Network)[y][x].PercArea =
(float *) calloc((VType[VegMap[y][x].Veg - 1].NSoilLayers + 1), sizeof(float))))
ReportError((char *) Routine, 1);
}
}
}
numroadschan = 0;
numroads = 0;
/* If a road/channel Network is imposed on the area, read the Network
information, and calculate the storage adjustment factors */
if (Options->HasNetwork) {
for (y = 0; y < NY; y++) {
for (x = 0; x < NX; x++) {
if (INBASIN(TopoMap[y][x].Mask)) {
ChannelCut(y, x, ChannelData, &((*Network)[y][x]));
AdjustStorage(VType[VegMap[y][x].Veg - 1].NSoilLayers,
SoilMap[y][x].Depth,
VType[VegMap[y][x].Veg - 1].RootDepth,
(*Network)[y][x].Area, DX, DY,
(*Network)[y][x].BankHeight,
(*Network)[y][x].PercArea,
(*Network)[y][x].Adjust,
&((*Network)[y][x].CutBankZone));
(*Network)[y][x].IExcess = 0.;
if (channel_grid_has_channel(ChannelData->road_map, x, y)) {
numroads++;
if (channel_grid_has_channel(ChannelData->stream_map, x, y)) {
numroadschan++;
}
(*Network)[y][x].fraction =
ChannelFraction(&(TopoMap[y][x]), ChannelData->road_map[x][y]);
(*Network)[y][x].MaxInfiltrationRate =
MaxRoadInfiltration(ChannelData->road_map, x, y);
(*Network)[y][x].RoadClass =
channel_grid_class(ChannelData->road_map, x, y);
(*Network)[y][x].FlowSlope =
channel_grid_flowslope(ChannelData->road_map, x, y);
(*Network)[y][x].FlowLength =
channel_grid_flowlength(ChannelData->road_map, x, y,(*Network)[y][x].FlowSlope);
(*Network)[y][x].RoadArea = channel_grid_cell_width(ChannelData->road_map, x, y) * channel_grid_cell_length(ChannelData->road_map, x, y);
}
else {
(*Network)[y][x].MaxInfiltrationRate = DHSVM_HUGE;
(*Network)[y][x].FlowSlope = 0.;
(*Network)[y][x].FlowLength = 0.;
(*Network)[y][x].RoadArea = 0.;
(*Network)[y][x].RoadClass = NULL;
(*Network)[y][x].IExcess = 0.;
}
}
}
}
}
/* if no road/channel Network is imposed, set the adjustment factors to the
values they have in the absence of an imposed network */
else {
for (y = 0; y < NY; y++) {
for (x = 0; x < NX; x++) {
if (INBASIN(TopoMap[y][x].Mask)) {
for (i = 0; i <= VType[VegMap[y][x].Veg - 1].NSoilLayers; i++) {
(*Network)[y][x].Adjust[i] = 1.0;
(*Network)[y][x].PercArea[i] = 1.0;
(*Network)[y][x].CutBankZone = NO_CUT;
(*Network)[y][x].MaxInfiltrationRate = 0.;
}
(*Network)[y][x].FlowSlope = 0.;
(*Network)[y][x].FlowLength = 0.;
(*Network)[y][x].RoadArea = 0.;
(*Network)[y][x].RoadClass = NULL;
(*Network)[y][x].IExcess = 0.;
}
}
}
}
if (numroads > 0){
printf("There are %d pixels with a road and %d with a road and a channel.\n",
numroads, numroadschan);
}
/* this all pertains to the impervious surface */
doimpervious = 0;
for (i = 0; i < Veg.NTypes; i++)
if (VType[i].ImpervFrac > 0.0)
doimpervious = 1;
if (doimpervious) {
if (!(inputfile = fopen(Options->ImperviousFilePath, "rt"))) {
fprintf(stderr,
"User has specified a percentage impervious area \n");
fprintf(stderr,
"To incorporate impervious area, DHSVM needs a file\n");
fprintf(stderr,
"identified by the key: \"IMPERVIOUS SURFACE ROUTING FILE\",\n");
fprintf(stderr,
"in the \"VEGETATION\" section. This file is used to \n");
fprintf(stderr,
"determine the fate of surface runoff, i.e. where does it go \n");
fprintf(stderr,
"This file was not found: see InitNetwork.c \n");
fprintf(stderr,
"The code find_nearest_channel.c will make the file\n");
ReportError(Options->ImperviousFilePath, 3);
}
for (y = 0; y < NY; y++) {
for (x = 0; x < NX; x++) {
if (INBASIN(TopoMap[y][x].Mask)) {
if (fscanf(inputfile, "%d %d %d %d \n", &sy, &sx, &miny, &minx) !=
EOF) {
TopoMap[y][x].drains_x = minx;
TopoMap[y][x].drains_y = miny;
}
else {
ReportError(Options->ImperviousFilePath, 63);
}
if (sx != x || sy != y) {
ReportError(Options->ImperviousFilePath, 64);
}
}
}
}
}
}