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

Last change on this file since 1834 was 1834, checked in by mervart, 15 years ago

* empty log message *

File size: 7.5 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() {
88
89 if (!_fileNameSkl.isEmpty()) {
90
91 bncSettings settings;
92
93 QDateTime datTim = currentDateAndTimeGPS();
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'))
100 + hlpStr + datTim.toString(".yyC");
101
102 if (_fileName == fileName) {
103 return;
104 }
105 else {
106 _fileName = fileName;
107 }
108
109 delete _out;
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 }
117 }
118}
119
120//
121////////////////////////////////////////////////////////////////////////////
122t_irc RTCM3coDecoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
123
124 errmsg.clear();
125
126 _buffer.append(QByteArray(buffer,bufLen));
127
128 t_irc retCode = failure;
129
130 while(_buffer.size()) {
131
132 int bytesused = 0;
133 struct ClockOrbit co_sav;
134 memcpy(&co_sav, &_co, sizeof(co_sav)); // save state
135
136 GCOB_RETURN irc = GetClockOrbitBias(&_co, &_bias, _buffer.data(),
137 _buffer.size(), &bytesused);
138
139 if (irc <= -30) { // not enough data - restore state and exit loop
140 memcpy(&_co, &co_sav, sizeof(co_sav));
141 break;
142 }
143
144 else if (irc < 0) { // error - skip 1 byte and retry
145 memset(&_co, 0, sizeof(_co));
146 _buffer = _buffer.mid(1);
147 }
148
149 else { // OK or MESSAGEFOLLOWS
150 _buffer = _buffer.mid(bytesused);
151
152 if (irc == GCOBR_OK) {
153 reopen();
154 int GPSweek;
155 currentGPSWeeks(GPSweek, _GPSweeks);
156 if (_co.NumberOfGPSSat > 0) {
157 if (_GPSweeks > _co.GPSEpochTime + 86400.0) {
158 GPSweek += 1;
159 }
160 else if (_GPSweeks < _co.GPSEpochTime - 86400.0) {
161 GPSweek -= 1;
162 }
163 _GPSweeks = _co.GPSEpochTime;
164 }
165 else {
166
167 // Guess GPS week and sec using system time
168 // ----------------------------------------
169 int week;
170 double sec;
171 currentGPSWeeks(week, sec);
172 int weekDay = int(sec/86400.0);
173 int GPSDaySecHlp = int(sec) - weekDay * 86400;
174
175 // cout << "week, sec " << week << " " << sec << endl;
176
177 // Second of day (GPS time) from Glonass Epoch
178 // -------------------------------------------
179 QDate date = dateAndTimeFromGPSweek(week, sec).date();
180 int leapSecond = gnumleap(date.year(), date.month(), date.day());
181 int GPSDaySec = _co.GLONASSEpochTime + 3 * 3600 + leapSecond;
182
183 // cout << "GlonassEpoch, leapSecond, GPSDaySec "
184 // << _co.GLONASSEpochTime << " " << leapSecond << " "
185 // << GPSDaySec << endl;
186
187 // Handle the difference between system clock and correction epoch
188 // ---------------------------------------------------------------
189 if (GPSDaySec < GPSDaySecHlp - 3600) {
190 weekDay += 1;
191 }
192 else if (GPSDaySec > GPSDaySecHlp + 3600) {
193 weekDay -= 1;
194 }
195
196 _GPSweeks = weekDay * 86400.0 + GPSDaySec;
197
198 // cout << "weekDay, _GPSweeks " << weekDay << " "
199 // << _GPSweeks << endl;
200 }
201
202 for(int ii = 0; ii < _co.NumberOfGPSSat; ++ii) {
203 QString line;
204 line.sprintf("%d %.1f G%2.2d %3d %8.3f %8.3f %8.3f %8.3f",
205 GPSweek, _GPSweeks, _co.Sat[ii].ID, _co.Sat[ii].IOD,
206 _co.Sat[ii].Clock.DeltaA0,
207 _co.Sat[ii].Orbit.DeltaRadial,
208 _co.Sat[ii].Orbit.DeltaAlongTrack,
209 _co.Sat[ii].Orbit.DeltaCrossTrack);
210 long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
211 printLine(line, coTime);
212 }
213 for(int ii = CLOCKORBIT_NUMGPS;
214 ii < CLOCKORBIT_NUMGPS + _co.NumberOfGLONASSSat; ++ii) {
215 QString line;
216 line.sprintf("%d %.1f R%2.2d %3d %8.3f %8.3f %8.3f %8.3f",
217 GPSweek, _GPSweeks, _co.Sat[ii].ID, _co.Sat[ii].IOD,
218 _co.Sat[ii].Clock.DeltaA0,
219 _co.Sat[ii].Orbit.DeltaRadial,
220 _co.Sat[ii].Orbit.DeltaAlongTrack,
221 _co.Sat[ii].Orbit.DeltaCrossTrack);
222 long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
223 printLine(line, coTime);
224 }
225 retCode = success;
226 memset(&_co, 0, sizeof(_co));
227 }
228 }
229 }
230
231 if (retCode != success) {
232 _GPSweeks = -1.0;
233 }
234 return retCode;
235}
236
237//
238////////////////////////////////////////////////////////////////////////////
239void RTCM3coDecoder::printLine(const QString& line, long coTime) {
240 if (_out) {
241 *_out << line.toAscii().data() << endl;
242 _out->flush();
243 }
244
245 emit newCorrLine(line, _staID, coTime);
246}
Note: See TracBrowser for help on using the repository browser.