TLELib  0.1.0
tlenode.cpp
Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------+
00002  | TLELib                                                                      |
00003  | Copyright 2011 Sergei V. Fundaev                                            |
00004  +-----------------------------------------------------------------------------+
00005  | This file is part of TLELib.                                                |
00006  |                                                                             |
00007  | TLELib is free software: you can redistribute it and/or modify              |
00008  | it under the terms of the GNU Lesser General Public License as published by |
00009  | the Free Software Foundation, either version 3 of the License, or           |
00010  | (at your option) any later version.                                         |
00011  |                                                                             |
00012  | TLELib is distributed in the hope that it will be useful,                   |
00013  | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00014  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00015  | GNU Lesser General Public License for more details.                         |
00016  |                                                                             |
00017  | You should have received a copy of the GNU Lesser General Public License    |
00018  | along with TLELib. If not, see <http://www.gnu.org/licenses/>.              |
00019  +----------------------------------------------------------------------------*/
00025 #define CHECKSUM_INDEX 68 //!< Index of checksum symbol in the TLE format line
00026 
00027 #include <string>
00028 #include <cstdlib>
00029 #include <ctime>
00030 
00031 #include <tlelib/tlenode.h>
00032 #include <tlelib/tlefunc.h>
00033 #include <tlelib/tleexception.h>
00034 
00035 using namespace tlelib;
00036 
00037 tle_node::tle_node()
00038 {
00039     init();
00040 }
00041 //------------------------------------------------------------------------------
00042 
00043 tle_node::tle_node(const std::string& line1, const std::string& line2, const std::string& line3, bool forceParsing)
00044 {
00045     init();
00046     assign(line1, line2, line3, forceParsing);
00047 }
00048 //------------------------------------------------------------------------------
00049 
00050 tle_node::tle_node(const std::string& line1, const std::string& line2, bool forceParsing)
00051 {
00052     init();
00053     assign(line1, line2, forceParsing);
00054 }
00055 //------------------------------------------------------------------------------
00056 
00057 tle_node::~tle_node()
00058 {
00059     free();
00060 }
00061 //------------------------------------------------------------------------------
00062 
00063 void tle_node::init()
00064 {
00065     m_line1 = m_line2 = m_line3 = m_satName = m_satNumber = m_designator = NULL;
00066     m_dn = m_d2n = m_Bstar = m_i = m_Omega = m_omega = m_M = m_n = m_e = NULL;
00067     m_classification = m_ephemerisType = NULL;
00068     m_elementNumber = m_revolutionNumber = NULL;
00069     m_date = NULL;
00070 }
00071 //------------------------------------------------------------------------------
00072 
00073 void tle_node::free()
00074 {
00075     if (m_line1) delete m_line1;
00076     if (m_line2) delete m_line2;
00077     if (m_line3) delete m_line3;
00078     if (m_satName) delete m_satName;
00079     if (m_satNumber) delete m_satNumber;
00080     if (m_designator) delete m_designator;
00081     if (m_dn) delete m_dn;
00082     if (m_d2n) delete m_d2n;
00083     if (m_n) delete m_n;
00084     if (m_Bstar) delete m_Bstar;
00085     if (m_i) delete m_i;
00086     if (m_Omega) delete m_Omega;
00087     if (m_M) delete m_M;
00088     if (m_omega) delete m_omega;
00089     if (m_e) delete m_e;
00090     if (m_classification) delete m_classification;
00091     if (m_date) delete m_date;
00092     if (m_ephemerisType) delete m_ephemerisType;
00093     if (m_elementNumber) delete m_elementNumber;
00094     if (m_revolutionNumber) delete m_revolutionNumber;
00095     init();
00096 }
00097 //------------------------------------------------------------------------------
00098 
00099 void tle_node::assign(const std::string& line1, const std::string& line2, const std::string& line3, bool forceParsing)
00100 {
00101     // Check checksums
00102     check_line(line2);
00103     check_line(line3);
00104     // Assign
00105     free();
00106     m_line1 = new std::string(line1);
00107     m_line2 = new std::string(line2);
00108     m_line3 = new std::string(line3);
00109     // Parse
00110     if (forceParsing) parse_all();
00111 }
00112 //------------------------------------------------------------------------------
00113 
00114 void tle_node::assign(const std::string& line1, const std::string& line2, bool forceParsing)
00115 {
00116     // Check checksums
00117     check_line(line1);
00118     check_line(line2);
00119     // Assign
00120     free();
00121     m_line2 = new std::string(line1);
00122     m_line3 = new std::string(line2);
00123     // Parse
00124     if (forceParsing) parse_all();
00125 }
00126 //------------------------------------------------------------------------------
00127 
00128 void tle_node::parse_all()
00129 {
00130     n(); dn(); d2n(); i(); Omega(); omega(); M(); e(); BSTAR();
00131     sat_number(); sat_name(); designator();
00132     classification(); ephemeris_type();
00133     element_number(); revolution_number();
00134     precise_epoch();
00135 }
00136 //------------------------------------------------------------------------------
00137 
00138 void tle_node::check_line(const std::string &str) const
00139 {
00140     if (str.length() < CHECKSUM_INDEX + 1)
00141         throw tle_too_short_string(str);
00142 
00143     int expected_checksum = checksum(str.substr(0, CHECKSUM_INDEX));
00144     int actual_checksum = atoi(str.substr(CHECKSUM_INDEX, 1).c_str());
00145     if (expected_checksum != actual_checksum)
00146         throw tle_checksum_error(str, expected_checksum, actual_checksum);
00147 }
00148 //------------------------------------------------------------------------------
00149 
00150 std::string &tle_node::sat_number()
00151 {
00152     if (!m_satNumber)
00153     {
00154         // Try to obtain the satellite number from the second line...
00155         m_satNumber = new std::string(trim(parseString(m_line2, 2, 5)));
00156         // or from third line
00157         if (*m_satNumber == "")
00158         {
00159             delete m_satNumber;
00160             m_satNumber = new std::string(trim(parseString(m_line3, 2, 5)));
00161         }
00162     }
00163     return *m_satNumber;
00164 }
00165 //------------------------------------------------------------------------------
00166 
00167 std::string &tle_node::sat_name()
00168 {
00169     if (!m_satName)
00170     {
00171         if (m_line1)
00172         {
00173             std::size_t l = m_line1->length();
00174             if (l > 24) l = 24;
00175             m_satName = new std::string(trim(parseString(m_line1, 0, l)));
00176         }
00177         else
00178             m_satName = new std::string("");
00179     }
00180 
00181     return *m_satName;
00182 }
00183 //------------------------------------------------------------------------------
00184 
00185 std::string &tle_node::designator()
00186 {
00187     if (!m_designator)
00188     {
00189         m_designator = new std::string(trim(parseString(m_line2, 9, 8)));
00190     }
00191 
00192     return *m_designator;
00193 }
00194 //------------------------------------------------------------------------------
00195 
00196 double &tle_node::n()
00197 {
00198     if (!m_n)
00199     {
00200         m_n = new double(parseDouble(m_line3, 52, 11));
00201     }
00202 
00203     return *m_n;
00204 }
00205 //------------------------------------------------------------------------------
00206 
00207 double &tle_node::dn()
00208 {
00209     if (!m_dn)
00210     {
00211         m_dn = new double(parseDouble(m_line2, 33, 10));
00212     }
00213 
00214     return *m_dn;
00215 }
00216 //------------------------------------------------------------------------------
00217 
00218 double &tle_node::d2n()
00219 {
00220     if (!m_d2n)
00221     {
00222         m_d2n = new double(parseDouble(m_line2, 44, 8, true));
00223     }
00224 
00225     return *m_d2n;
00226 }
00227 //------------------------------------------------------------------------------
00228 
00229 double &tle_node::i()
00230 {
00231     if (!m_i)
00232     {
00233         m_i = new double(parseDouble(m_line3, 8, 8));
00234     }
00235 
00236     return *m_i;
00237 }
00238 //------------------------------------------------------------------------------
00239 
00240 double &tle_node::Omega()
00241 {
00242     if (!m_Omega)
00243     {
00244         m_Omega = new double(parseDouble(m_line3, 17, 8));
00245     }
00246 
00247     return *m_Omega;
00248 }
00249 //------------------------------------------------------------------------------
00250 
00251 double &tle_node::omega()
00252 {
00253     if (!m_omega)
00254     {
00255         m_omega = new double(parseDouble(m_line3, 34, 8));
00256     }
00257 
00258     return *m_omega;
00259 }
00260 //------------------------------------------------------------------------------
00261 
00262 double &tle_node::M()
00263 {
00264     if (!m_M)
00265     {
00266         m_M = new double(parseDouble(m_line3, 43, 8));
00267     }
00268 
00269     return *m_M;
00270 }
00271 //------------------------------------------------------------------------------
00272 
00273 double &tle_node::BSTAR()
00274 {
00275     if (!m_Bstar)
00276     {
00277         m_Bstar = new double(parseDouble(m_line2, 53, 8, true));
00278     }
00279 
00280     return *m_Bstar;
00281 }
00282 //------------------------------------------------------------------------------
00283 
00284 double &tle_node::e()
00285 {
00286     if (!m_e)
00287     {
00288         m_e = new double(parseDouble(m_line3, 26, 7, true));
00289     }
00290 
00291     return *m_e;
00292 }
00293 //------------------------------------------------------------------------------
00294 
00295 char &tle_node::classification()
00296 {
00297     if (!m_classification)
00298     {
00299         m_classification = new char(parseChar(m_line2, 7));
00300     }
00301 
00302     return *m_classification;
00303 }
00304 //------------------------------------------------------------------------------
00305 
00306 char &tle_node::ephemeris_type()
00307 {
00308     if (!m_ephemerisType)
00309     {
00310         m_ephemerisType = new char(parseChar(m_line2, 62));
00311     }
00312 
00313     return *m_ephemerisType;
00314 }
00315 //------------------------------------------------------------------------------
00316 
00317 int &tle_node::element_number()
00318 {
00319     if (!m_elementNumber)
00320     {
00321         m_elementNumber = new int(parseInt(m_line2, 64, 4));
00322     }
00323 
00324     return *m_elementNumber;
00325 }
00326 //------------------------------------------------------------------------------
00327 
00328 int &tle_node::revolution_number()
00329 {
00330     if (!m_revolutionNumber)
00331     {
00332         m_revolutionNumber = new int(parseInt(m_line3, 63, 5));
00333     }
00334 
00335     return *m_revolutionNumber;
00336 }
00337 //------------------------------------------------------------------------------
00338 
00339 double &tle_node::precise_epoch()
00340 {
00341     if (!m_date)
00342     {
00343         std::string date = parseString(m_line2, 18, 14);
00344         m_date = new double(string2date(date));
00345     }
00346 
00347     return *m_date;
00348 }
00349 //------------------------------------------------------------------------------
00350 
00351 std::time_t tle_node::epoch()
00352 {
00353     return static_cast<std::time_t>(precise_epoch());
00354 }
00355 //------------------------------------------------------------------------------
00356 
00357 std::string tle_node::first_string()
00358 {
00359     std::string res = sat_name();
00360     while (res.length() < 24) res += " ";
00361     return res;
00362 }
00363 //------------------------------------------------------------------------------
00364 
00365 std::string tle_node::second_string()
00366 {
00367     std::string res = "1 ";
00368     res += string2string(sat_number(), 5);
00369     const char cl = classification();
00370     res += (isprint(cl) ? std::string(1, cl) : " ") + " ";
00371     res += string2string(designator(), 8) + " ";
00372     res += date2string(precise_epoch(), 14) + " ";
00373     res += double2string(dn(), 10, 8, false, false, false) + " ";
00374     res += double2string(d2n(), 8, 3, true, true, false) + " ";
00375     res += double2string(BSTAR(), 8, 3, true, true, false) + " ";
00376     const char eph = ephemeris_type();
00377     res += (isprint(eph) ? std::string(1, eph) : " ") + " ";
00378     res += int2string(element_number(), 4, false);
00379 
00380     // Checksum
00381     int sum = checksum(res);
00382     res += int2string(sum, 1);
00383 
00384     return res;
00385 }
00386 //------------------------------------------------------------------------------
00387 
00388 std::string tle_node::third_string()
00389 {
00390     std::string res = "2 ";
00391     res += string2string(sat_number(), 5) + " ";
00392     res += double2string(i(), 8, 4, false, false, false) + " ";
00393     res += double2string(Omega(), 8, 4, false, false, false) + " ";
00394     res += double2string(e(), 7, 7, false, true, false) + " ";
00395     res += double2string(omega(), 8, 4, false, false, false) + " ";
00396     res += double2string(M(), 8, 4, false, false, false) + " ";
00397     res += double2string(n(), 11, 8, false, false, false);
00398     res += int2string(revolution_number(), 5, false);
00399 
00400     // Checksum
00401     int sum = checksum(res);
00402     res += int2string(sum, 1);
00403 
00404     return res;
00405 }
00406 //------------------------------------------------------------------------------
 All Classes Namespaces Files Functions Variables Defines