source: ntrip/trunk/BNC/RTCM3/RTCM3coDecoder.cpp@ 2545

Last change on this file since 2545 was 2545, checked in by mervart, 14 years ago
File size: 12.1 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: RTCM3coDecoder
30 *
31 * Purpose: RTCM3 Clock Orbit Decoder
32 *
33 * Author: L. Mervart
34 *
35 * Created: 05-May-2008
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <stdio.h>
42#include <math.h>
43
44#include "RTCM3coDecoder.h"
45#include "bncutils.h"
46#include "bncrinex.h"
47#include "bncapp.h"
48#include "bncsettings.h"
49#include "rtcm3torinex.h"
50
51using namespace std;
52
53// Constructor
54////////////////////////////////////////////////////////////////////////////
55RTCM3coDecoder::RTCM3coDecoder(const QString& staID) {
56
57 _staID = staID;
58
59 // File Output
60 // -----------
61 bncSettings settings;
62 QString path = settings.value("corrPath").toString();
63 if (!path.isEmpty()) {
64 expandEnvVar(path);
65 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
66 path += QDir::separator();
67 }
68 _fileNameSkl = path + staID;
69 }
70 _out = 0;
71 _GPSweeks = -1.0;
72
73 connect(this, SIGNAL(newCorrLine(QString, QString, long)),
74 (bncApp*) qApp, SLOT(slotNewCorrLine(QString, QString, long)),
75 Qt::DirectConnection);
76
77 memset(&_co, 0, sizeof(_co));
78}
79
80// Destructor
81////////////////////////////////////////////////////////////////////////////
82RTCM3coDecoder::~RTCM3coDecoder() {
83 delete _out;
84}
85
86// Reopen Output File
87////////////////////////////////////////////////////////////////////////
88void RTCM3coDecoder::reopen() {
89
90 if (!_fileNameSkl.isEmpty()) {
91
92 bncSettings settings;
93
94 QDateTime datTim = currentDateAndTimeGPS();
95
96 QString hlpStr = bncRinex::nextEpochStr(datTim,
97 settings.value("corrIntr").toString());
98
99 QString fileName = _fileNameSkl
100 + QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'))
101 + hlpStr + datTim.toString(".yyC");
102
103 if (_fileName == fileName) {
104 return;
105 }
106 else {
107 _fileName = fileName;
108 }
109
110 delete _out;
111 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
112 _out = new ofstream( _fileName.toAscii().data(),
113 ios_base::out | ios_base::app );
114 }
115 else {
116 _out = new ofstream( _fileName.toAscii().data() );
117 }
118 }
119}
120
121//
122////////////////////////////////////////////////////////////////////////////
123t_irc RTCM3coDecoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
124
125 errmsg.clear();
126
127 _buffer.append(QByteArray(buffer,bufLen));
128
129 t_irc retCode = failure;
130
131 while(_buffer.size()) {
132
133 int bytesused = 0;
134 struct ClockOrbit co_sav;
135 memcpy(&co_sav, &_co, sizeof(co_sav)); // save state
136
137 GCOB_RETURN irc = GetClockOrbitBias(&_co, &_bias, _buffer.data(),
138 _buffer.size(), &bytesused);
139
140 if (irc <= -30) { // not enough data - restore state and exit loop
141 memcpy(&_co, &co_sav, sizeof(co_sav));
142 break;
143 }
144
145 else if (irc < 0) { // error - skip 1 byte and retry
146 memset(&_co, 0, sizeof(_co));
147 memset(&_bias, 0, sizeof(_bias));
148 _buffer = _buffer.mid(bytesused ? bytesused : 1);
149 }
150
151 else { // OK or MESSAGEFOLLOWS
152 _buffer = _buffer.mid(bytesused);
153
154 if ( irc == GCOBR_OK &&
155 (_co.NumberOfGPSSat > 0 || _co.NumberOfGLONASSSat > 0) ) {
156
157 reopen();
158
159 // Guess GPS week and sec using system time
160 // ----------------------------------------
161 int GPSweek;
162 double GPSweeksHlp;
163 currentGPSWeeks(GPSweek, GPSweeksHlp);
164
165 // Correction Epoch from GPSEpochTime
166 // ----------------------------------
167 if (_co.NumberOfGPSSat > 0) {
168 if (GPSweeksHlp > _co.GPSEpochTime + 86400.0) {
169 GPSweek += 1;
170 }
171 else if (GPSweeksHlp < _co.GPSEpochTime - 86400.0) {
172 GPSweek -= 1;
173 }
174 _GPSweeks = _co.GPSEpochTime;
175 }
176
177 // Correction Epoch from Glonass Epoch
178 // -----------------------------------
179 else if (_co.NumberOfGLONASSSat > 0){
180
181 // Second of day (GPS time) from Glonass Epoch
182 // -------------------------------------------
183 QDate date = dateAndTimeFromGPSweek(GPSweek, GPSweeksHlp).date();
184 int leapSecond = gnumleap(date.year(), date.month(), date.day());
185 int GPSDaySec = _co.GLONASSEpochTime - 3 * 3600 + leapSecond;
186
187 int weekDay = int(GPSweeksHlp/86400.0);
188 int GPSDaySecHlp = int(GPSweeksHlp) - weekDay * 86400;
189
190 // Handle the difference between system clock and correction epoch
191 // ---------------------------------------------------------------
192 if (GPSDaySec < GPSDaySecHlp - 3600) {
193 weekDay += 1;
194 if (weekDay > 6) {
195 weekDay = 0;
196 GPSweek += 1;
197 }
198 }
199 else if (GPSDaySec > GPSDaySecHlp + 3600) {
200 weekDay -= 1;
201 if (weekDay < 0) {
202 weekDay = 6;
203 GPSweek -= 1;
204 }
205 }
206
207 _GPSweeks = weekDay * 86400.0 + GPSDaySec;
208 }
209
210 long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
211
212 // Loop over all satellites (GPS and Glonass)
213 // ------------------------------------------
214 if (_co.NumberOfGPSSat > 0 || _co.NumberOfGLONASSSat > 0) {
215 QString line1;
216 line1.sprintf("! Orbits/Clocks: %d GPS %d Glonass",
217 _co.NumberOfGPSSat, _co.NumberOfGLONASSSat);
218 printLine(line1, coTime);
219 }
220 for (int ii = 0; ii < CLOCKORBIT_NUMGPS+_co.NumberOfGLONASSSat; ii++) {
221 char sysCh = ' ';
222 if (ii < _co.NumberOfGPSSat) {
223 sysCh = 'G';
224 }
225 else if (ii >= CLOCKORBIT_NUMGPS) {
226 sysCh = 'R';
227 }
228 if (sysCh != ' ') {
229
230 QString linePart;
231 linePart.sprintf("%d %d %d %.1f %c%2.2d",
232 _co.messageType, _co.UpdateInterval, GPSweek, _GPSweeks,
233 sysCh, _co.Sat[ii].ID);
234
235 // Combined message (orbit and clock)
236 // ----------------------------------
237 if ( _co.messageType == COTYPE_GPSCOMBINED ||
238 _co.messageType == COTYPE_GLONASSCOMBINED ) {
239 QString line;
240 line.sprintf(" %3d"
241 " %8.3f %8.3f %8.3f %8.3f"
242 " %10.5f %10.5f %10.5f %10.5f"
243 " %10.5f",
244 _co.Sat[ii].IOD,
245 _co.Sat[ii].Clock.DeltaA0,
246 _co.Sat[ii].Orbit.DeltaRadial,
247 _co.Sat[ii].Orbit.DeltaAlongTrack,
248 _co.Sat[ii].Orbit.DeltaCrossTrack,
249 _co.Sat[ii].Clock.DeltaA1,
250 _co.Sat[ii].Orbit.DotDeltaRadial,
251 _co.Sat[ii].Orbit.DotDeltaAlongTrack,
252 _co.Sat[ii].Orbit.DotDeltaCrossTrack,
253 _co.Sat[ii].Clock.DeltaA2);
254 printLine(linePart+line, coTime);
255 }
256
257 // Orbits only
258 // -----------
259 else if ( _co.messageType == COTYPE_GPSORBIT ||
260 _co.messageType == COTYPE_GLONASSORBIT ) {
261 QString line;
262 line.sprintf(" %3d"
263 " %8.3f %8.3f %8.3f"
264 " %10.5f %10.5f %10.5f",
265 _co.Sat[ii].IOD,
266 _co.Sat[ii].Orbit.DeltaRadial,
267 _co.Sat[ii].Orbit.DeltaAlongTrack,
268 _co.Sat[ii].Orbit.DeltaCrossTrack,
269 _co.Sat[ii].Orbit.DotDeltaRadial,
270 _co.Sat[ii].Orbit.DotDeltaAlongTrack,
271 _co.Sat[ii].Orbit.DotDeltaCrossTrack);
272 printLine(linePart+line, coTime);
273 }
274
275 // Clocks only
276 // -----------
277 else if ( _co.messageType == COTYPE_GPSCLOCK ||
278 _co.messageType == COTYPE_GLONASSCLOCK ) {
279 QString line;
280 line.sprintf(" %3d %8.3f %10.5f %10.5f",
281 _co.Sat[ii].IOD,
282 _co.Sat[ii].Clock.DeltaA0,
283 _co.Sat[ii].Clock.DeltaA1,
284 _co.Sat[ii].Clock.DeltaA2);
285 printLine(linePart+line, coTime);
286 }
287
288 // User Range Accuracy
289 // -------------------
290 else if ( _co.messageType == COTYPE_GPSURA ||
291 _co.messageType == COTYPE_GLONASSURA ) {
292 QString line;
293 line.sprintf(" %3d %f",
294 _co.Sat[ii].IOD, _co.Sat[ii].UserRangeAccuracy);
295 printLine(linePart+line, coTime);
296 }
297
298 // High-Resolution Clocks
299 // ----------------------
300 else if ( _co.messageType == COTYPE_GPSHR ||
301 _co.messageType == COTYPE_GLONASSHR ) {
302 QString line;
303 line.sprintf(" %3d %8.3f",
304 _co.Sat[ii].IOD, _co.Sat[ii].hrclock);
305 printLine(linePart+line, coTime);
306 }
307 }
308 }
309
310 // Loop over all satellites (GPS and Glonass)
311 // ------------------------------------------
312 if (_bias.NumberOfGPSSat > 0 || _bias.NumberOfGLONASSSat > 0) {
313 QString line1;
314 line1.sprintf("! Biases: %d GPS %d Glonass",
315 _bias.NumberOfGPSSat, _bias.NumberOfGLONASSSat);
316 printLine(line1, coTime);
317 }
318 for (int ii = 0; ii < CLOCKORBIT_NUMGPS + _bias.NumberOfGLONASSSat; ii++) {
319 char sysCh = ' ';
320 int messageType;
321 if (ii < _bias.NumberOfGPSSat) {
322 sysCh = 'G';
323 messageType = BTYPE_GPS;
324 }
325 else if (ii >= CLOCKORBIT_NUMGPS) {
326 sysCh = 'R';
327 messageType = BTYPE_GLONASS;
328 }
329 if (sysCh != ' ') {
330 QString line;
331 line.sprintf("%d %d %d %.1f %c%2.2d %d",
332 messageType, _bias.UpdateInterval, GPSweek, _GPSweeks,
333 sysCh, _bias.Sat[ii].ID,
334 _bias.Sat[ii].NumberOfCodeBiases);
335 for (int jj = 0; jj < _bias.Sat[ii].NumberOfCodeBiases; jj++) {
336 QString hlp;
337 hlp.sprintf(" %d %8.3f", _bias.Sat[ii].Biases[jj].Type,
338 _bias.Sat[ii].Biases[jj].Bias);
339 line += hlp;
340 }
341 printLine(line, coTime);
342 }
343 }
344
345 retCode = success;
346 memset(&_co, 0, sizeof(_co));
347 memset(&_bias, 0, sizeof(_bias));
348 }
349 }
350 }
351
352 if (retCode != success) {
353 _GPSweeks = -1.0;
354 }
355 return retCode;
356}
357
358//
359////////////////////////////////////////////////////////////////////////////
360void RTCM3coDecoder::printLine(const QString& line, long coTime) {
361 if (_out) {
362 *_out << line.toAscii().data() << endl;
363 _out->flush();
364 }
365
366 emit newCorrLine(line, _staID, coTime);
367}
Note: See TracBrowser for help on using the repository browser.