-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpredict.cpp
More file actions
410 lines (342 loc) · 13.9 KB
/
predict.cpp
File metadata and controls
410 lines (342 loc) · 13.9 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* @file predict.cpp
* @author Mit Bailey (mitbailey99@gmail.com)
* @brief Displays visible pass times for a given satellite.
* @version See Git tags for version information.
* @date 2021.11.19
*
* @copyright Copyright (c) 2021
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "CoordTopocentric.hpp"
#include "DateTime.hpp"
#include "Observer.hpp"
#include "SGP4.hpp"
#include "meb_print.h"
#include "predict.h"
#define WEBSTREAM_URL "http://celestrak.com/NORAD/elements/stations.txt"
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
#include <windows.h>
#include <urlmon.h>
#define WEBSTREAM_TMP "webstream.tmp"
#endif
// TODO: Update README.md file.
// TLE Object SX [https://www.n2yo.com/satellite/?s=49278]
// TLE Object SW [https://www.n2yo.com/satellite/?s=49277]
/*
Usage:
predict.out TLE.txt output.txt
predict.out TLE.txt output.txt {Days to Predict}
predict.out NORADID output.txt
predict.out NORADID output.txt {Days to Predict}
*/
int main(int argc, char *argv[])
{
char TLE[2][TLE_LEN] = {0};
FILE *fp_tle = NULL;
FILE *fp_out = NULL;
int timezone_adjust = 0 // Assume UTC+0 by default.
, norad_id = 0; // Assume no NORAD ID by default.
char identifier[256] = {0}; // NORAD ID or TLE filename.
char fname_out[256] = {0};
int days_to_predict = 2; // Predict two days (48 hours) by default.
bool use_norad_id = true; // Assume the use of a NORAD ID by default.
bool use_out_file = false; // No out-file printing by default.
if (argc == 1)
{
// ./predict.out
// Ask the user for input.
// TODO: Make last two optional
bprintf("NORAD ID or TLE input filename: ");
scanf("%s", identifier);
bprintf("Integer, where UTC+Int is the output timezone: ");
scanf("%d", &timezone_adjust);
bprintf("Output filename: ");
scanf("%s", fname_out);
use_out_file = true;
bprintf("Days to predict: ");
scanf("%d", &days_to_predict);
}
else if (argc == 3)
{
// ./predict.out {Input File}
// ./predict.out {NORAD ID}
// TODO: Add usages printout and exit() if user uses -help.
strcpy(identifier, argv[1]);
timezone_adjust = atoi(argv[2]);
}
else if (argc == 4)
{
// ./predict.out {Input File} {Output File}
// ./predict.out {NORAD ID} {Output File}
// Default prediction time.
strcpy(identifier, argv[1]);
timezone_adjust = atoi(argv[2]);
strcpy(fname_out, argv[3]);
use_out_file = true;
}
else if (argc == 5)
{
// ./predict.out {Input File} {Output File} {Days to Predict}
// ./predict.out {NORAD ID} {Output File} {Days to Predict}
// Given prediction time.
strcpy(identifier, argv[1]);
timezone_adjust = atoi(argv[2]);
strcpy(fname_out, argv[3]);
use_out_file = true;
days_to_predict = atoi(argv[4]);
}
else
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
dbprintlf(FATAL "Invalid usage.");
dbprintlf("Usages (REQuired, OPTional):");
dbprintlf(RED_FG "./predict.out");
dbprintlf(RED_FG "./predict.out {REQ: Five-Digit NORAD ID or Input File} {REQ: Int, where UTC+Int is the output timezone} {OPT: Output File} {OPT: Days to Predict}");
#else // defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
dbprintlf(FATAL "Invalid usage.");
dbprintlf("Usages (" RED_FG "required" TERMINATOR ", " BLUE_FG "optional" TERMINATOR "):");
dbprintlf(RED_FG "./predict.out");
dbprintlf(RED_FG "./predict.out {Five-Digit NORAD ID or Input File} " TERMINATOR BLUE_FG "{Output File} {Days to Predict}");
#endif // defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
return -1;
}
for (int i = 0; i < 5; i++)
{
if (!isdigit(identifier[i]))
{
// Non-digit found, cannot be NORAD ID.
use_norad_id = false;
break;
}
}
bprintlf();
if (use_norad_id)
{
dbprintlf("NORAD ID detected, using %s.", identifier);
}
else
{
dbprintlf("Filename detected, using %s.", identifier);
}
// If filename is given, continue on as before.
// If NORAD ID is given, fetch the TLE from the internet.
if (use_norad_id)
{
// Update from internet.
FILE *pp = NULL;
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
// #include <windows.h>
// #include <urlmon.h>
// #pragma comment(lib, "urlmon.lib")
LPCTSTR wurl = (LPCTSTR)WEBSTREAM_URL;
LPCTSTR wdest = (LPCTSTR)WEBSTREAM_TMP;
char cdest[] = WEBSTREAM_TMP;
if (S_OK == URLDownloadToFile(NULL, wurl, wdest, 0, NULL))
{
dbprintlf("Windows reports file successfully downloaded.");
}
else
{
dbprintlf("Fail");
}
pp = fopen(cdest, "r");
#else // defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
dbprintlf("Non-Windows OS detected, updating TLE from internet.");
char cmd[512] = {0};
snprintf(cmd, sizeof(cmd), "wget -q -O- %s", WEBSTREAM_URL);
pp = popen(cmd, "r");
#endif // defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
if (pp == NULL)
{
dbprintlf(RED_FG "Could not open pipe to obtain online TLE data.");
return -2;
}
char search_str1[512] = {0};
snprintf(search_str1, sizeof(search_str1), "1 %s", identifier);
char search_str2[512] = {0};
snprintf(search_str2, sizeof(search_str2), "2 %s", identifier);
char buf[512] = {0};
bool l1_updated = false;
bool l2_updated = false;
while (fgets(buf, sizeof(buf), pp))
{
if (strstr(buf, search_str1) != NULL)
{
strcpy(TLE[0], buf);
l1_updated = true;
}
if (strstr(buf, search_str2) != NULL)
{
strcpy(TLE[1], buf);
l2_updated = true;
}
if (l1_updated && l2_updated)
break;
}
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
fclose(pp);
#else
pclose(pp);
#endif
if (l1_updated && l2_updated)
{
tprintlf(GREEN_FG "Obtained updated TLE for %s.", identifier);
TLE[0][69] = '\0';
TLE[1][69] = '\0';
tprintlf("%s", TLE[0]);
tprintlf("%s", TLE[1]);
}
else
{
tprintlf(RED_FG "Failed to update TLE for %s.", identifier);
return -3;
}
}
else
{
// Read from file.
fp_tle = fopen(identifier, "r");
if (fp_tle == NULL)
{
dbprintlf(FATAL "Could not open \"%s\"!", identifier);
return -4;
}
fgets(TLE[0], TLE_LEN, fp_tle);
fgetc(fp_tle);
fgets(TLE[1], TLE_LEN, fp_tle);
fclose(fp_tle);
}
// Open the output file.
if (use_out_file)
{
fp_out = fopen(fname_out, "w");
if (fp_out == NULL)
{
dbprintlf(FATAL "Failed to open %s for write.", fname_out);
return -5;
}
}
bprintlf();
bprintlf("PASS PREDICTIONS FOR:");
bprintlf("%s", TLE[0]);
bprintlf("%s", TLE[1]);
SGP4 *satellite = new SGP4(Tle(TLE[0], TLE[1]));
Observer *dish = new Observer(GS_LAT, GS_LON, GS_ELEV);
DateTime tnow = DateTime::Now(true);
Eci pos_now = satellite->FindPosition(tnow);
CoordTopocentric current_pos = dish->GetLookAngle(pos_now);
CoordGeodetic current_lla = pos_now.ToGeodetic();
bprintlf(YELLOW_FG "DATA FOR %d DAYS", days_to_predict);
bprintlf("Current Position: %.2f AZ, %.2f EL | %.2f LA, %.2f LN", current_pos.azimuth DEG, current_pos.elevation DEG, current_lla.latitude DEG, current_lla.longitude DEG);
DateTime tnext = tnow;
bool in_pass = false;
double max_el = 0.0;
double max_el_az = 0.0;
DateTime max_el_time;
int begin_et = 0;
TimeSpan AdjustHours(timezone_adjust, 0, 0);
DateTime tADJ = tnow + AdjustHours;
// TimeSpan FiveHours(5, 0, 0);
// DateTime tEST = tnow - FiveHours;
if (use_out_file)
{
// fprintf(fp_out, "Report Generated %04d.%02d.%02d %02d:%02d:%02d EST\nData for %d days.\n\n", tEST.Year(), tEST.Month(), tEST.Day(), tEST.Hour(), tEST.Minute(), tEST.Second(), days_to_predict);
fprintf(fp_out, "Report Generated %04d.%02d.%02d %02d:%02d:%02d UTC%+d\nData for %d days.\n\n", tADJ.Year(), tADJ.Month(), tADJ.Day(), tADJ.Hour(), tADJ.Minute(), tADJ.Second(), timezone_adjust, days_to_predict);
fprintf(fp_out, "%s\n%s\n", TLE[0], TLE[1]);
fprintf(fp_out, "======================================================================\n\n");
}
bprintlf("It is currently %04d.%02d.%02d %02d:%02d:%02d UTC\n", tnow.Year(), tnow.Month(), tnow.Day(), tnow.Hour(), tnow.Minute(), tnow.Second());
bprintlf("It is currently %04d.%02d.%02d %02d:%02d:%02d UTC%+d\n", tADJ.Year(), tADJ.Month(), tADJ.Day(), tADJ.Hour(), tADJ.Minute(), tADJ.Second(), timezone_adjust);
for (int i = 0; i < 86400 * days_to_predict; i++)
{
tnext = tnext.AddSeconds(1);
Eci eci_ahd = satellite->FindPosition(tnext);
CoordTopocentric pos_ahd = dish->GetLookAngle(eci_ahd);
double ahd_el = pos_ahd.elevation DEG;
if (ahd_el > 10.0)
{
if (!in_pass)
{
bprintlf("== SATELLITE PASS (Now + %d minutes) ==", i / 60);
bprintlf("Time (UTC%+d) Az (deg) El (deg)", timezone_adjust);
bprintlf("------------------------------------------");
if (use_out_file)
{
fprintf(fp_out, "== SATELLITE PASS (Now + %d minutes) ==\n", i / 60);
fprintf(fp_out, "Time (UTC%+d) Az (deg) El (deg)\n", timezone_adjust);
fprintf(fp_out, "------------------------------------------\n");
}
in_pass = true;
begin_et = i;
}
if (ahd_el > max_el)
{
max_el = ahd_el;
max_el_az = pos_ahd.azimuth DEG;
max_el_time = tnext;
}
// Print data for every 60 seconds after the pass begins, and once immediately when the pass begins..
if ((i - begin_et) % 60 == 0)
{
// tEST = tnext - FiveHours;
tADJ = tnext - AdjustHours;
bprintlf("%04d.%02d.%02d %02d:%02d:%02d %6.02lf %6.02lf", tADJ.Year(), tADJ.Month(), tADJ.Day(), tADJ.Hour(), tADJ.Minute(), tADJ.Second(), pos_ahd.azimuth DEG, ahd_el);
if (use_out_file)
{
fprintf(fp_out, "%04d.%02d.%02d %02d:%02d:%02d %6.02lf %6.02lf\n", tADJ.Year(), tADJ.Month(), tADJ.Day(), tADJ.Hour(), tADJ.Minute(), tADJ.Second(), pos_ahd.azimuth DEG, ahd_el);
}
}
}
else if (in_pass)
{
// Make sure to print final state when pass ends.
// tEST = tnext - FiveHours;
tADJ = tnext - AdjustHours;
bprintlf("%04d.%02d.%02d %02d:%02d:%02d %6.02lf %6.02lf\n", tADJ.Year(), tADJ.Month(), tADJ.Day(), tADJ.Hour(), tADJ.Minute(), tADJ.Second(), pos_ahd.azimuth DEG, ahd_el);
if (use_out_file)
{
fprintf(fp_out, "%04d.%02d.%02d %02d:%02d:%02d %6.02lf %6.02lf\n", tADJ.Year(), tADJ.Month(), tADJ.Day(), tADJ.Hour(), tADJ.Minute(), tADJ.Second(), pos_ahd.azimuth DEG, ahd_el);
}
bprintlf("Pass Statistics");
bprintlf(" Maximum Elevation:");
// tEST = max_el_time - FiveHours;
tADJ = max_el_time - AdjustHours;
bprintlf("%04d.%02d.%02d %02d:%02d:%02d %6.02lf %6.02lf", tADJ.Year(), tADJ.Month(), tADJ.Day(), tADJ.Hour(), tADJ.Minute(), tADJ.Second(), max_el_az, max_el);
printf("\n\n");
if (use_out_file)
{
fprintf(fp_out, "\nPass Statistics\n Maximum Elevation:\n%04d.%02d.%02d %02d:%02d:%02d %6.02lf %6.02lf\n\n\n", tADJ.Year(), tADJ.Month(), tADJ.Day(), tADJ.Hour(), tADJ.Minute(), tADJ.Second(), max_el_az, max_el);
}
in_pass = false;
max_el = 0;
}
}
if (use_out_file)
{
fclose(fp_out);
}
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
remove(WEBSTREAM_TMP);
#endif // defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
return 0;
}