2013-04-01 10:33:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Daniel Warner <contact@danrw.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-12-31 16:42:18 +00:00
|
|
|
#include <Tle.h>
|
|
|
|
#include <SGP4.h>
|
|
|
|
#include <Observer.h>
|
|
|
|
#include <CoordGeodetic.h>
|
2012-10-25 19:40:47 +00:00
|
|
|
#include <CoordTopocentric.h>
|
2011-12-24 00:26:41 +00:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <string>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2022-11-13 11:09:27 +00:00
|
|
|
void RunTle(libsgp4::Tle tle, double start, double end, double inc)
|
2011-12-24 00:26:41 +00:00
|
|
|
{
|
|
|
|
double current = start;
|
2022-11-13 11:09:27 +00:00
|
|
|
libsgp4::SGP4 model(tle);
|
2011-12-24 00:26:41 +00:00
|
|
|
bool running = true;
|
|
|
|
bool first_run = true;
|
2012-01-05 22:01:14 +00:00
|
|
|
|
|
|
|
std::cout << std::setprecision(0) << tle.NoradNumber() << " xx"
|
|
|
|
<< std::endl;
|
|
|
|
|
2011-12-24 00:26:41 +00:00
|
|
|
while (running)
|
|
|
|
{
|
2012-01-07 23:26:33 +00:00
|
|
|
bool error = false;
|
2022-11-13 11:09:27 +00:00
|
|
|
libsgp4::Vector position;
|
|
|
|
libsgp4::Vector velocity;
|
2012-01-07 23:26:33 +00:00
|
|
|
double tsince;
|
|
|
|
|
2011-12-24 00:26:41 +00:00
|
|
|
try
|
|
|
|
{
|
2022-11-13 11:09:27 +00:00
|
|
|
if (first_run && current != 0.0)
|
2011-12-24 00:26:41 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* make sure first run is always as zero
|
|
|
|
*/
|
2012-01-07 23:26:33 +00:00
|
|
|
tsince = 0.0;
|
2011-12-24 00:26:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* otherwise run as normal
|
|
|
|
*/
|
2012-01-07 23:26:33 +00:00
|
|
|
tsince = current;
|
|
|
|
}
|
|
|
|
|
2022-11-13 11:09:27 +00:00
|
|
|
libsgp4::Eci eci = model.FindPosition(tsince);
|
2012-10-24 17:01:17 +00:00
|
|
|
position = eci.Position();
|
|
|
|
velocity = eci.Velocity();
|
2012-01-07 23:26:33 +00:00
|
|
|
}
|
2022-11-13 11:09:27 +00:00
|
|
|
catch (libsgp4::SatelliteException& e)
|
2012-01-07 23:26:33 +00:00
|
|
|
{
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
error = true;
|
|
|
|
running = false;
|
|
|
|
}
|
2022-11-13 11:09:27 +00:00
|
|
|
catch (libsgp4::DecayedException& e)
|
2012-01-07 23:26:33 +00:00
|
|
|
{
|
|
|
|
std::cerr << e.what() << std::endl;
|
|
|
|
|
2012-10-24 17:01:17 +00:00
|
|
|
position = e.Position();
|
|
|
|
velocity = e.Velocity();
|
2012-01-07 23:26:33 +00:00
|
|
|
|
|
|
|
if (!first_run)
|
|
|
|
{
|
|
|
|
// print out position on first run
|
|
|
|
error = true;
|
2011-12-24 00:26:41 +00:00
|
|
|
}
|
|
|
|
|
2012-01-07 23:26:33 +00:00
|
|
|
running = false;
|
|
|
|
}
|
2011-12-24 00:26:41 +00:00
|
|
|
|
2012-01-07 23:26:33 +00:00
|
|
|
if (!error)
|
|
|
|
{
|
2011-12-24 00:26:41 +00:00
|
|
|
std::cout << std::setprecision(8) << std::fixed;
|
|
|
|
std::cout.width(17);
|
2012-01-07 23:26:33 +00:00
|
|
|
std::cout << tsince << " ";
|
2011-12-24 00:26:41 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-01-07 23:26:33 +00:00
|
|
|
|
2011-12-24 00:26:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tokenize(const std::string& str, std::vector<std::string>& tokens)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
while (std::string::npos != pos || std::string::npos != last_pos)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunTest(const char* infile)
|
|
|
|
{
|
|
|
|
std::ifstream file;
|
|
|
|
|
|
|
|
file.open(infile);
|
|
|
|
|
|
|
|
if (!file.is_open())
|
|
|
|
{
|
|
|
|
std::cerr << "Error opening file" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool got_first_line = false;
|
|
|
|
std::string line1;
|
|
|
|
std::string line2;
|
|
|
|
std::string parameters;
|
|
|
|
|
|
|
|
while (!file.eof())
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
std::getline(file, line);
|
|
|
|
|
2022-11-13 11:09:27 +00:00
|
|
|
libsgp4::Util::Trim(line);
|
2011-12-24 00:26:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* skip blank lines or lines starting with #
|
|
|
|
*/
|
|
|
|
if (line.length() == 0 || line[0] == '#')
|
|
|
|
{
|
|
|
|
got_first_line = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find first line
|
|
|
|
*/
|
|
|
|
if (!got_first_line)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-11-13 11:09:27 +00:00
|
|
|
if (line.length() >= libsgp4::Tle::LineLength())
|
2012-01-04 22:31:54 +00:00
|
|
|
{
|
2012-10-24 17:01:17 +00:00
|
|
|
//Tle::IsValidLine(line.substr(0, Tle::LineLength()), 1);
|
2012-01-04 22:31:54 +00:00
|
|
|
/*
|
|
|
|
* store line and now read in second line
|
|
|
|
*/
|
|
|
|
got_first_line = true;
|
|
|
|
line1 = line;
|
|
|
|
}
|
2011-12-24 00:26:41 +00:00
|
|
|
}
|
2022-11-13 11:09:27 +00:00
|
|
|
catch (libsgp4::TleException& e)
|
2011-12-24 00:26:41 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Error: " << e.what() << std::endl;
|
|
|
|
std::cerr << line << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
2022-11-13 11:09:27 +00:00
|
|
|
line2 = line.substr(0, libsgp4::Tle::LineLength());
|
2011-12-24 00:26:41 +00:00
|
|
|
double start = 0.0;
|
|
|
|
double end = 1440.0;
|
|
|
|
double inc = 120.0;
|
|
|
|
if (line.length() > 69)
|
|
|
|
{
|
|
|
|
std::vector<std::string> tokens;
|
2022-11-13 11:09:27 +00:00
|
|
|
parameters = line.substr(libsgp4::Tle::LineLength() + 1,
|
|
|
|
line.length() - libsgp4::Tle::LineLength());
|
2011-12-24 00:26:41 +00:00
|
|
|
tokenize(parameters, tokens);
|
|
|
|
if (tokens.size() >= 3)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
try
|
|
|
|
{
|
2022-11-13 11:09:27 +00:00
|
|
|
if (line.length() >= libsgp4::Tle::LineLength())
|
2012-01-04 22:31:54 +00:00
|
|
|
{
|
2012-10-24 17:01:17 +00:00
|
|
|
//Tle::IsValidLine(line.substr(0, Tle::LineLength()), 2);
|
2022-11-13 11:09:27 +00:00
|
|
|
libsgp4::Tle tle("Test", line1, line2);
|
2012-01-04 22:31:54 +00:00
|
|
|
RunTle(tle, start, end, inc);
|
|
|
|
}
|
2011-12-24 00:26:41 +00:00
|
|
|
}
|
2022-11-13 11:09:27 +00:00
|
|
|
catch (libsgp4::TleException& e)
|
2011-12-24 00:26:41 +00:00
|
|
|
{
|
|
|
|
std::cerr << "Error: " << e.what() << std::endl;
|
|
|
|
std::cerr << line << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close file
|
|
|
|
*/
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
const char* file_name = "SGP4-VER.TLE";
|
|
|
|
|
|
|
|
RunTest(file_name);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|