-
Notifications
You must be signed in to change notification settings - Fork 11
/
eventscomp.cpp
393 lines (317 loc) · 16.1 KB
/
eventscomp.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
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
//==========================================================================
/*
* 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 <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <math.h>
#include <chrono>
#include <thread>
#include <IO_utils.h>
#include <VarTypes.h>
#include <Constants.h>
#include <Transformations.h>
#include <Solarsys.h>
#include <Events.h>
// External libraries: Eigen
#include <Eigen/Core>
#ifdef USE_SPICE
extern "C"
{
#include <SpiceUsr.h>
}
#endif
using namespace std;
using namespace SC;
using namespace solarsystem;
using namespace constants;
using namespace mathconst;
using namespace math;
using namespace events;
using namespace Eigen;
////////////////////////// Initialization ///////////////////////////
int main(int argc, char *argv[])
{
chrono::time_point<chrono::high_resolution_clock> clockstart, clockend;
clockstart = chrono::high_resolution_clock::now();
/////////////// 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/EventsComputation /home/username/path1/path2/input/simparam.xml" << endl;
return 1;
}
string XML_events_file(argv[1]);
// Step of input ephemerides
int simstep;
// Time span considered for the events computation
int duration;
// Field of view half angle perpendicolar to the direction of motion
double FOV_cross;
// Field of view half angle along the direction of motion
double FOV_along;
// Spacecraft number to start with
int SC_start;
// Spacecraft number to end with
int SC_end;
// Orbital plane number to start with
int PL_start;
// Orbital plane number to end with
int PL_end;
// Compute contacts of payload with targets
bool TGs_on;
// Compute contacts of spacecraft with ground stations
bool GSs_on;
// Use targets grid for computation of contacts of payload with targets
bool TGs_grid_on;
// Compute eclipse times
bool Eclipse_on;
// Minimum and maximum longitute and latitude of targets grid
Vec4d TG_grid_limits;
// Step of the targets grid
double gridstep;
// Targets list
ground::TG TGs_list[1000];
// Ground stations list
ground::GS GSs_list[1000];
// Input files path
string ephem_file, Orbit_ephemeris_path, Orbit_ephemeris_rootname, Data_path, planetephemeris, eop, pck_data, leapsecond;
// Output files path
string TG_filename, GS_filename, Time_sorted_GS_filename, Eclipse_filename;
// Vector of GS contacts structs
vector<Pass> GS_passes;
// Vector of TG contacts structs
vector<Pass> TG_passes;
XML_parser_events(XML_events_file, simstep, duration, FOV_cross, FOV_along, SC_start, SC_end, PL_start, PL_end, TGs_on, GSs_on, TGs_grid_on, Eclipse_on, TG_grid_limits, gridstep, TGs_list, GSs_list, Orbit_ephemeris_path, Orbit_ephemeris_rootname, Data_path, planetephemeris, eop, pck_data, leapsecond, TG_filename, GS_filename, Eclipse_filename);
//const string ReadXML_TXT_file = "../input/readXMLevents.txt";
size_t last_slash = XML_events_file.find_last_of("/");
string ReadXML_TXT_file_name = XML_events_file.substr(last_slash+1);
size_t lastspoint = ReadXML_TXT_file_name.find_last_of(".");
ReadXML_TXT_file_name = ReadXML_TXT_file_name.substr(0,lastspoint);
const string ReadXML_TXT_file = XML_events_file.substr(0,last_slash) + "/Read_" + ReadXML_TXT_file_name + ".txt";
// Put read XML in a text file (for check purposes)
ReadXMLeventstoTXT(ReadXML_TXT_file, simstep, duration, FOV_cross, FOV_along, SC_start, SC_end, PL_start, PL_end, TGs_on, GSs_on, TGs_grid_on, Eclipse_on, TG_grid_limits, gridstep, TGs_list, GSs_list, Orbit_ephemeris_path, Orbit_ephemeris_rootname, Data_path, planetephemeris, eop, pck_data, leapsecond, TG_filename, GS_filename, Eclipse_filename);
string TG_output_path, GS_output_path, Umbras_output_path;
last_slash = TG_filename.rfind("/");
TG_output_path = TG_filename;
TG_output_path.erase(last_slash, TG_filename.npos);
last_slash = GS_filename.rfind("/");
GS_output_path = GS_filename;
GS_output_path.erase(last_slash, GS_filename.npos);
last_slash = Eclipse_filename.rfind("/");
Umbras_output_path = Eclipse_filename;
Umbras_output_path.erase(last_slash, Eclipse_filename.npos);
double grid_minlon, grid_maxlon, grid_minlat, grid_maxlat;
grid_minlon = TG_grid_limits(0);
grid_maxlon = TG_grid_limits(1);
grid_minlat = TG_grid_limits(2);
grid_maxlat = TG_grid_limits(3);
// SPICE Kernels
#ifdef USE_SPICE
planetephemeris = Data_path + "/cspice/" + planetephemeris;
eop = Data_path + "/cspice/" + eop;
leapsecond = Data_path + "/cspice/" + leapsecond;
pck_data = Data_path + "/cspice/" + pck_data;
furnsh_c(planetephemeris.c_str( ));
furnsh_c(eop.c_str( ));
furnsh_c(leapsecond.c_str( ));
furnsh_c(pck_data.c_str( ));
#endif
/////////////////////////////////////////////////////////////////////////
////////////////////////// ANALYSIS PARAMETERS //////////////////////////
/////////////////////////////////////////////////////////////////////////
///////////////////
// Targets grid //
//////////////////
VectorXd TGs_grid_lons, TGs_grid_lats;
if(TGs_grid_on)
{
if(grid_maxlon < grid_minlon)
{
// Tell the user how to run the program
std::cerr << "Maximum targets grid longitude has to be larger than minimum grid longitude" << std::endl;
return 1;
}
if(grid_maxlat < grid_minlat)
{
// Tell the user how to run the program
std::cerr << "Maximum targets grid latitude has to be larger than minimum grid latitude" << std::endl;
return 1;
}
if( (fabs(grid_maxlon-grid_minlon)/gridstep < 1) && (grid_minlon != grid_maxlon) )
{
// Tell the user how to run the program
std::cerr << "The grid step has to be smaller than the difference between the maximum and minimum longitudes of the targets grid" << std::endl;
return 1;
}
if( (fabs(grid_maxlat-grid_minlat)/gridstep < 1) && (grid_minlat != grid_maxlat) )
{
// Tell the user how to run the program
std::cerr << "The grid step has to be smaller than the difference between the maximum and minimum latitudes of the targets grid" << std::endl;
return 1;
}
int grid_rows = fabs(grid_maxlat-grid_minlat)/gridstep + 1;
int grid_cols = fabs(grid_maxlon-grid_minlon)/gridstep + 1;
TGs_grid_lons.resize(grid_cols);
TGs_grid_lats.resize(grid_rows);
double lon = grid_minlon;
double lat = grid_minlat;
for(int i = 0 ; i < grid_rows; i++)
{
TGs_grid_lats(i) = lat;
lat = lat + gridstep;
}
for(int i = 0 ; i < grid_cols; i++)
{
TGs_grid_lons(i) = lon;
lon = lon + gridstep;
}
}
////////////////////////////////////////////////////////////////
/////////////// FILE WITH ALL CONTACTS INFORMATION /////////////
////////////////////////////////////////////////////////////////
ofstream AllContacts_file;
if(TGs_on || TGs_grid_on)
{
AllContacts_file.open(TG_filename);
AllContacts_file << "TG,Epoch UTC in,Epoch UTC out,GPS time in [s],Duration [min],El. in [deg],El. out [deg],Epoch UTC Max. El.,Max. El. [deg],Az. in [deg],Az. out [deg],Az. Max El. [deg],lon [deg],lat [deg],P#,S#" << endl;
}
ofstream GS_AllContacts_file;
if(GSs_on)
{
GS_AllContacts_file.open(GS_filename);
GS_AllContacts_file << "GS,AOS UTC,LOS UTC,AOS [GPS secs],Duration [min],Epoch UTC Max. El.,Max Elevation [deg],Az. AOS [deg],Az. LOS [deg],Az. Max El. [deg],lon [deg],lat [deg],P#,S#" << endl;
}
string TG_GS_filename;
ofstream TG_GS_AllContacts_file;
if( GSs_on && (TGs_on || TGs_grid_on) )
{
TG_GS_filename = TG_output_path + "/All_TG_GS_Contacts.csv";
TG_GS_AllContacts_file.open(TG_GS_filename);
TG_GS_AllContacts_file << "Pass type,Location,Epoch UTC in,Epoch UTC out,GPS time [s] in,Duration [min],El. in [deg],El. out [deg],Epoch UTC Max. El.,Max. El. [deg],Az. in [deg],Az. out [deg],Az. Max El. [deg],lon [deg],lat [deg],P#,S#" << endl;
}
SOLSYS Suncompute;
ofstream AllUmbras_file;
if(Eclipse_on)
{
AllUmbras_file.open(Eclipse_filename);
AllUmbras_file << "Penumbra start UTC,Umbra start UTC,Umbra end UTC,Penumbra end UTC,Umbra duration [min],Penumbra duration [min],P#,S#" << endl;
}
if(TGs_on) cout << "Compute TG contacts (list)" << endl;
if(TGs_grid_on) cout << "Compute TG contacts (grid)" << endl;
if(GSs_on) cout << "Compute GS contacts" << endl;
if(Eclipse_on) cout << "Compute umbra/penumbra times" << endl;
//////////////////////////////////////////////////////////////////////
////////////////////////// COMPUTE CONTACTS //////////////////////////
//////////////////////////////////////////////////////////////////////
MatrixXd loaded_ephem;
VectorNd<15> ephem_row = VectorNd<15>::Zero();
Vector6d ECIstate = Vector6d::Zero();
Vector6d orbel = Vector6d::Zero();
Vector6d mean_orbel = Vector6d::Zero();
double GPStime;
double ephem_duration;
int matrows = 0;
//#pragma omp parallel for
for(int p_ind = PL_start ; p_ind <= PL_end; p_ind++)
{
//#pragma omp for
for(int s_ind = SC_start ; s_ind <= SC_end; s_ind++)
{
// Load spacecraft ephemeris file
bool valid;
Vec4d a_ex_ey_i;
ephem_file = Orbit_ephemeris_path + "/" + "S" + to_string(s_ind) + "-P" + to_string(p_ind) + "_" + Orbit_ephemeris_rootname;
if(PL_start == 1 && PL_end == 1 && SC_start == 1 && SC_end == 1) ephem_file = Orbit_ephemeris_path + "/" + Orbit_ephemeris_rootname;
//cout << ephem_file << endl;
loaded_ephem = read_csvfile(ephem_file.c_str(),15);
matrows = loaded_ephem.rows();
//cout << matrows << endl;
int indECI = round(matrows/2); // A state placed in the middle of ephemeris file
ephem_row = loaded_ephem.row(indECI);
ECIstate = ephem_row.segment(2,6);
orbel = rv2oe(ECIstate, valid); // Osculating elements
mean_orbel = osc2mean(orbel, valid);
a_ex_ey_i = mean_orbel.segment(0,4);
//Matrix<double,Dynamic,7> orbstateECEF;
MatrixXd orbstateECEF = loaded_ephem.block(0,7,matrows,7); // Column 0 is ECI v_Z to be replaced by GPS time
MatrixXd time_posECI = loaded_ephem.block(0,1,matrows,4); // Column 0 is GPS secs
ephem_duration = loaded_ephem(matrows-1,1) - loaded_ephem(0,1);
if(duration > ephem_duration)
{
cerr << "\nThe duration of the computation (<simduration>) cannot be longer than the duration of the input orbit ephemeris" << endl;
exit(EXIT_FAILURE);
}
for(int i = 0 ; i < matrows; i++)
{
ephem_row = loaded_ephem.row(i);
GPStime = ephem_row(1);
orbstateECEF(i,0) = GPStime;
}
if(TGs_on || TGs_grid_on) TGsContacts(s_ind, p_ind, a_ex_ey_i, orbstateECEF, TGs_grid_lons, TGs_grid_lats, TGs_list, TG_output_path, FOV_cross, FOV_along, duration, simstep, TGs_on, TGs_grid_on, TG_passes);
if(GSs_on) GSsContacts(s_ind, p_ind, a_ex_ey_i, orbstateECEF, GSs_list, GS_output_path, duration, simstep, GS_passes);
if(Eclipse_on) Umbras(s_ind, p_ind, Suncompute, time_posECI, Umbras_output_path, duration, simstep, AllUmbras_file);
}
}
//////////////////////////////////////////////////////////////////////
/////////////// WRITE FILE WITH ALL CONTACTS INFORMATION /////////////
//////////////////////////////////////////////////////////////////////
//if(TGs_on || TGs_grid_on) AllContacts_file.close();
//if(GSs_on) GS_AllContacts_file.close();
if(TGs_on || TGs_grid_on)
{
sort(TG_passes.begin(), TG_passes.end());
for(unsigned int i = 0 ; i < TG_passes.size(); i++)
{
AllContacts_file << TG_passes[i].Location_name << "," << TG_passes[i].Epoch_in << "," << TG_passes[i].Epoch_out << "," << fixed << TG_passes[i].GPSsecs_in << "," << TG_passes[i].duration << "," << TG_passes[i].elev_in << "," << TG_passes[i].elev_out << "," << TG_passes[i].maxel_time << "," << TG_passes[i].maxel << "," << TG_passes[i].Az_in << "," << TG_passes[i].Az_out << "," << TG_passes[i].Az_maxel << "," << TG_passes[i].lon << "," << TG_passes[i].lat << "," << TG_passes[i].PP << "," << TG_passes[i].SS << endl;
}
AllContacts_file.close();
}
if(Eclipse_on) AllUmbras_file.close();
if(GSs_on)
{
sort(GS_passes.begin(), GS_passes.end());
for(unsigned int i = 0 ; i < GS_passes.size(); i++)
{
GS_AllContacts_file << GS_passes[i].Location_name << "," << GS_passes[i].Epoch_in << "," << GS_passes[i].Epoch_out << "," << fixed << GS_passes[i].GPSsecs_in << "," << GS_passes[i].duration << "," << GS_passes[i].maxel_time << "," << GS_passes[i].maxel << "," << GS_passes[i].Az_in << "," << GS_passes[i].Az_out << "," << GS_passes[i].Az_maxel << "," << GS_passes[i].lon << "," << GS_passes[i].lat << "," << GS_passes[i].PP << "," << GS_passes[i].SS << endl;
GS_passes[i].pass_type = "GS";
}
GS_AllContacts_file.close();
}
if( GSs_on && (TGs_on || TGs_grid_on) )
{
TG_passes.insert( TG_passes.end(), GS_passes.begin(), GS_passes.end() );
sort(TG_passes.begin(), TG_passes.end());
for(unsigned int i = 0 ; i < TG_passes.size(); i++)
{
TG_GS_AllContacts_file << TG_passes[i].pass_type << "," << TG_passes[i].Location_name << "," << TG_passes[i].Epoch_in << "," << TG_passes[i].Epoch_out << "," << fixed << TG_passes[i].GPSsecs_in << "," << TG_passes[i].duration << "," << TG_passes[i].elev_in << "," << TG_passes[i].elev_out << "," << TG_passes[i].maxel_time << "," << TG_passes[i].maxel << "," << TG_passes[i].Az_in << "," << TG_passes[i].Az_out << "," << TG_passes[i].Az_maxel << "," << TG_passes[i].lon << "," << TG_passes[i].lat << "," << TG_passes[i].PP << "," << TG_passes[i].SS << endl;
}
TG_GS_AllContacts_file.close();
}
clockend = chrono::high_resolution_clock::now();
chrono::duration<double,milli> elapsed_millisecs = clockend - clockstart;
cout << "Elapsed seconds: " << elapsed_millisecs.count()/1000.0 << endl;
return(0);
}