WxObsMap.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 
44 #include "WxObsMap.hpp"
45 #include "TimeString.hpp"
46 
47 using namespace std;
48 using namespace gnsstk;
49 
50 namespace gnsstk
51 {
52 
53  WxObservation WxObsData::getMostRecent( const CommonTime& t ) const
54  noexcept
55  {
56  if(obs.size() == 0)
57  return WxObservation();
58 
59  WxObsMap::const_iterator i = obs.upper_bound(t);
60  if (i== obs.end())
61  i--;
62  if (i != obs.begin())
63  i--;
64  return i->second;
65  };
66 
67  void WxObsData::insertObservation( const WxObservation& wx )
68  noexcept
69  {
70  obs[wx.t] = wx;
71  if (wx.t > lastTime) lastTime=wx.t;
72  if (wx.t < firstTime) firstTime=wx.t;
73  }
74 
75  bool WxObservation::isAllValid() const
76  noexcept
77  {
78  return temperatureSource != noWx
79  && pressureSource != noWx
80  && humiditySource != noWx;
81  };
82 
83  void WxObsData::flush(const CommonTime& t) noexcept
84  {
85  // remove data from the WxObsMap
86  // map is sorted by time, stop removing data at
87  // first point after t
88  WxObsMap::iterator i = obs.begin();
89  while (i != obs.end())
90  {
91  if (i->first < t)
92  {
93  obs.erase(i);
94  i = obs.begin();
95  firstTime = i->second.t;
96  }
97  else
98  break;
99  }
100  }
101 
102  WxObservation WxObsData::getWxObservation(const CommonTime& t,
103  unsigned iv,
104  bool interpolate) const
105  {
106  if (obs.empty())
107  {
108  ObjectNotFound e("No WxObservation available near time " +
109  printTime(t,"%02H:%02M:%02S on day %03j of %4Y"));
110  GNSSTK_THROW(e);
111  }
112 
113  // get the first object after time t;
114  WxObsMap::const_iterator after = obs.upper_bound(t);
115 
116  if (after == obs.begin())
117  {
118  const WxObservation& wxa = after->second;
119  if ((wxa.t >= (t - iv)) && (wxa.t <= (t + iv)))
120  {
121  // only after point fits
122  return wxa;
123  }
124  else
125  {
126  ObjectNotFound e("No WxObservation available near time " +
127  printTime(t,"%02H:%02M:%02S on day %03j of %4Y"));
128  GNSSTK_THROW(e);
129  }
130  }
131 
132 
133  // get the first object at or before time t;
134  WxObsMap::const_iterator before = after;
135  before--;
136 
137  if (after == obs.end())
138  {
139  const WxObservation& wxb = before->second;
140  if((wxb.t >= (t - iv)) && (wxb.t <= (t + iv)))
141  {
142  // only before point fits
143  return wxb;
144  }
145  else
146  {
147  ObjectNotFound e("No WeatherData available near time " +
148  printTime(t,"%02H:%02M:%02S on day %03j of %4Y"));
149  GNSSTK_THROW(e);
150  }
151  }
152  else
153  {
154  const WxObservation& wxa = after->second;
155  const WxObservation& wxb = before->second;
156 
157  if (interpolate)
158  {
159  if((wxb.t >= (t - iv)) && (wxb.t <= (t + iv)))
160  {
161  if ((wxa.t >= (t - iv)) && (wxa.t <= (t + iv)))
162  {
163  // both points fit, linearly interpolate and create
164  // a WeatherData object with those values
165  double dtw = wxa.t - wxb.t;
166  double dt = t - wxb.t;
167 
168  double slope = (wxa.pressure - wxb.pressure) / dtw;
169  double pressure = slope * dt + wxb.pressure;
170 
171  slope = (wxa.humidity - wxb.humidity) / dtw;
172  double humidity = slope * dt + wxb.humidity;
173 
174  slope = (wxa.temperature - wxb.temperature) / dtw;
175  double temp = slope * dt + wxb.temperature;
176 
178  return wx;
179  }
180  else
181  {
182  // only before point fits
183  return wxb;
184  }
185  }
186  else if ((wxa.t >= (t - iv)) && (wxa.t <= (t + iv)))
187  {
188  // only after point fits
189  return wxa;
190  }
191  else
192  {
193  ObjectNotFound e("No WeatherData available near time " +
194  printTime(t,"%02H:%02M:%02S on day %03j of %4Y"));
195  GNSSTK_THROW(e);
196  }
197  }
198  else
199  {
200  if((wxb.t >= (t - iv)) && (wxb.t <= (t + iv)))
201  {
202  if ((wxa.t >= (t - iv)) && (wxa.t <= (t + iv)))
203  {
204  // both points fit, return closer point, or
205  // before point if at same distance
206  double diffa = wxa.t - t;
207  double diffb = t - wxb.t;
208  return(diffa < diffb ? wxa : wxb);
209  }
210  else
211  {
212  // only before point fits
213  return wxb;
214  }
215  }
216  else if ((wxa.t >= (t - iv)) && (wxa.t <= (t + iv)))
217  {
218  // only after point fits
219  return wxa;
220  }
221  else
222  {
223  ObjectNotFound e("No WeatherData available near time " +
224  printTime(t,"%02H:%02M:%02S on day %03j of %4Y"));
225  GNSSTK_THROW(e);
226  }
227  }
228  }
229  }
230 
231  // These are just to facilitate debugging.
232  std::ostream& operator<<(std::ostream& s, const gnsstk::WxObservation& obs)
233  noexcept
234  {
235  // Note that this does not flag where the wx data came from
236  s << obs.t << ", t=" << obs.temperature
237  << ", p=" << obs.pressure
238  << ", rh=" << obs.humidity;
239  return s;
240  }
241 
242 
243 } // namespace
const
#define const
Definition: getopt.c:43
gnsstk::WxObservation::temperature
float temperature
degrees Centigrade
Definition: WxObsMap.hpp:82
example4.humidity
humidity
Definition: example4.py:37
gnsstk::WxObservation
A Single Weather Observation.
Definition: WxObsMap.hpp:55
gnsstk
For Sinex::InputHistory.
Definition: BasicFramework.cpp:50
example4.temp
temp
Definition: example4.py:35
gnsstk::WxObservation::pressure
float pressure
millibars
Definition: WxObsMap.hpp:83
WxObsMap.hpp
gnsstk::CommonTime
Definition: CommonTime.hpp:84
std::operator<<
std::ostream & operator<<(std::ostream &s, gnsstk::StringUtils::FFLead v)
Definition: FormattedDouble_T.cpp:44
gnsstk::printTime
std::string printTime(const CommonTime &t, const std::string &fmt)
Definition: TimeString.cpp:64
gnsstk::WxObservation::t
CommonTime t
Time that this data was collected, in Rx GPS time.
Definition: WxObsMap.hpp:81
std
Definition: Angle.hpp:142
GNSSTK_THROW
#define GNSSTK_THROW(exc)
Definition: Exception.hpp:366
gnsstk::WxObservation::humidity
float humidity
percent
Definition: WxObsMap.hpp:84
example4.pressure
pressure
Definition: example4.py:36
TimeString.hpp


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