2011-03-24 16:08:24 +00:00
|
|
|
#include "Tle.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
Tle::Tle(const std::string& line_one, const std::string& line_two) {
|
|
|
|
|
|
|
|
line_one_ = line_one;
|
|
|
|
line_two_ = line_two;
|
|
|
|
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
2011-03-24 16:08:24 +00:00
|
|
|
Tle::Tle(const std::string& name, const std::string& line_one, const std::string& line_two) {
|
2011-04-07 13:42:25 +00:00
|
|
|
|
2011-03-24 16:08:24 +00:00
|
|
|
name_ = name;
|
|
|
|
line_one_ = line_one;
|
|
|
|
line_two_ = line_two;
|
|
|
|
|
|
|
|
Initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Tle::Tle(const Tle& tle) {
|
2011-04-07 13:42:25 +00:00
|
|
|
|
2011-03-24 16:08:24 +00:00
|
|
|
name_ = tle.name_;
|
|
|
|
line_one_ = tle.line_one_;
|
|
|
|
line_two_ = tle.line_two_;
|
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
norad_number_ = tle.norad_number_;
|
|
|
|
international_designator_ = tle.international_designator_;
|
|
|
|
epoch_ = tle.epoch_;
|
|
|
|
mean_motion_dot_ = tle.mean_motion_dot_;
|
|
|
|
mean_motion_dot2_ = tle.mean_motion_dot2_;
|
|
|
|
bstar_ = tle.bstar_;
|
|
|
|
inclination_ = tle.inclination_;
|
|
|
|
right_ascending_node_ = tle.right_ascending_node_;
|
|
|
|
eccentricity_ = tle.eccentricity_;
|
|
|
|
argument_perigee_ = tle.argument_perigee_;
|
|
|
|
mean_anomaly_ = tle.mean_anomaly_;
|
|
|
|
mean_motion_ = tle.mean_motion_;
|
|
|
|
orbit_number_ = tle.orbit_number_;
|
2011-03-24 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Tle::~Tle() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-04-07 13:42:25 +00:00
|
|
|
* convert a tle raw string into an exponent string
|
2011-03-24 16:08:24 +00:00
|
|
|
*/
|
|
|
|
std::string Tle::ExpToDecimal(const std::string& str) {
|
2011-04-07 13:42:25 +00:00
|
|
|
|
|
|
|
static const int START_SIGN = 0;
|
|
|
|
static const int LENGTH_SIGN = 1;
|
|
|
|
static const int START_MANTISSA = 1;
|
|
|
|
static const int LENGTH_MANTISSA = 5;
|
|
|
|
static const int START_EXP = 6;
|
|
|
|
static const int LENGTH_EXP = 2;
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-05-31 12:06:22 +00:00
|
|
|
if ((LENGTH_SIGN + LENGTH_MANTISSA + LENGTH_EXP) != str.length()) {
|
|
|
|
throw TleException("Invalid string length for exponential conversion.");
|
|
|
|
}
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
std::string value = str.substr(START_SIGN, LENGTH_SIGN);
|
2011-03-24 16:08:24 +00:00
|
|
|
value += ".";
|
2011-04-07 13:42:25 +00:00
|
|
|
value += str.substr(START_MANTISSA, LENGTH_MANTISSA);
|
2011-03-24 16:08:24 +00:00
|
|
|
value += "e";
|
2011-04-07 13:42:25 +00:00
|
|
|
value += str.substr(START_EXP, LENGTH_EXP);
|
2011-03-24 16:08:24 +00:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extract all variables
|
|
|
|
*/
|
|
|
|
void Tle::Initialize() {
|
2011-04-07 13:42:25 +00:00
|
|
|
|
|
|
|
std::string temp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* trim whitespace
|
|
|
|
*/
|
2011-03-24 16:19:34 +00:00
|
|
|
TrimLeft(name_);
|
|
|
|
TrimRight(name_);
|
|
|
|
TrimLeft(line_one_);
|
|
|
|
TrimRight(line_one_);
|
|
|
|
TrimLeft(line_two_);
|
|
|
|
TrimRight(line_two_);
|
2011-03-30 17:16:37 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
/*
|
|
|
|
* check the two lines are valid
|
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
IsValidPair(line_one_, line_two_);
|
2011-03-26 12:47:53 +00:00
|
|
|
|
2011-03-24 16:08:24 +00:00
|
|
|
/*
|
|
|
|
* line 1
|
|
|
|
*/
|
|
|
|
|
2011-05-31 12:06:22 +00:00
|
|
|
temp = ExtractNoradNumber(line_one_, 1);
|
2011-04-07 13:42:25 +00:00
|
|
|
norad_number_ = atoi(temp.c_str());
|
|
|
|
/*
|
2011-05-31 12:06:22 +00:00
|
|
|
* if blank use norad number for name
|
2011-04-07 13:42:25 +00:00
|
|
|
*/
|
|
|
|
if (name_.empty())
|
|
|
|
name_ = temp;
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
international_designator_ = line_one_.substr(TLE1_COL_INTLDESC_A, TLE1_LEN_INTLDESC_A + TLE1_LEN_INTLDESC_B + TLE1_LEN_INTLDESC_C);
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
int year = atoi(line_one_.substr(TLE1_COL_EPOCH_A, TLE1_LEN_EPOCH_A).c_str());
|
|
|
|
double day = atof(line_one_.substr(TLE1_COL_EPOCH_B, TLE1_LEN_EPOCH_B).c_str());
|
|
|
|
/*
|
|
|
|
* generate julian date for epoch
|
|
|
|
*/
|
|
|
|
if (year < 57)
|
|
|
|
year += 2000;
|
|
|
|
else
|
|
|
|
year += 1900;
|
|
|
|
epoch_ = Julian(year, day);
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
if (line_one_[TLE1_COL_MEANMOTIONDT] == '-') {
|
|
|
|
temp = "-0";
|
|
|
|
} else
|
|
|
|
temp = "0";
|
|
|
|
temp += line_one_.substr(TLE1_COL_MEANMOTIONDT + 1, TLE1_LEN_MEANMOTIONDT);
|
|
|
|
mean_motion_dot_ = atof(temp.c_str());
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
temp = ExpToDecimal(line_one_.substr(TLE1_COL_MEANMOTIONDT2, TLE1_LEN_MEANMOTIONDT2));
|
|
|
|
mean_motion_dot2_ = atof(temp.c_str());
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
temp = ExpToDecimal(line_one_.substr(TLE1_COL_BSTAR, TLE1_LEN_BSTAR).c_str());
|
|
|
|
bstar_ = atof(temp.c_str());
|
2011-03-24 16:08:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* line 2
|
|
|
|
*/
|
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
temp = line_two_.substr(TLE2_COL_INCLINATION, TLE2_LEN_INCLINATION);
|
|
|
|
TrimLeft(temp);
|
|
|
|
inclination_ = atof(temp.c_str());
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
temp = line_two_.substr(TLE2_COL_RAASCENDNODE, TLE2_LEN_RAASCENDNODE);
|
|
|
|
TrimLeft(temp);
|
|
|
|
right_ascending_node_ = atof(temp.c_str());
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
temp = "0.";
|
|
|
|
temp += line_two_.substr(TLE2_COL_ECCENTRICITY, TLE2_LEN_ECCENTRICITY);
|
|
|
|
eccentricity_ = atof(temp.c_str());
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
temp = line_two_.substr(TLE2_COL_ARGPERIGEE, TLE2_LEN_ARGPERIGEE);
|
|
|
|
TrimLeft(temp);
|
|
|
|
argument_perigee_ = atof(temp.c_str());
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
temp = line_two_.substr(TLE2_COL_MEANANOMALY, TLE2_LEN_MEANANOMALY);
|
|
|
|
TrimLeft(temp);
|
|
|
|
mean_anomaly_ = atof(temp.c_str());
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
temp = line_two_.substr(TLE2_COL_MEANMOTION, TLE2_LEN_MEANMOTION);
|
|
|
|
TrimLeft(temp);
|
|
|
|
mean_motion_ = atof(temp.c_str());
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
temp = line_two_.substr(TLE2_COL_REVATEPOCH, TLE2_LEN_REVATEPOCH);
|
|
|
|
TrimLeft(temp);
|
|
|
|
orbit_number_ = atoi(temp.c_str());
|
|
|
|
}
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
/*
|
|
|
|
* check the two lines have matching norad numbers
|
2011-05-31 12:06:22 +00:00
|
|
|
* and that the lines themselves are equal
|
2011-04-07 13:42:25 +00:00
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
void Tle::IsValidPair(const std::string& line1, const std::string& line2) {
|
2011-03-30 17:16:37 +00:00
|
|
|
|
2011-05-31 12:06:22 +00:00
|
|
|
/*
|
|
|
|
* validate each line
|
|
|
|
*/
|
|
|
|
IsValidLine(line1, 1);
|
|
|
|
IsValidLine(line2, 2);
|
2011-05-08 12:39:20 +00:00
|
|
|
|
2011-05-31 12:06:22 +00:00
|
|
|
/*
|
|
|
|
* extract norad numbers
|
|
|
|
*/
|
|
|
|
std::string norad_1 = ExtractNoradNumber(line1, 1);
|
|
|
|
std::string norad_2 = ExtractNoradNumber(line2, 2);
|
2011-05-08 12:39:20 +00:00
|
|
|
|
2011-05-31 12:06:22 +00:00
|
|
|
/*
|
|
|
|
* make sure they match
|
|
|
|
*/
|
|
|
|
if (norad_1.compare(norad_2) != 0)
|
|
|
|
throw TleException("Norad numbers do not match.");
|
2011-03-24 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* validate a line
|
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
void Tle::IsValidLine(const std::string& str, const unsigned char line_number) {
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-05-31 12:06:22 +00:00
|
|
|
/*
|
|
|
|
* validation patterns
|
|
|
|
*/
|
|
|
|
static const std::string line1_pattern = "1 NNNNNC NNNNNXXX NNNNN.NNNNNNNN +.NNNNNNNN +NNNNN-N +NNNNN-N N NNNNN";
|
2011-04-07 13:42:25 +00:00
|
|
|
static const std::string line2_pattern = "2 NNNNN NNN.NNNN NNN.NNNN NNNNNNN NNN.NNNN NNN.NNNN NN.NNNNNNNNNNNNNN";
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
/*
|
|
|
|
* validate line against the pattern
|
|
|
|
*/
|
|
|
|
if (1 == line_number) {
|
2011-05-31 12:06:22 +00:00
|
|
|
ValidateLine(str, line1_pattern);
|
2011-04-07 13:42:25 +00:00
|
|
|
} else if (2 == line_number) {
|
2011-05-31 12:06:22 +00:00
|
|
|
ValidateLine(str, line2_pattern);
|
2011-05-27 16:03:11 +00:00
|
|
|
} else {
|
2011-05-31 12:06:22 +00:00
|
|
|
throw TleException("Invalid line number to check.");
|
2011-03-24 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
/*
|
|
|
|
* last char in string is modulo 10 checksum
|
2011-04-11 09:58:10 +00:00
|
|
|
* edited out as checksum isnt consistent
|
|
|
|
*
|
|
|
|
* int chk = CheckSum(str);
|
|
|
|
* if (chk != (str[TLE_LEN_LINE_DATA - 1] - '0'))
|
|
|
|
* return false;
|
2011-04-07 13:42:25 +00:00
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
}
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-05-31 12:06:22 +00:00
|
|
|
bool Tle::IsValidLineLength(const std::string& str) {
|
|
|
|
|
|
|
|
return str.length() == GetLineLength() ? true : false;
|
2011-03-24 16:08:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* validate line given a pattern
|
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
void Tle::ValidateLine(const std::string& line, const std::string& pattern) {
|
2011-04-05 23:53:11 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
/*
|
2011-05-31 12:06:22 +00:00
|
|
|
* check length of line
|
2011-04-07 13:42:25 +00:00
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
if (!IsValidLineLength(line)) {
|
|
|
|
throw TleException("Invalid line length.");
|
2011-04-05 23:53:11 +00:00
|
|
|
}
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
std::string::const_iterator pattern_itr = pattern.begin();
|
|
|
|
std::string::const_iterator line_itr = line.begin();
|
2011-03-24 16:08:24 +00:00
|
|
|
|
2011-04-07 13:42:25 +00:00
|
|
|
while (pattern_itr != pattern.end()) {
|
|
|
|
|
|
|
|
if (isdigit(*pattern_itr) || *pattern_itr == ' ' ||
|
2011-04-09 23:52:11 +00:00
|
|
|
*pattern_itr == '.') {
|
2011-04-07 13:42:25 +00:00
|
|
|
|
|
|
|
/*
|
2011-05-31 12:06:22 +00:00
|
|
|
* should match exactly
|
2011-04-07 13:42:25 +00:00
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
if (*pattern_itr != *line_itr) {
|
|
|
|
throw TleException("Invalid character.");
|
|
|
|
}
|
2011-04-07 13:42:25 +00:00
|
|
|
|
|
|
|
} else if (*pattern_itr == 'N') {
|
|
|
|
|
|
|
|
/*
|
2011-05-31 12:06:22 +00:00
|
|
|
* 'N' = number or ' '
|
2011-04-07 13:42:25 +00:00
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
if (!isdigit(*line_itr) && *line_itr != ' ') {
|
|
|
|
throw TleException("Invalid character.");
|
|
|
|
}
|
2011-04-07 13:42:25 +00:00
|
|
|
|
|
|
|
} else if (*pattern_itr == '+') {
|
|
|
|
|
|
|
|
/*
|
2011-05-31 12:06:22 +00:00
|
|
|
* '+' = '+' or '-' or ' ' or '0'
|
2011-04-07 13:42:25 +00:00
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
if (*line_itr != '+' && *line_itr != '-' && *line_itr != ' ' && *line_itr != '0') {
|
|
|
|
throw TleException("Invalid character.");
|
|
|
|
}
|
2011-04-09 23:52:11 +00:00
|
|
|
|
|
|
|
} else if (*pattern_itr == '-') {
|
|
|
|
|
|
|
|
/*
|
2011-05-31 12:06:22 +00:00
|
|
|
* '-' = '+' or '-'
|
|
|
|
*/
|
|
|
|
if (*line_itr != '+' && *line_itr != '-') {
|
|
|
|
throw TleException("Invalid character.");
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (*pattern_itr == 'C') {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'C' = 'U' or 'S'
|
|
|
|
*/
|
|
|
|
if (*line_itr != 'U' && *line_itr != 'S') {
|
|
|
|
throw TleException("Invalid character.");
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (*pattern_itr == 'X') {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'X' = A-Z or ' '
|
2011-04-09 23:52:11 +00:00
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
if (!(*line_itr >= 'A' || *line_itr <= 'Z') && *line_itr != ' ') {
|
|
|
|
throw TleException("Invalid character.");
|
|
|
|
}
|
2011-03-24 16:08:24 +00:00
|
|
|
}
|
2011-04-07 13:42:25 +00:00
|
|
|
|
|
|
|
pattern_itr++;
|
|
|
|
line_itr++;
|
2011-03-24 16:08:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compute checksum
|
|
|
|
*/
|
2011-05-31 12:06:22 +00:00
|
|
|
int Tle::CheckSum(const std::string & str) {
|
2011-04-07 13:42:25 +00:00
|
|
|
|
2011-03-24 16:08:24 +00:00
|
|
|
size_t len = str.size() - 1;
|
|
|
|
int xsum = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
2011-04-07 13:42:25 +00:00
|
|
|
|
2011-03-24 16:08:24 +00:00
|
|
|
char ch = str[i];
|
2011-04-07 13:42:25 +00:00
|
|
|
|
2011-03-24 16:08:24 +00:00
|
|
|
if (isdigit(ch))
|
|
|
|
xsum += (ch - '0');
|
2011-04-07 13:42:25 +00:00
|
|
|
else
|
|
|
|
if (ch == '-')
|
2011-03-24 16:08:24 +00:00
|
|
|
xsum++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (xsum % 10);
|
|
|
|
}
|
2011-05-31 12:06:22 +00:00
|
|
|
|
|
|
|
std::string Tle::ExtractNoradNumber(const std::string& str, const unsigned char line_number) {
|
|
|
|
|
|
|
|
std::string norad_number;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check length
|
|
|
|
*/
|
|
|
|
if (!IsValidLineLength(str)) {
|
|
|
|
throw TleException("Invalid line length.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* extract string
|
|
|
|
*/
|
|
|
|
if (1 == line_number) {
|
|
|
|
norad_number = str.substr(TLE1_COL_NORADNUM, TLE1_LEN_NORADNUM);
|
|
|
|
} else if (2 == line_number) {
|
|
|
|
norad_number = str.substr(TLE2_COL_NORADNUM, TLE2_LEN_NORADNUM);
|
|
|
|
} else {
|
|
|
|
throw TleException("Invalid line number to check.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return norad_number;
|
|
|
|
}
|