WxObsMap_T.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 
39 #include "WxObsMap.hpp"
40 
41 #include "TestUtil.hpp"
42 #include <iostream>
43 #include <string>
44 #include <sstream>
45 #include <cmath>
46 
48 {
49 public:
50  WxObsMap_T(){ singlePrecisionError = 1E-7; }// Default Constructor, set the precision value for single precision. Note WxObsMap stores floating point values in single precision.
51  ~WxObsMap_T() {} // Default Desructor
52 
53 //================================================================
54 // WxObsObservations
55 
56  int observationsTest(void)
57  {
58  gnsstk::TestUtil testFramework("WxObsMap", "Default Constructor", __FILE__, __LINE__);
59  std::string failMesg;
60 
61  gnsstk::WxObservation Compare;
62 
63  failMesg = "Was the time value set correctly?";
64  testFramework.assert(Compare.t == gnsstk::CommonTime::END_OF_TIME, failMesg, __LINE__);
65 
66  failMesg = "Was the temperature source set correctly?";
67  testFramework.assert(Compare.temperatureSource == gnsstk::WxObservation::noWx, failMesg, __LINE__);
68 
69  failMesg = "Was the pressure source set correctly?";
70  testFramework.assert(Compare.pressureSource == gnsstk::WxObservation::noWx, failMesg, __LINE__);
71 
72  failMesg = "Was the humidity source set correctly?";
73  testFramework.assert(Compare.humiditySource == gnsstk::WxObservation::noWx, failMesg, __LINE__);
74 
75  gnsstk::CommonTime cTime;
76  cTime.set(500005,6,.7);
77 
78  gnsstk::WxObservation Compare1(cTime, 100, .5f, .8f);
79 
80  testFramework.changeSourceMethod("Explicit Constructor");
81 
82  failMesg = "Was the time value set correctly?";
83  testFramework.assert(Compare1.t == cTime, failMesg, __LINE__);
84 
85  failMesg = "Was the temperature value set correctly?";
86  testFramework.assert(std::abs(Compare1.temperature - 100) < singlePrecisionError, failMesg, __LINE__);
87 
88  failMesg = "Was the pressure value set correctly?";
89  testFramework.assert(std::abs(Compare1.pressure - 0.5) < singlePrecisionError, failMesg, __LINE__);
90 
91  failMesg = "Was the humidity value set correctly?";
92  testFramework.assert(std::abs(Compare1.humidity - 0.8) < singlePrecisionError, failMesg, __LINE__);
93 
94  failMesg = "Was the temperature source set correctly?";
95  testFramework.assert(Compare1.temperatureSource == gnsstk::WxObservation::obsWx, failMesg, __LINE__);
96 
97  failMesg = "Was the temperature source set correctly?";
98  testFramework.assert(Compare1.pressureSource == gnsstk::WxObservation::obsWx, failMesg, __LINE__);
99 
100  failMesg = "Was the temperature source set correctly?";
101  testFramework.assert(Compare1.humiditySource == gnsstk::WxObservation::obsWx, failMesg, __LINE__);
102 
103  testFramework.changeSourceMethod("isAllValid");
104 
105  failMesg = "Does the isAllValid method function properly?";
106  testFramework.assert(Compare1.isAllValid(), failMesg, __LINE__);
107 
108  testFramework.changeSourceMethod("<< Operator");
109 
110  std::string outputString, referenceString;
111  std::stringstream outputStream, referenceStream;
112  outputStream << Compare1;
113  outputString = outputStream.str();
114  referenceStream << cTime << ", t=" << 100 << ", p=" << 0.5 << ", rh=" << 0.8;
115  referenceString = referenceStream.str();
116 
117  failMesg = "Does the << operator function properly?";
118  testFramework.assert(referenceString == outputString, failMesg, __LINE__);
119 
120  return testFramework.countFails();
121  }
122 //================================================================
123 
124 
125 
126 //================================================================
127 // WxObsMap
128 
129 // Only tests can do here would verify that a map works
130 // with CommonTime and WxObservation objects. Implicitly
131 // tested in WxObsData, don't need explicit code for now
132 
133 //================================================================
134 
135 
136 
137 //================================================================
138 // WxObsData
139 
140  int WxObsDataTest(void)
141  {
142  gnsstk::TestUtil testFramework("WxObsMap", "Default Constructor", __FILE__, __LINE__);
143  std::string failMesg;
144 
145  gnsstk::WxObsData Compare;
146 
147  failMesg = "Was the default value stored?";
148  testFramework.assert(Compare.firstTime == gnsstk::CommonTime::END_OF_TIME, failMesg, __LINE__);
149 
150  failMesg = "Was the default value stored?";
151  testFramework.assert(Compare.lastTime == gnsstk::CommonTime::BEGINNING_OF_TIME, failMesg, __LINE__);
152 
153  testFramework.changeSourceMethod("insertObservation");
154 
155  gnsstk::CommonTime cTime;
156  cTime.set((double)1000);
157 
158  for (int i = 0; i < 10; i++)
159  {
160  cTime.set((double)1000+i);
162  100+i, (50+i)*0.001, (80+i)*0.001));
163  }
164 
165  //scale time counter back to access a stored observation
166  cTime.set((double)1008);
167 
168  failMesg = "Was the WxObservation object stored in the map?";
169  testFramework.assert(Compare.obs.count(cTime), failMesg, __LINE__);
170 
171 
172  testFramework.changeSourceMethod("getMostRecent");
173 
174  gnsstk::WxObservation obs1 = Compare.getMostRecent(cTime);
175  gnsstk::WxObservation obs2(cTime, 108, 0.058f, 0.088f);
176 
177  //std::cout<<obs2<<std::endl<<obs1<<std::endl;
178 
179  //std::cout<<Compare.obs[cTime]<<std::endl;
180 
181 
182  failMesg = "Did the getMostRecent method function correctly?";
183 
184  //should be able to do obs1 == obs2, but b/c theres no == operator...
185  testFramework.assert(obs2.t == obs1.t &&
186  obs2.temperature == obs1.temperature &&
187  obs2.pressure == obs1.pressure &&
188  obs2.humidity == obs1.humidity, failMesg, __LINE__);
189 
190  //verifying objects exist before deleting them
191  cTime.set((double) 1001);
192  failMesg = "Is the WxObservation object stored? ";
193  testFramework.assert(Compare.obs.count(cTime), failMesg, __LINE__);
194  cTime.set((double) 1002);
195  failMesg = "Is the WxObservation object stored? ";
196  testFramework.assert(Compare.obs.count(cTime), failMesg, __LINE__);
197 
198  cTime.set((double) 1003);
199  //should erase 1001 and 1002 entries
200  Compare.flush(cTime);
201 
202  cTime.set((double) 1001);
203  failMesg = "Was the WxObservation object erased? ";
204  testFramework.assert(!Compare.obs.count(cTime), failMesg, __LINE__);
205  cTime.set((double) 1002);
206  failMesg = "Was the WxObservation object erased? ";
207  testFramework.assert(!Compare.obs.count(cTime), failMesg, __LINE__);
208 
209  testFramework.changeSourceMethod("getWxObservation");
210 
211  failMesg = "[testing] WxObs.getWxObservation with a non-existent object, [expected] exception gnsstk::Exception, [actual] threw no exception";
212  try{ Compare.getWxObservation(cTime, 0, false); testFramework.assert(false, failMesg, __LINE__); }
213  catch (gnsstk::Exception e) { testFramework.assert(true, failMesg, __LINE__); }
214 
215  //return right object for explicit case
216  cTime.set((double)1003);
217  gnsstk::WxObservation obs3 = Compare.getWxObservation(cTime, 0, false);
218  gnsstk::WxObservation obs4(cTime, 103, 0.053f, 0.083f);
219 
220  failMesg = "Did the getWxObservation return the correct object when called explicitly? ";
221  testFramework.assert(obs3.t == obs3.t &&
222  obs3.temperature == obs4.temperature &&
223  obs3.pressure == obs4.pressure &&
224  obs3.humidity == obs4.humidity, failMesg, __LINE__);
225 
226  //return right object within range when theres only one
227  cTime.set((double)1009);
228  gnsstk::WxObservation obs6(cTime, 109, 0.059f, 0.089f);
229  cTime.set((double)1010);
230  gnsstk::WxObservation obs5 = Compare.getWxObservation(cTime, 86401, false);
231 
232  failMesg = "Did the getWxObservation return the correct object when called in a range containing 1 object?";
233  testFramework.assert(obs5.t == obs6.t &&
234  obs5.temperature == obs6.temperature &&
235  obs5.pressure == obs6.pressure &&
236  obs5.humidity == obs6.humidity, failMesg, __LINE__);
237 
238  //return right object within range when theres multiple
239  cTime.set((double)1006);
240  gnsstk::WxObservation obs8(cTime, 106, 0.056f, 0.086f);
241  gnsstk::WxObservation obs7 = Compare.getWxObservation(cTime, 86401, false);
242 
243  failMesg = "Did the getWxObservation return the correct object when called in a range containing 2 objects?";
244  testFramework.assert(obs7.t == obs8.t &&
245  obs7.temperature == obs8.temperature &&
246  obs7.pressure == obs8.pressure &&
247  obs7.humidity == obs8.humidity, failMesg, __LINE__);
248 
249  //Linear Interpolation
250  cTime.set((long)1007, (double)43200);
251  //should have two values in range, where counter is 7 and 8
252  gnsstk::WxObservation obs9 = Compare.getWxObservation(cTime, 43200, true);
253  //create object with what the interpolated values should be at the given time
254  gnsstk::WxObservation obs10(cTime, 107.5f, 0.0575f, 0.0875f);
255 
256  failMesg = "Did the getWxObservation perform linear interpolation succesfully?";
257  testFramework.assert(std::abs(obs9.t - obs10.t) < singlePrecisionError &&
258  std::abs(obs9.temperature - obs10.temperature) < singlePrecisionError&&
259  std::abs(obs9.pressure - obs10.pressure) < singlePrecisionError &&
260  std::abs(obs9.humidity - obs10.humidity) < singlePrecisionError, failMesg, __LINE__);
261 
262  return testFramework.countFails();
263  }
264 
265 //================================================================
266 
267 private:
269 
270 };
271 
272 
273 int main() //Main function to initialize and run all tests above
274 {
275  WxObsMap_T testClass;
276  int check = 0, errorCounter = 0;
277 
278  check = testClass.observationsTest();
279  errorCounter += check;
280 
281  check = testClass.WxObsDataTest();
282  errorCounter += check;
283 
284  std::cout << "Total Failures for " << __FILE__ << ": " << errorCounter << std::endl;
285 
286  return errorCounter;
287 }
gnsstk::TestUtil::countFails
int countFails(void)
Definition: TestUtil.hpp:771
gnsstk::TestUtil::assert
void assert(bool testExpression, const std::string &testMsg, const int lineNumber)
Definition: TestUtil.hpp:607
gnsstk::WxObservation::temperature
float temperature
degrees Centigrade
Definition: WxObsMap.hpp:82
gnsstk::WxObsData::getWxObservation
WxObservation getWxObservation(const CommonTime &t, unsigned iv=3600, bool interpolate=true) const
Definition: WxObsMap.cpp:102
main
int main()
Definition: WxObsMap_T.cpp:273
gnsstk::WxObservation
A Single Weather Observation.
Definition: WxObsMap.hpp:55
gnsstk::CommonTime::BEGINNING_OF_TIME
static const GNSSTK_EXPORT CommonTime BEGINNING_OF_TIME
earliest representable CommonTime
Definition: CommonTime.hpp:102
gnsstk::WxObservation::pressure
float pressure
millibars
Definition: WxObsMap.hpp:83
gnsstk::WxObservation::humiditySource
enum EWxSrc humiditySource
source of the humidity meas.
Definition: WxObsMap.hpp:96
gnsstk::Exception
Definition: Exception.hpp:151
WxObsMap_T::WxObsMap_T
WxObsMap_T()
Definition: WxObsMap_T.cpp:50
gnsstk::TestUtil::changeSourceMethod
void changeSourceMethod(const std::string &newMethod)
Definition: TestUtil.hpp:785
gnsstk::CommonTime::END_OF_TIME
static const GNSSTK_EXPORT CommonTime END_OF_TIME
latest representable CommonTime
Definition: CommonTime.hpp:104
gnsstk::WxObservation::noWx
@ noWx
No weather measurements were available.
Definition: WxObsMap.hpp:89
WxObsMap_T
Definition: WxObsMap_T.cpp:47
TestUtil.hpp
WxObsMap.hpp
gnsstk::WxObservation::isAllValid
bool isAllValid() const noexcept
Definition: WxObsMap.cpp:75
gnsstk::WxObsData::flush
void flush(const CommonTime &t) noexcept
Definition: WxObsMap.cpp:83
gnsstk::CommonTime
Definition: CommonTime.hpp:84
gnsstk::WxObsData::getMostRecent
WxObservation getMostRecent(const CommonTime &t) const noexcept
Definition: WxObsMap.cpp:53
gnsstk::WxObsData::obs
WxObsMap obs
The actual data.
Definition: WxObsMap.hpp:133
gnsstk::WxObsData::firstTime
CommonTime firstTime
Time of the oldest data in this object.
Definition: WxObsMap.hpp:126
WxObsMap_T::WxObsDataTest
int WxObsDataTest(void)
Definition: WxObsMap_T.cpp:140
gnsstk::WxObservation::temperatureSource
enum EWxSrc temperatureSource
source of the temperature meas.
Definition: WxObsMap.hpp:94
WxObsMap_T::observationsTest
int observationsTest(void)
Definition: WxObsMap_T.cpp:56
gnsstk::WxObservation::t
CommonTime t
Time that this data was collected, in Rx GPS time.
Definition: WxObsMap.hpp:81
WxObsMap_T::~WxObsMap_T
~WxObsMap_T()
Definition: WxObsMap_T.cpp:51
WxObsMap_T::singlePrecisionError
double singlePrecisionError
Definition: WxObsMap_T.cpp:268
gnsstk::WxObsData::insertObservation
void insertObservation(const WxObservation &obs) noexcept
Definition: WxObsMap.cpp:67
gnsstk::WxObservation::obsWx
@ obsWx
The Observed Weather was used.
Definition: WxObsMap.hpp:91
gnsstk::WxObservation::humidity
float humidity
percent
Definition: WxObsMap.hpp:84
gnsstk::WxObsData::lastTime
CommonTime lastTime
Time of the youngest data in this object.
Definition: WxObsMap.hpp:127
gnsstk::TestUtil
Definition: TestUtil.hpp:265
gnsstk::WxObsData
This is a time history weather data from a single site.
Definition: WxObsMap.hpp:118
gnsstk::CommonTime::set
CommonTime & set(long day, long sod, double fsod=0.0, TimeSystem timeSystem=TimeSystem::Unknown)
Definition: CommonTime.cpp:87
gnsstk::WxObservation::pressureSource
enum EWxSrc pressureSource
source of the pressure meas.
Definition: WxObsMap.hpp:95


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