GPSWeekZcount.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 "GPSWeekZcount.hpp"
42 #include "TimeConstants.hpp"
43 #include "TimeConverters.hpp"
44 #include "GPSWeekSecond.hpp"
45 
46 namespace gnsstk
47 {
48  // Not really keen on having the day of week names in
49  // GPSWeekZcount but this is where the %w translation takes
50  // place.
51  static const char *weekdayAbbr[] =
52  {
53  "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
54  };
55  static const char *weekday[] =
56  {
57  "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
58  "Saturday"
59  };
60 
62  {
63  GPSWeek::operator=(right);
64  zcount = right.zcount;
65  return *this;
66  }
67 
69  {
70  try
71  {
72  int dow = zcount / ZCOUNT_PER_DAY;
73  int jday = MJD_JDAY + GPS_EPOCH_MJD + ( 7 * week ) + dow;
74  double sod = static_cast<double>( zcount % ZCOUNT_PER_DAY ) * 1.5;
75  CommonTime ct;
76  return ct.set( jday,
77  static_cast<long>( sod ),
78  sod - static_cast<long>( sod ),
79  timeSystem );
80  }
81  catch (InvalidParameter& ip)
82  {
83  InvalidRequest ir(ip);
84  GNSSTK_THROW(ir);
85  }
86  }
87 
89  {
91  static const CommonTime MIN_CT = GPSWeekZcount(0,0,TimeSystem::Any);
92 
93  if (ct < MIN_CT)
94  {
95  InvalidRequest ir("Unable to convert CommonTime to GPSWeekZcount.");
96  GNSSTK_THROW(ir);
97  }
98 
99  long day, sod;
100  double fsod;
101  ct.get( day, sod, fsod, timeSystem );
102 
103  // find the number of days since the beginning of the GPS Epoch
105  // find out how many weeks that is
106  week = static_cast<int>( day / 7 );
107  // find out what the day of week is
108  day %= 7;
109 
110  zcount = static_cast<long>( day * ZCOUNT_PER_DAY )
111  + static_cast<long>( static_cast<double>( sod + fsod ) / 1.5 );
112  }
113 
114  std::string GPSWeekZcount::printf( const std::string& fmt ) const
115  {
116  try
117  {
119 
120  std::string rv = GPSWeek::printf( fmt );
121 
122  rv = formattedPrint( rv, getFormatPrefixInt() + "a",
123  "as", weekdayAbbr[getDayOfWeek()] );
124  rv = formattedPrint( rv, getFormatPrefixInt() + "A",
125  "As", weekday[getDayOfWeek()] );
126  rv = formattedPrint( rv, getFormatPrefixInt() + "w",
127  "wu", getDayOfWeek() );
128  rv = formattedPrint( rv, getFormatPrefixInt() + "z",
129  "zu", zcount );
130  rv = formattedPrint( rv, getFormatPrefixInt() + "Z",
131  "Zu", zcount );
132  rv = formattedPrint( rv, getFormatPrefixInt() + "c",
133  "cu", getZcount29() );
134  rv = formattedPrint( rv, getFormatPrefixInt() + "C",
135  "Cu", getZcount32() );
136  rv = formattedPrint( rv, getFormatPrefixInt() + "P",
137  "Ps", StringUtils::asString(timeSystem).c_str() );
138  return rv;
139  }
140  catch( gnsstk::StringUtils::StringException& exc )
141  {
142  GNSSTK_RETHROW( exc );
143  }
144  }
145 
146  std::string GPSWeekZcount::printError( const std::string& fmt ) const
147  {
148  try
149  {
151 
152  std::string rv = GPSWeek::printError( fmt );
153 
154  rv = formattedPrint( rv, getFormatPrefixInt() + "w",
155  "ws", getError().c_str() );
156  rv = formattedPrint( rv, getFormatPrefixInt() + "z",
157  "zs", getError().c_str() );
158  rv = formattedPrint( rv, getFormatPrefixInt() + "Z",
159  "Zs", getError().c_str() );
160  rv = formattedPrint( rv, getFormatPrefixInt() + "c",
161  "cs", getError().c_str() );
162  rv = formattedPrint( rv, getFormatPrefixInt() + "C",
163  "Cs", getError().c_str() );
164  rv = formattedPrint( rv, getFormatPrefixInt() + "P",
165  "Ps", getError().c_str() );
166  return rv;
167  }
168  catch( gnsstk::StringUtils::StringException& exc )
169  {
170  GNSSTK_RETHROW( exc );
171  }
172  }
173 
175  {
176  using namespace gnsstk::StringUtils;
177 
178  GPSWeek::setFromInfo(info);
179 
180  for( IdToValue::const_iterator i = info.begin(); i != info.end(); i++ )
181  {
182  // based on the character, we know what to do...
183  switch( i->first )
184  {
185  case 'F':
186  week = asInt( i->second );
187  break;
188  case 'w':
189  zcount = asInt( i->second) * ZCOUNT_PER_DAY;
190  break;
191 
192  case 'z':
193  case 'Z':
194  zcount = asInt( i->second );
195  break;
196 
197  case 'c':
198  setZcount29( asInt( i->second ) );
199  break;
200 
201  case 'C':
202  setZcount32( asInt( i->second ) );
203  break;
204 
205  case 'P':
207  break;
208 
209  default:
210  // do nothing
211  break;
212  };
213  }
214 
215  return true;
216  }
217 
218  //inline bool GPSWeekZcount::isValid() const
219  //{
220  // return ( GPSWeek::isValid() &&
221  // zcount < ZCOUNT_PER_WEEK );
222  //}
223 
224  //void GPSWeekZcount::reset()
225  //{
226  // GPSWeek::reset();
227  // zcount = 0;
228  //}
229 
231  {
232  setWeek10( (z >> 19) & bits10 );
233  zcount = z & bits19;
234  return *this;
235  }
236 
238  {
239  week = z >> 19;
240  zcount = z & bits19;
241  return *this;
242  }
243 
244 
246  {
247  week += inWeeks;
248  if (week < 0)
249  {
250  gnsstk::InvalidRequest exc("addWeeks results in negative week");
251  GNSSTK_THROW(exc);
252  }
253  return *this;
254  }
255 
256 
258  {
259  if (inZcounts == 0)
260  return *this;
261 
262  short originalWeek(week);
263  long originalZcount(zcount);
264 
265  try
266  {
267  // First, do week modifications.
268  addWeeks(inZcounts / ZCOUNT_PER_WEEK);
269 
270  // Now, take care of Z-counts.
271  long tmp = zcount + (inZcounts % ZCOUNT_PER_WEEK);
272 
273  if (tmp < 0)
274  {
275  addWeeks(-1);
276  tmp += ZCOUNT_PER_WEEK;
277  }
278  else if (tmp >= ZCOUNT_PER_WEEK)
279  {
280  addWeeks(1);
281  tmp -= ZCOUNT_PER_WEEK;
282  }
283 
284  zcount = tmp;
285  return *this;
286  }
287  catch (gnsstk::InvalidRequest& ir)
288  {
289  week = originalWeek;
290  zcount = originalZcount;
291  ir.addText("Did not add " + StringUtils::asString(inZcounts) +
292  " Z-counts.");
293  GNSSTK_RETHROW(ir);
294  }
295  catch (gnsstk::InvalidParameter& ip)
296  {
297  week = originalWeek;
298  zcount = originalZcount;
299  gnsstk::InvalidRequest ir(ip);
300  ir.addText("Did not add " + StringUtils::asString(inZcounts) +
301  " Z-counts.");
302  GNSSTK_THROW(ir);
303  }
304  }
305 
306 
308  {
309  GPSWeekZcount temp = *this;
310  ++(*this);
311  return temp;
312  }
313 
314 
316  {
317  return addZcounts(1);
318  }
319 
320 
322  {
323  GPSWeekZcount temp = *this;
324  --(*this);
325  return temp;
326  }
327 
328 
330  {
331  return addZcounts(-1);
332  }
333 
334 
336  {
337  return GPSWeekZcount(*this).addZcounts(inZcounts);
338  }
339 
340 
342  {
343  return operator+(-inZcounts);
344  }
345 
346 
347  long GPSWeekZcount::operator-(const GPSWeekZcount& right) const
348  noexcept
349  {
350  return (((long(week) - long(right.week)) * ZCOUNT_PER_WEEK) +
351  (long(zcount) - long(right.zcount)));
352  }
353 
354 
356  {
357  return addZcounts(inZcounts);
358  }
359 
360 
362  {
363  return addZcounts(-inZcounts);
364  }
365 
366 
368  unsigned long inZcountBlock,
369  unsigned long inZcountOffset)
370  noexcept
371  {
372  if (inZcountBlock < ZCOUNT_PER_WEEK)
373  {
374  // Make sure that we're in the same week, and then checkto
375  // see if we're in the same time block.
376  if ((week == other.week) &&
377  (((zcount - inZcountOffset) / inZcountBlock) ==
378  ((other.zcount - inZcountOffset) / inZcountBlock)))
379  {
380  return true;
381  }
382  }
383  else
384  {
385  // Comare using the total number of Z-counts
386  if (long((getTotalZcounts() - inZcountOffset) / inZcountBlock) ==
387  long((other.getTotalZcounts() - inZcountOffset) / inZcountBlock))
388  {
389  return true;
390  }
391  }
392  return false;
393  }
394 
395 } // namespace
gnsstk::TimeTag::getFormatPrefixInt
static std::string getFormatPrefixInt()
Definition: TimeTag.hpp:152
GPSWeekZcount.hpp
gnsstk::StringUtils::asInt
long asInt(const std::string &s)
Definition: StringUtils.hpp:713
gnsstk::GPSWeekZcount::addWeeks
GPSWeekZcount & addWeeks(short inWeeks)
Definition: GPSWeekZcount.cpp:245
gnsstk::GPSWeek::setFromInfo
virtual bool setFromInfo(const IdToValue &info)
Definition: GPSWeek.cpp:107
TimeConstants.hpp
gnsstk::GPSWeekZcount::zcount
unsigned int zcount
Definition: GPSWeekZcount.hpp:371
gnsstk::GPSWeekZcount::operator+
GPSWeekZcount operator+(long inZcounts) const
Definition: GPSWeekZcount.cpp:335
gnsstk::GPSWeekZcount::getZcount32
unsigned int getZcount32() const
Definition: GPSWeekZcount.hpp:173
gnsstk::GPSWeek::operator=
GPSWeek & operator=(const GPSWeek &right)
Assignment Operator.
Definition: GPSWeek.cpp:48
example6.day
day
Definition: example6.py:66
gnsstk::GPSWeekZcount::getTotalZcounts
unsigned long getTotalZcounts() const noexcept
Definition: GPSWeekZcount.hpp:244
gnsstk::TimeTag::IdToValue
std::map< char, std::string > IdToValue
Definition: TimeTag.hpp:103
gnsstk::GPSWeekZcount::convertFromCommonTime
virtual void convertFromCommonTime(const CommonTime &ct)
Definition: GPSWeekZcount.cpp:88
gnsstk::GPSWeekZcount::setZcount32
GPSWeekZcount & setZcount32(unsigned int z)
Definition: GPSWeekZcount.cpp:237
gnsstk::GPSWeekZcount::getDayOfWeek
virtual unsigned int getDayOfWeek() const
Force this interface on this classes descendants.
Definition: GPSWeekZcount.hpp:183
gnsstk::TimeSystem::Any
@ Any
wildcard; allows comparison with any other type
gnsstk::GPSWeekZcount::operator--
GPSWeekZcount & operator--()
Definition: GPSWeekZcount.cpp:329
gnsstk::StringUtils::asString
std::string asString(IonexStoreStrategy e)
Convert a IonexStoreStrategy to a whitespace-free string name.
Definition: IonexStoreStrategy.cpp:46
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::GPSWeek::setWeek10
virtual void setWeek10(unsigned int w)
Definition: GPSWeek.hpp:215
gnsstk::GPSWeekZcount::convertToCommonTime
virtual CommonTime convertToCommonTime() const
Definition: GPSWeekZcount.cpp:68
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
gnsstk::GPSWeekZcount::bits19
static const unsigned int bits19
This is just a 19-bit mask.
Definition: GPSWeekZcount.hpp:59
gnsstk::TimeTag::timeSystem
TimeSystem timeSystem
time system (representation) of the data
Definition: TimeTag.hpp:204
example4.temp
temp
Definition: example4.py:35
gnsstk::weekday
static const char * weekday[]
Definition: GPSWeekZcount.cpp:55
gnsstk::GPSWeek::week
int week
Definition: GPSWeek.hpp:271
gnsstk::GPSWeekZcount::getZcount29
unsigned int getZcount29() const
Definition: GPSWeekZcount.hpp:168
gnsstk::StringUtils::asTimeSystem
TimeSystem asTimeSystem(const std::string &s)
Convert a string representation of TimeSystem to an enum.
Definition: TimeSystem.cpp:324
gnsstk::GPSWeekZcount::printf
virtual std::string printf(const std::string &fmt) const
Definition: GPSWeekZcount.cpp:114
gnsstk::GPSWeek::printError
virtual std::string printError(const std::string &fmt) const
Definition: GPSWeek.cpp:78
gnsstk::GPSWeekZcount::operator-
GPSWeekZcount operator-(long inZcounts) const
Definition: GPSWeekZcount.cpp:341
gnsstk::GPS_EPOCH_MJD
const long GPS_EPOCH_MJD
Modified Julian Date of GPS epoch (Jan. 6, 1980).
Definition: TimeConstants.hpp:83
gnsstk::weekdayAbbr
static const char * weekdayAbbr[]
Definition: GPSWeekZcount.cpp:51
gnsstk::GPSWeekZcount::operator-=
GPSWeekZcount & operator-=(long inZcounts)
Definition: GPSWeekZcount.cpp:361
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::GPSWeekZcount::operator+=
GPSWeekZcount & operator+=(long inZcounts)
Definition: GPSWeekZcount.cpp:355
gnsstk::GPSWeekZcount::GPSWeekZcount
GPSWeekZcount(int w=0, int z=0, TimeSystem ts=TimeSystem::GPS)
Definition: GPSWeekZcount.hpp:69
gnsstk::CommonTime::get
void get(long &day, long &sod, double &fsod, TimeSystem &timeSystem) const
Definition: CommonTime.cpp:213
TimeConverters.hpp
GNSSTK_RETHROW
#define GNSSTK_RETHROW(exc)
Definition: Exception.hpp:369
gnsstk::GPSWeekZcount
Definition: GPSWeekZcount.hpp:55
gnsstk::GPSWeekZcount::operator++
GPSWeekZcount & operator++()
Definition: GPSWeekZcount.cpp:315
gnsstk::GPSWeekZcount::setFromInfo
virtual bool setFromInfo(const IdToValue &info)
Definition: GPSWeekZcount.cpp:174
gnsstk::StringUtils
Definition: IonexStoreStrategy.cpp:44
GPSWeekSecond.hpp
example6.sod
sod
Definition: example6.py:103
gnsstk::GPSWeek::bits10
static const GNSSTK_EXPORT int bits10
Definition: GPSWeek.hpp:87
gnsstk::GPSWeekZcount::printError
virtual std::string printError(const std::string &fmt) const
Definition: GPSWeekZcount.cpp:146
gnsstk::GPSWeekZcount::inSameTimeBlock
bool inSameTimeBlock(const GPSWeekZcount &other, unsigned long inZcountBlock, unsigned long inZcountOffset=0) noexcept
Definition: GPSWeekZcount.cpp:367
gnsstk::ZCOUNT_PER_DAY
const long ZCOUNT_PER_DAY
Zcounts in a day.
Definition: TimeConstants.hpp:88
gnsstk::ZCOUNT_PER_WEEK
const long ZCOUNT_PER_WEEK
Zcounts in a week.
Definition: TimeConstants.hpp:92
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::GPSWeekZcount::setZcount29
GPSWeekZcount & setZcount29(unsigned int z)
Definition: GPSWeekZcount.cpp:230
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::GPSWeekZcount::operator=
GPSWeekZcount & operator=(const GPSWeekZcount &right)
Definition: GPSWeekZcount.cpp:61
gnsstk::GPSWeekZcount::addZcounts
GPSWeekZcount & addZcounts(long inZcounts)
Definition: GPSWeekZcount.cpp:257
gnsstk::GPSWeek::printf
virtual std::string printf(const std::string &fmt) const
Definition: GPSWeek.cpp:55
gnsstk::CommonTime::set
CommonTime & set(long day, long sod, double fsod=0.0, TimeSystem timeSystem=TimeSystem::Unknown)
Definition: CommonTime.cpp:87


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