-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathpred.c
380 lines (319 loc) · 13.4 KB
/
pred.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// --------------------------------------------------------------
// CU Spaceflight Landing Prediction
// Copyright (c) CU Spaceflight 2009, All Right Reserved
//
// Written by Rob Anderson
// Modified by Fergus Noble
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// --------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include "ini/iniparser.h"
#include "util/gopt.h"
#include "wind/wind_file_cache.h"
#include "run_model.h"
#include "pred.h"
#include "altitude.h"
FILE* output;
FILE* kml_file;
const char* data_dir;
int verbosity;
int main(int argc, const char *argv[]) {
const char* argument;
long int initial_timestamp;
float initial_lat, initial_lng, initial_alt;
float burst_alt, ascent_rate, drag_coeff, rmswinderror;
int descent_mode;
int scenario_idx, n_scenarios;
int alarm_time;
char* endptr; // used to check for errors on strtod calls
wind_file_cache_t* file_cache;
dictionary* scenario = NULL;
// configure command-line options parsing
void *options = gopt_sort(&argc, argv, gopt_start(
gopt_option('h', 0, gopt_shorts('h', '?'), gopt_longs("help")),
gopt_option('z', 0, gopt_shorts(0), gopt_longs("version")),
gopt_option('v', GOPT_REPEAT, gopt_shorts('v'), gopt_longs("verbose")),
gopt_option('o', GOPT_ARG, gopt_shorts('o'), gopt_longs("output")),
gopt_option('k', GOPT_ARG, gopt_shorts('k'), gopt_longs("kml")),
gopt_option('t', GOPT_ARG, gopt_shorts('t'), gopt_longs("start_time")),
gopt_option('i', GOPT_ARG, gopt_shorts('i'), gopt_longs("data_dir")),
gopt_option('d', 0, gopt_shorts('d'), gopt_longs("descending")),
gopt_option('e', GOPT_ARG, gopt_shorts('e'), gopt_longs("wind_error")),
gopt_option('a', GOPT_ARG, gopt_shorts('a'), gopt_longs("alarm"))
));
if (gopt(options, 'h')) {
// Help!
// Print usage information
printf("Usage: %s [options] [scenario files]\n", argv[0]);
printf("Options:\n\n");
printf(" -h --help Display this information.\n");
printf(" --version Display version information.\n");
printf(" -v --verbose Display more information while running,\n");
printf(" Use -vv, -vvv etc. for even more verbose output.\n");
printf(" -t --start_time <int> Start time of model, defaults to current time.\n");
printf(" Should be a UNIX standard format timestamp.\n");
printf(" -o --output <file> Output file for CSV data, defaults to stdout. Overrides scenario.\n");
printf(" -k --kml <file> Output KML file.\n");
printf(" -d --descending We are in the descent phase of the flight, i.e. after\n");
printf(" burst or cutdown. burst_alt and ascent_rate ignored.\n");
printf(" -i --data_dir <dir> Input directory for wind data, defaults to current dir.\n\n");
printf(" -e --wind_error <err> RMS windspeed error (m/s).\n");
printf(" -a --alarm <seconds> Use alarm() to kill pred incase it hangs.\n");
printf("The scenario file is an INI-like file giving the launch scenario. If it is\n");
printf("omitted, the scenario is read from standard input.\n");
exit(0);
}
if (gopt(options, 'z')) {
// Version information
printf("Landing Prediction version: %s\nCopyright (c) CU Spaceflight 2009\n", VERSION);
exit(0);
}
if (gopt_arg(options, 'a', &argument) && strcmp(argument, "-")) {
alarm_time = strtol(argument, &endptr, 0);
if (endptr == argument) {
fprintf(stderr, "ERROR: %s: invalid alarm length\n", argument);
exit(1);
}
alarm(alarm_time);
}
verbosity = gopt(options, 'v');
if (gopt(options, 'd'))
descent_mode = DESCENT_MODE_DESCENDING;
else
descent_mode = DESCENT_MODE_NORMAL;
if (gopt_arg(options, 'k', &argument) && strcmp(argument, "-")) {
kml_file = fopen(argument, "wb");
if (!kml_file) {
fprintf(stderr, "ERROR: %s: could not open KML file for output\n", argument);
exit(1);
}
}
else
kml_file = NULL;
if (gopt_arg(options, 't', &argument) && strcmp(argument, "-")) {
initial_timestamp = strtol(argument, &endptr, 0);
if (endptr == argument) {
fprintf(stderr, "ERROR: %s: invalid start timestamp\n", argument);
exit(1);
}
} else {
initial_timestamp = time(NULL);
}
if (!(gopt_arg(options, 'i', &data_dir) && strcmp(data_dir, "-")))
data_dir = "./";
// populate wind data file cache
file_cache = wind_file_cache_new(data_dir);
// read in flight parameters
n_scenarios = argc - 1;
if(n_scenarios == 0) {
// we'll parse from std in
n_scenarios = 1;
}
for(scenario_idx = 0; scenario_idx < n_scenarios; ++scenario_idx) {
char* scenario_output = NULL;
if(argc > scenario_idx+1) {
scenario = iniparser_load(argv[scenario_idx+1]);
} else {
scenario = iniparser_loadfile(stdin);
}
if(!scenario) {
fprintf(stderr, "ERROR: could not parse scanario file.\n");
exit(1);
}
if(verbosity > 1) {
fprintf(stderr, "INFO: Parsed scenario file:\n");
iniparser_dump_ini(scenario, stderr);
}
scenario_output = iniparser_getstring(scenario, "output:filename", NULL);
if (gopt_arg(options, 'o', &argument) && strcmp(argument, "-")) {
if(verbosity > 0) {
fprintf(stderr, "INFO: Writing output to file specified on command line: %s\n", argument);
}
output = fopen(argument, "wb");
if (!output) {
fprintf(stderr, "ERROR: %s: could not open CSV file for output\n", argument);
exit(1);
}
} else if (scenario_output != NULL) {
if(verbosity > 0) {
fprintf(stderr, "INFO: Writing output to file specified in scenario: %s\n", scenario_output);
}
output = fopen(scenario_output, "wb");
if (!output) {
fprintf(stderr, "ERROR: %s: could not open CSV file for output\n", scenario_output);
exit(1);
}
} else {
if(verbosity > 0) {
fprintf(stderr, "INFO: Writing output to stdout.\n");
}
output = stdout;
}
// write KML header
if (kml_file)
start_kml();
// The observant amongst you will notice that there are default values for
// *all* keys. This information should not be spread around too well.
// Unfortunately, this means we lack some error checking.
initial_lat = iniparser_getdouble(scenario, "launch-site:latitude", 0.0);
initial_lng = iniparser_getdouble(scenario, "launch-site:longitude", 0.0);
initial_alt = iniparser_getdouble(scenario, "launch-site:altitude", 0.0);
ascent_rate = iniparser_getdouble(scenario, "altitude-model:ascent-rate", 1.0);
// The 1.1045 comes from a magic constant buried in
// ~cuspaceflight/public_html/predict/index.php.
drag_coeff = iniparser_getdouble(scenario, "altitude-model:descent-rate", 1.0) * 1.1045;
burst_alt = iniparser_getdouble(scenario, "altitude-model:burst-altitude", 1.0);
rmswinderror = iniparser_getdouble(scenario, "atmosphere:wind-error", 0.0);
if(gopt_arg(options, 'e', &argument) && strcmp(argument, "-")) {
rmswinderror = strtod(argument, &endptr);
if (endptr == argument) {
fprintf(stderr, "ERROR: %s: invalid RMS wind speed error\n", argument);
exit(1);
}
}
{
int year, month, day, hour, minute, second;
year = iniparser_getint(scenario, "launch-time:year", -1);
month = iniparser_getint(scenario, "launch-time:month", -1);
day = iniparser_getint(scenario, "launch-time:day", -1);
hour = iniparser_getint(scenario, "launch-time:hour", -1);
minute = iniparser_getint(scenario, "launch-time:minute", -1);
second = iniparser_getint(scenario, "launch-time:second", -1);
if((year >= 0) && (month >= 0) && (day >= 0) && (hour >= 0)
&& (minute >= 0) && (second >= 0))
{
struct tm timeval = { 0 };
time_t scenario_launch_time = -1;
if(verbosity > 0) {
fprintf(stderr, "INFO: Using launch time from scenario: "
"%i/%i/%i %i:%i:%i\n",
year, month, day, hour, minute, second);
}
timeval.tm_sec = second;
timeval.tm_min = minute;
timeval.tm_hour = hour;
timeval.tm_mday = day; /* 1 - 31 */
timeval.tm_mon = month - 1; /* 0 - 11 */
timeval.tm_year = year - 1900; /* fuck you Millenium Bug! */
#ifndef _BSD_SOURCE
# warning This version of mktime does not allow explicit setting of timezone.
#else
timeval.tm_zone = "UTC";
#endif
scenario_launch_time = mktime(&timeval);
if(scenario_launch_time <= 0) {
fprintf(stderr, "ERROR: Launch time in scenario is invalid\n");
exit(1);
} else {
initial_timestamp = scenario_launch_time;
}
}
}
if(verbosity > 0) {
fprintf(stderr, "INFO: Scenario loaded:\n");
fprintf(stderr, " - Initial latitude : %lf deg N\n", initial_lat);
fprintf(stderr, " - Initial longitude : %lf deg E\n", initial_lng);
fprintf(stderr, " - Initial altitude : %lf m above sea level\n", initial_alt);
fprintf(stderr, " - Initial timestamp : %li\n", initial_timestamp);
fprintf(stderr, " - Drag coeff. : %lf\n", drag_coeff);
if(!descent_mode) {
fprintf(stderr, " - Ascent rate : %lf m/s\n", ascent_rate);
fprintf(stderr, " - Burst alt. : %lf m\n", burst_alt);
}
fprintf(stderr, " - Windspeed err. : %f m/s\n", rmswinderror);
}
{
// do the actual stuff!!
altitude_model_t* alt_model = altitude_model_new(descent_mode, burst_alt,
ascent_rate, drag_coeff);
if(!alt_model) {
fprintf(stderr, "ERROR: error initialising altitude profile\n");
exit(1);
}
if (!run_model(file_cache, alt_model,
initial_lat, initial_lng, initial_alt, initial_timestamp,
rmswinderror)) {
fprintf(stderr, "ERROR: error during model run!\n");
exit(1);
}
altitude_model_free(alt_model);
}
// release the scenario
iniparser_freedict(scenario);
// write footer to KML and close output files
if (kml_file) {
finish_kml();
fclose(kml_file);
}
if (output != stdout) {
fclose(output);
}
}
// release gopt data,
gopt_free(options);
// release the file cache resources.
wind_file_cache_free(file_cache);
return 0;
}
void write_position(float lat, float lng, float alt, int timestamp) {
// the predictor uses 0<=lng<360; most other things expect -180<lng<=180
if (lng > 180)
lng -= 360;
if (kml_file) {
fprintf(kml_file, "%g,%g,%g\n", lng, lat, alt);
if (ferror(kml_file)) {
fprintf(stderr, "ERROR: error writing to KML file\n");
exit(1);
}
}
fprintf(output, "%d,%g,%g,%g\n", timestamp, lat, lng, alt);
if (ferror(output)) {
fprintf(stderr, "ERROR: error writing to CSV file\n");
exit(1);
}
}
void start_kml() {
FILE* kml_header;
char c;
kml_header = fopen("kml_header", "r");
while (!feof(kml_header)) {
c = fgetc(kml_header);
if (ferror(kml_header)) {
fprintf(stderr, "ERROR: error reading KML header file\n");
exit(1);
}
if (!feof(kml_header)) fputc(c, kml_file);
if (ferror(kml_file)) {
fprintf(stderr, "ERROR: error writing header to KML file\n");
exit(1);
}
}
fclose(kml_header);
}
void finish_kml() {
FILE* kml_footer;
char c;
kml_footer = fopen("kml_footer", "r");
while (!feof(kml_footer)) {
c = fgetc(kml_footer);
if (ferror(kml_footer)) {
fprintf(stderr, "ERROR: error reading KML footer file\n");
exit(1);
}
if (!feof(kml_footer)) fputc(c, kml_file);
if (ferror(kml_file)) {
fprintf(stderr, "ERROR: error writing footer to KML file\n");
exit(1);
}
}
fclose(kml_footer);
}