MJD.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: MJD.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 <cmath>
00042 #include "MJD.hpp"
00043 #include "TimeConstants.hpp"
00044 
00045 namespace gpstk
00046 {
00047    MJD& MJD::operator=( const MJD& right )
00048       throw()
00049    {
00050       mjd = right.mjd;
00051       timeSystem = right.timeSystem;
00052       return *this;
00053    }
00054    
00055    CommonTime MJD::convertToCommonTime() const
00056       throw( gpstk::InvalidRequest )
00057    {
00058       try
00059       {
00060             // convert to Julian Day
00061          long double tmp( mjd + MJD_JDAY );
00062             // get the whole number of days
00063          long jday( static_cast<long>( tmp ) );
00064             // tmp now holds the partial days
00065          tmp -= static_cast<long>( tmp );
00066             // convert tmp to seconds of day
00067          tmp *= SEC_PER_DAY;
00068             // Lose excess precision in 'tmp' because it may cause rounding
00069             // problems in the conversion to CommonTime.
00070          double dTmp( static_cast<double>( tmp ) );
00071       
00072          CommonTime ct;
00073          return ct.set( jday,
00074                         static_cast<long>( dTmp ),
00075                         dTmp - static_cast<long>( dTmp ),
00076                         timeSystem );
00077       }
00078       catch (InvalidParameter& ip)
00079       {
00080          InvalidRequest ir(ip);
00081          GPSTK_THROW(ip);
00082       }
00083       
00084    }
00085    
00086    void MJD::convertFromCommonTime( const CommonTime& ct )
00087       throw()
00088    {
00089       long jday, sod;
00090       double fsod;
00091       ct.get( jday, sod, fsod, timeSystem );
00092      
00093       mjd =  static_cast<long double>( jday - MJD_JDAY ) +
00094            (  static_cast<long double>( sod ) 
00095             + static_cast<long double>( fsod ) ) * DAY_PER_SEC;
00096    }
00097    
00098    std::string MJD::printf( const std::string& fmt ) const
00099       throw( gpstk::StringUtils::StringException )
00100    {
00101       try
00102       {
00103          using gpstk::StringUtils::formattedPrint;
00104          std::string rv( fmt );
00105          
00106          rv = formattedPrint( rv, getFormatPrefixFloat() + "Q",
00107                               "QLf", mjd );
00108          rv = formattedPrint( rv, getFormatPrefixInt() + "P",
00109                               "Ps", timeSystem.asString().c_str() );
00110          return rv;
00111       }
00112       catch( gpstk::StringUtils::StringException& se )
00113       {
00114          GPSTK_RETHROW( se );
00115       }
00116    }
00117    
00118    std::string MJD::printError( const std::string& fmt ) const
00119       throw( gpstk::StringUtils::StringException )
00120    {
00121       try
00122       {
00123          using gpstk::StringUtils::formattedPrint;
00124          std::string rv( fmt );
00125          
00126          rv = formattedPrint( rv, getFormatPrefixFloat() + "Q",
00127                               "Qs", getError().c_str() );
00128          rv = formattedPrint( rv, getFormatPrefixInt() + "P",
00129                               "Ps", getError().c_str() );
00130          return rv;         
00131       }
00132       catch( gpstk::StringUtils::StringException& se )
00133       {
00134          GPSTK_RETHROW( se );
00135       }
00136    }
00137    
00138    bool MJD::setFromInfo( const IdToValue& info )
00139       throw()
00140    {
00141       using namespace gpstk::StringUtils;
00142       
00143       for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
00144       {
00145          switch( i->first )
00146          {
00147             case 'Q':
00148                mjd = asLongDouble( i->second );
00149                break;
00150 
00151             case 'P':
00152                timeSystem = static_cast<TimeSystem>(asInt( i->second ));
00153                break;
00154 
00155             default:
00156                   // do nothing
00157                break;
00158          };
00159       }      
00160 
00161       return true;
00162    }
00163    
00164    bool MJD::isValid() const
00165       throw()
00166    {
00167       MJD temp;
00168       temp.convertFromCommonTime( convertToCommonTime() );
00169       if( *this == temp )
00170       {
00171          return true;
00172       }
00173       return false;
00174    }
00175    
00176    void MJD::reset()
00177       throw()
00178    {
00179       mjd = 0.0;
00180       timeSystem = TimeSystem::Unknown;
00181    }
00182 
00183    bool MJD::operator==( const MJD& right ) const
00184       throw()
00185    {
00187       if ((timeSystem != TimeSystem::Any &&
00188            right.timeSystem != TimeSystem::Any) &&
00189           timeSystem != right.timeSystem)
00190          return false;
00191 
00192       if( fabs(mjd - right.mjd) < CommonTime::eps )
00193       {
00194          return true;
00195       }
00196       return false;
00197    }
00198 
00199    bool MJD::operator!=( const MJD& right ) const
00200       throw()
00201    {
00202       return ( !operator==( right ) );
00203    }
00204 
00205    bool MJD::operator<( const MJD& right ) const
00206       throw( gpstk::InvalidRequest )
00207    {
00209       if ((timeSystem != TimeSystem::Any &&
00210            right.timeSystem != TimeSystem::Any) &&
00211           timeSystem != right.timeSystem)
00212       {
00213          gpstk::InvalidRequest ir("CommonTime objects not in same time system, cannot be compared");
00214          GPSTK_THROW(ir);
00215       }
00216 
00217       if( mjd < right.mjd )
00218       {
00219          return true;
00220       }
00221       return false;
00222    }
00223 
00224    bool MJD::operator>( const MJD& right ) const
00225       throw( gpstk::InvalidRequest )
00226    {
00227       return ( !operator<=( right ) );
00228    }
00229 
00230    bool MJD::operator<=( const MJD& right ) const
00231       throw( gpstk::InvalidRequest )
00232    {
00233       return ( operator<(  right ) ||
00234                operator==( right )   );
00235    }
00236 
00237    bool MJD::operator>=( const MJD& right ) const
00238       throw( gpstk::InvalidRequest )
00239    {
00240       return ( !operator<( right ) );
00241    }
00242 
00243 } // namespace

Generated on Tue May 21 03:31:11 2013 for GPS ToolKit Software Library by  doxygen 1.3.9.1