-
Notifications
You must be signed in to change notification settings - Fork 23
/
InitTerrainMaps.c
executable file
·263 lines (235 loc) · 8.8 KB
/
InitTerrainMaps.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
/*
* SUMMARY: InitTerrainMaps() - Initialize terrain coverages
* USAGE: Part of DHSVM
*
* AUTHOR: Bart Nijssen
* ORG: University of Washington, Department of Civil Engineering
* E-MAIL: [email protected]
* ORIG-DATE: Apr-96
* DESCRIPTION: Initialize terrain coverages
* DESCRIP-END.
* FUNCTIONS: InitTerrainMaps()
* InitTopoMap()
* InitSoilMap()
* InitVegMap()
* COMMENTS:
* $Id: InitTerrainMaps.c,v 1.7 2003/11/18 00:24:24 tbohn Exp $
*/
#include <stdio.h>
#include <stdlib.h>
#include "settings.h"
#include "data.h"
#include "DHSVMerror.h"
#include "fileio.h"
#include "functions.h"
#include "constants.h"
#include "getinit.h"
#include "sizeofnt.h"
#include "slopeaspect.h"
#include "varid.h"
/*****************************************************************************
InitTerrainMaps()
*****************************************************************************/
void InitTerrainMaps(LISTPTR Input, OPTIONSTRUCT * Options, MAPSIZE * Map,
LAYER * Soil, TOPOPIX *** TopoMap, SOILPIX *** SoilMap,
VEGPIX *** VegMap)
{
printf("Initializing terrain maps\n");
InitTopoMap(Input, Options, Map, TopoMap);
InitSoilMap(Input, Options, Map, Soil, *TopoMap, SoilMap);
InitVegMap(Input, Map, VegMap);
}
/*****************************************************************************
InitTopoMap()
*****************************************************************************/
void InitTopoMap(LISTPTR Input, OPTIONSTRUCT * Options, MAPSIZE * Map,
TOPOPIX *** TopoMap)
{
const char *Routine = "InitTopoMap";
char VarName[BUFSIZE + 1]; /* Variable name */
int i; /* Counter */
int x; /* Counter */
int y; /* Counter */
int NumberType; /* Number type of data set */
unsigned char *Mask = NULL; /* Basin mask */
float *Elev; /* Surface elevation */
STRINIENTRY StrEnv[] = {
{"TERRAIN", "DEM FILE", "", ""},
{"TERRAIN", "BASIN MASK FILE", "", ""},
{NULL, NULL, "", NULL}
};
/* Process the [TERRAIN] section in the input file */
/* Read the key-entry pairs from 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);
}
/* Read the elevation data from the DEM dataset */
GetVarName(001, 0, VarName);
GetVarNumberType(001, &NumberType);
if (!(Elev = (float *) calloc(Map->NX * Map->NY,
SizeOfNumberType(NumberType))))
ReportError((char *) Routine, 1);
Read2DMatrix(StrEnv[demfile].VarStr, Elev, NumberType, Map->NY, Map->NX, 0,
VarName);
/* Read the mask */
GetVarName(002, 0, VarName);
GetVarNumberType(002, &NumberType);
if (!(Mask = (unsigned char *) calloc(Map->NX * Map->NY,
SizeOfNumberType(NumberType))))
ReportError((char *) Routine, 1);
Read2DMatrix(StrEnv[maskfile].VarStr, Mask, NumberType, Map->NY, Map->NX, 0,
VarName);
/* Assign the attributes to the correct map pixel */
if (!(*TopoMap = (TOPOPIX **) calloc(Map->NY, sizeof(TOPOPIX *))))
ReportError((char *) Routine, 1);
for (y = 0; y < Map->NY; y++) {
if (!((*TopoMap)[y] = (TOPOPIX *) calloc(Map->NX, sizeof(TOPOPIX))))
ReportError((char *) Routine, 1);
}
for (y = 0, i = 0; y < Map->NY; y++)
for (x = 0; x < Map->NX; x++, i++)
(*TopoMap)[y][x].Dem = Elev[i];
free(Elev);
for (y = 0, i = 0; y < Map->NY; y++)
for (x = 0; x < Map->NX; x++, i++)
(*TopoMap)[y][x].Mask = Mask[i];
free(Mask);
/* Calculate slope, aspect, magnitude of subsurface flow gradient, and
fraction of flow flowing in each direction based on the land surface
slope. */
ElevationSlopeAspect(Map, *TopoMap);
/* After calculating the slopes and aspects for all the points, reset the
mask if the model is to be run in point mode */
if (Options->Extent == POINT) {
for (y = 0; y < Map->NY; y++)
for (x = 0; x < Map->NX; x++)
(*TopoMap)[y][x].Mask = OUTSIDEBASIN;
(*TopoMap)[Options->PointY][Options->PointX].Mask = (1 != OUTSIDEBASIN);
}
}
/*****************************************************************************
InitSoilMap()
*****************************************************************************/
void InitSoilMap(LISTPTR Input, OPTIONSTRUCT * Options, MAPSIZE * Map,
LAYER * Soil, TOPOPIX ** TopoMap, SOILPIX *** SoilMap)
{
const char *Routine = "InitSoilMap";
char VarName[BUFSIZE + 1]; /* Variable name */
int i; /* counter */
int x; /* counter */
int y; /* counter */
int NumberType; /* number type */
unsigned char *Type; /* Soil type */
float *Depth; /* Soil depth */
STRINIENTRY StrEnv[] = {
{"SOILS", "SOIL MAP FILE", "", ""},
{"SOILS", "SOIL DEPTH FILE", "", ""},
{NULL, NULL, "", NULL}
};
/* Process the filenames in the [SOILS] section in the input file */
/* Read the key-entry pairs from 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);
}
/* Read the soil type */
GetVarName(003, 0, VarName);
GetVarNumberType(003, &NumberType);
if (!(Type = (unsigned char *) calloc(Map->NX * Map->NY,
SizeOfNumberType(NumberType))))
ReportError((char *) Routine, 1);
Read2DMatrix(StrEnv[soiltype_file].VarStr, Type, NumberType, Map->NY,
Map->NX, 0, VarName);
/* Read the total soil depth */
GetVarName(004, 0, VarName);
GetVarNumberType(004, &NumberType);
if (!(Depth = (float *) calloc(Map->NX * Map->NY,
SizeOfNumberType(NumberType))))
ReportError((char *) Routine, 1);
Read2DMatrix(StrEnv[soildepth_file].VarStr, Depth, NumberType, Map->NY,
Map->NX, 0, VarName);
/* Assign the attributes to the correct map pixel */
if (!(*SoilMap = (SOILPIX **) calloc(Map->NY, sizeof(SOILPIX *))))
ReportError((char *) Routine, 1);
for (y = 0; y < Map->NY; y++) {
if (!((*SoilMap)[y] = (SOILPIX *) calloc(Map->NX, sizeof(SOILPIX))))
ReportError((char *) Routine, 1);
}
for (y = 0, i = 0; y < Map->NY; y++) {
for (x = 0; x < Map->NX; x++, i++) {
if (((int) Type[i]) > Soil->NTypes)
ReportError(StrEnv[soiltype_file].VarStr, 32);
(*SoilMap)[y][x].Soil = Type[i];
(*SoilMap)[y][x].Depth = Depth[i];
if (Options->Infiltration == DYNAMIC)
(*SoilMap)[y][x].InfiltAcc = 0.;
(*SoilMap)[y][x].MoistInit = 0.;
/* allocate memory for the number of root layers, plus an additional
layer below the deepest root layer */
if (INBASIN(TopoMap[y][x].Mask)) {
if (!((*SoilMap)[y][x].Moist =
(float *) calloc((Soil->NLayers[Type[i] - 1] + 1),
sizeof(float))))
ReportError((char *) Routine, 1);
if (!((*SoilMap)[y][x].Perc =
(float *) calloc(Soil->NLayers[Type[i] - 1], sizeof(float))))
ReportError((char *) Routine, 1);
if (!((*SoilMap)[y][x].Temp =
(float *) calloc(Soil->NLayers[Type[i] - 1], sizeof(float))))
ReportError((char *) Routine, 1);
}
else {
(*SoilMap)[y][x].Moist = NULL;
(*SoilMap)[y][x].Perc = NULL;
(*SoilMap)[y][x].Temp = NULL;
}
}
}
free(Type);
free(Depth);
}
/*****************************************************************************
InitVegMap()
*****************************************************************************/
void InitVegMap(LISTPTR Input, MAPSIZE * Map, VEGPIX *** VegMap)
{
const char *Routine = "InitVegMap";
char VarName[BUFSIZE + 1];
char VegMapFileName[BUFSIZE + 1];
int i; /* counter */
int x; /* counter */
int y; /* counter */
int NumberType; /* number type */
unsigned char *Type; /* Vegetation type */
/* Get the map filename from the [VEGETATION] section */
GetInitString("VEGETATION", "VEGETATION MAP FILE", "", VegMapFileName,
(unsigned long) BUFSIZE, Input);
if (!VegMapFileName)
ReportError("VEGETATION MAP FILE", 51);
/* Read the vegetation type */
GetVarName(005, 0, VarName);
GetVarNumberType(005, &NumberType);
if (!(Type = (unsigned char *) calloc(Map->NX * Map->NY,
SizeOfNumberType(NumberType))))
ReportError((char *) Routine, 1);
Read2DMatrix(VegMapFileName, Type, NumberType, Map->NY, Map->NX, 0, VarName);
/* Assign the attributes to the correct map pixel */
if (!(*VegMap = (VEGPIX **) calloc(Map->NY, sizeof(VEGPIX *))))
ReportError((char *) Routine, 1);
for (y = 0; y < Map->NY; y++) {
if (!((*VegMap)[y] = (VEGPIX *) calloc(Map->NX, sizeof(VEGPIX))))
ReportError((char *) Routine, 1);
}
for (y = 0, i = 0; y < Map->NY; y++) {
for (x = 0; x < Map->NX; x++, i++) {
(*VegMap)[y][x].Veg = Type[i];
(*VegMap)[y][x].Tcanopy = 0.0;
}
}
free(Type);
}