-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrarea.c
executable file
·139 lines (114 loc) · 2.72 KB
/
drarea.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
/*
** drarea.c: Computes contributing drainage areas.
*/
#include <stdio.h>
#define XSIZE 609
#define YSIZE 1765
int area[XSIZE][YSIZE];
short nbrx[XSIZE][YSIZE];
short nbry[XSIZE][YSIZE];
void GetFileName( basename )
char *basename;
{
printf("Enter base name of flow direction files (no ext): ");
gets( basename );
}
void ReadFlowDirFiles( basename )
char *basename;
{
int i;
FILE *fp;
char outfile[80];
/* Write x-direction file */
strcpy( outfile, basename );
strcat( outfile, ".nbrx" );
if( (fp = fopen( outfile, "r" ))==NULL )
{
printf( "Sorry, I can't find %s\n",outfile );
exit(1);
}
printf( "Reading %s...", outfile );
fread( nbrx, sizeof( nbrx ), 1, fp );
fclose( fp );
printf( "done.\n" );
printf("Area at (64,944) = %d\n",nbrx[64][944]);
/* Write y-direction file */
strcpy( outfile, basename );
strcat( outfile, ".nbry" );
if( (fp = fopen( outfile, "r" ))==NULL )
{
printf( "Sorry, I can't find %s\n",outfile );
exit(1);
}
printf( "Reading %s...", outfile );
fread( nbry, sizeof( nbry ), 1, fp );
fclose( fp );
printf( "done.\n" );
}
void StreamTrace()
{
int i, j, /* counters for x and y coords */
p, q, /* x and y coords of cells traced along stream line */
newp, /* used so that old value of p won't get lost */
test; /* counter tests for endless loops when cells */
/* point to one another. */
printf("Computing contibuting areas...");
for( i=1; i<XSIZE-1; i++ )
for( j=1; j<YSIZE-1; j++ )
area[i][j] = 0;
for( i=1; i<XSIZE-2; i++ ) for( j=1; j<YSIZE-2; j++ )
{
if( i>=64 ) {printf("\n[%d,%d] ",i,j);
printf("<nbrx: %d> ",nbrx[i][j]);
} if( nbrx[i][j]> -1 )
{
p = i;
q = j;
printf("bad ");
test = 0;
printf("iw ");
while( p>0 && q>0 && p<XSIZE-1 && q<YSIZE-1 )
{
printf("(%d,%d) ",p,q);
area[p][q] ++;
test++;
printf("yuk ");
if( test > 9980 )
printf( "-> (%d,%d) \n",p,q );
if( test > 10000 )
{
printf( "There seems to be an endless loop in StreamTrace.\n" );
exit( 0 );
}
newp = nbry[p][q];
q = nbrx[p][q];
p = newp;
}
}
}
printf("done.\n");
}
void WriteAreaFile( basename )
char *basename;
{
int i;
FILE *fp;
char outfile[80];
/* Write x-direction file */
strcpy( outfile, basename );
strcat( outfile, ".area" );
fp = fopen( outfile, "w" );
printf( "Writing %s...", outfile );
fwrite( area, sizeof( area ), 1, fp );
fclose( fp );
printf( "done.\n" );
}
main()
{
char fname[80];
GetFileName( fname );
ReadFlowDirFiles( fname );
StreamTrace();
WriteAreaFile( fname );
printf( "All done!\n");
}