SatDataReader.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: SatDataReader.cpp 1718 2009-02-22 11:19:26Z architest $"
00002 
00008 //============================================================================
00009 //
00010 //  This file is part of GPSTk, the GPS Toolkit.
00011 //
00012 //  The GPSTk is free software; you can redistribute it and/or modify
00013 //  it under the terms of the GNU Lesser General Public License as published
00014 //  by the Free Software Foundation; either version 2.1 of the License, or
00015 //  any later version.
00016 //
00017 //  The GPSTk is distributed in the hope that it will be useful,
00018 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //  GNU Lesser General Public License for more details.
00021 //
00022 //  You should have received a copy of the GNU Lesser General Public
00023 //  License along with GPSTk; if not, write to the Free Software Foundation,
00024 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 //
00026 //  Dagoberto Salazar - gAGE ( http://www.gage.es ). 2007, 2009
00027 //
00028 //============================================================================
00029 
00030 
00031 
00032 
00033 #include "SatDataReader.hpp"
00034 
00035 
00036 namespace gpstk
00037 {
00038 
00039       // Method to store satellite data in this class' data map
00040    void SatDataReader::loadData(void)
00041       throw(FFStreamError, gpstk::StringUtils::StringException)
00042    {
00043 
00044          // Do this until end-of-file reached or something else happens
00045       while(1)
00046       {
00047          try
00048          {
00049             std::string line;
00050 
00051             formattedGetLine(line, true);
00052 
00053                // If line is too long, we throw an exception
00054             if (line.size()>255)
00055             {
00056                FFStreamError e("Line too long");
00057                GPSTK_THROW(e);
00058             }
00059 
00060                // Let's find and strip comments, wherever they are
00061             if( StringUtils::firstWord(line)[0] == '#' )
00062             {
00063                formattedGetLine(line, true);
00064             }
00065 
00066             std::string::size_type idx = line.find('#');
00067             if( !(idx == std::string::npos) )
00068             {
00069                line = line.substr(0, idx);
00070             }
00071 
00072                // We erase the header (first line)
00073             if( StringUtils::firstWord(line) == "Launch" )
00074             {
00075                formattedGetLine(line, true);
00076             }
00077 
00078                // Remove trailing and leading blanks
00079             line = StringUtils::strip(line);
00080 
00081                // Skip blank lines
00082             if (line.size()==0)
00083             {
00084                continue;
00085             }
00086 
00087                // Let's start to get data out of file
00088                // Launch date
00089             string ldate(StringUtils::stripFirstWord(line));
00090                // Deactivation date
00091             string ddate(StringUtils::stripFirstWord(line));
00092                // GPS number
00093             string gnumber(StringUtils::stripFirstWord(line));
00094                // PRN number
00095             string prn(StringUtils::stripFirstWord(line));
00096                // Block tipe
00097             string block(StringUtils::upperCase(
00098                StringUtils::stripFirstWord(line)));
00099 
00100                // Get satellite id. If it doesn't fit GPS or Glonass, it is
00101                // marked as unknown
00102             SatID sat(StringUtils::asInt(prn),SatID::systemUnknown);
00103                // Let's identify satellite system
00104             if(block[0] == 'I')
00105             {
00106                sat.system = SatID::systemGPS;
00107             }
00108             else
00109             { 
00110                if (block.substr(0, 3) == "GLO")
00111                {
00112                   sat.system = SatID::systemGlonass;
00113                }
00114             }
00115 
00116                // Declare the structure to store data
00117             SatDataReader::svData data;
00118 
00119             data.block = block;
00120             data.gpsNumber = StringUtils::asInt(gnumber);
00121 
00122                // Get launch date in a proper format
00123             if(ldate[0] != '0')
00124             {
00125                 ldate = StringUtils::translate(ldate, "-", " ");
00126                 data.launchDate.setToString(ldate, "%Y %m %d");
00127             }
00128 
00129                // Get deactivation date in a proper format
00130             if(ddate[0] != '0')
00131             {
00132                 ddate = StringUtils::translate(ddate, "-", " ");
00133                 data.deactivationDate.setToString(ddate, "%Y %m %d");
00134             }
00135 
00136 
00137                // Insert data in data map
00138             setData(sat, data);
00139 
00140          }  // End of try block
00141          catch (EndOfFile& e)
00142          {
00143                // Close this data stream
00144             (*this).close();
00145 
00146             return;
00147          }
00148          catch (...)
00149          {
00150                // Close this data stream
00151             (*this).close();
00152 
00153             return;
00154          }
00155 
00156       } // End of while(1)
00157 
00158    }  // End of method 'SatDataReader::loadData()'
00159 
00160 
00161 
00162       // Method to open AND load satellite data file.
00163    void SatDataReader::open(const char* fn)
00164    {
00165 
00166          // We need to be sure current data stream is closed
00167       (*this).close();
00168 
00169          // Open data stream
00170       FFTextStream::open(fn, std::ios::in);
00171 
00172          // Load data
00173       loadData();
00174 
00175       return;
00176 
00177    }  // End of method 'SatDataReader::open()'
00178 
00179 
00180       // Method to open AND load satellite data file.
00181    void SatDataReader::open(const string& fn)
00182    {
00183 
00184          // We need to be sure current data stream is closed
00185       (*this).close();
00186 
00187          // Open data stream
00188       FFTextStream::open(fn.c_str(), std::ios::in);
00189 
00190          // Load data
00191       loadData();
00192 
00193       return;
00194 
00195    }  // End of method 'SatDataReader::open()'
00196 
00197 
00198 
00199       /* Method to get the block type of a given SV at a given epoch.
00200        *
00201        * @param sat   Satellite ID.
00202        * @param epoch Epoch of interest.
00203        *
00204        * @return String containing satellite's block. If satellite is
00205        * not found or epoch is out of proper launch/deactivation bounds,
00206        * this method will return an empty string.
00207        */
00208    string SatDataReader::getBlock(const SatID& sat,
00209                                   const DayTime& epoch) const
00210    {
00211 
00212          // Create a pair of range belonging to this SatID
00213       pair<satDataIt, satDataIt> range = SatelliteData.equal_range(sat);
00214 
00215          // If SatID is not found, an empty string is returned
00216       if(range.first == range.second)
00217       {
00218          return "";
00219       }
00220 
00221          // Declare an iterator to travel in this range
00222       satDataIt iter(range.first);
00223 
00224          // If this epoch is before launch date, return an empty string
00225       if( (*iter).second.launchDate > epoch )
00226       {
00227          return "";
00228       }
00229 
00230          // Increment iterator "iter" if we are not yet at proper epoch range
00231       while( (*iter).second.deactivationDate < epoch )
00232       {
00233          ++iter;
00234       }
00235 
00236          // Test if epoch is after corresponding launch date
00237       if( (*iter).second.launchDate > epoch )
00238       {
00239          return "";
00240       }
00241 
00242 
00243       return ((*iter).second.block);
00244 
00245    }  // End of method 'SatDataReader::getBlock()'
00246 
00247 
00248 
00249       /* Method to get the GPS number of a given SV at a given epoch.
00250        *
00251        * @param sat   Satellite ID.
00252        * @param epoch Epoch of interest.
00253        *
00254        * @return Integer containing satellite's block. If satellite is
00255        * not found or epoch is out of proper launch/deactivation bounds,
00256        * this method will return -1.
00257        */
00258    int SatDataReader::getGPSNumber(const SatID& sat,
00259                                    const DayTime& epoch) const
00260    {
00261 
00262          // Create a pair of range belonging to this SatID
00263       pair<satDataIt, satDataIt> range = SatelliteData.equal_range(sat);
00264 
00265          // If SatID is not found, -1 is returned
00266       if(range.first == range.second)
00267       {
00268          return -1;
00269       }
00270 
00271          // Declare an iterator to travel in this range
00272       satDataIt iter(range.first);
00273 
00274          // If this epoch is before launch date, return -1
00275       if( (*iter).second.launchDate > epoch )
00276       {
00277          return -1;
00278       }
00279 
00280          // Increment iterator "iter" if we are not yet at proper epoch range
00281       while( (*iter).second.deactivationDate < epoch )
00282       {
00283          ++iter;
00284       }
00285 
00286          // Test if epoch is after corresponding launch date
00287       if( (*iter).second.launchDate > epoch )
00288       {
00289          return -1;
00290       }
00291 
00292 
00293       return ((*iter).second.gpsNumber);
00294 
00295    }  // End of method 'SatDataReader::getGPSNumber()'
00296 
00297 
00298 
00299       /* Method to get the launch date of a given SV.
00300        *
00301        * @param sat   Satellite ID.
00302        * @param epoch Epoch of interest.
00303        *
00304        * @return DayTime object containing satellite's launch date. If
00305        * satellite is not found or epoch is out of proper launch/deactivation
00306        * bounds, this method will return DayTime::END_OF_TIME.
00307        */
00308    DayTime SatDataReader::getLaunchDate(const SatID& sat,
00309                                         const DayTime& epoch) const
00310    {
00311 
00312          // Create a pair of range belonging to this SatID
00313       pair<satDataIt, satDataIt> range = SatelliteData.equal_range(sat);
00314 
00315          // If SatID is not found, DayTime::END_OF_TIME is returned
00316       if(range.first == range.second)
00317       {
00318          return DayTime::END_OF_TIME;
00319       }
00320 
00321          // Declare an iterator to travel in this range
00322       satDataIt iter(range.first);
00323 
00324          // If this epoch is before launch date, return DayTime::END_OF_TIME
00325       if( (*iter).second.launchDate > epoch )
00326       {
00327          return DayTime::END_OF_TIME;
00328       }
00329 
00330          // Increment iterator "iter" if we are not yet at proper epoch range
00331       while( (*iter).second.deactivationDate < epoch )
00332       {
00333          ++iter;
00334       }
00335 
00336          // Test if epoch is after corresponding launch date
00337       if( (*iter).second.launchDate > epoch )
00338       {
00339          return DayTime::END_OF_TIME;
00340       }
00341 
00342       return ((*iter).second.launchDate);
00343 
00344    }  // End of method 'SatDataReader::getLaunchDate()'
00345 
00346 
00347 
00348       /* Method to get the deactivation date of a given SV.
00349        *
00350        * @param sat   Satellite ID.
00351        * @param epoch Epoch of interest.
00352        *
00353        * @return DayTime object containing satellite's deactivation date. If
00354        * satellite is not found, epoch is out of proper launch/deactivation
00355        * bounds or satellite is still active, this method will return
00356        * DayTime::BEGINNING_OF_TIME.
00357        */
00358    DayTime SatDataReader::getDeactivationDate(const SatID& sat,
00359                                               const DayTime& epoch) const
00360    {
00361 
00362          // Create a pair of range belonging to this SatID
00363       pair<satDataIt, satDataIt> range = SatelliteData.equal_range(sat);
00364 
00365          // If SatID is not found, DayTime::BEGINNING_OF_TIME is returned
00366       if(range.first == range.second)
00367       {
00368          return DayTime::BEGINNING_OF_TIME;
00369       }
00370 
00371          // Declare an iterator to travel in this range
00372       satDataIt iter(range.first);
00373 
00374          // If this epoch is before launch date, return BEGINNING_OF_TIME
00375       if( (*iter).second.launchDate > epoch )
00376       {
00377          return DayTime::BEGINNING_OF_TIME;
00378       }
00379 
00380          // Increment iterator "iter" if we are not yet at proper epoch range
00381       while( (*iter).second.deactivationDate < epoch )
00382       {
00383          ++iter;
00384       }
00385 
00386          // Test if epoch is after corresponding launch date
00387       if( (*iter).second.launchDate > epoch )
00388       {
00389          return DayTime::BEGINNING_OF_TIME;
00390       }
00391 
00392       return ((*iter).second.deactivationDate);
00393 
00394    }  // End of method 'SatDataReader::getDeactivationDate()'
00395 
00396 
00397 
00398 }  // End of namespace gpstk

Generated on Thu Jul 29 03:30:55 2010 for GPS ToolKit Software Library by  doxygen 1.3.9.1