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

Last change on this file since 3077 was 3077, 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 memset(&_bias, 0, sizeof(_bias));
78}
79
80// Destructor
81////////////////////////////////////////////////////////////////////////////
82RTCM3coDecoder::~RTCM3coDecoder() {
83 delete _out;
84}
85
86// Reopen Output File
87////////////////////////////////////////////////////////////////////////
88void RTCM3coDecoder::reopen(const QString& fileNameSkl, QString& fileName,
89 ofstream*& out) {
90
91 if (!fileNameSkl.isEmpty()) {
92
93 bncSettings settings;
94
95 QDateTime datTim = currentDateAndTimeGPS();
96
97 QString hlpStr = bncRinex::nextEpochStr(datTim,
98 settings.value("corrIntr").toString());
99
100 QString fileNameHlp = fileNameSkl
101 + QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'))
102 + hlpStr + datTim.toString(".yyC");
103
104 if (fileName == fileNameHlp) {
105 return;
106 }
107 else {
108 fileName = fileNameHlp;
109 }
110
111 delete out;
112 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
113 out = new ofstream( fileName.toAscii().data(),
114 ios_base::out | ios_base::app );
115 }
116 else {
117 out = new ofstream( fileName.toAscii().data() );
118 }
119 }
120}
121
122//
123////////////////////////////////////////////////////////////////////////////
124t_irc RTCM3coDecoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
125
126 errmsg.clear();
127
128 _buffer.append(QByteArray(buffer,bufLen));
129
130 t_irc retCode = failure;
131
132 while(_buffer.size()) {
133
134 int bytesused = 0;
135 struct ClockOrbit co_sav;
136 memcpy(&co_sav, &_co, sizeof(co_sav)); // save state
137
138 GCOB_RETURN irc = GetClockOrbitBias(&_co, &_bias, _buffer.data(),
139 _buffer.size(), &bytesused);
140
141 if (irc <= -30) { // not enough data - restore state and exit loop
142 memcpy(&_co, &co_sav, sizeof(co_sav));
143 break;
144 }
145
146 else if (irc < 0) { // error - skip 1 byte and retry
147 memset(&_co, 0, sizeof(_co));
148 memset(&_bias, 0, sizeof(_bias));
149 _buffer = _buffer.mid(bytesused ? bytesused : 1);
150 }
151
152 else { // OK or MESSAGEFOLLOWS
153 _buffer = _buffer.mid(bytesused);
154
155 if ( irc == GCOBR_OK &&
156 (_co.NumberOfGPSSat > 0 || _co.NumberOfGLONASSSat > 0) ) {
157
158 reopen(_fileNameSkl, _fileName, _out);
159
160 // Guess GPS week and sec using system time
161 // ----------------------------------------
162 int GPSweek;
163 double GPSweeksHlp;
164 currentGPSWeeks(GPSweek, GPSweeksHlp);
165
166 // Correction Epoch from GPSEpochTime
167 // ----------------------------------
168 if (_co.NumberOfGPSSat > 0) {
169 if (GPSweeksHlp > _co.GPSEpochTime + 86400.0) {
170 GPSweek += 1;
171 }
172 else if (GPSweeksHlp < _co.GPSEpochTime - 86400.0) {
173 GPSweek -= 1;
174 }
175 _GPSweeks = _co.GPSEpochTime;
176 }
177
178 // Correction Epoch from Glonass Epoch
179 // -----------------------------------
180 else if (_co.NumberOfGLONASSSat > 0){
181
182 // Second of day (GPS time) from Glonass Epoch
183 // -------------------------------------------
184 QDate date = dateAndTimeFromGPSweek(GPSweek, GPSweeksHlp).date();
185 int leapSecond = gnumleap(date.year(), date.month(), date.day());
186 int GPSDaySec = _co.GLONASSEpochTime - 3 * 3600 + leapSecond;
187
188 int weekDay = int(GPSweeksHlp/86400.0);
189 int GPSDaySecHlp = int(GPSweeksHlp) - weekDay * 86400;
190
191 // Handle the difference between system clock and correction epoch
192 // ---------------------------------------------------------------
193 if (GPSDaySec < GPSDaySecHlp - 3600) {
194 weekDay += 1;
195 if (weekDay > 6) {
196 weekDay = 0;
197 GPSweek += 1;
198 }
199 }
200 else if (GPSDaySec > GPSDaySecHlp + 3600) {
201 weekDay -= 1;
202 if (weekDay < 0) {
203 weekDay = 6;
204 GPSweek -= 1;
205 }
206 }
207
208 _GPSweeks = weekDay * 86400.0 + GPSDaySec;
209 }
210
211 QStringList asciiLines = corrsToASCIIlines(GPSweek, _GPSweeks,
212 _co, &_bias);
213
214 long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
215
216 QStringListIterator it(asciiLines);
217 while (it.hasNext()) {
218 QString line = it.next();
219 printLine(line, coTime);
220 }
221
222 retCode = success;
223 memset(&_co, 0, sizeof(_co));
224 memset(&_bias, 0, sizeof(_bias));
225 }
226 }
227 }
228
229 if (retCode != success) {
230 _GPSweeks = -1.0;
231 }
232 return retCode;
233}
234
235//
236////////////////////////////////////////////////////////////////////////////
237void RTCM3coDecoder::printLine(const QString& line, long coTime) {
238 if (_out) {
239 *_out << line.toAscii().data() << endl;
240 _out->flush();
241 }
242
243 emit newCorrLine(line, _staID, coTime);
244}
245
246//
247////////////////////////////////////////////////////////////////////////////
248QStringList RTCM3coDecoder::corrsToASCIIlines(int GPSweek, double GPSweeks,
249 const ClockOrbit& co,
250 const Bias* bias) {
251
252 QStringList retLines;
253
254 // Loop over all satellites (GPS and Glonass)
255 // ------------------------------------------
256 if (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) {
257 QString line1;
258 line1.sprintf("! Orbits/Clocks: %d GPS %d Glonass",
259 co.NumberOfGPSSat, co.NumberOfGLONASSSat);
260 retLines << line1;
261 }
262 for (int ii = 0; ii < CLOCKORBIT_NUMGPS+co.NumberOfGLONASSSat; ii++) {
263 char sysCh = ' ';
264 if (ii < co.NumberOfGPSSat) {
265 sysCh = 'G';
266 }
267 else if (ii >= CLOCKORBIT_NUMGPS) {
268 sysCh = 'R';
269 }
270
271 if (sysCh != ' ') {
272
273 QString linePart;
274 linePart.sprintf("%d %d %d %.1f %c%2.2d",
275 co.messageType, co.UpdateInterval, GPSweek, GPSweeks,
276 sysCh, co.Sat[ii].ID);
277
278 // Combined message (orbit and clock)
279 // ----------------------------------
280 if ( co.messageType == COTYPE_GPSCOMBINED ||
281 co.messageType == COTYPE_GLONASSCOMBINED ) {
282 QString line;
283 line.sprintf(" %3d"
284 " %8.3f %8.3f %8.3f %8.3f"
285 " %10.5f %10.5f %10.5f %10.5f"
286 " %10.5f",
287 co.Sat[ii].IOD,
288 co.Sat[ii].Clock.DeltaA0,
289 co.Sat[ii].Orbit.DeltaRadial,
290 co.Sat[ii].Orbit.DeltaAlongTrack,
291 co.Sat[ii].Orbit.DeltaCrossTrack,
292 co.Sat[ii].Clock.DeltaA1,
293 co.Sat[ii].Orbit.DotDeltaRadial,
294 co.Sat[ii].Orbit.DotDeltaAlongTrack,
295 co.Sat[ii].Orbit.DotDeltaCrossTrack,
296 co.Sat[ii].Clock.DeltaA2);
297 retLines << linePart+line;
298 }
299
300 // Orbits only
301 // -----------
302 else if ( co.messageType == COTYPE_GPSORBIT ||
303 co.messageType == COTYPE_GLONASSORBIT ) {
304 QString line;
305 line.sprintf(" %3d"
306 " %8.3f %8.3f %8.3f"
307 " %10.5f %10.5f %10.5f",
308 co.Sat[ii].IOD,
309 co.Sat[ii].Orbit.DeltaRadial,
310 co.Sat[ii].Orbit.DeltaAlongTrack,
311 co.Sat[ii].Orbit.DeltaCrossTrack,
312 co.Sat[ii].Orbit.DotDeltaRadial,
313 co.Sat[ii].Orbit.DotDeltaAlongTrack,
314 co.Sat[ii].Orbit.DotDeltaCrossTrack);
315 retLines << linePart+line;
316 }
317
318 // Clocks only
319 // -----------
320 else if ( co.messageType == COTYPE_GPSCLOCK ||
321 co.messageType == COTYPE_GLONASSCLOCK ) {
322 QString line;
323 line.sprintf(" %3d %8.3f %10.5f %10.5f",
324 co.Sat[ii].IOD,
325 co.Sat[ii].Clock.DeltaA0,
326 co.Sat[ii].Clock.DeltaA1,
327 co.Sat[ii].Clock.DeltaA2);
328 retLines << linePart+line;
329 }
330
331 // User Range Accuracy
332 // -------------------
333 else if ( co.messageType == COTYPE_GPSURA ||
334 co.messageType == COTYPE_GLONASSURA ) {
335 QString line;
336 line.sprintf(" %3d %f",
337 co.Sat[ii].IOD, co.Sat[ii].UserRangeAccuracy);
338 retLines << linePart+line;
339 }
340
341 // High-Resolution Clocks
342 // ----------------------
343 else if ( co.messageType == COTYPE_GPSHR ||
344 co.messageType == COTYPE_GLONASSHR ) {
345 QString line;
346 line.sprintf(" %3d %8.3f",
347 co.Sat[ii].IOD, co.Sat[ii].hrclock);
348 retLines << linePart+line;
349 }
350 }
351 }
352
353 // Loop over all satellites (GPS and Glonass)
354 // ------------------------------------------
355 if (bias) {
356 if (bias->NumberOfGPSSat > 0 || bias->NumberOfGLONASSSat > 0) {
357 QString line1;
358 line1.sprintf("! Biases: %d GPS %d Glonass",
359 bias->NumberOfGPSSat, bias->NumberOfGLONASSSat);
360 retLines << line1;
361 }
362 for (int ii = 0; ii < CLOCKORBIT_NUMGPS + bias->NumberOfGLONASSSat; ii++) {
363 char sysCh = ' ';
364 int messageType;
365 if (ii < bias->NumberOfGPSSat) {
366 sysCh = 'G';
367 messageType = BTYPE_GPS;
368 }
369 else if (ii >= CLOCKORBIT_NUMGPS) {
370 sysCh = 'R';
371 messageType = BTYPE_GLONASS;
372 }
373 if (sysCh != ' ') {
374 QString line;
375 line.sprintf("%d %d %d %.1f %c%2.2d %d",
376 messageType, bias->UpdateInterval, GPSweek, GPSweeks,
377 sysCh, bias->Sat[ii].ID,
378 bias->Sat[ii].NumberOfCodeBiases);
379 for (int jj = 0; jj < bias->Sat[ii].NumberOfCodeBiases; jj++) {
380 QString hlp;
381 hlp.sprintf(" %d %8.3f", bias->Sat[ii].Biases[jj].Type,
382 bias->Sat[ii].Biases[jj].Bias);
383 line += hlp;
384 }
385 retLines << line;
386 }
387 }
388 }
389
390 return retLines;
391}
Note: See TracBrowser for help on using the repository browser.