-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
174 lines (161 loc) · 5.98 KB
/
main.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
#define _MAIN_C_
#include <stdio.h>
#include <stdlib.h>
#include <gdal.h>
#ifdef _MSC_VER
#include <winsock2.h>
#else
#include <sys/time.h>
#endif
#include "global.h"
int main(int argc, char *argv[])
{
int i;
int print_usage = 1, use_lessmem = 0, compress_output = 0;
double (*recode)(double, void *) = NULL;
int *recode_data = NULL, encoding[8];
char *dir_path = NULL, *accum_path = NULL;
struct raster_map *dir_map, *accum_map;
struct timeval start_time, end_time;
for (i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
int j, n = strlen(argv[i]);
int unknown = 0;
for (j = 1; j < n && !unknown; j++) {
switch (argv[i][j]) {
case 'l':
use_lessmem = 1;
break;
case 'c':
compress_output = 1;
break;
case 'e':
if (i == argc - 1) {
fprintf(stderr, "-%c: Missing encoding\n",
argv[i][j]);
print_usage = 2;
break;
}
if (strcmp(argv[++i], "power2") == 0) {
recode = NULL;
recode_data = NULL;
break;
}
else if (strcmp(argv[i], "taudem") == 0) {
int k;
for (k = 1; k < 9; k++)
encoding[k % 8] = 9 - k;
}
else if (strcmp(argv[i], "45degree") == 0) {
int k;
for (k = 0; k < 8; k++)
encoding[k] = 8 - k;
}
else if (strcmp(argv[i], "degree") == 0) {
recode = recode_degree;
recode_data = NULL;
break;
}
else if (sscanf
(argv[i], "%d,%d,%d,%d,%d,%d,%d,%d",
&encoding[0], &encoding[1], &encoding[2],
&encoding[3], &encoding[4], &encoding[5],
&encoding[6], &encoding[7]) != 8) {
fprintf(stderr, "%s: Invalid encoding\n", argv[i]);
print_usage = 2;
break;
}
recode = recode_encoding;
recode_data = encoding;
break;
default:
unknown = 1;
break;
}
}
if (unknown) {
fprintf(stderr, "%c: Unknown flag\n", argv[i][j]);
print_usage = 2;
break;
}
}
else if (!dir_path)
dir_path = argv[i];
else if (!accum_path) {
accum_path = argv[i];
print_usage = 0;
}
else {
fprintf(stderr, "%s: Unable to process extra arguments\n",
argv[i]);
print_usage = 2;
break;
}
}
if (print_usage) {
if (print_usage == 2)
printf("\n");
printf("Usage: mefa [-lc] [-e encoding] dir.tif accum.tif\n");
printf("\n");
printf(" -l\t\tUse less memory\n");
printf(" -c\t\tCompress output GeoTIFF file\n");
printf(" -e encoding\tDirection encoding\n");
printf
("\t\tpower2 (default): 2^0-7 CW from E (e.g., r.terraflow, ArcGIS)\n");
printf("\t\ttaudem: 1-8 (E-SE CCW) (e.g., d8flowdir)\n");
printf("\t\t45degree: 1-8 (NE-E CCW) (e.g., r.watershed)\n");
printf("\t\tdegree: (0,360] (E-E CCW)\n");
printf
("\t\tE,SE,S,SW,W,NW,N,NE: custom (e.g., 1,8,7,6,5,4,3,2 for taudem)\n");
printf(" dir.tif\tInput GeoTIFF file of flow direction raster\n");
printf
(" accum.tif\tOutput GeoTIFF file for flow accumulation raster\n");
exit(EXIT_SUCCESS);
}
GDALAllRegister();
printf("Reading flow direction raster <%s>...\n", dir_path);
gettimeofday(&start_time, NULL);
if (recode) {
printf("Converting flow direction encoding...\n");
if (!(dir_map = read_raster(dir_path, RASTER_MAP_TYPE_BYTE, 0, recode,
recode_data))) {
fprintf(stderr, "%s: Failed to read flow direction raster\n",
dir_path);
exit(EXIT_FAILURE);
}
}
else if (!
(dir_map =
read_raster(dir_path, RASTER_MAP_TYPE_BYTE, 0, NULL, NULL))) {
fprintf(stderr, "%s: Failed to read flow direction raster\n",
dir_path);
exit(EXIT_FAILURE);
}
gettimeofday(&end_time, NULL);
printf("Input time for flow direction: %lld microsec\n",
timeval_diff(NULL, &end_time, &start_time));
accum_map =
init_raster(dir_map->nrows, dir_map->ncols, RASTER_MAP_TYPE_UINT32);
copy_raster_metadata(accum_map, dir_map);
printf("Accumulating flows...\n");
gettimeofday(&start_time, NULL);
accumulate(dir_map, accum_map, use_lessmem);
gettimeofday(&end_time, NULL);
printf("Computation time for flow accumulation: %lld microsec\n",
timeval_diff(NULL, &end_time, &start_time));
free_raster(dir_map);
accum_map->compress = compress_output;
printf("Writing flow accumulation raster <%s>...\n", accum_path);
gettimeofday(&start_time, NULL);
if (write_raster(accum_path, accum_map, RASTER_MAP_TYPE_AUTO) > 0) {
fprintf(stderr, "%s: Failed to write flow accumulation raster\n",
accum_path);
free_raster(accum_map);
exit(EXIT_FAILURE);
}
gettimeofday(&end_time, NULL);
printf("Output time for flow accumulation: %lld microsec\n",
timeval_diff(NULL, &end_time, &start_time));
free_raster(accum_map);
exit(EXIT_SUCCESS);
}