From dfe868cae7d93de0621309035fb07968b5c07001 Mon Sep 17 00:00:00 2001 From: Daniel Warner Date: Wed, 14 Dec 2011 10:13:00 +0000 Subject: [PATCH] Updated Tle --- Tle.cpp | 212 +++++++++++++++++++++++++++++++++++++------------------- Tle.h | 149 ++++++++++++++++++++------------------- 2 files changed, 216 insertions(+), 145 deletions(-) diff --git a/Tle.cpp b/Tle.cpp index 5c87837..990cec5 100644 --- a/Tle.cpp +++ b/Tle.cpp @@ -1,27 +1,59 @@ #include "Tle.h" -#include #include -Tle::Tle(const std::string& line_one, const std::string& line_two) { +namespace +{ + /* + * line 1 + */ + static const unsigned int TLE1_COL_NORADNUM = 2; + static const unsigned int TLE1_LEN_NORADNUM = 5; + static const unsigned int TLE1_COL_INTLDESC_A = 9; + static const unsigned int TLE1_LEN_INTLDESC_A = 2; + static const unsigned int TLE1_COL_INTLDESC_B = 11; + static const unsigned int TLE1_LEN_INTLDESC_B = 3; + static const unsigned int TLE1_COL_INTLDESC_C = 14; + static const unsigned int TLE1_LEN_INTLDESC_C = 3; + static const unsigned int TLE1_COL_EPOCH_A = 18; + static const unsigned int TLE1_LEN_EPOCH_A = 2; + static const unsigned int TLE1_COL_EPOCH_B = 20; + static const unsigned int TLE1_LEN_EPOCH_B = 12; + static const unsigned int TLE1_COL_MEANMOTIONDT = 33; + static const unsigned int TLE1_LEN_MEANMOTIONDT = 10; + static const unsigned int TLE1_COL_MEANMOTIONDT2 = 44; + static const unsigned int TLE1_LEN_MEANMOTIONDT2 = 8; + static const unsigned int TLE1_COL_BSTAR = 53; + static const unsigned int TLE1_LEN_BSTAR = 8; + static const unsigned int TLE1_COL_EPHEMTYPE = 62; + static const unsigned int TLE1_LEN_EPHEMTYPE = 1; + static const unsigned int TLE1_COL_ELNUM = 64; + static const unsigned int TLE1_LEN_ELNUM = 4; - line_one_ = line_one; - line_two_ = line_two; + /* + * line 2 + */ + static const unsigned int TLE2_COL_NORADNUM = 2; + static const unsigned int TLE2_LEN_NORADNUM = 5; + static const unsigned int TLE2_COL_INCLINATION = 8; + static const unsigned int TLE2_LEN_INCLINATION = 8; + static const unsigned int TLE2_COL_RAASCENDNODE = 17; + static const unsigned int TLE2_LEN_RAASCENDNODE = 8; + static const unsigned int TLE2_COL_ECCENTRICITY = 26; + static const unsigned int TLE2_LEN_ECCENTRICITY = 7; + static const unsigned int TLE2_COL_ARGPERIGEE = 34; + static const unsigned int TLE2_LEN_ARGPERIGEE = 8; + static const unsigned int TLE2_COL_MEANANOMALY = 43; + static const unsigned int TLE2_LEN_MEANANOMALY = 8; + static const unsigned int TLE2_COL_MEANMOTION = 52; + static const unsigned int TLE2_LEN_MEANMOTION = 11; + static const unsigned int TLE2_COL_REVATEPOCH = 63; + static const unsigned int TLE2_LEN_REVATEPOCH = 5; - Initialize(); } -Tle::Tle(const std::string& name, const std::string& line_one, const std::string& line_two) { - - name_ = name; - line_one_ = line_one; - line_two_ = line_two; - - Initialize(); -} - -Tle::Tle(const Tle& tle) { - +Tle::Tle(const Tle& tle) +{ name_ = tle.name_; line_one_ = tle.line_one_; line_two_ = tle.line_two_; @@ -41,14 +73,11 @@ Tle::Tle(const Tle& tle) { orbit_number_ = tle.orbit_number_; } -Tle::~Tle() { -} - /* * convert a tle raw string into an exponent string */ -std::string Tle::ExpToDecimal(const std::string& str) { - +std::string Tle::ExpToDecimal(const std::string& str) +{ static const int START_SIGN = 0; static const int LENGTH_SIGN = 1; static const int START_MANTISSA = 1; @@ -56,7 +85,8 @@ std::string Tle::ExpToDecimal(const std::string& str) { static const int START_EXP = 6; static const int LENGTH_EXP = 2; - if ((LENGTH_SIGN + LENGTH_MANTISSA + LENGTH_EXP) != str.length()) { + if ((LENGTH_SIGN + LENGTH_MANTISSA + LENGTH_EXP) != str.length()) + { throw TleException("Invalid string length for exponential conversion."); } @@ -72,8 +102,8 @@ std::string Tle::ExpToDecimal(const std::string& str) { /* * extract all variables */ -void Tle::Initialize() { - +void Tle::Initialize() +{ std::string temp; /* @@ -101,12 +131,17 @@ void Tle::Initialize() { * if blank use norad number for name */ if (name_.empty()) + { name_ = temp; + } - international_designator_ = line_one_.substr(TLE1_COL_INTLDESC_A, TLE1_LEN_INTLDESC_A + TLE1_LEN_INTLDESC_B + TLE1_LEN_INTLDESC_C); + international_designator_ = line_one_.substr(TLE1_COL_INTLDESC_A, + TLE1_LEN_INTLDESC_A + TLE1_LEN_INTLDESC_B + TLE1_LEN_INTLDESC_C); - 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()); + 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 */ @@ -123,10 +158,12 @@ void Tle::Initialize() { temp += line_one_.substr(TLE1_COL_MEANMOTIONDT + 1, TLE1_LEN_MEANMOTIONDT); mean_motion_dot_ = atof(temp.c_str()); - temp = ExpToDecimal(line_one_.substr(TLE1_COL_MEANMOTIONDT2, TLE1_LEN_MEANMOTIONDT2)); + temp = ExpToDecimal(line_one_.substr(TLE1_COL_MEANMOTIONDT2, + TLE1_LEN_MEANMOTIONDT2)); mean_motion_dot2_ = atof(temp.c_str()); - temp = ExpToDecimal(line_one_.substr(TLE1_COL_BSTAR, TLE1_LEN_BSTAR).c_str()); + temp = ExpToDecimal(line_one_.substr(TLE1_COL_BSTAR, + TLE1_LEN_BSTAR).c_str()); bstar_ = atof(temp.c_str()); /* @@ -166,8 +203,8 @@ void Tle::Initialize() { * check the two lines have matching norad numbers * and that the lines themselves are equal */ -void Tle::IsValidPair(const std::string& line1, const std::string& line2) { - +void Tle::IsValidPair(const std::string& line1, const std::string& line2) +{ /* * validate each line */ @@ -184,14 +221,16 @@ void Tle::IsValidPair(const std::string& line1, const std::string& line2) { * make sure they match */ if (norad_1.compare(norad_2) != 0) + { throw TleException("Norad numbers do not match."); + } } /* * validate a line */ -void Tle::IsValidLine(const std::string& str, const unsigned char line_number) { - +void Tle::IsValidLine(const std::string& str, int line_number) +{ /* * validation patterns */ @@ -201,11 +240,16 @@ void Tle::IsValidLine(const std::string& str, const unsigned char line_number) { /* * validate line against the pattern */ - if (1 == line_number) { + if (1 == line_number) + { ValidateLine(str, line1_pattern); - } else if (2 == line_number) { + } + else if (2 == line_number) + { ValidateLine(str, line2_pattern); - } else { + } + else + { throw TleException("Invalid line number to check."); } @@ -219,80 +263,93 @@ void Tle::IsValidLine(const std::string& str, const unsigned char line_number) { */ } -bool Tle::IsValidLineLength(const std::string& str) { - +bool Tle::IsValidLineLength(const std::string& str) +{ return str.length() == GetLineLength() ? true : false; } /* * validate line given a pattern */ -void Tle::ValidateLine(const std::string& line, const std::string& pattern) { - +void Tle::ValidateLine(const std::string& line, const std::string& pattern) +{ /* * check length of line */ - if (!IsValidLineLength(line)) { + if (!IsValidLineLength(line)) + { throw TleException("Invalid line length."); } std::string::const_iterator pattern_itr = pattern.begin(); std::string::const_iterator line_itr = line.begin(); - while (pattern_itr != pattern.end()) { - + while (pattern_itr != pattern.end()) + { if (isdigit(*pattern_itr) || *pattern_itr == ' ' || - *pattern_itr == '.') { - + *pattern_itr == '.') + { /* * should match exactly */ - if (*pattern_itr != *line_itr) { + if (*pattern_itr != *line_itr) + { throw TleException("Invalid character."); } - } else if (*pattern_itr == 'N') { - + } + else if (*pattern_itr == 'N') + { /* * 'N' = number or ' ' */ - if (!isdigit(*line_itr) && *line_itr != ' ') { + if (!isdigit(*line_itr) && *line_itr != ' ') + { throw TleException("Invalid character."); } - } else if (*pattern_itr == '+') { - + } + else if (*pattern_itr == '+') + { /* * '+' = '+' or '-' or ' ' or '0' */ - if (*line_itr != '+' && *line_itr != '-' && *line_itr != ' ' && *line_itr != '0') { + if (*line_itr != '+' && *line_itr != '-' && + *line_itr != ' ' && *line_itr != '0') + { throw TleException("Invalid character."); } - } else if (*pattern_itr == '-') { - + } + else if (*pattern_itr == '-') + { /* * '-' = '+' or '-' */ - if (*line_itr != '+' && *line_itr != '-') { + if (*line_itr != '+' && *line_itr != '-') + { throw TleException("Invalid character."); } - } else if (*pattern_itr == 'C') { - + } + else if (*pattern_itr == 'C') + { /* * 'C' = 'U' or 'S' */ - if (*line_itr != 'U' && *line_itr != 'S') { + if (*line_itr != 'U' && *line_itr != 'S') + { throw TleException("Invalid character."); } - } else if (*pattern_itr == 'X') { - + } + else if (*pattern_itr == 'X') + { /* * 'X' = A-Z or ' ' */ - if (!(*line_itr >= 'A' || *line_itr <= 'Z') && *line_itr != ' ') { + if (!(*line_itr >= 'A' || *line_itr <= 'Z') && *line_itr != ' ') + { throw TleException("Invalid character."); } } @@ -305,44 +362,53 @@ void Tle::ValidateLine(const std::string& line, const std::string& pattern) { /* * compute checksum */ -int Tle::CheckSum(const std::string & str) { - +int Tle::CheckSum(const std::string & str) +{ size_t len = str.size() - 1; int xsum = 0; - for (size_t i = 0; i < len; i++) { - + for (size_t i = 0; i < len; i++) + { char ch = str[i]; if (isdigit(ch)) + { xsum += (ch - '0'); - else - if (ch == '-') + } + else if (ch == '-') + { xsum++; + } } return (xsum % 10); } -std::string Tle::ExtractNoradNumber(const std::string& str, const unsigned char line_number) { - +std::string Tle::ExtractNoradNumber(const std::string& str, int line_number) +{ std::string norad_number; /* * check length */ - if (!IsValidLineLength(str)) { + if (!IsValidLineLength(str)) + { throw TleException("Invalid line length."); } /* * extract string */ - if (1 == line_number) { + if (1 == line_number) + { norad_number = str.substr(TLE1_COL_NORADNUM, TLE1_LEN_NORADNUM); - } else if (2 == line_number) { + } + else if (2 == line_number) + { norad_number = str.substr(TLE2_COL_NORADNUM, TLE2_LEN_NORADNUM); - } else { + } + else + { throw TleException("Invalid line number to check."); } diff --git a/Tle.h b/Tle.h index fa9107a..3458662 100644 --- a/Tle.h +++ b/Tle.h @@ -7,103 +7,152 @@ #include -class Tle { +class Tle +{ public: - Tle(const std::string& line_one, const std::string& line_two); - Tle(const std::string& name, const std::string& line_one, const std::string& line_two); + Tle(const std::string& line_one, const std::string& line_two) + : line_one_(line_one), line_two_(line_two) + { + Initialize(); + } + + Tle(const std::string& name, const std::string& line_one, + const std::string& line_two) + : name_(name),line_one_(line_one), line_two_(line_two) + { + Initialize(); + } + Tle(const Tle& tle); - virtual ~Tle(); + + virtual ~Tle() + { + } /* * get raw strings */ - std::string GetName() const { + std::string GetName() const + { return name_; } - std::string GetLine1() const { + std::string GetLine1() const + { return line_one_; } - std::string GetLine2() const { + std::string GetLine2() const + { return line_two_; } /* * get tle values */ - unsigned int NoradNumber() const { + unsigned int NoradNumber() const + { return norad_number_; } - std::string InternationlDesignator() const { + std::string InternationlDesignator() const + { return international_designator_; } - Julian Epoch()const { + Julian Epoch() const + { return epoch_; } - double MeanMotionDot()const { + double MeanMotionDot() const + { return mean_motion_dot_; } - double MeanMotionDot2()const { + double MeanMotionDot2() const + { return mean_motion_dot2_; } - double BStar()const { + double BStar() const + { return bstar_; } - double Inclination(const bool in_degrees)const { + double Inclination(bool in_degrees) const + { if (in_degrees) + { return inclination_; + } else + { return DegreesToRadians(inclination_); + } } - double RightAscendingNode(const bool in_degrees)const { + double RightAscendingNode(const bool in_degrees) const + { if (in_degrees) + { return right_ascending_node_; + } else + { return DegreesToRadians(right_ascending_node_); + } } - double Eccentricity()const { + double Eccentricity() const + { return eccentricity_; } - double ArgumentPerigee(const bool in_degrees)const { + double ArgumentPerigee(const bool in_degrees) const + { if (in_degrees) + { return argument_perigee_; + } else + { return DegreesToRadians(argument_perigee_); + } } - double MeanAnomaly(const bool in_degrees)const { + double MeanAnomaly(const bool in_degrees) const + { if (in_degrees) + { return mean_anomaly_; + } else + { return DegreesToRadians(mean_anomaly_); + } } - double MeanMotion()const { + double MeanMotion() const + { return mean_motion_; } - unsigned int OrbitNumber()const { + unsigned int OrbitNumber() const + { return orbit_number_; } /* * helper / validation methods */ - static unsigned int GetLineLength() { + static unsigned int GetLineLength() + { return TLE_LEN_LINE_DATA; } + static void IsValidPair(const std::string& line1, const std::string& line2); - static void IsValidLine(const std::string& str, unsigned char line_number); + static void IsValidLine(const std::string& str, int line_number); private: /* @@ -117,13 +166,15 @@ private: /* * validate a line against a pattern */ - static void ValidateLine(const std::string& line, const std::string& pattern); + static void ValidateLine(const std::string& line, + const std::string& pattern); /* * compute checksum */ static int CheckSum(const std::string& str); - static std::string ExtractNoradNumber(const std::string& str, const unsigned char line_number); + static std::string ExtractNoradNumber(const std::string& str, + int line_number); static bool IsValidLineLength(const std::string& str); @@ -153,58 +204,12 @@ private: unsigned int orbit_number_; /* - * name line + * line lengths */ static const unsigned int TLE_LEN_LINE_DATA = 69; static const unsigned int TLE_LEN_LINE_NAME = 22; - /* - * line 1 - */ - static const unsigned int TLE1_COL_NORADNUM = 2; - static const unsigned int TLE1_LEN_NORADNUM = 5; - static const unsigned int TLE1_COL_INTLDESC_A = 9; - static const unsigned int TLE1_LEN_INTLDESC_A = 2; - static const unsigned int TLE1_COL_INTLDESC_B = 11; - static const unsigned int TLE1_LEN_INTLDESC_B = 3; - static const unsigned int TLE1_COL_INTLDESC_C = 14; - static const unsigned int TLE1_LEN_INTLDESC_C = 3; - static const unsigned int TLE1_COL_EPOCH_A = 18; - static const unsigned int TLE1_LEN_EPOCH_A = 2; - static const unsigned int TLE1_COL_EPOCH_B = 20; - static const unsigned int TLE1_LEN_EPOCH_B = 12; - static const unsigned int TLE1_COL_MEANMOTIONDT = 33; - static const unsigned int TLE1_LEN_MEANMOTIONDT = 10; - static const unsigned int TLE1_COL_MEANMOTIONDT2 = 44; - static const unsigned int TLE1_LEN_MEANMOTIONDT2 = 8; - static const unsigned int TLE1_COL_BSTAR = 53; - static const unsigned int TLE1_LEN_BSTAR = 8; - static const unsigned int TLE1_COL_EPHEMTYPE = 62; - static const unsigned int TLE1_LEN_EPHEMTYPE = 1; - static const unsigned int TLE1_COL_ELNUM = 64; - static const unsigned int TLE1_LEN_ELNUM = 4; - - /* - * line 2 - */ - static const unsigned int TLE2_COL_NORADNUM = 2; - static const unsigned int TLE2_LEN_NORADNUM = 5; - static const unsigned int TLE2_COL_INCLINATION = 8; - static const unsigned int TLE2_LEN_INCLINATION = 8; - static const unsigned int TLE2_COL_RAASCENDNODE = 17; - static const unsigned int TLE2_LEN_RAASCENDNODE = 8; - static const unsigned int TLE2_COL_ECCENTRICITY = 26; - static const unsigned int TLE2_LEN_ECCENTRICITY = 7; - static const unsigned int TLE2_COL_ARGPERIGEE = 34; - static const unsigned int TLE2_LEN_ARGPERIGEE = 8; - static const unsigned int TLE2_COL_MEANANOMALY = 43; - static const unsigned int TLE2_LEN_MEANANOMALY = 8; - static const unsigned int TLE2_COL_MEANMOTION = 52; - static const unsigned int TLE2_LEN_MEANMOTION = 11; - static const unsigned int TLE2_COL_REVATEPOCH = 63; - static const unsigned int TLE2_LEN_REVATEPOCH = 5; -}; + }; #endif -