MJD.cpp
Go to the documentation of this file.
1 //==============================================================================
2 //
3 // This file is part of GNSSTk, the ARL:UT GNSS Toolkit.
4 //
5 // The GNSSTk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation; either version 3.0 of the License, or
8 // any later version.
9 //
10 // The GNSSTk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with GNSSTk; if not, write to the Free Software Foundation,
17 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 // This software was developed by Applied Research Laboratories at the
20 // University of Texas at Austin.
21 // Copyright 2004-2022, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 // This software was developed by Applied Research Laboratories at the
28 // University of Texas at Austin, under contract to an agency or agencies
29 // within the U.S. Department of Defense. The U.S. Government retains all
30 // rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 // Pursuant to DoD Directive 523024
33 //
34 // DISTRIBUTION STATEMENT A: This software has been approved for public
35 // release, distribution is unlimited.
36 //
37 //==============================================================================
38 
40 
41 #include <cmath>
42 #include "MJD.hpp"
43 #include "TimeConstants.hpp"
44 
45 namespace gnsstk
46 {
47  MJD& MJD::operator=( const MJD& right )
48  {
49  mjd = right.mjd;
50  timeSystem = right.timeSystem;
51  return *this;
52  }
53 
55  {
56  try
57  {
58  // convert to Julian Day
59  long double tmp( mjd + MJD_JDAY );
60  // get the whole number of days
61  long jday( static_cast<long>( tmp ) );
62  // tmp now holds the partial days
63  tmp -= static_cast<long>( tmp );
64  // convert tmp to seconds of day
65  tmp *= SEC_PER_DAY;
66  // Lose excess precision in 'tmp' because it may cause rounding
67  // problems in the conversion to CommonTime.
68  double dTmp( static_cast<double>( tmp ) );
69 
70  CommonTime ct;
71  return ct.set( jday,
72  static_cast<long>( dTmp ),
73  dTmp - static_cast<long>( dTmp ),
74  timeSystem );
75  }
76  catch (InvalidParameter& ip)
77  {
78  InvalidRequest ir(ip);
79  GNSSTK_THROW(ip);
80  }
81 
82  }
83 
85  {
86  long jday, sod;
87  double fsod;
88  ct.get( jday, sod, fsod, timeSystem );
89 
90  mjd = static_cast<long double>( jday - MJD_JDAY ) +
91  ( static_cast<long double>( sod )
92  + static_cast<long double>( fsod ) ) * DAY_PER_SEC;
93  }
94 
95  std::string MJD::printf( const std::string& fmt ) const
96  {
97  try
98  {
100  std::string rv( fmt );
101 
102  rv = formattedPrint( rv, getFormatPrefixFloat() + "Q",
103  "QLf", mjd );
104  rv = formattedPrint( rv, getFormatPrefixInt() + "P",
105  "Ps", StringUtils::asString(timeSystem).c_str() );
106  return rv;
107  }
108  catch( gnsstk::StringUtils::StringException& se )
109  {
110  GNSSTK_RETHROW( se );
111  }
112  }
113 
114  std::string MJD::printError( const std::string& fmt ) const
115  {
116  try
117  {
119  std::string rv( fmt );
120 
121  rv = formattedPrint( rv, getFormatPrefixFloat() + "Q",
122  "Qs", getError().c_str() );
123  rv = formattedPrint( rv, getFormatPrefixInt() + "P",
124  "Ps", getError().c_str() );
125  return rv;
126  }
127  catch( gnsstk::StringUtils::StringException& se )
128  {
129  GNSSTK_RETHROW( se );
130  }
131  }
132 
133  bool MJD::setFromInfo( const IdToValue& info )
134  {
135  using namespace gnsstk::StringUtils;
136 
137  for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
138  {
139  switch( i->first )
140  {
141  case 'Q':
142  mjd = asLongDouble( i->second );
143  break;
144 
145  case 'P':
147  break;
148 
149  default:
150  // do nothing
151  break;
152  };
153  }
154 
155  return true;
156  }
157 
158  bool MJD::isValid() const
159  {
160  MJD temp;
161  temp.convertFromCommonTime( convertToCommonTime() );
162  if( *this == temp )
163  {
164  return true;
165  }
166  return false;
167  }
168 
169  void MJD::reset()
170  {
171  mjd = 0.0;
173  }
174 
175  bool MJD::operator==( const MJD& right ) const
176  {
178  if ((timeSystem != TimeSystem::Any &&
179  right.timeSystem != TimeSystem::Any) &&
180  timeSystem != right.timeSystem)
181  return false;
182 
183  if( std::fabs(mjd - right.mjd) < CommonTime::eps )
184  {
185  return true;
186  }
187  return false;
188  }
189 
190  bool MJD::operator!=( const MJD& right ) const
191  {
192  return ( !operator==( right ) );
193  }
194 
195  bool MJD::operator<( const MJD& right ) const
196  {
198  if ((timeSystem != TimeSystem::Any &&
199  right.timeSystem != TimeSystem::Any) &&
200  timeSystem != right.timeSystem)
201  {
202  gnsstk::InvalidRequest ir("CommonTime objects not in same time system, cannot be compared");
203  GNSSTK_THROW(ir);
204  }
205 
206  if( mjd < right.mjd )
207  {
208  return true;
209  }
210  return false;
211  }
212 
213  bool MJD::operator>( const MJD& right ) const
214  {
215  return ( !operator<=( right ) );
216  }
217 
218  bool MJD::operator<=( const MJD& right ) const
219  {
220  return ( operator<( right ) ||
221  operator==( right ) );
222  }
223 
224  bool MJD::operator>=( const MJD& right ) const
225  {
226  return ( !operator<( right ) );
227  }
228 
229 } // namespace
gnsstk::TimeTag::getFormatPrefixInt
static std::string getFormatPrefixInt()
Definition: TimeTag.hpp:152
se
double se
obliquity cos, T*cos, sin coefficients
Definition: IERS2003NutationData.hpp:48
gnsstk::CommonTime::eps
static const GNSSTK_EXPORT double eps
Default tolerance for time equality in days.
Definition: CommonTime.hpp:107
TimeConstants.hpp
gnsstk::MJD::reset
virtual void reset()
Reset this object to the default state.
Definition: MJD.cpp:169
gnsstk::TimeTag::IdToValue
std::map< char, std::string > IdToValue
Definition: TimeTag.hpp:103
gnsstk::MJD::printError
virtual std::string printError(const std::string &fmt) const
Definition: MJD.cpp:114
gnsstk::SEC_PER_DAY
const long SEC_PER_DAY
Seconds per day.
Definition: TimeConstants.hpp:63
gnsstk::StringUtils::asLongDouble
long double asLongDouble(const std::string &s)
Definition: StringUtils.hpp:1652
gnsstk::TimeSystem::Any
@ Any
wildcard; allows comparison with any other type
gnsstk::StringUtils::asString
std::string asString(IonexStoreStrategy e)
Convert a IonexStoreStrategy to a whitespace-free string name.
Definition: IonexStoreStrategy.cpp:46
gnsstk::MJD::setFromInfo
virtual bool setFromInfo(const IdToValue &info)
Definition: MJD.cpp:133
gnsstk::TimeTag::getError
static std::string getError()
This returns the default error string for the TimeTag classes.
Definition: TimeTag.hpp:161
gnsstk::MJD_JDAY
const long MJD_JDAY
'Julian day' offset from MJD
Definition: TimeConstants.hpp:53
gnsstk::MJD::operator!=
bool operator!=(const MJD &right) const
Definition: MJD.cpp:190
gnsstk::MJD::operator<=
bool operator<=(const MJD &right) const
Definition: MJD.cpp:218
gnsstk::TimeSystem::Unknown
@ Unknown
unknown time frame; for legacy code compatibility
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
MJD.hpp
gnsstk::TimeTag::timeSystem
TimeSystem timeSystem
time system (representation) of the data
Definition: TimeTag.hpp:204
example4.temp
temp
Definition: example4.py:35
gnsstk::MJD::operator<
bool operator<(const MJD &right) const
Definition: MJD.cpp:195
gnsstk::MJD::operator==
bool operator==(const MJD &right) const
Definition: MJD.cpp:175
gnsstk::TimeTag::getFormatPrefixFloat
static std::string getFormatPrefixFloat()
Definition: TimeTag.hpp:157
gnsstk::StringUtils::asTimeSystem
TimeSystem asTimeSystem(const std::string &s)
Convert a string representation of TimeSystem to an enum.
Definition: TimeSystem.cpp:324
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::MJD::operator=
MJD & operator=(const MJD &right)
Definition: MJD.cpp:47
gnsstk::CommonTime::get
void get(long &day, long &sod, double &fsod, TimeSystem &timeSystem) const
Definition: CommonTime.cpp:213
gnsstk::MJD::isValid
virtual bool isValid() const
Returns true if this object's members are valid, false otherwise.
Definition: MJD.cpp:158
GNSSTK_RETHROW
#define GNSSTK_RETHROW(exc)
Definition: Exception.hpp:369
gnsstk::MJD::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: MJD.cpp:84
gnsstk::MJD::mjd
long double mjd
Definition: MJD.hpp:169
gnsstk::MJD::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: MJD.cpp:54
gnsstk::StringUtils
Definition: IonexStoreStrategy.cpp:44
gnsstk::MJD::operator>=
bool operator>=(const MJD &right) const
Definition: MJD.cpp:224
example6.sod
sod
Definition: example6.py:103
gnsstk::DAY_PER_SEC
const double DAY_PER_SEC
Days per second.
Definition: TimeConstants.hpp:65
gnsstk::MJD::printf
virtual std::string printf(const std::string &fmt) const
Definition: MJD.cpp:95
gnsstk::MJD
Definition: MJD.hpp:54
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::StringUtils::formattedPrint
std::string formattedPrint(const std::string &fmt, const std::string &pat, const std::string &rep, T to)
Definition: StringUtils.hpp:2020
gnsstk::CommonTime::set
CommonTime & set(long day, long sod, double fsod=0.0, TimeSystem timeSystem=TimeSystem::Unknown)
Definition: CommonTime.cpp:87
gnsstk::MJD::operator>
bool operator>(const MJD &right) const
Definition: MJD.cpp:213


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:40