-
Notifications
You must be signed in to change notification settings - Fork 23
/
CalcTopoIndex.c
executable file
·346 lines (287 loc) · 10.3 KB
/
CalcTopoIndex.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* SUMMARY: CalcTopoIndex.c - Calculate the topographic index
* USAGE: Part of MWM
*
* AUTHOR: Colleen O. Doten
* ORG: University of Washington, Department of Civil Engineering
* DESCRIPTION: Calculate the topographic index for the redistribution of
soil moisture from the coarse grid to the fine grid.
* DESCRIP-END.
* FUNCTIONS: CalcTopoIndex()
* COMMENTS:
*/
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "DHSVMerror.h"
#include "constants.h"
#include "data.h"
#include "settings.h"
#include "slopeaspect.h"
#define VERTRES 1 /* vertical resolution of the dem */
/*****************************************************************************
Function name: CalcTopoIndex()
Purpose : Calculate the topographic index for the redistribution of
soil moisture from the coarse grid to the fine grid.
Required :
MAPSIZE *Map - mass wasting resolution map data
FINEPIX **FineMap - mask and dem for mass wasting resoltuion map
Returns : float, topographic index
Modifies : none
Comments : Based on the topographic index, ln (a/tan beta) from TOPMODEL
(Beven & Kirkby 1979). Calculated according to Wolock 1995.
The surrounding grid cells are numbered in the following way
|-----| DX
0-----1-----2 ---
|\ | /| |
| \ | / | |
| \ | / | | DY
| \ | / | |
| \|/ | |
7-----*-----3 ---
| /|\ |
| / | \ |
| / | \ |
| / | \ |
|/ | \|
6-----5-----4
For the current implementation it is assumed that the resolution is the
same in both the X and the Y direction.
Source : Beven K.J. and M.J. Kirkby, 1979, A physically based, variable
contributing area model of basin hydrology, Hydrol Sci Bull 24,
43-69.
Wolock, David M. and Gregory J. McCabe, Jr., 1995, Comparison
of single and multiple flow direction algorithms for computing
topographic parameters in TOPMODEL, Water Resources Research,
31 (5), 1315-1324.
*****************************************************************************/
void CalcTopoIndex(MAPSIZE *Map, FINEPIX ***FineMap, TOPOPIX **TopoMap)
{
FILE *fo;
char topoindexmap[100];
int printmap;
int i, j, k, x, y, n, lower; /* counters */
int dx, dy;
float celev;
float neighbor_elev[NNEIGHBORS];
float temp_slope[NNEIGHBORS];
float delta_a[NNEIGHBORS];
float length_diagonal;
float **a; /* Area of hillslope per unit contour (m2) */
float **tanbeta;
float **contour_length;
int coarsei, coarsej;
ITEM *OrderedCellsfine;
/* These indices are so neighbors can be looked up quickly */
int xneighbor[NNEIGHBORS] = {
#if NNEIGHBORS == 4
0, 1, 0, -1
#elif NNEIGHBORS == 8
-1, 0, 1, 1, 1, 0, -1, -1
#endif
};
int yneighbor[NNEIGHBORS] = {
#if NNEIGHBORS == 4
-1, 0, 1, 0
#elif NNEIGHBORS == 8
1, 1, 1, 0, -1, -1, -1, 0
#endif
};
/**************************************************************************
Allocate memory
***************************************************************************/
if (!(a = (float **)calloc(Map->NYfine, sizeof(float *))))
ReportError("CalcTopoIndex", 1);
for(i=0; i<Map->NYfine; i++) {
if (!(a[i] = (float *)calloc(Map->NXfine, sizeof(float))))
ReportError("CalcTopoIndex", 1);
}
if (!(tanbeta = (float **)calloc(Map->NYfine, sizeof(float *))))
ReportError("CalcTopoIndex", 1);
for(i=0; i<Map->NYfine; i++) {
if (!(tanbeta[i] = (float *)calloc(Map->NXfine, sizeof(float))))
ReportError("CalcTopoIndex", 1);
}
if (!(contour_length = (float **)calloc(Map->NYfine, sizeof(float *))))
ReportError("CalcTopoIndex", 1);
for(i=0; i<Map->NYfine; i++) {
if (!(contour_length[i] = (float *)calloc(Map->NXfine, sizeof(float))))
ReportError("CalcTopoIndex", 1);
}
if (!(OrderedCellsfine = (ITEM *) calloc(Map->NumCellsfine, sizeof(ITEM))))
ReportError("CalcTopoIndex", 1);
dx = Map->DMASS;
dy = Map->DMASS;
length_diagonal = sqrt((pow(dx, 2)) + (pow(dy, 2)));
k = 0;
for (y = 0; y < Map->NYfine; y++) {
for (x = 0; x < Map->NXfine; x++) {
coarsei = floor(y*Map->DMASS/Map->DY);
coarsej = floor(x*Map->DMASS/Map->DX);
/* Save the elevation, y, and x in the ITEM structure. */
if (INBASIN(TopoMap[coarsei][coarsej].Mask)) {
OrderedCellsfine[k].Rank = (*FineMap[y][x]).Dem;
OrderedCellsfine[k].y = y;
OrderedCellsfine[k].x = x;
k++;
}
}
}
/* Sort Elev in descending order-- Elev.x and Elev.y hold indices. */
quick(OrderedCellsfine, Map->NumCellsfine);
for (k = (Map->NumCellsfine)-1; k >-1; k--) {
y = OrderedCellsfine[k].y;
x = OrderedCellsfine[k].x;
a[y][x]= dx * dy; /* Intialize cummulative area to cell area */
}
/* Loop through all cells in descending order of elevation */
for (k = (Map->NumCellsfine)-1; k >-1; k--) {
y = OrderedCellsfine[k].y;
x = OrderedCellsfine[k].x;
/* fill neighbor array */
for (n = 0; n < NNEIGHBORS; n++) {
int xn = x + xneighbor[n];
int yn = y + yneighbor[n];
// Initialize neighbor_elev
neighbor_elev[n] = (float) OUTSIDEBASIN;
// Check whether yn, xn are within FineMap array bounds
if (valid_cell_fine(Map,xn,yn)){
coarsei = floor(yn*Map->DMASS/Map->DY);
coarsej = floor(xn*Map->DMASS/Map->DX);
// Check whether FineMap element has been allocated for this cell
// (equivalent to checking whether parent coarse grid cell is within coarse mask)
if (INBASIN(TopoMap[coarsei][coarsej].Mask)){
// Solve for all grid cells within the coarse mask, not just the fine mask.
neighbor_elev[n] = ((TopoMap[coarsei][coarsej].Mask) ? (*FineMap[yn][xn]).Dem : (float) OUTSIDEBASIN);
}
}
}
celev = (*FineMap[y][x]).Dem;
switch (NNEIGHBORS) {
case 8:
lower = 0;
for (n = 0; n < NNEIGHBORS; n++) {
if(neighbor_elev[n] == OUTSIDEBASIN) {
neighbor_elev[n] = celev;
}
/* Calculating tanbeta as tanbeta * length of cell boundary between
the cell of interest and downsloping neighbor. */
if(neighbor_elev[n] < celev){
if(n==0 || n==2 || n==4 || n==6){
temp_slope[n] = (celev - neighbor_elev[n]) /length_diagonal;
contour_length[y][x] += 0.4*Map->DMASS;
tanbeta[y][x] += temp_slope[n]*(0.4*Map->DMASS);
delta_a[n] = a[y][x] * temp_slope[n]*(0.4*Map->DMASS);
}
else {
temp_slope[n] = (celev - neighbor_elev[n]) /Map->DMASS;
contour_length[y][x] += 0.6*Map->DMASS;
tanbeta[y][x] += temp_slope[n]*(0.6*Map->DMASS);
delta_a[n] = a[y][x] * temp_slope[n]*(0.6*Map->DMASS);
}
}
else
lower++;
} /* end for (n = 0; n < NNEIGHBORS; n++) { */
if (lower == 8){
/* if this is a flat area then tanbeta = sum of (0.5 * vertical resolution of elevation
data)/ honizontal distance between centers of neighboring grid cells */
tanbeta[y][x] = ((NNEIGHBORS/2)*((0.5 * VERTRES)/length_diagonal)) +
((NNEIGHBORS/2)*((0.5 * VERTRES)/(Map->DMASS)));
}
/* Distributing total upslope area to downslope neighboring cells */
for (n = 0; n < NNEIGHBORS; n++) {
if(neighbor_elev[n]<celev){
switch (n) {
case 0:
a[y+1][x-1] += delta_a[n]/tanbeta[y][x];
break;
case 1:
a[y+1][x] += delta_a[n]/tanbeta[y][x];
break;
case 2:
a[y+1][x+1] += delta_a[n]/tanbeta[y][x];
break;
case 3:
a[y][x+1] += delta_a[n]/tanbeta[y][x];
break;
case 4:
a[y-1][x+1] += delta_a[n]/tanbeta[y][x];
break;
case 5:
a[y-1][x] += delta_a[n]/tanbeta[y][x];
break;
case 6:
a[y-1][x-1] += delta_a[n]/tanbeta[y][x];
break;
case 7:
a[y][x-1] += delta_a[n]/tanbeta[y][x];
break;
default:
ReportError("CalcTopoIndex", 65);
assert(0);
} /* end switch (n) {*/
}/* end if(neighbor_elev[n]<celev){ */
} /* for (n = 0; n < NNEIGHBORS; n++) { */
break; /*end case 8: */
case 4:
ReportError("CalcTopoIndex", 65);
assert(0); /* not set up to do this */
break;
default:
ReportError("CalcTopoIndex", 65);
assert(0); /* other cases don't work either */
} /* end switch (NNEIGHBORS) { */
} /* end for (k = 0; k < Map->NumCellsfine; k++) { */
for (k = (Map->NumCellsfine)-1; k >-1; k--) {
y = OrderedCellsfine[k].y;
x = OrderedCellsfine[k].x;
(*FineMap[y][x]).TopoIndex = log(a[y][x]/tanbeta[y][x]);
}
/*************************************************************************/
/* Create output files...currently hard-coded, chould be moved to dump */
/* functions for user specification. Creates the following file in the */
/* topoindex.asc - TopoIndex for the mass wasting resolution map */
/*************************************************************************/
printmap = 0;
if (printmap == 1){
sprintf(topoindexmap, "topoindex.asc");
if((fo=fopen(topoindexmap,"a")) == NULL)
{
printf("Cannot open TopoIndex output file.\n");
exit(0);
}
fprintf(fo,"ncols %11d\n",Map->NXfine);
fprintf(fo,"nrows %11d\n",Map->NYfine);
fprintf(fo,"xllcorner %.1f\n",Map->Xorig);
fprintf(fo,"yllcorner %.1f\n",Map->Yorig - Map->NY*Map->DY);
fprintf(fo,"cellsize %.0f\n",Map->DMASS);
fprintf(fo,"NODATA_value %d\n",0);
for (y = 0; y < Map->NYfine; y++) {
for (x = 0; x < Map->NXfine; x++) {
i = (int) floor(y/(Map->DY/Map->DMASS));
j = (int) floor(x/(Map->DX/Map->DMASS));
/* Check to make sure region is in the basin. */
if (INBASIN(TopoMap[i][j].Mask))
fprintf(fo, "%2.3f ", (*FineMap[y][x]).TopoIndex);
/* fprintf(fo, "%2.3f ", log(a[y][x])); */
/* fprintf(fo, "%2.3f ", log(1/tanbeta[y][x])); */
else
fprintf(fo, "0. ");
}
fprintf(fo, "\n");
}
fclose(fo);
}
for(i=0; i<Map->NYfine; i++) {
free(a[i]);
free(tanbeta[i]);
free(contour_length[i]);
}
free(a);
free(tanbeta);
free(contour_length);
free(OrderedCellsfine);
return;
}