EphTime.hpp
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 
44 #ifndef GEOMATICS_EPHTIME_HPP
45 #define GEOMATICS_EPHTIME_HPP
46 
47 #include "Exception.hpp"
48 #include "TimeSystem.hpp"
49 
50 #include "CivilTime.hpp"
51 #include "CommonTime.hpp"
52 #include "MJD.hpp"
53 #include "TimeConverters.hpp"
54 
55 namespace gnsstk
56 {
58 
59 
61 
67  class EphTime
68  {
69  private:
70  long iMJD;
71  double dSOD;
73 
74  public:
77 
79  EphTime(int imjd, double dsod, TimeSystem sys = TimeSystem::UTC)
80  : iMJD(imjd), dSOD(dsod), system(sys)
81  {
82  }
83 
86  {
87  setMJD(mjd);
88  setTimeSystem(sys);
89  }
90 
96  void convertSystemTo(const TimeSystem& ts)
97  {
98  try
99  {
100  long jday(static_cast<long>(iMJD + dSOD / SEC_PER_DAY + MJD_JDAY));
101  int yy, mm, dd;
102  convertJDtoCalendar(jday, yy, mm, dd);
103  double dt, days = dd + dSOD / SEC_PER_DAY;
104  // correct time
105  dt = gnsstk::getTimeSystemCorrection(system, ts, yy, mm, days);
106  *this += dt;
107  // set new system
108  this->setTimeSystem(ts);
109  }
110  catch (Exception& e)
111  {
112  GNSSTK_RETHROW(e);
113  }
114  }
115 
120  EphTime& operator+=(double seconds)
121  {
122  dSOD += seconds;
123  while (dSOD >= 86400.0)
124  {
125  dSOD -= 86400.0;
126  iMJD++;
127  }
128  while (dSOD < 0.0)
129  {
130  dSOD += 86400.0;
131  iMJD--;
132  }
133  return *this;
134  }
135 
142  {
143  if (sys != TimeSystem::UTC && sys != TimeSystem::TT &&
144  sys != TimeSystem::TDB)
145  {
146  GNSSTK_THROW(Exception("Time system not allowed"));
147  }
148  system = sys;
149  }
150 
155  void setMJD(long double mjd)
156  {
157  iMJD = long(mjd);
158  dSOD = (mjd - static_cast<double>(iMJD)) * 86400.0;
159  }
160 
165  long lMJD() const { return iMJD; }
166 
171  double dMJD() const
172  {
173  return (static_cast<double>(iMJD) + dSOD / 86400.);
174  }
175 
177  double secOfDay() const { return dSOD; }
178 
180  int year() const
181  {
182  long jday(static_cast<long>(iMJD + dSOD / SEC_PER_DAY + MJD_JDAY));
183  int yy, mm, dd;
184  convertJDtoCalendar(jday, yy, mm, dd);
185  return yy;
186  }
187 
194  EphTime(const CommonTime& dt)
195  {
196  try
197  {
198  CommonTime ct(dt);
199  TimeSystem sys = ct.getTimeSystem();
200  if (sys == TimeSystem::Unknown || sys == TimeSystem::Any)
201  {
203  }
204  // if its not UTC TT or TDB, convert it to UTC
205  else if (sys != TimeSystem::UTC && sys != TimeSystem::TT &&
206  sys != TimeSystem::TDB)
207  {
208  CivilTime civt;
209  civt.convertFromCommonTime(ct);
211  sys, TimeSystem::UTC, civt.year, civt.month, civt.day);
212  ct += dt;
213  }
214 
215  MJD ctmjd;
216  ctmjd.convertFromCommonTime(ct);
217  iMJD = static_cast<long>(ctmjd.mjd);
218  dSOD = (ctmjd.mjd - static_cast<long double>(iMJD)) * SEC_PER_DAY;
219  system = ctmjd.getTimeSystem();
220  }
221  catch (Exception& e)
222  {
223  GNSSTK_RETHROW(e);
224  }
225  }
226 
228  operator CommonTime() const
229  {
230  MJD ctmjd;
231  ctmjd.mjd = static_cast<long double>(iMJD + dSOD / SEC_PER_DAY);
232  CommonTime ct = ctmjd.convertToCommonTime();
233  ct.setTimeSystem(system);
234  return ct;
235  }
236 
237  // no this is not a duplicate of the previous function
239  operator CommonTime()
240  {
241  MJD ctmjd;
242  ctmjd.mjd = static_cast<long double>(iMJD + dSOD / SEC_PER_DAY);
243  CommonTime ct = ctmjd.convertToCommonTime();
244  ct.setTimeSystem(system);
245  return ct;
246  }
247 
252  std::string asGPSString(const int prec = 2) const
253  {
254  std::ostringstream oss;
255  int wk((iMJD - GPS_EPOCH_MJD) / 7);
256  double sow((iMJD - wk * 7) * SEC_PER_DAY + dSOD);
257  if (sow >= FULLWEEK)
258  {
259  sow -= FULLWEEK;
260  wk++;
261  }
262  oss << wk << " " << std::fixed << std::setprecision(prec) << sow;
263  return oss.str();
264  }
265 
270  std::string asMJDString(const int prec = 3) const
271  {
272  std::ostringstream oss;
273  int hh, mm;
274  double sec;
275  convertSODtoTime(dSOD, hh, mm, sec);
276  oss << iMJD << " " << std::setfill('0') << std::setw(2) << hh << ":"
277  << std::setw(2) << mm << ":" << std::setfill(' ') << std::fixed
278  << std::setprecision(prec) << sec;
279  return oss.str();
280  }
281 
282  }; // end class EphTime
283 
285 
286 } // namespace gnsstk
287 
288 #endif // GEOMATICS_EPHTIME_HPP
gnsstk::TimeSystem::TT
@ TT
Terrestrial time (used in IERS conventions)
TimeSystem.hpp
gnsstk::EphTime::EphTime
EphTime()
empty constructor
Definition: EphTime.hpp:76
gnsstk::EphTime::convertSystemTo
void convertSystemTo(const TimeSystem &ts)
Definition: EphTime.hpp:96
example6.mjd
mjd
Definition: example6.py:102
gnsstk::CivilTime::year
int year
Definition: CivilTime.hpp:198
gnsstk::SEC_PER_DAY
const long SEC_PER_DAY
Seconds per day.
Definition: TimeConstants.hpp:63
gnsstk::EphTime::asGPSString
std::string asGPSString(const int prec=2) const
Definition: EphTime.hpp:252
gnsstk::EphTime::setTimeSystem
void setTimeSystem(TimeSystem sys)
Definition: EphTime.hpp:141
gnsstk::EphTime::dSOD
double dSOD
double seconds of day
Definition: EphTime.hpp:71
gnsstk::FULLWEEK
const long FULLWEEK
Seconds per whole week.
Definition: TimeConstants.hpp:60
gnsstk::TimeSystem::Any
@ Any
wildcard; allows comparison with any other type
gnsstk::CivilTime::day
int day
Definition: CivilTime.hpp:200
gnsstk::CommonTime::setTimeSystem
CommonTime & setTimeSystem(TimeSystem timeSystem)
Definition: CommonTime.hpp:195
gnsstk::MJD_JDAY
const long MJD_JDAY
'Julian day' offset from MJD
Definition: TimeConstants.hpp:53
gnsstk::CivilTime::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: CivilTime.cpp:97
gnsstk::TimeSystem::Unknown
@ Unknown
unknown time frame; for legacy code compatibility
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::IonexStoreStrategy::Unknown
@ Unknown
Unknown or uninitialized stategy value.
MJD.hpp
gnsstk::Exception
Definition: Exception.hpp:151
gnsstk::TimeSystem::TDB
@ TDB
Barycentric dynamical time (JPL ephemeris); very near TT.
gnsstk::EphTime::lMJD
long lMJD() const
Definition: EphTime.hpp:165
gnsstk::convertJDtoCalendar
void convertJDtoCalendar(long jd, int &iyear, int &imonth, int &iday)
Definition: TimeConverters.cpp:52
gnsstk::convertSODtoTime
void convertSODtoTime(double sod, int &hh, int &mm, double &sec)
Definition: TimeConverters.cpp:149
gnsstk::EphTime::EphTime
EphTime(const CommonTime &dt)
Definition: EphTime.hpp:194
gnsstk::GPS_EPOCH_MJD
const long GPS_EPOCH_MJD
Modified Julian Date of GPS epoch (Jan. 6, 1980).
Definition: TimeConstants.hpp:83
gnsstk::EphTime::operator+=
EphTime & operator+=(double seconds)
Definition: EphTime.hpp:120
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::EphTime::asMJDString
std::string asMJDString(const int prec=3) const
Definition: EphTime.hpp:270
gnsstk::EphTime
Definition: EphTime.hpp:67
gnsstk::TimeSystem
TimeSystem
Definition of various time systems.
Definition: TimeSystem.hpp:51
CivilTime.hpp
TimeConverters.hpp
gnsstk::EphTime::system
TimeSystem system
time system, limited to Unknown, UTC, TT, TDB
Definition: EphTime.hpp:72
GNSSTK_RETHROW
#define GNSSTK_RETHROW(exc)
Definition: Exception.hpp:369
gnsstk::MJD::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: MJD.cpp:84
gnsstk::EphTime::year
int year() const
Definition: EphTime.hpp:180
gnsstk::TimeSystem::UTC
@ UTC
Coordinated Universal Time (e.g., from NTP)
gnsstk::MJD::mjd
long double mjd
Definition: MJD.hpp:169
gnsstk::MJD::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: MJD.cpp:54
gnsstk::CivilTime
Definition: CivilTime.hpp:55
gnsstk::CommonTime::getTimeSystem
TimeSystem getTimeSystem() const
Obtain time system info (enum).
Definition: CommonTime.cpp:288
Exception.hpp
CommonTime.hpp
gnsstk::CivilTime::month
int month
Definition: CivilTime.hpp:199
gnsstk::EphTime::secOfDay
double secOfDay() const
Definition: EphTime.hpp:177
gnsstk::MJD
Definition: MJD.hpp:54
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::EphTime::dMJD
double dMJD() const
Definition: EphTime.hpp:171
gnsstk::getTimeSystemCorrection
double getTimeSystemCorrection(const TimeSystem inTS, const TimeSystem outTS, const int year, const int month, const double day)
Definition: TimeSystem.cpp:181
gnsstk::EphTime::setMJD
void setMJD(long double mjd)
Definition: EphTime.hpp:155
gnsstk::EphTime::iMJD
long iMJD
integer MJD
Definition: EphTime.hpp:70
gnsstk::EphTime::EphTime
EphTime(int imjd, double dsod, TimeSystem sys=TimeSystem::UTC)
constructor, input int mjd, seconds of day, and system
Definition: EphTime.hpp:79
gnsstk::TimeTag::getTimeSystem
TimeSystem getTimeSystem() const
Obtain time system info (enum).
Definition: TimeTag.hpp:169
gnsstk::EphTime::EphTime
EphTime(double mjd, TimeSystem sys=TimeSystem::UTC)
constructor from full MJD and system
Definition: EphTime.hpp:85


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