RasterProcessTool/SPG4Tool/SPG4Function.cpp

179 lines
4.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "SPG4Function.h"
#include "SPG4Tool.h"
#include <Tle.h>
#include <SGP4.h>
#include <Observer.h>
#include <CoordGeodetic.h>
#include <CoordTopocentric.h>
#include <list>
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <QDate>
#include <QDateTime>
std::vector<SatelliteAntPos> RunTle(libsgp4::Tle tle, double start, double end, double inc, bool printfinfoflag, bool running, bool first_run)
{
std::cout << "RunTle\t" ;
std::cout << "Start time:\t" << start;
std::cout << "End time:\t" << end;
std::cout << "inc time:\t" << inc << std::endl;
std::vector<SatelliteAntPos> resultpos(0);
double current = start;
libsgp4::SGP4 model(tle);
//bool running = true;
//bool first_run = true;
std::cout << std::setprecision(0) << tle.NoradNumber() << " xx"
<< std::endl;
while (running)
{
bool error = false;
libsgp4::Vector position;
libsgp4::Vector velocity;
double tsince;
try
{
if (first_run && current != 0.0)
{
/*
* make sure first run is always as zero
*/
tsince = 0.0;
}
else
{
/*
* otherwise run as normal
*/
tsince = current;
}
libsgp4::Eci eci = model.FindPosition(tsince);
position = eci.Position();
velocity = eci.Velocity();
}
catch (libsgp4::SatelliteException& e)
{
std::cerr << "SatelliteException:\t" << e.what() << std::endl;
error = true;
running = false;
}
catch (libsgp4::DecayedException& e)
{
std::cerr <<"DecayedException:\t" << e.what() << std::endl;
position = e.Position();
velocity = e.Velocity();
if (!first_run)
{
// print out position on first run
error = true;
}
running = false;
}
if (!error)
{
SatelliteAntPos antpos{};
antpos.time = tsince;
antpos.Px = position.x;
antpos.Py = position.y;
antpos.Pz = position.z;
antpos.Vx = velocity.x;
antpos.Vy = velocity.y;
antpos.Vz = velocity.z;
resultpos.push_back(antpos);
if (printfinfoflag) {
std::cout << std::setprecision(8) << std::fixed;
std::cout.width(17);
std::cout << tsince << " ";
std::cout.width(16);
std::cout << position.x << " ";
std::cout.width(16);
std::cout << position.y << " ";
std::cout.width(16);
std::cout << position.z << " ";
std::cout << std::setprecision(9) << std::fixed;
std::cout.width(14);
std::cout << velocity.x << " ";
std::cout.width(14);
std::cout << velocity.y << " ";
std::cout.width(14);
std::cout << velocity.z << std::endl;
}
}
if ((first_run && current == 0.0) || !first_run)
{
if (current == end)
{
running = false;
}
else if ((current + inc) > end)
{
current = end;
}
else
{
current += inc;
}
}
first_run = false;
}
return resultpos;
}
std::vector<SatelliteAntPos> getGPSPoints(std::string line1, std::string line2, double start, double end, double inc, bool printfinfoflag, bool running , bool first_run )
{
libsgp4::Tle tle("satellites", line1, line2);
std::cout << "Start time:\t" << start;
std::cout << "End time:\t" << end;
std::cout << "inc time:\t" << inc << std::endl;
std::vector<SatelliteAntPos> result= RunTle(tle, start, end, inc, printfinfoflag,running,first_run);
return result;
}
double parseTLETimeOffset(const std::string& tle) {
// <20><><EFBFBD><EFBFBD>TLE<4C>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD>ض<EFBFBD>λ<EFBFBD><CEBB>
// <20><><EFBFBD>磬TLE<4C>ĵ<EFBFBD>19<31><39>32<33><32><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ʾ<EFBFBD><CABE>Ԫʱ<D4AA><CAB1>
if (tle.length() < 32) {
return 0.0;
}
std::string epochStr = tle.substr(18, 14);
int year = std::stoi(epochStr.substr(0, 2));
double dayOfYear = std::stod(epochStr.substr(2));
// <20><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD><CEAA>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (year < 57) {
year += 2000;
}
else {
year += 1900;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>Ӽ<EFBFBD>Ԫ<EFBFBD><D4AA>ʼ<EFBFBD><CABC>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ڵĺ<DAB5><C4BA><EFBFBD><EFBFBD><EFBFBD>
QDate date(year, 1, 1);
QDateTime dateTime(date.addDays(static_cast<int>(dayOfYear) - 1), QTime(0, 0), Qt::UTC);
qint64 millisecondsSinceEpoch = dateTime.toMSecsSinceEpoch() + static_cast<qint64>((dayOfYear - static_cast<int>(dayOfYear)) * 86400000);
return millisecondsSinceEpoch / 1000.0;
}