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

Last change on this file since 3035 was 3035, checked in by mervart, 13 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
76 memset(&_co, 0, sizeof(_co));
77}
78
79// Destructor
80////////////////////////////////////////////////////////////////////////////
81RTCM3coDecoder::~RTCM3coDecoder() {
82 delete _out;
83}
84
85// Reopen Output File
86////////////////////////////////////////////////////////////////////////
87void RTCM3coDecoder::reopen(const QString& fileNameSkl, QString& fileName,
88 ofstream*& out) {
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 fileNameHlp = fileNameSkl
100 + QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'))
101 + hlpStr + datTim.toString(".yyC");
102
103 if (fileName == fileNameHlp) {
104 return;
105 }
106 else {
107 fileName = fileNameHlp;
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(_fileNameSkl, _fileName, _out);
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 QStringList asciiLines = corrsToASCIIlines(GPSweek, _GPSweeks,
211 _co, &_bias);
212
213 long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
214
215 QStringListIterator it(asciiLines);
216 while (it.hasNext()) {
217 QString line = it.next();
218 printLine(line, coTime);
219 }
220
221 retCode = success;
222 memset(&_co, 0, sizeof(_co));
223 memset(&_bias, 0, sizeof(_bias));
224 }
225 }
226 }
227
228 if (retCode != success) {
229 _GPSweeks = -1.0;
230 }
231 return retCode;
232}
233
234//
235////////////////////////////////////////////////////////////////////////////
236void RTCM3coDecoder::printLine(const QString& line, long coTime) {
237 if (_out) {
238 *_out << line.toAscii().data() << endl;
239 _out->flush();
240 }
241
242 emit newCorrLine(line, _staID, coTime);
243}
244
245//
246////////////////////////////////////////////////////////////////////////////
247QStringList RTCM3coDecoder::corrsToASCIIlines(int GPSweek, double GPSweeks,
248 const ClockOrbit& co,
249 const Bias* bias) {
250
251 QStringList retLines;
252
253 // Loop over all satellites (GPS and Glonass)
254 // ------------------------------------------
255 if (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) {
256 QString line1;
257 line1.sprintf("! Orbits/Clocks: %d GPS %d Glonass",
258 co.NumberOfGPSSat, co.NumberOfGLONASSSat);
259 retLines << line1;
260 }
261 for (int ii = 0; ii < CLOCKORBIT_NUMGPS+co.NumberOfGLONASSSat; ii++) {
262 char sysCh = ' ';
263 if (ii < co.NumberOfGPSSat) {
264 sysCh = 'G';
265 }
266 else if (ii >= CLOCKORBIT_NUMGPS) {
267 sysCh = 'R';
268 }
269
270 if (sysCh != ' ') {
271
272 QString linePart;
273 linePart.sprintf("%d %d %d %.1f %c%2.2d",
274 co.messageType, co.UpdateInterval, GPSweek, GPSweeks,
275 sysCh, co.Sat[ii].ID);
276
277 // Combined message (orbit and clock)
278 // ----------------------------------
279 if ( co.messageType == COTYPE_GPSCOMBINED ||
280 co.messageType == COTYPE_GLONASSCOMBINED ) {
281 QString line;
282 line.sprintf(" %3d"
283 " %8.3f %8.3f %8.3f %8.3f"
284 " %10.5f %10.5f %10.5f %10.5f"
285 " %10.5f",
286 co.Sat[ii].IOD,
287 co.Sat[ii].Clock.DeltaA0,
288 co.Sat[ii].Orbit.DeltaRadial,
289 co.Sat[ii].Orbit.DeltaAlongTrack,
290 co.Sat[ii].Orbit.DeltaCrossTrack,
291 co.Sat[ii].Clock.DeltaA1,
292 co.Sat[ii].Orbit.DotDeltaRadial,
293 co.Sat[ii].Orbit.DotDeltaAlongTrack,
294 co.Sat[ii].Orbit.DotDeltaCrossTrack,
295 co.Sat[ii].Clock.DeltaA2);
296 retLines << linePart+line;
297 }
298
299 // Orbits only
300 // -----------
301 else if ( co.messageType == COTYPE_GPSORBIT ||
302 co.messageType == COTYPE_GLONASSORBIT ) {
303 QString line;
304 line.sprintf(" %3d"
305 " %8.3f %8.3f %8.3f"
306 " %10.5f %10.5f %10.5f",
307 co.Sat[ii].IOD,
308 co.Sat[ii].Orbit.DeltaRadial,
309 co.Sat[ii].Orbit.DeltaAlongTrack,
310 co.Sat[ii].Orbit.DeltaCrossTrack,
311 co.Sat[ii].Orbit.DotDeltaRadial,
312 co.Sat[ii].Orbit.DotDeltaAlongTrack,
313 co.Sat[ii].Orbit.DotDeltaCrossTrack);
314 retLines << linePart+line;
315 }
316
317 // Clocks only
318 // -----------
319 else if ( co.messageType == COTYPE_GPSCLOCK ||
320 co.messageType == COTYPE_GLONASSCLOCK ) {
321 QString line;
322 line.sprintf(" %3d %8.3f %10.5f %10.5f",
323 co.Sat[ii].IOD,
324 co.Sat[ii].Clock.DeltaA0,
325 co.Sat[ii].Clock.DeltaA1,
326 co.Sat[ii].Clock.DeltaA2);
327 retLines << linePart+line;
328 }
329
330 // User Range Accuracy
331 // -------------------
332 else if ( co.messageType == COTYPE_GPSURA ||
333 co.messageType == COTYPE_GLONASSURA ) {
334 QString line;
335 line.sprintf(" %3d %f",
336 co.Sat[ii].IOD, co.Sat[ii].UserRangeAccuracy);
337 retLines << linePart+line;
338 }
339
340 // High-Resolution Clocks
341 // ----------------------
342 else if ( co.messageType == COTYPE_GPSHR ||
343 co.messageType == COTYPE_GLONASSHR ) {
344 QString line;
345 line.sprintf(" %3d %8.3f",
346 co.Sat[ii].IOD, co.Sat[ii].hrclock);
347 retLines << linePart+line;
348 }
349 }
350 }
351
352 // Loop over all satellites (GPS and Glonass)
353 // ------------------------------------------
354 if (bias) {
355 if (bias->NumberOfGPSSat > 0 || bias->NumberOfGLONASSSat > 0) {
356 QString line1;
357 line1.sprintf("! Biases: %d GPS %d Glonass",
358 bias->NumberOfGPSSat, bias->NumberOfGLONASSSat);
359 retLines << line1;
360 }
361 for (int ii = 0; ii < CLOCKORBIT_NUMGPS + bias->NumberOfGLONASSSat; ii++) {
362 char sysCh = ' ';
363 int messageType;
364 if (ii < bias->NumberOfGPSSat) {
365 sysCh = 'G';
366 messageType = BTYPE_GPS;
367 }
368 else if (ii >= CLOCKORBIT_NUMGPS) {
369 sysCh = 'R';
370 messageType = BTYPE_GLONASS;
371 }
372 if (sysCh != ' ') {
373 QString line;
374 line.sprintf("%d %d %d %.1f %c%2.2d %d",
375 messageType, bias->UpdateInterval, GPSweek, GPSweeks,
376 sysCh, bias->Sat[ii].ID,
377 bias->Sat[ii].NumberOfCodeBiases);
378 for (int jj = 0; jj < bias->Sat[ii].NumberOfCodeBiases; jj++) {
379 QString hlp;
380 hlp.sprintf(" %d %8.3f", bias->Sat[ii].Biases[jj].Type,
381 bias->Sat[ii].Biases[jj].Bias);
382 line += hlp;
383 }
384 retLines << line;
385 }
386 }
387 }
388
389 return retLines;
390}
Note: See TracBrowser for help on using the repository browser.