-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathSatellite_Updates_Projects.cpp
135 lines (115 loc) · 4.37 KB
/
Satellite_Updates_Projects.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
#include "skyfield/almanac.h"
#include "skyfield/api.h"
#include "skyfield/position.h"
using json = nlohmann::json;
using namespace std;
using namespace skyfield;
using namespace skyfield::almanac;
using namespace skyfield::api;
using namespace skyfield::positions;
struct SatelliteData {
string name;
string tle_line1;
string tle_line2;
double angle;
};
struct SatelliteDbEntry {
string name;
string country;
};
size_t WriteCallback(void* contents, size_t size, size_t nmemb, string* response) {
response->append((char*)contents, size * nmemb);
return size * nmemb;
}
string SendGetRequest(const string& url) {
string response;
CURL* curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
cout << "Error in SendGetRequest: " << curl_easy_strerror(res) << endl;
}
curl_easy_cleanup(curl);
}
return response;
}
int main() {
try {
// Step 1: Retrieve satellite data from the API
string satelliteDataApiUrl = "API_URL_HERE";
string satelliteDataJson = SendGetRequest(satelliteDataApiUrl);
vector<SatelliteData> satelliteData = json::parse(satelliteDataJson);
// Step 2: Parse TLE data using Skyfield
vector<string[2]> tleData;
for (const auto& satellite : satelliteData) {
string line1 = satellite.tle_line1;
string line2 = satellite.tle_line2;
tleData.push_back({ line1, line2 });
}
// Step 3: Visualize satellite orbits in 3D
Loader loader("path_to_data_directory");
auto ephemeris = loader.load("de421.bsp");
auto satellites = loader.parse_tle_file(tleData);
auto fig = plt::figure();
auto ax = fig.add_subplot(111, projection='3d');
for (const auto& satellite : satellites) {
// Calculate the satellite's position over time
auto ts = loader.make_timescale();
auto t = ts.utc(2023, 7, 11, 0, vector<int>(3600, 0));
auto geocentric = satellite.at(t);
auto subpoint = geocentric.subpoint();
// Extract latitude, longitude, and altitude
auto latitude = subpoint.latitude().degrees();
auto longitude = subpoint.longitude().degrees();
auto altitude = subpoint.elevation().km();
// Plot the satellite's trajectory in 3D
ax.plot(longitude, latitude, altitude);
}
ax.set_xlabel("Longitude");
ax.set_ylabel("Latitude");
ax.set_zlabel("Altitude (km)");
// Step 4: Map satellites to countries using the satellite database API
string satelliteDbApiUrl = "SATELLITE_DB_API_URL_HERE";
string satelliteDbJson = SendGetRequest(satelliteDbApiUrl);
vector<SatelliteDbEntry> satelliteDb = json::parse(satelliteDbJson);
// Mapping satellite names to countries
map<string, string> satelliteCountryMap;
for (const auto& satellite : satelliteData) {
string name = satellite.name;
for (const auto& entry : satelliteDb) {
if (entry.name == name) {
string country = entry.country;
satelliteCountryMap[name] = country;
break;
}
}
}
// Printing satellite information
for (const auto& satellite : satelliteData) {
string name = satellite.name;
double angle = satellite.angle;
string country = satelliteCountryMap[name];
cout << "Satellite Name: " << name << endl;
cout << "Orbital Angle: " << angle << " degrees" << endl;
cout << "Country: " << country << endl;
cout << endl;
}
// Show the 3D plot
plt::show();
}
catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
return 0;
}