-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
272 lines (252 loc) · 9.34 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
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
#define _MAIN_C_
#include <stdlib.h>
#include <string.h>
#include <gdal.h>
#include <cpl_conv.h>
#include <math.h>
#include <omp.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, write_outlet = 0, find_full = 0, use_lessmem = 0;
double (*recode)(double, void *) = NULL;
int *recode_data = NULL, encoding[8];
char *dir_path = NULL, *outlets_path = NULL, *id_col =
NULL, *output_path = NULL;
struct raster_map *dir_map;
struct outlet_list *outlet_l;
struct timeval start_time, end_time;
#ifdef LOOP_THEN_TASK
char *p;
if ((p = getenv("MELFP_TRACING_STACK_SIZE")))
tracing_stack_size = atoi(p);
else
tracing_stack_size = 1024 * 3;
#endif
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 'o':
write_outlet = 1;
break;
case 'f':
find_full = 1;
break;
case 'l':
if (!use_lessmem)
use_lessmem = 1;
break;
case 'L':
use_lessmem = 2;
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;
#ifdef LOOP_THEN_TASK
case 's':
if (i == argc - 1) {
fprintf(stderr, "-%c: Missing tracing stack size\n",
argv[i][j]);
print_usage = 2;
break;
}
tracing_stack_size = atoi(argv[++i]);
break;
#endif
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 (!outlets_path)
outlets_path = argv[i];
else if (!id_col)
id_col = argv[i];
else if (!output_path) {
output_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: melfp [-ofl] [-s size] fdr.tif outlets.shp id_col output.ext\n");
printf("\n");
printf(" -o\t\tWrite outlet rows and columns, and exit\n");
printf(" -f\t\tFind full longest flow paths\n");
printf
(" -l\t\tUse less memory and don't preserve input data (faster than -L)\n");
printf(" -L\t\tUse less memory and preserve input data\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");
#ifdef LOOP_THEN_TASK
printf(" -s size\tTracing stack size (default %d)\n",
tracing_stack_size);
#endif
printf(" fdr.tif\tInput flow direction GeoTIFF\n");
printf(" outlets.shp\tInput outlets Shapefile\n");
printf(" id_col\tID column\n");
printf
(" output.ext\tOutput longest flow path head coordinatess (TODO: Shapefile)\n");
printf
(" \t\tOutput text file for outlet rows and columns with -o\n");
exit(print_usage == 1 ? EXIT_SUCCESS : EXIT_FAILURE);
}
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));
printf("Reading outlets <%s>...\n", outlets_path);
gettimeofday(&start_time, NULL);
if (!(outlet_l = read_outlets(outlets_path, id_col, dir_map, find_full))) {
fprintf(stderr, "%s: Failed to read outlets\n", outlets_path);
exit(EXIT_FAILURE);
}
gettimeofday(&end_time, NULL);
printf("Input time for outlets: %lld microsec\n",
timeval_diff(NULL, &end_time, &start_time));
if (write_outlet) {
printf("Writing outlets...\n");
gettimeofday(&start_time, NULL);
if (write_outlets(output_path, outlet_l) > 0) {
fprintf(stderr, "%s: Failed to write outlets file\n",
output_path);
free_raster(dir_map);
free_outlet_list(outlet_l);
exit(EXIT_FAILURE);
}
gettimeofday(&end_time, NULL);
printf("Output time for outlets: %lld microsec\n",
timeval_diff(NULL, &end_time, &start_time));
}
else {
#pragma omp parallel
#pragma omp single
{
size_t num_cells = (size_t)dir_map->nrows * dir_map->ncols;
int num_threads = omp_get_num_threads();
printf("Number of cells: %zu\n", num_cells);
printf("Number of outlets: %d\n", outlet_l->n);
#ifdef LOOP_THEN_TASK
if (tracing_stack_size <= 0) {
printf
("Guessing tracing stack size using sqrt(nrows * ncols) / num_threads...\n");
tracing_stack_size =
sqrt((size_t)dir_map->nrows * dir_map->ncols) /
num_threads;
}
printf("Tracing stack size for loop-then-task: %d\n",
tracing_stack_size);
#endif
printf("Finding longest flow paths using %d thread(s)...\n",
num_threads);
}
gettimeofday(&start_time, NULL);
lfp(dir_map, outlet_l, find_full, use_lessmem);
gettimeofday(&end_time, NULL);
printf
("Computation time for longest flow paths: %lld microsec\n",
timeval_diff(NULL, &end_time, &start_time));
printf("Writing longest flow path source points <%s>...\n",
output_path);
gettimeofday(&start_time, NULL);
if (write_heads(output_path, id_col, outlet_l, dir_map) > 0) {
fprintf(stderr,
"%s: Failed to write longest flow path source points\n",
output_path);
free_raster(dir_map);
free_outlet_list(outlet_l);
exit(EXIT_FAILURE);
}
gettimeofday(&end_time, NULL);
printf
("Output time for longest flow path source points: %lld microsec\n",
timeval_diff(NULL, &end_time, &start_time));
}
free_raster(dir_map);
free_outlet_list(outlet_l);
exit(EXIT_SUCCESS);
}