-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
210 lines (192 loc) · 7.32 KB
/
Copy pathmain.c
File metadata and controls
210 lines (192 loc) · 7.32 KB
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
#define _MAIN_C_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#include <gdal.h>
#include "global.h"
int main(int argc, char *argv[])
{
int i;
int print_usage = 1, use_lessmem = 1, compress_output = 0;
double (*recode)(double, void *) = NULL;
int *recode_data = NULL, encoding[8];
char *dir_path = NULL, *dir_opts = NULL, *accum_path = NULL;
int num_threads = 0;
struct raster_map *dir_map, *accum_map;
struct timeval first_time, start_time, end_time;
gettimeofday(&first_time, NULL);
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 'm':
use_lessmem = 0;
break;
case 'z':
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;
case 'D':
if (i == argc - 1) {
fprintf(stderr,
"-%c: Missing GDAL options for input direction\n",
argv[i][j]);
print_usage = 2;
break;
}
dir_opts = argv[++i];
break;
case 't':
if (i == argc - 1) {
fprintf(stderr, "-%c: Missing number of threads\n",
argv[i][j]);
print_usage = 2;
break;
}
num_threads = atoi(argv[++i]);
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 OPTIONS dir accum\n\n"
" dir\t\tInput flow direction raster (e.g., gpkg:file.gpkg:layer)\n"
" accum\t\tOutput GeoTIFF\n"
" -m\t\tUse more memory\n"
" -z\t\tCompress output GeoTIFF\n"
" -e encoding\tInput flow direction encoding\n"
"\t\tpower2 (default): 2^0-7 CW from E (e.g., r.terraflow, ArcGIS)\n"
"\t\ttaudem: 1-8 (E-SE CCW) (e.g., d8flowdir)\n"
"\t\t45degree: 1-8 (NE-E CCW) (e.g., r.watershed)\n"
"\t\tdegree: (0,360] (E-E CCW)\n"
"\t\tE,SE,S,SW,W,NW,N,NE: custom (e.g., 1,8,7,6,5,4,3,2 for taudem)\n"
" -D opts\tComma-separated list of GDAL options for dir\n"
" -t threads\tNumber of threads (default OMP_NUM_THREADS)\n");
exit(EXIT_SUCCESS);
}
if (num_threads == 0)
num_threads = omp_get_max_threads();
else {
if (num_threads < 0) {
num_threads += omp_get_num_procs();
if (num_threads < 1)
num_threads = 1;
}
omp_set_num_threads(num_threads);
}
printf("Using %d threads...\n", num_threads);
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, dir_opts, 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, dir_opts, 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);
gettimeofday(&end_time, NULL);
printf("Total elapsed time: %lld microsec\n",
timeval_diff(NULL, &end_time, &first_time));
exit(EXIT_SUCCESS);
}