GPSWeekSecond.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: GPSWeekSecond.cpp 3178 2012-06-29 16:32:18Z snelsen $"
00002 
00003 
00004 
00005 //============================================================================
00006 //
00007 //  This file is part of GPSTk, the GPS Toolkit.
00008 //
00009 //  The GPSTk is free software; you can redistribute it and/or modify
00010 //  it under the terms of the GNU Lesser General Public License as published
00011 //  by the Free Software Foundation; either version 2.1 of the License, or
00012 //  any later version.
00013 //
00014 //  The GPSTk is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 //  GNU Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with GPSTk; if not, write to the Free Software Foundation,
00021 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
00022 //  
00023 //  Copyright 2004, The University of Texas at Austin
00024 //
00025 //============================================================================
00026 
00027 //============================================================================
00028 //
00029 //This software developed by Applied Research Laboratories at the University of
00030 //Texas at Austin, under contract to an agency or agencies within the U.S. 
00031 //Department of Defense. The U.S. Government retains all rights to use,
00032 //duplicate, distribute, disclose, or release this software. 
00033 //
00034 //Pursuant to DoD Directive 523024 
00035 //
00036 // DISTRIBUTION STATEMENT A: This software has been approved for public 
00037 //                           release, distribution is unlimited.
00038 //
00039 //=============================================================================
00040 
00041 #include "GPSWeekSecond.hpp"
00042 #include "TimeConstants.hpp"
00043 
00044 namespace gpstk
00045 {
00046    GPSWeekSecond& 
00047    GPSWeekSecond::operator=( const GPSWeekSecond& right )
00048       throw()
00049    {
00050       GPSWeek::operator=(right);
00051       sow = right.sow;
00052       return *this;
00053    }
00054    
00055    CommonTime GPSWeekSecond::convertToCommonTime() const
00056       throw( gpstk::InvalidRequest )
00057    {
00058       try
00059       {
00060         //int dow = static_cast<int>( sow * DAY_PER_SEC ); // Appears to have rounding issues on 32-bit platforms
00061          int dow = static_cast<int>( sow / SEC_PER_DAY );
00062          int jday = GPS_EPOCH_JDAY + ( 7 * week ) + dow;
00063          double sod(  sow - SEC_PER_DAY * dow );
00064          CommonTime ct;
00065          return ct.set( jday, 
00066                         static_cast<long>( sod ),
00067                         sod - static_cast<long>( sod ),
00068                         timeSystem );
00069       }
00070       catch (InvalidParameter& ip)
00071       {
00072          InvalidRequest ir(ip);
00073          GPSTK_THROW(ir);
00074       }
00075    }
00076    
00077    void GPSWeekSecond::convertFromCommonTime( const CommonTime& ct )
00078       throw( gpstk::InvalidRequest )
00079    {
00081       static const CommonTime MIN_CT = GPSWeekSecond(0,0.,TimeSystem::Any);
00082 
00083       if (ct < MIN_CT)
00084       {
00085          InvalidRequest ir("Unable to convert CommonTime to GPSWeekSecond.");
00086          GPSTK_THROW(ir);
00087       }
00088       
00089       long day, sod;
00090       double fsod;
00091       ct.get( day, sod, fsod, timeSystem );
00092 
00093          // find the number of days since the beginning of the GPS Epoch
00094       day -= GPS_EPOCH_JDAY;
00095          // find out how many weeks that is
00096       week = static_cast<int>( day / 7 );
00097          // find out what the day of week is
00098       day %= 7;
00099 
00100       sow = static_cast<double>( day * SEC_PER_DAY + sod ) + fsod;
00101    }
00102    
00103    std::string GPSWeekSecond::printf( const std::string& fmt ) const
00104       throw( gpstk::StringUtils::StringException )
00105    {
00106       try
00107       {
00108          using gpstk::StringUtils::formattedPrint;
00109          
00110          std::string rv = GPSWeek::printf( fmt );
00111 
00112          rv = formattedPrint( rv, getFormatPrefixInt() + "w",
00113                               "wu", getDayOfWeek() );
00114          rv = formattedPrint( rv, getFormatPrefixFloat() + "g",
00115                               "gf", sow);
00116          rv = formattedPrint( rv, getFormatPrefixInt() + "P",
00117                               "Ps", timeSystem.asString().c_str() );
00118          return rv;
00119       }
00120       catch( gpstk::StringUtils::StringException& exc )
00121       {
00122          GPSTK_RETHROW( exc );
00123       }
00124    }
00125       
00126    std::string GPSWeekSecond::printError( const std::string& fmt ) const
00127       throw( gpstk::StringUtils::StringException )
00128    {
00129       try
00130       {
00131          using gpstk::StringUtils::formattedPrint;
00132          
00133          std::string rv = GPSWeek::printError( fmt );
00134          
00135          rv = formattedPrint( rv, getFormatPrefixInt() + "w",
00136                               "ws", getError().c_str() );
00137          rv = formattedPrint( rv, getFormatPrefixFloat() + "g",
00138                               "gs", getError().c_str() );
00139          rv = formattedPrint( rv, getFormatPrefixInt() + "P",
00140                               "Ps", getError().c_str() );               
00141          return rv;
00142       }
00143       catch( gpstk::StringUtils::StringException& exc )
00144       {
00145          GPSTK_RETHROW( exc );
00146       }
00147    }
00148    
00149    bool GPSWeekSecond::setFromInfo( const IdToValue& info )
00150       throw()
00151    {
00152       using namespace gpstk::StringUtils;
00153 
00154       GPSWeek::setFromInfo( info );
00155 
00156       for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
00157       {
00158             // based on the character, we know what to do...
00159          switch ( i->first ) 
00160          {
00161             case 'w':
00162                sow = static_cast<double>( asInt( i->second ) ) * SEC_PER_DAY;
00163                break;
00164             case 'g':
00165                sow = asDouble( i->second );
00166                break;
00167             case 'P':
00168                     timeSystem = static_cast<TimeSystem>(asInt( i->second ));
00169                break;
00170             default:
00171                   // do nothing
00172                break;
00173          };
00174          
00175       } // end of for loop
00176       
00177       return true;
00178    }
00179 
00180    bool GPSWeekSecond::isValid() const
00181       throw()
00182    {
00183       return ( GPSWeek::isValid() &&
00184                sow < FULLWEEK );
00185    }
00186 
00187    void GPSWeekSecond::reset()
00188       throw()
00189    {
00190       GPSWeek::reset();
00191       sow = 0.0;
00192    }
00193 
00194    bool 
00195    GPSWeekSecond::operator==( const GPSWeekSecond& right ) const
00196       throw()
00197    {
00198       return ( GPSWeek::operator==(right) &&
00199                sow == right.sow );
00200    }
00201 
00202    bool 
00203    GPSWeekSecond::operator!=( const GPSWeekSecond& right ) const
00204       throw()
00205    {
00206       return ( !operator==( right ) );
00207    }
00208 
00209    bool 
00210    GPSWeekSecond::operator<( const GPSWeekSecond& right ) const
00211       throw()
00212    {
00213       if( GPSWeek::operator<(right) )
00214       {
00215          return true;
00216       }
00217       if( GPSWeek::operator>(right) )
00218       {
00219          return false;
00220       }
00221       if( sow < right.sow )
00222       {
00223          return true;
00224       }
00225       return false;
00226    }
00227 
00228    bool 
00229    GPSWeekSecond::operator>( const GPSWeekSecond& right ) const
00230       throw()
00231    {
00232       return ( !operator<=( right ) );
00233    }
00234 
00235    bool 
00236    GPSWeekSecond::operator<=( const GPSWeekSecond& right ) const
00237       throw()
00238    {
00239       return ( operator<( right ) || operator==( right ) );
00240    }
00241 
00242    bool 
00243    GPSWeekSecond::operator>=( const GPSWeekSecond& right ) const
00244       throw()
00245    {
00246       return ( !operator<( right ) );
00247    }
00248 
00249 } // namespace

Generated on Sun May 19 03:31:08 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1