-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcams-download.c
358 lines (305 loc) · 13.1 KB
/
cams-download.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
347
348
349
350
351
352
353
354
355
356
357
358
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/stat.h>
#define __USE_XOPEN
#include <time.h>
#include <curl/curl.h>
#include <assert.h>
#include "src/download.h"
#define DEBUG
#ifdef DEBUG
#define NO_GETOPT_ERROR_OUTPUT 0
#else
#define NO_GETOPT_ERROR_OUTPUT 1
#endif // DEBUG
int main(int argc, char *argv[]) {
char error_string[NPOW16];
char *time_conversion_result;
char time_formats[NPOW2][NPOW8] = {
"%F",
"%D",
"%d.%m.%Y",
"%d/%m/%Y"
};
int optid;
int option_index = 0; // will only be set, if parsing was successful
opterr = NO_GETOPT_ERROR_OUTPUT ? 0 : 1;
static struct OPTIONS options = {0};
bool use_area_subset = false;
static struct API_AUTHENTICATION api_authentication = {0};
struct CLIENT client = (struct CLIENT) {
.auth = {0},
.quiet = 1,
.debug = 0,
.timeout = 1800, // cdsapi sets it to 60, to low when downloading 2 GB
.progress = 0,
.full_stack = 0,
.delete = 0,
.max_retries = 10,
.max_sleep = 60,
.last_state = 0,
.wait_until_complete = 1,
.metadata = NULL,
.forget = 0,
.retries = 0,
.curl_handle = NULL
};
static struct PRODUCT_REQUEST request = {0};
CURLcode curl;
CURL *handle;
__attribute__((unused)) CURLcode res;
// populate default/non-user-changeable-options
request.dates.start = (struct tm) {.tm_year = 103, .tm_mon = 0, .tm_mday = 1};
request.dates.end = (struct tm) {.tm_year = 103, .tm_mon = 0, .tm_mday = 1};
request.product = PRODUCT_CAMS_REPROCESSED;
request.variable = "total_aerosol_optical_depth_469nm";
request.format = "grib";
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"purpose", no_argument, NULL, 'i'},
{"authentication", required_argument, NULL, 'a'},
{"coordinates", required_argument, NULL, 'c'},
{"start", required_argument, NULL, '0'},
{"end", required_argument, NULL, '1'},
{"product", required_argument, NULL, '2'},
{"time", required_argument, NULL, '3'},
{"lead-time-hour", required_argument, NULL, '4'},
{"output_directory", required_argument, NULL, 'o'},
{0, 0, 0, 0}
};
// TODO why can I remove a letter from shortopts and still match the short version?
while ((optid = getopt_long_only(argc, argv, "+:hvia:c:o:012:3:4:", long_options, &option_index)) != -1) {
switch (optid) {
case 0: // getopt_long returns `val` if flag == NULL; otherwise 0 (in which case it stores val in flag)
break;
case 'h':
print_usage();
exit(EXIT_SUCCESS);
case 'v':
print_version();
exit(EXIT_SUCCESS);
case 'i':
print_purpose();
exit(EXIT_SUCCESS);
case 'a':
if (optarg == 0) {
// if text is present, optarg points to it; otherwise it is set to 0
fprintf(stderr, "You shouldn't be able to reach this code!!\n");
}
options.use_custom_authentication = 1;
strncpy(options.authentication, optarg, NPOW16);
break;
case 'c':
if (optarg == 0) {
// if text is present, optarg points to it; otherwise it is set to 0
fprintf(stderr, "You shouldn't be able to reach this code!!\n");
}
use_area_subset = true;
strncpy(options.coordinates, optarg, NPOW16);
break;
case 'o':
if (optarg == 0) {
// if text is present, optarg points to it; otherwise it is set to 0
fprintf(stderr, "You shouldn't be able to reach this code!!\n");
}
strncpy(options.output_directory, optarg, NPOW16);
break;
case '0': {
int failed_attempts = 0;
for (int i = 0; i < NPOW2; ++i) {
time_conversion_result = strptime(optarg, time_formats[i], &request.dates.start);
if (time_conversion_result != NULL) break;
failed_attempts++;
}
if (failed_attempts == NPOW2) {
fprintf(stderr, "ERROR: Failed to parse date \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
}
break;
case '1': {
int failed_attempts = 0;
for (int i = 0; i < NPOW2; ++i) {
time_conversion_result = strptime(optarg, time_formats[i], &request.dates.end);
if (time_conversion_result != NULL) break;
failed_attempts++;
}
if (failed_attempts == NPOW2) {
fprintf(stderr, "ERROR: Failed to parse date \"%s\"\n", optarg);
exit(EXIT_FAILURE);
}
}
break;
case '2':
if (optarg == 0) {
// if text is present, optarg points to it; otherwise it is set to 0
fprintf(stderr, "You shouldn't be able to reach this code!!\n");
}
request.product = product_string_to_type(optarg);
break;
case '3': {
if (optarg == 0) {
// if text is present, optarg points to it; otherwise it is set to 0
fprintf(stderr, "You shouldn't be able to reach this code!!\n");
}
char *c = optarg;
char **r = &optarg;
long val;
while (request.time_length < 9 && *c) {
if (*c == '\0' || **r == '\0') break;
val = strtol(c, r, 10);
if (c == *r && (val == 0 || val == LONG_MIN || val == LONG_MAX)) {
fprintf(stderr,
"ERROR: Either no valid number given, or and under-/overflow occured while parsing\n");
exit(EXIT_FAILURE);
}
assert(val >= 0 && val <= 21 && (val % 3) == 0);
request.time_length++;
request.time[request.time_length - 1] = long_to_time(val);
if (**r == '\0') break;
c = ++(*r);
}
assert(request.time_length < 9);
assert(all_unique((int*) request.time, request.time_length));
}
break;
case '4': {
if (optarg == 0) {
// if text is present, optarg points to it; otherwise it is set to 0
fprintf(stderr, "You shouldn't be able to reach this code!!\n");
}
char *c = optarg;
char **r = &optarg;
long val;
while (request.leadtime_length < 121 && *c) {
if (*c == '\0' || **r == '\0') break;
val = strtol(c, r, 10);
if (c == *r && (val == 0 || val == LONG_MIN || val == LONG_MAX)) {
fprintf(stderr,
"ERROR: Either no valid number given, or and under-/overflow occured while parsing\n");
exit(EXIT_FAILURE);
}
assert(val >= 0 && val <= 120);
request.leadtime_length++;
request.leadtime_hour[request.leadtime_length - 1] = (int) val;
if (**r == '\0') break;
c = ++(*r);
}
assert(request.leadtime_length < 121);
assert(all_unique((int *) request.leadtime_hour, request.leadtime_length));
}
break;
case ':':
fprintf(stderr, "Error: expected option for argument -%c/-%s is missing\n", optopt,
reverse_code_optopt(error_string, optopt));
exit(EXIT_FAILURE);
case '?': // fall through
default:
fprintf(stderr, "Error: got unexpected argument \"%c\"\n\n", optopt);
print_usage();
exit(EXIT_FAILURE);
}
}
// after all parsing is done, the variable optind indexes into argv to point to the first non-option argument
// that is: an entry in argv, which is not caught by the option-struct above
while (optind < argc) {
fprintf(stderr, "Warning: Excess argument \"%s\" is ignored.\n", argv[optind]);
optind++;
}
if (!constrain_dates(&request.dates)) {
fprintf(stderr, "Error: Start date is more recent than end date.\n");
exit(EXIT_FAILURE);
}
if ((options.use_custom_authentication && validate_file(options.authentication, F_OK | R_OK) == false) ||
(use_area_subset && validate_file(options.coordinates, F_OK | R_OK) == false) ||
validate_directory(options.output_directory) == false) {
fprintf(stderr, "Error: Credential file, coordinate file or output directory either do not "
"exist, or are not accessible.\n");
exit(EXIT_FAILURE);
}
if (request.time_length == 0) {
request.time[0] = SENSING_TIME_00;
request.time_length++;
}
if (request.leadtime_length == 0) {
request.leadtime_hour[0] = 0;
request.leadtime_length++;
}
if (init_api_authentication(&api_authentication, &options) != 0) {
fprintf(stderr, "Error: Failed to load API configuration from file\n");
exit(EXIT_FAILURE);
}
client.auth = api_authentication;
if (use_area_subset) {
double *longitude, *latitude;
request.bbox = parse_coordinate_file(options.coordinates,
&longitude, &latitude);
printf("North: %d, East: %d, South: %d, West: %d\n",
request.bbox.north, request.bbox.east, request.bbox.south, request.bbox.west);
free(longitude);
free(latitude);
} else {
request.bbox.area_subset = 0;
}
curl = curl_global_init(CURL_GLOBAL_DEFAULT);
if (curl != 0) {
fprintf(stderr, "Error: Failed to set up curl\n");
exit(EXIT_FAILURE);
}
handle = curl_easy_init();
if (!handle) {
fprintf(stderr, "Error: Failed to perform curl_easy_init\n");
exit(EXIT_FAILURE);
}
if (check_ads_status(&handle, &client) == ADS_STATUS_WARNING) {
fprintf(stderr,
"Error: Encountered warning with ADS. Please visit %s/%s.\n",
client.auth.base_url,
"status.json");
exit(EXIT_FAILURE);
}
struct PRODUCT_RESPONSE product_response = ads_request_product(&request, &handle, &client);
if (product_response.state == PRODUCT_STATUS_INVALID) {
fprintf(stderr, "Error: Encountered unknown product status in response to POST request\n");
exit(EXIT_FAILURE);
}
while (product_response.state != PRODUCT_STATUS_COMPLETED && product_response.state != PRODUCT_STATUS_FAILED &&
product_response.state != PRODUCT_STATUS_INVALID && client.retries < client.max_retries) {
printf("Product request in preparation. Try %d/%d. Next request will be made in %d seconds.\n",
client.retries, client.max_retries, client.max_sleep);
if (sleep(client.max_sleep)) {
fprintf(stderr, "Error: Program received a SIGNAL while sleeping. Those are unhandled.\n");
exit(EXIT_FAILURE);
}
ads_check_product_state(&product_response, &handle, &client);
client.retries++;
}
if (client.retries == client.max_retries && product_response.state != PRODUCT_STATUS_FAILED) {
fprintf(stderr, "Error: Exceed maximum number of retries. Product request unsuccessful.\n"
"You can try to run the program with the same request later, to download the requested data.\n");
exit(EXIT_FAILURE);
}
if (product_response.state == PRODUCT_STATUS_FAILED) {
fprintf(stderr, "Error: Product request failed. Please check the website for more information\n");
exit(EXIT_FAILURE);
}
const char *download_path = assemble_download_path(&request, &options);
if (ads_download_product(&product_response, &handle, &client, download_path)) {
fprintf(stderr, "Error: Failed to download file\n");
exit(EXIT_FAILURE);
}
if (client.delete) {
int deletion_status __attribute__((unused)) = ads_delete_product_request(&product_response, &handle, &client);
}
free(product_response.location);
free(product_response.id);
free((char *) download_path);
curl_easy_cleanup(handle);
curl_global_cleanup();
free_api_authentication(&api_authentication);
return 0;
}