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

Last change on this file since 3059 was 3035, checked in by mervart, 14 years ago
File size: 12.1 KB
RevLine 
[866]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
[868]41#include <stdio.h>
[920]42#include <math.h>
[868]43
[866]44#include "RTCM3coDecoder.h"
[918]45#include "bncutils.h"
[934]46#include "bncrinex.h"
[936]47#include "bncapp.h"
[1535]48#include "bncsettings.h"
[1834]49#include "rtcm3torinex.h"
[866]50
51using namespace std;
52
53// Constructor
54////////////////////////////////////////////////////////////////////////////
[970]55RTCM3coDecoder::RTCM3coDecoder(const QString& staID) {
[934]56
[970]57 _staID = staID;
58
[934]59 // File Output
60 // -----------
[1535]61 bncSettings settings;
[934]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 }
[970]68 _fileNameSkl = path + staID;
[934]69 }
[1582]70 _out = 0;
71 _GPSweeks = -1.0;
[934]72
[974]73 connect(this, SIGNAL(newCorrLine(QString, QString, long)),
[2585]74 (bncApp*) qApp, SLOT(slotNewCorrLine(QString, QString, long)));
[1828]75
76 memset(&_co, 0, sizeof(_co));
[866]77}
78
79// Destructor
80////////////////////////////////////////////////////////////////////////////
81RTCM3coDecoder::~RTCM3coDecoder() {
[935]82 delete _out;
[866]83}
84
[934]85// Reopen Output File
86////////////////////////////////////////////////////////////////////////
[3035]87void RTCM3coDecoder::reopen(const QString& fileNameSkl, QString& fileName,
88 ofstream*& out) {
[934]89
[3035]90 if (!fileNameSkl.isEmpty()) {
[934]91
[1535]92 bncSettings settings;
[934]93
[1154]94 QDateTime datTim = currentDateAndTimeGPS();
[934]95
96 QString hlpStr = bncRinex::nextEpochStr(datTim,
97 settings.value("corrIntr").toString());
98
[3035]99 QString fileNameHlp = fileNameSkl
[934]100 + QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'))
[938]101 + hlpStr + datTim.toString(".yyC");
[934]102
[3035]103 if (fileName == fileNameHlp) {
[934]104 return;
105 }
106 else {
[3035]107 fileName = fileNameHlp;
[934]108 }
109
[3035]110 delete out;
[1727]111 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
[3035]112 out = new ofstream( fileName.toAscii().data(),
[1727]113 ios_base::out | ios_base::app );
114 }
115 else {
[3035]116 out = new ofstream( fileName.toAscii().data() );
[1727]117 }
[934]118 }
119}
120
[866]121//
122////////////////////////////////////////////////////////////////////////////
[1227]123t_irc RTCM3coDecoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
[868]124
[1218]125 errmsg.clear();
126
[1227]127 _buffer.append(QByteArray(buffer,bufLen));
[868]128
[1023]129 t_irc retCode = failure;
130
[1832]131 while(_buffer.size()) {
132
[879]133 int bytesused = 0;
[1832]134 struct ClockOrbit co_sav;
135 memcpy(&co_sav, &_co, sizeof(co_sav)); // save state
[1829]136
137 GCOB_RETURN irc = GetClockOrbitBias(&_co, &_bias, _buffer.data(),
[879]138 _buffer.size(), &bytesused);
139
[1833]140 if (irc <= -30) { // not enough data - restore state and exit loop
[1832]141 memcpy(&_co, &co_sav, sizeof(co_sav));
[1833]142 break;
[869]143 }
[1832]144
[1833]145 else if (irc < 0) { // error - skip 1 byte and retry
146 memset(&_co, 0, sizeof(_co));
[1842]147 memset(&_bias, 0, sizeof(_bias));
148 _buffer = _buffer.mid(bytesused ? bytesused : 1);
[1833]149 }
150
151 else { // OK or MESSAGEFOLLOWS
[1828]152 _buffer = _buffer.mid(bytesused);
153
[2435]154 if ( irc == GCOBR_OK &&
155 (_co.NumberOfGPSSat > 0 || _co.NumberOfGLONASSSat > 0) ) {
156
[3035]157 reopen(_fileNameSkl, _fileName, _out);
[1835]158
159 // Guess GPS week and sec using system time
160 // ----------------------------------------
[1829]161 int GPSweek;
[1835]162 double GPSweeksHlp;
163 currentGPSWeeks(GPSweek, GPSweeksHlp);
164
165 // Correction Epoch from GPSEpochTime
166 // ----------------------------------
[2435]167 if (_co.NumberOfGPSSat > 0) {
[1835]168 if (GPSweeksHlp > _co.GPSEpochTime + 86400.0) {
[919]169 GPSweek += 1;
170 }
[1835]171 else if (GPSweeksHlp < _co.GPSEpochTime - 86400.0) {
[919]172 GPSweek -= 1;
173 }
[1829]174 _GPSweeks = _co.GPSEpochTime;
[919]175 }
[1835]176
177 // Correction Epoch from Glonass Epoch
178 // -----------------------------------
[1842]179 else if (_co.NumberOfGLONASSSat > 0){
[1834]180
181 // Second of day (GPS time) from Glonass Epoch
182 // -------------------------------------------
[1835]183 QDate date = dateAndTimeFromGPSweek(GPSweek, GPSweeksHlp).date();
[1834]184 int leapSecond = gnumleap(date.year(), date.month(), date.day());
[1837]185 int GPSDaySec = _co.GLONASSEpochTime - 3 * 3600 + leapSecond;
[1834]186
[1835]187 int weekDay = int(GPSweeksHlp/86400.0);
188 int GPSDaySecHlp = int(GPSweeksHlp) - weekDay * 86400;
[1834]189
190 // Handle the difference between system clock and correction epoch
191 // ---------------------------------------------------------------
192 if (GPSDaySec < GPSDaySecHlp - 3600) {
[1829]193 weekDay += 1;
[1835]194 if (weekDay > 6) {
195 weekDay = 0;
196 GPSweek += 1;
197 }
[1829]198 }
[1834]199 else if (GPSDaySec > GPSDaySecHlp + 3600) {
[1829]200 weekDay -= 1;
[1835]201 if (weekDay < 0) {
202 weekDay = 6;
203 GPSweek -= 1;
204 }
[1834]205 }
206
207 _GPSweeks = weekDay * 86400.0 + GPSDaySec;
[1829]208 }
[918]209
[3022]210 QStringList asciiLines = corrsToASCIIlines(GPSweek, _GPSweeks,
[3024]211 _co, &_bias);
[3022]212
[1852]213 long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
214
[3022]215 QStringListIterator it(asciiLines);
216 while (it.hasNext()) {
217 QString line = it.next();
218 printLine(line, coTime);
[1852]219 }
220
[1829]221 retCode = success;
222 memset(&_co, 0, sizeof(_co));
[1842]223 memset(&_bias, 0, sizeof(_bias));
[869]224 }
[1829]225 }
[1833]226 }
[1832]227
[1833]228 if (retCode != success) {
229 _GPSweeks = -1.0;
[868]230 }
[1829]231 return retCode;
[866]232}
[934]233
234//
235////////////////////////////////////////////////////////////////////////////
[974]236void RTCM3coDecoder::printLine(const QString& line, long coTime) {
[934]237 if (_out) {
[971]238 *_out << line.toAscii().data() << endl;
[934]239 _out->flush();
240 }
241
[974]242 emit newCorrLine(line, _staID, coTime);
[934]243}
[3022]244
245//
246////////////////////////////////////////////////////////////////////////////
247QStringList RTCM3coDecoder::corrsToASCIIlines(int GPSweek, double GPSweeks,
248 const ClockOrbit& co,
[3024]249 const Bias* bias) {
[3022]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 }
[3024]269
[3022]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 // ------------------------------------------
[3024]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;
[3022]360 }
[3024]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;
[3022]367 }
[3024]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 }
[3022]386 }
387 }
388
389 return retLines;
390}
Note: See TracBrowser for help on using the repository browser.