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

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