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

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