-
Notifications
You must be signed in to change notification settings - Fork 11
/
sgp4prop.cpp
308 lines (251 loc) · 9.55 KB
/
sgp4prop.cpp
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
//==========================================================================
/*
* Copyright 2020 Sergio De Florio
* All rigths reserved
*
* This file is part of SpOCK
*
* SpOCK 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 version 3
*
* SpOCK 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 SpOCK. If not, see <https://www.gnu.org/licenses/>.
*
*/
//==========================================================================
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <IO_utils.h>
#include <Transformations.h>
#include <Eigen/Core>
#include <Eigen/Geometry>
#ifdef _WIN32
#include <io.h>
#endif
#include <sgp4ext.h>
#include <sgp4unit.h>
#include <sgp4io.h>
using namespace std;
using namespace Eigen;
//int main()
int main(int argc, char *argv[])
{
/////////////// Simmulation parameters XML file ///////////////
if(argc < 2)
{
// Tell the user how to run the program
cerr << "Usage: " << argv[0] << " path_to/simulation/parameters/file.xml\nEx: ./bin/OrbitPropagator /home/username/path1/path2/input/simparam.xml" << endl;
return 1;
}
string XML_simparam_file(argv[1]);
// if(argc < 5)
// {
// // Tell the user how to run the program
// cerr << "Usage: " << argv[0] << " path_to_TLEs/TLE_file.tle" << endl;
// return 1;
// }
//
// double step, duration;
//
// step = atof(argv[1]); // [s]
// duration = atof(argv[2]); // [d]
//
// string TLE_file(argv[3]);
// string ephemname(argv[4]);
// Input files path
string TLE_file, Data_path, eop, pck_data, leapsecond;
// Output files path
string ephemname;
// Simulation step
int SIM_STEP;
// Simulation duration
int SIM_DURATION;
SGP4_XML_parser(XML_simparam_file, TLE_file, Data_path, eop, pck_data, leapsecond, ephemname, SIM_STEP, SIM_DURATION);
cout << "TLE file: " << TLE_file << endl;
size_t charnum_TLEname = TLE_file.size() + 10;
char* fgetsout;
char str[2];
char infilename[charnum_TLEname];
double ro[3];
double vo[3];
Vector6d UTCdate;
Vector6d ECI_state;
Vector6d ECEF_state;
string sec_str;
//string UTC_epoch;
int propsecs = 0;
int init_GPSsecs = 0;
int GPSsecs = 0;
char typerun, typeinput, opsmode;
gravconsttype whichconst;
//int whichcon;
ofstream orbstate_file;
//FILE *infile, *outfile, *outfilee;
FILE *infile;
// Load SPICE Kernels
#ifdef USE_SPICE
eop = Data_path + "/cspice/" + eop;
leapsecond = Data_path + "/cspice/" + leapsecond;
pck_data = Data_path + "/cspice/" + pck_data;
furnsh_c(eop.c_str( ));
furnsh_c(leapsecond.c_str( ));
furnsh_c(pck_data.c_str( ));
#endif
// ---------------------------- Locals -------------------------------
double sec, jd, startmfe, stopmfe, deltamin;
double tsince = 0.0;
double tumin, mu, radiusearthkm, xke, j2, j3, j4, j3oj2;
int year; int mon; int day; int hr; int min;
char longstr1[130];
typedef char str3[4];
str3 monstr[13];
char longstr2[130];
elsetrec satrec;
// ------------------------ Implementation --------------------------
strcpy(monstr[1], "Jan");
strcpy(monstr[2], "Feb");
strcpy(monstr[3], "Mar");
strcpy(monstr[4], "Apr");
strcpy(monstr[5], "May");
strcpy(monstr[6], "Jun");
strcpy(monstr[7], "Jul");
strcpy(monstr[8], "Aug");
strcpy(monstr[9], "Sep");
strcpy(monstr[10], "Oct");
strcpy(monstr[11], "Nov");
strcpy(monstr[12], "Dec");
startmfe = 0.0;
stopmfe = SIM_DURATION/60.0; // [min] 10.0*1440.0; // [min]
deltamin = SIM_STEP/60.0; // [min] 1.0/6.0; // 10 s
printf("\n%s\n",SGP4Version );
//opsmode = 'a' best understanding of how afspc code works
//opsmode = 'i' imporved sgp4 resulting in smoother behavior
//printf("input operation mode a, i \n\n");
//opsmode = getchar();
//fflush(stdin);
opsmode = 'i';
//typerun = 'c' compare 1 year of full satcat data
//typerun = 'v' verification run, requires modified elm file with
// start, stop, and delta times
//typerun = 'm' maunual operation- either mfe, epoch, or dayof yr also
//printf("input type of run c, v, m \n\n");
//typerun = getchar();
//fflush(stdin);
typerun = 'm';
//typeinput = 'm' input start stop mfe
//typeinput = 'e' input start stop ymd hms
//typeinput = 'd' input start stop yr dayofyr
//if ((typerun != 'v') && (typerun != 'c'))
// {
// printf("input mfe, epoch (YMDHMS), or dayofyr approach, m,e,d \n\n");
// typeinput = getchar();
// }
// else
// typeinput = 'e';
typeinput = 'm';
//printf("input which constants 721 72 84 \n");
//scanf( "%i",&whichcon );
//if (whichcon == 721) whichconst = wgs72old;
//if (whichcon == 72) whichconst = wgs72;
//if (whichcon == 84) whichconst = wgs84;
whichconst = wgs84;
getgravconst( whichconst, tumin, mu, radiusearthkm, xke, j2, j3, j4, j3oj2 );
// ---------------- Setup files for operation ------------------
strcpy(infilename, TLE_file.c_str());
infile = fopen(infilename, "r");
if(infile == NULL)
{
printf("Failed to open file: %s\n", infilename);
return 1;
}
while (feof(infile) == 0)
{
do
{
fgetsout = fgets( longstr1,130,infile);
if(fgetsout == NULL){};
strncpy(str, &longstr1[0], 1);
str[1] = '\0';
} while ((strcmp(str, "#")==0)&&(feof(infile) == 0));
if (feof(infile) == 0)
{
fgetsout = fgets( longstr2,130,infile);
if(fgetsout == NULL){};
// Convert the char string to sgp4 elements
// Includes initialization of sgp4
twoline2rv(longstr1, longstr2, typerun, typeinput, opsmode, whichconst, startmfe, stopmfe, deltamin, satrec);
//ephemname = "SGP4_Ephemerides/SGP4_ephemeris_ECI-ECEF_" + to_string(satrec.satnum) + ".csv";
orbstate_file.open(ephemname);
cout << "TLE NORAD ID " + to_string(satrec.satnum) + "\n";
// call the propagator to get the initial state vector value
sgp4 (whichconst, satrec, 0.0, ro, vo);
ECI_state(0) = ro[0]*1E3; // [m]
ECI_state(1) = ro[1]*1E3; // [m]
ECI_state(2) = ro[2]*1E3; // [m]
ECI_state(3) = vo[0]*1E3; // [m]
ECI_state(4) = vo[1]*1E3; // [m]
ECI_state(5) = vo[2]*1E3; // [m]
jd = satrec.jdsatepoch + tsince/1440.0;
invjday(jd,year,mon,day,hr,min,sec);
UTCdate << year, mon, day, hr, min, sec;
GPSsecs = UTCdate2GPSsecs(UTCdate);
init_GPSsecs = GPSsecs;
propsecs = GPSsecs - init_GPSsecs;
ECEF_state = ECI2ECEF(GPSsecs,ECI_state);
//stringstream sec_stream;
//sec_stream << setfill('0') << setw(2) << round(sec);
//sec_str = sec_stream.str();
//UTC_epoch = to_string(year) + "-" + to_string(mon) + "-" + to_string(day) + " " + to_string(hr) + ":" + to_string(min) + ":" + sec_str;
orbstate_file << propsecs << "," << fixed << GPSsecs << "," << ECI_state(0) << "," << ECI_state(1) << "," << ECI_state(2) << "," << ECI_state(3) << "," << ECI_state(4) << "," << ECI_state(5) << "," << ECEF_state(0) << "," << ECEF_state(1) << "," << ECEF_state(2) << "," << ECEF_state(3) << "," << ECEF_state(4) << "," << ECEF_state(5) << endl;
tsince = startmfe;
// check so the first value isn't written twice
if ( fabs(tsince) > 1.0e-8 )
tsince = tsince - deltamin;
// ----------------- Loop to perform the propagation ----------------
while ((tsince < stopmfe) && (satrec.error == 0))
{
tsince = tsince + deltamin;
if(tsince > stopmfe) tsince = stopmfe;
sgp4(whichconst, satrec, tsince, ro, vo);
if (satrec.error > 0) printf("# *** error: t:= %f *** code = %3d\n", satrec.t, satrec.error);
if (satrec.error == 0)
{
jd = satrec.jdsatepoch + tsince/1440.0;
invjday( jd, year,mon,day,hr,min, sec );
ECI_state(0) = ro[0]*1E3; // [m]
ECI_state(1) = ro[1]*1E3; // [m]
ECI_state(2) = ro[2]*1E3; // [m]
ECI_state(3) = vo[0]*1E3; // [m]
ECI_state(4) = vo[1]*1E3; // [m]
ECI_state(5) = vo[2]*1E3; // [m]
UTCdate << year, mon, day, hr, min, sec;
GPSsecs = UTCdate2GPSsecs(UTCdate);
propsecs = GPSsecs - init_GPSsecs;
ECEF_state = ECI2ECEF(GPSsecs,ECI_state);
//stringstream sec_stream;
//sec_stream << setfill('0') << setw(2) << round(sec);
//sec_str = sec_stream.str();
//UTC_epoch = to_string(year) + "-" + to_string(mon) + "-" + to_string(day) + " " + to_string(hr) + ":" + to_string(min) + ":" + sec_str;
orbstate_file << propsecs << "," << fixed << GPSsecs << "," << ECI_state(0) << "," << ECI_state(1) << "," << ECI_state(2) << "," << ECI_state(3) << "," << ECI_state(4) << "," << ECI_state(5) << "," << ECEF_state(0) << "," << ECEF_state(1) << "," << ECEF_state(2) << "," << ECEF_state(3) << "," << ECEF_state(4) << "," << ECEF_state(5) << endl;
} // if satrec.error == 0
} // while propagating the orbit
//fprintf(outfilee," END Ephemeris \n");
//fclose (outfilee);
orbstate_file.close();
} // if not eof
} // while through the input file
return 0;
} // end testcpp