-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathSatellite_Updates.cpp
More file actions
342 lines (271 loc) · 11.5 KB
/
Satellite_Updates.cpp
File metadata and controls
342 lines (271 loc) · 11.5 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
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <fstream>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>
#include <armadillo>
// Step 1: Retrieve satellite data from the API
std::string satellite_data_api_url = "API_URL_HERE";
std::string response;
// Function to handle the HTTP response
size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response)
{
size_t totalSize = size * nmemb;
response->append(static_cast<char*>(contents), totalSize);
return totalSize;
}
// Perform HTTP GET request
CURL* curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, satellite_data_api_url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
// Parse the JSON response
Json::Value satellite_data;
Json::CharReaderBuilder jsonBuilder;
std::istringstream jsonStream(response);
std::string error;
if (!Json::parseFromStream(jsonBuilder, jsonStream, &satellite_data, &error))
{
std::cerr << "Error parsing JSON: " << error << std::endl;
return 1;
}
// Step 2: Parse TLE data using skyfield
std::vector<std::pair<std::string, std::string>> tle_data;
for (const auto& satellite : satellite_data)
{
std::string line1 = satellite["tle_line1"].asString();
std::string line2 = satellite["tle_line2"].asString();
tle_data.push_back(std::make_pair(line1, line2));
}
// Step 3: Visualize satellite orbits in 3D
// Define the necessary variables and functions for plotting in C++
// You can use a suitable plotting library for C++ like Gnuplot, ROOT, etc.
// Step 4: Map satellites to countries using the satellite database API
std::string satellite_db_api_url = "SATELLITE_DB_API_URL_HERE";
// Perform HTTP GET request to the satellite database API
std::string satellite_db_response;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, satellite_db_api_url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &satellite_db_response);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
// Parse the JSON response from the satellite database API
Json::Value satellite_db;
jsonStream.str(satellite_db_response);
if (!Json::parseFromStream(jsonBuilder, jsonStream, &satellite_db, &error))
{
std::cerr << "Error parsing JSON: " << error << std::endl;
return 1;
}
// Mapping satellite names to countries
std::map<std::string, std::string> satellite_country_map;
for (const auto& satellite : satellite_data)
{
std::string name = satellite["name"].asString();
for (const auto& entry : satellite_db)
{
if (entry["name"].asString() == name)
{
std::string country = entry["country"].asString();
satellite_country_map[name] = country;
break;
}
}
}
// Printing satellite information
for (const auto& satellite : satellite_data)
{
std::string name = satellite["name"].asString();
double angle = satellite["angle"].asDouble();
std::string country = satellite_country_map[name];
std::cout << "Satellite Name: " << name << std::endl;
std::cout << "Orbital Angle: " << angle << " degrees" << std::endl;
std::cout << "Country: " << country << std::endl;
std::cout << std::endl;
}
// Step 5: Perform the necessary calculations and plotting using an appropriate C++ library
return 0;
// -----------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <sstream>
#include <curl/curl.h>
#include <jsoncpp/json/json.h>
#include <armadillo>
// Step 1: Retrieve satellite data from the API
std::string satelliteDataAPIURL = "API_URL_HERE";
CURL* curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, satelliteDataAPIURL.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
std::string responseString;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](char* ptr, size_t size, size_t nmemb, std::string* userdata) {
userdata->append(ptr, size * nmemb);
return size * nmemb;
});
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseString);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "Error retrieving satellite data: " << curl_easy_strerror(res) << std::endl;
curl_easy_cleanup(curl);
return 1;
}
curl_easy_cleanup(curl);
Json::Reader reader;
Json::Value satelliteData;
if (!reader.parse(responseString, satelliteData)) {
std::cerr << "Error parsing satellite data" << std::endl;
return 1;
}
// Step 2: Parse TLE data using skyfield
std::vector<std::pair<std::string, std::string>> tleData;
for (const auto& satellite : satelliteData) {
std::string line1 = satellite["tle_line1"].asString();
std::string line2 = satellite["tle_line2"].asString();
tleData.emplace_back(line1, line2);
}
// Load the required data files for calculations
// You need to provide the path to the data directory where 'de421.bsp' file is located
std::string dataDirectory = "path_to_data_directory";
Loader load(dataDirectory);
auto ephemeris = load("de421.bsp");
auto satellites = load.tle_file(tleData);
// Step 3: Visualize satellite orbits in 3D
// Code for visualizing satellite orbits in C++ using a plotting library is more complex and depends on the library you choose.
// Here's an example using the Gnuplot C++ API:
// Include the necessary Gnuplot headers
#include "gnuplot-iostream.h"
// Create a Gnuplot object
Gnuplot gp;
// Set the plot style and labels
gp << "set xlabel 'Longitude'\n";
gp << "set ylabel 'Latitude'\n";
gp << "set zlabel 'Altitude (km)'\n";
// Plot each satellite's trajectory
for (const auto& satellite : satellites) {
// Calculate the satellite's position over time
auto ts = load.timescale();
auto t = ts.utc(2023, 7, 11, 0, arma::linspace<arma::vec>(0, 3600, 61));
auto geocentric = satellite.at(t);
auto subpoint = geocentric.subpoint();
// Extract latitude, longitude, and altitude
arma::vec latitude = subpoint.latitude.degrees();
arma::vec longitude = subpoint.longitude.degrees();
arma::vec altitude = subpoint.elevation.km();
// Convert longitude, latitude, and altitude to vectors of strings
std::vector<std::string> latitudeStr;
std::vector<std::string> longitudeStr;
std::vector<std::string> altitudeStr;
for (size_t i = 0; i < latitude.n_elem; i++) {
latitudeStr.push_back(std::to_string(latitude(i)));
longitudeStr.push_back(std::to_string(longitude(i)));
altitudeStr.push_back(std::to_string(altitude(i)));
}
// Plot the satellite's trajectory in 3D
gp << "splot '-' with lines title 'Satellite Trajectory'\n";
gp.send1d(boost::make_tuple(longitudeStr, latitudeStr, altitudeStr));
}
// Wait for a key press to exit the plot
std::cout << "Press enter to exit." << std::endl;
std::cin.ignore();
// Step 4: Map satellites to countries using the satellite database API
std::string satelliteDBAPIURL = "SATELLITE_DB_API_URL_HERE";
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, satelliteDBAPIURL.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
responseString.clear();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](char* ptr, size_t size, size_t nmemb, std::string* userdata) {
userdata->append(ptr, size * nmemb);
return size * nmemb;
});
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseString);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "Error retrieving satellite database: " << curl_easy_strerror(res) << std::endl;
curl_easy_cleanup(curl);
return 1;
}
curl_easy_cleanup(curl);
Json::Value satelliteDB;
if (!reader.parse(responseString, satelliteDB)) {
std::cerr << "Error parsing satellite database" << std::endl;
return 1;
}
// Mapping satellite names to countries
std::map<std::string, std::string> satelliteCountryMap;
for (const auto& satellite : satelliteData) {
std::string name = satellite["name"].asString();
for (const auto& entry : satelliteDB) {
if (entry["name"] == name) {
std::string country = entry["country"].asString();
satelliteCountryMap[name] = country;
break;
}
}
}
// Printing satellite information
for (const auto& satellite : satelliteData) {
std::string name = satellite["name"].asString();
double angle = satellite["angle"].asDouble();
std::string country = satelliteCountryMap[name];
std::cout << "Satellite Name: " << name << std::endl;
std::cout << "Orbital Angle: " << angle << " degrees" << std::endl;
std::cout << "Country: " << country << std::endl;
std::cout << std::endl;
}
}
}
return 0;
//-----------------------------------------------------------------------------------------------------------------------------
// Step 3: Visualize satellite orbits in 3D
// Code for visualizing satellite orbits in C++ using a plotting library is more complex and depends on the library you choose.
// Here's an example using the Gnuplot C++ API:
// Include the necessary Gnuplot headers
#include "gnuplot-iostream.h"
// Create a Gnuplot object
Gnuplot gp;
// Set the plot style and labels
gp << "set xlabel 'Longitude'\n";
gp << "set ylabel 'Latitude'\n";
gp << "set zlabel 'Altitude (km)'\n";
// Plot each satellite's trajectory
for (const auto& satellite : satellites) {
// Calculate the satellite's position over time
auto ts = load.timescale();
auto t = ts.utc(2023, 7, 11, 0, arma::linspace<arma::vec>(0, 3600, 61));
auto geocentric = satellite.at(t);
auto subpoint = geocentric.subpoint();
// Extract latitude, longitude, and altitude
arma::vec latitude = subpoint.latitude.degrees();
arma::vec longitude = subpoint.longitude.degrees();
arma::vec altitude = subpoint.elevation.km();
// Convert longitude, latitude, and altitude to vectors of strings
std::vector<std::string> latitudeStr;
std::vector<std::string> longitudeStr;
std::vector<std::string> altitudeStr;
for (size_t i = 0; i < latitude.n_elem; i++) {
latitudeStr.push_back(std::to_string(latitude(i)));
longitudeStr.push_back(std::to_string(longitude(i)));
altitudeStr.push_back(std::to_string(altitude(i)));
}
// Plot the satellite's trajectory in 3D
gp << "splot '-' with lines title 'Satellite Trajectory'\n";
gp.send1d(boost::make_tuple(longitudeStr, latitudeStr, altitudeStr));
}
// Wait for a key press to exit the plot
std::cout << "Press enter to exit." << std::endl;
std::cin.ignore();