sgp4/RunTest.cpp

239 lines
5.9 KiB
C++
Raw Normal View History

2011-04-05 23:56:17 +00:00
#include "Julian.h"
#include "Tle.h"
2011-04-23 11:18:51 +00:00
#include "SGP4.h"
2011-04-05 23:56:17 +00:00
#include "Globals.h"
#include "Util.h"
2011-04-05 23:56:17 +00:00
#include "Observer.h"
2011-04-23 11:18:51 +00:00
#include "CoordGeodetic.h"
#include "CoordTopographic.h"
2011-04-05 23:56:17 +00:00
#include <list>
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <vector>
2011-04-23 12:13:06 +00:00
#include <cstdlib>
2011-04-05 23:56:17 +00:00
2011-12-18 13:56:20 +00:00
void RunTle(Tle tle, double start, double end, double inc)
{
2011-04-05 23:56:17 +00:00
double current = start;
SGP4 model(tle);
2011-04-05 23:56:17 +00:00
bool running = true;
bool first_run = true;
2011-04-23 11:18:51 +00:00
std::cout << " " << std::setprecision(0) << tle.NoradNumber() << " xx" << std::endl;
2011-12-18 13:56:20 +00:00
while (running)
{
try
{
2011-04-05 23:56:17 +00:00
double val;
2011-12-18 13:56:20 +00:00
if (first_run && current != 0.0)
{
2011-04-05 23:56:17 +00:00
/*
* make sure first run is always as zero
*/
val = 0.0;
2011-12-18 13:56:20 +00:00
}
else
{
2011-04-05 23:56:17 +00:00
/*
* otherwise run as normal
*/
val = current;
}
2011-12-13 22:37:13 +00:00
Eci eci = model.FindPosition(val);
2011-04-05 23:56:17 +00:00
Vector position = eci.GetPosition();
Vector velocity = eci.GetVelocity();
std::cout << std::setprecision(8) << std::fixed;
std::cout.width(17);
std::cout << val << " ";
std::cout.width(16);
2011-05-26 23:22:38 +00:00
std::cout << position.x << " ";
2011-04-05 23:56:17 +00:00
std::cout.width(16);
2011-05-26 23:22:38 +00:00
std::cout << position.y << " ";
2011-04-05 23:56:17 +00:00
std::cout.width(16);
2011-05-26 23:22:38 +00:00
std::cout << position.z << " ";
2011-04-05 23:56:17 +00:00
std::cout << std::setprecision(9) << std::fixed;
std::cout.width(14);
2011-05-26 23:22:38 +00:00
std::cout << velocity.x << " ";
2011-04-05 23:56:17 +00:00
std::cout.width(14);
2011-05-26 23:22:38 +00:00
std::cout << velocity.y << " ";
2011-04-05 23:56:17 +00:00
std::cout.width(14);
2011-05-26 23:22:38 +00:00
std::cout << velocity.z << std::endl;
2011-04-05 23:56:17 +00:00
2011-12-18 13:56:20 +00:00
}
catch (SatelliteException& e)
{
2011-05-31 12:06:22 +00:00
std::cout << e.what() << std::endl;
2011-04-05 23:56:17 +00:00
running = false;
}
2011-12-18 13:56:20 +00:00
if ((first_run && current == 0.0) || !first_run)
{
2011-04-05 23:56:17 +00:00
if (current == end)
2011-12-18 13:56:20 +00:00
{
2011-04-05 23:56:17 +00:00
running = false;
2011-12-18 13:56:20 +00:00
}
2011-04-05 23:56:17 +00:00
else if (current + inc > end)
2011-12-18 13:56:20 +00:00
{
2011-04-05 23:56:17 +00:00
current = end;
2011-12-18 13:56:20 +00:00
}
2011-04-05 23:56:17 +00:00
else
2011-12-18 13:56:20 +00:00
{
2011-04-05 23:56:17 +00:00
current += inc;
2011-12-18 13:56:20 +00:00
}
2011-04-05 23:56:17 +00:00
}
first_run = false;
}
}
2011-12-18 13:56:20 +00:00
void tokenize(const std::string& str, std::vector<std::string>& tokens)
{
2011-04-05 23:56:17 +00:00
const std::string& delimiters = " ";
/*
* skip delimiters at beginning
*/
std::string::size_type last_pos = str.find_first_not_of(delimiters, 0);
/*
* find first non-delimiter
*/
std::string::size_type pos = str.find_first_of(delimiters, last_pos);
2011-12-18 13:56:20 +00:00
while (std::string::npos != pos || std::string::npos != last_pos)
{
2011-04-05 23:56:17 +00:00
/*
* add found token to vector
*/
tokens.push_back(str.substr(last_pos, pos - last_pos));
/*
* skip delimiters
*/
last_pos = str.find_first_not_of(delimiters, pos);
/*
* find next non-delimiter
*/
pos = str.find_first_of(delimiters, last_pos);
}
}
2011-12-18 13:56:20 +00:00
void RunTest(const char* infile)
{
2011-04-05 23:56:17 +00:00
std::ifstream file;
file.open(infile);
2011-12-18 13:56:20 +00:00
if (!file.is_open())
{
2011-04-05 23:56:17 +00:00
std::cerr << "Error opening file" << std::endl;
return;
}
bool got_first_line = false;
std::string line1;
std::string line2;
std::string parameters;
2011-12-18 13:56:20 +00:00
while (!file.eof())
{
2011-04-05 23:56:17 +00:00
std::string line;
std::getline(file, line);
Util::Trim(line);
2011-04-05 23:56:17 +00:00
/*
* skip blank lines or lines starting with #
*/
2011-12-18 13:56:20 +00:00
if (line.length() == 0 || line[0] == '#')
{
2011-04-05 23:56:17 +00:00
got_first_line = false;
continue;
}
/*
* find first line
*/
2011-12-18 13:56:20 +00:00
if (!got_first_line)
{
try
{
2011-05-31 12:06:22 +00:00
Tle::IsValidLine(line, 1);
2011-04-05 23:56:17 +00:00
/*
* store line and now read in second line
*/
got_first_line = true;
line1 = line;
2011-12-18 13:56:20 +00:00
}
catch (TleException& e)
{
2011-05-31 12:06:22 +00:00
std::cerr << "Error: " << e.what() << std::endl;
2011-04-05 23:56:17 +00:00
std::cerr << line << std::endl;
}
2011-12-18 13:56:20 +00:00
}
else
{
2011-04-05 23:56:17 +00:00
/*
* no second chances, second line should follow the first
*/
got_first_line = false;
/*
* split line, first 69 is the second line of the tle
* the rest is the test parameters, if there is any
*/
line2 = line.substr(0, 69);
double start = 0.0;
double end = 1440.0;
double inc = 120.0;
2011-12-18 13:56:20 +00:00
if (line.length() > 69)
{
2011-04-05 23:56:17 +00:00
std::vector<std::string> tokens;
parameters = line.substr(70, line.length() - 69);
tokenize(parameters, tokens);
2011-12-18 13:56:20 +00:00
if (tokens.size() >= 3)
{
2011-04-05 23:56:17 +00:00
start = atof(tokens[0].c_str());
end = atof(tokens[1].c_str());
inc = atof(tokens[2].c_str());
}
}
/*
* following line must be the second line
*/
2011-12-18 13:56:20 +00:00
try
{
2011-05-31 12:06:22 +00:00
Tle::IsValidLine(line2, 2);
2011-04-05 23:56:17 +00:00
Tle tle("Test", line1, line2);
2011-04-27 13:36:19 +00:00
RunTle(tle, start, end, inc);
2011-12-18 13:56:20 +00:00
}
catch (TleException& e)
{
2011-05-31 12:06:22 +00:00
std::cerr << "Error: " << e.what() << std::endl;
std::cerr << line << std::endl;
2011-04-05 23:56:17 +00:00
}
}
}
/*
* close file
*/
file.close();
return;
}
2011-12-18 13:56:20 +00:00
int main()
{
2011-04-05 23:56:17 +00:00
const char* file_name = "SGP4-VER.TLE";
RunTest(file_name);
2011-12-18 13:56:20 +00:00
return 1;
2011-04-05 23:56:17 +00:00
}