source: ntrip/trunk/BNC/upload/bncrtnetdecoder.cpp@ 3174

Last change on this file since 3174 was 3174, checked in by mervart, 13 years ago
File size: 14.2 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: bncRtnetDecoder
30 *
31 * Purpose: Implementation of RTNet (SP3-like) output decoder
32 *
33 * Author: L. Mervart
34 *
35 * Created: 28-Mar-2011
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42#include "bncrtnetdecoder.h"
43#include "bncutils.h"
44#include "bncsettings.h"
45
46using namespace std;
47
48// Constructor
49////////////////////////////////////////////////////////////////////////
50bncRtnetDecoder::bncRtnetDecoder() {
51 bncSettings settings;
52 _year = 0;
53}
54
55// Destructor
56////////////////////////////////////////////////////////////////////////
57bncRtnetDecoder::~bncRtnetDecoder() {
58}
59
60//
61////////////////////////////////////////////////////////////////////////
62void bncRtnetDecoder::readEpochTime(const QString& line) {
63 QTextStream in(line.toAscii());
64 QString hlp;
65 in >> hlp >> _year >> _month >> _day >> _hour >> _min >> _sec;
66 GPSweekFromYMDhms(_year, _month, _day, _hour, _min, _sec, _GPSweek, _GPSweeks);
67}
68
69// Decode Method
70////////////////////////////////////////////////////////////////////////
71t_irc bncRtnetDecoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
72
73 errmsg.clear();
74 _buffer.append(QByteArray(buffer, bufLen));
75
76 // Prepare list of lines with satellite positions in SP3-like format
77 // -----------------------------------------------------------------
78 QStringList lines;
79 int iLast = _buffer.lastIndexOf('\n');
80 if (iLast != -1) {
81 QStringList hlpLines = _buffer.split('\n', QString::SkipEmptyParts);
82 _buffer = _buffer.mid(iLast+1);
83 for (int ii = 0; ii < hlpLines.size(); ii++) {
84 if (hlpLines[ii].indexOf('*') != -1) {
85 readEpochTime(hlpLines[ii]);
86 }
87 else if (_year != 0) {
88 lines << hlpLines[ii];
89 }
90 }
91 }
92
93 // Satellite positions to be processed
94 // -----------------------------------
95 if (lines.size() > 0) {
96
97 QStringList prns;
98
99 for (int ic = 0; ic < _caster.size(); ic++) {
100 _caster.at(ic)->open();
101
102 struct ClockOrbit co;
103 memset(&co, 0, sizeof(co));
104 co.GPSEpochTime = (int)_GPSweeks;
105 co.GLONASSEpochTime = (int)fmod(_GPSweeks, 86400.0)
106 + 3 * 3600 - gnumleap(_year, _month, _day);
107 co.ClockDataSupplied = 1;
108 co.OrbitDataSupplied = 1;
109 co.SatRefDatum = DATUM_ITRF;
110
111 struct Bias bias;
112 memset(&bias, 0, sizeof(bias));
113 bias.GPSEpochTime = (int)_GPSweeks;
114 bias.GLONASSEpochTime = (int)fmod(_GPSweeks, 86400.0)
115 + 3 * 3600 - gnumleap(_year, _month, _day);
116
117 for (int ii = 0; ii < lines.size(); ii++) {
118
119 QString prn;
120 ColumnVector xx(14); xx = 0.0;
121 t_ephPair* pair = 0;
122
123 if (ic == 0) {
124 QTextStream in(lines[ii].toAscii());
125 in >> prn;
126 prns << prn;
127 if ( _eph.contains(prn) ) {
128 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5)
129 >> xx(6) >> xx(7) >> xx(8) >> xx(9) >> xx(10)
130 >> xx(11) >> xx(12) >> xx(13) >> xx(14);
131 xx(1) *= 1e3; // x-crd
132 xx(2) *= 1e3; // y-crd
133 xx(3) *= 1e3; // z-crd
134 xx(4) *= 1e-6; // clk
135 xx(5) *= 1e-6; // rel. corr.
136 // xx(6), xx(7), xx(8) ... PhaseCent - CoM
137 // xx(9) ... P1-C1 DCB in meters
138 // xx(10) ... P1-P2 DCB in meters
139 // xx(11) ... dT
140 xx(12) *= 1e3; // x-crd at time + dT
141 xx(13) *= 1e3; // y-crd at time + dT
142 xx(14) *= 1e3; // z-crd at time + dT
143
144 pair = _eph[prn];
145 pair->xx = xx;
146 }
147 }
148 else {
149 prn = prns[ii];
150 if ( _eph.contains(prn) ) {
151 pair = _eph[prn];
152 xx = pair->xx;
153 }
154 }
155
156 // Use old ephemeris if the new one is too recent
157 // ----------------------------------------------
158 t_eph* ep = 0;
159 if (pair) {
160 ep = pair->last;
161 if (pair->prev && ep &&
162 ep->receptDateTime().secsTo(QDateTime::currentDateTime()) < 60) {
163 ep = pair->prev;
164 }
165 }
166
167 if (ep != 0) {
168 struct ClockOrbit::SatData* sd = 0;
169 if (prn[0] == 'G') {
170 sd = co.Sat + co.NumberOfGPSSat;
171 ++co.NumberOfGPSSat;
172 }
173 else if (prn[0] == 'R') {
174 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
175 ++co.NumberOfGLONASSSat;
176 }
177 if (sd) {
178 QString outLine;
179 processSatellite(ic, _caster.at(ic)->crdTrafo(),
180 _caster.at(ic)->CoM(), ep,
181 _GPSweek, _GPSweeks, prn, xx, sd, outLine);
182 _caster.at(ic)->printAscii(outLine);
183 }
184
185 struct Bias::BiasSat* biasSat = 0;
186 if (prn[0] == 'G') {
187 biasSat = bias.Sat + bias.NumberOfGPSSat;
188 ++bias.NumberOfGPSSat;
189 }
190 else if (prn[0] == 'R') {
191 biasSat = bias.Sat + CLOCKORBIT_NUMGPS + bias.NumberOfGLONASSSat;
192 ++bias.NumberOfGLONASSSat;
193 }
194
195 // Coefficient of Ionosphere-Free LC
196 // ---------------------------------
197 const static double a_L1_GPS = 2.54572778;
198 const static double a_L2_GPS = -1.54572778;
199 const static double a_L1_Glo = 2.53125000;
200 const static double a_L2_Glo = -1.53125000;
201
202 if (biasSat) {
203 biasSat->ID = prn.mid(1).toInt();
204 biasSat->NumberOfCodeBiases = 3;
205 if (prn[0] == 'G') {
206 biasSat->Biases[0].Type = CODETYPEGPS_L1_Z;
207 biasSat->Biases[0].Bias = - a_L2_GPS * xx(10);
208 biasSat->Biases[1].Type = CODETYPEGPS_L1_CA;
209 biasSat->Biases[1].Bias = - a_L2_GPS * xx(10) + xx(9);
210 biasSat->Biases[2].Type = CODETYPEGPS_L2_Z;
211 biasSat->Biases[2].Bias = a_L1_GPS * xx(10);
212 }
213 else if (prn[0] == 'R') {
214 biasSat->Biases[0].Type = CODETYPEGLONASS_L1_P;
215 biasSat->Biases[0].Bias = - a_L2_Glo * xx(10);
216 biasSat->Biases[1].Type = CODETYPEGLONASS_L1_CA;
217 biasSat->Biases[1].Bias = - a_L2_Glo * xx(10) + xx(9);
218 biasSat->Biases[2].Type = CODETYPEGLONASS_L2_P;
219 biasSat->Biases[2].Bias = a_L1_Glo * xx(10);
220 }
221 }
222 }
223 }
224
225 if ( _caster.at(ic)->usedSocket() &&
226 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
227 char obuffer[CLOCKORBIT_BUFFERSIZE];
228
229 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
230 if (len > 0) {
231 _caster.at(ic)->write(obuffer, len);
232 }
233 }
234
235 if ( _caster.at(ic)->usedSocket() &&
236 (bias.NumberOfGPSSat > 0 || bias.NumberOfGLONASSSat > 0) ) {
237 char obuffer[CLOCKORBIT_BUFFERSIZE];
238 int len = MakeBias(&bias, BTYPE_AUTO, 0, obuffer, sizeof(obuffer));
239 if (len > 0) {
240 _caster.at(ic)->write(obuffer, len);
241 }
242 }
243 }
244 }
245
246 return success;
247}
248
249//
250////////////////////////////////////////////////////////////////////////////
251void bncRtnetDecoder::processSatellite(int iCaster, const QString trafo,
252 bool CoM, t_eph* ep, int GPSweek,
253 double GPSweeks, const QString& prn,
254 const ColumnVector& xx,
255 struct ClockOrbit::SatData* sd,
256 QString& outLine) {
257
258 const double secPerWeek = 7.0 * 86400.0;
259
260 ColumnVector rsw(3);
261 ColumnVector rsw2(3);
262 double dClk;
263
264 for (int ii = 1; ii <= 2; ++ii) {
265
266 int GPSweek12 = GPSweek;
267 double GPSweeks12 = GPSweeks;
268 if (ii == 2) {
269 GPSweeks12 += xx(11);
270 if (GPSweeks12 > secPerWeek) {
271 GPSweek12 += 1;
272 GPSweeks12 -= secPerWeek;
273 }
274 }
275
276 ColumnVector xB(4);
277 ColumnVector vv(3);
278
279 ep->position(GPSweek12, GPSweeks12, xB.data(), vv.data());
280
281 ColumnVector xyz;
282 if (ii == 1) {
283 xyz = xx.Rows(1,3);
284 }
285 else {
286 xyz = xx.Rows(12,14);
287 }
288
289 // Correction Center of Mass -> Antenna Phase Center
290 // -------------------------------------------------
291 if (! CoM) {
292 xyz(1) += xx(6);
293 xyz(2) += xx(7);
294 xyz(3) += xx(8);
295 }
296
297 if (trafo != "IGS05") {
298 crdTrafo(GPSweek12, xyz, trafo);
299 }
300
301 ColumnVector dx = xB.Rows(1,3) - xyz ;
302
303 if (ii == 1) {
304 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
305 dClk = (xx(4) + xx(5) - xB(4)) * 299792458.0;
306 }
307 else {
308 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw2);
309 }
310 }
311
312 if (sd) {
313 sd->ID = prn.mid(1).toInt();
314 sd->IOD = ep->IOD();
315 sd->Clock.DeltaA0 = dClk;
316 sd->Orbit.DeltaRadial = rsw(1);
317 sd->Orbit.DeltaAlongTrack = rsw(2);
318 sd->Orbit.DeltaCrossTrack = rsw(3);
319 sd->Orbit.DotDeltaRadial = (rsw2(1) - rsw(1)) / xx(11);
320 sd->Orbit.DotDeltaAlongTrack = (rsw2(2) - rsw(2)) / xx(11);
321 sd->Orbit.DotDeltaCrossTrack = (rsw2(3) - rsw(3)) / xx(11);
322 }
323
324 outLine.sprintf("%d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
325 GPSweek, GPSweeks, ep->prn().c_str(),
326 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
327
328 if (iCaster == 0) {
329 if (_rnx) {
330 _rnx->write(GPSweek, GPSweeks, prn, xx);
331 }
332 if (_sp3) {
333 _sp3->write(GPSweek, GPSweeks, prn, xx, _append);
334 }
335 }
336}
337
338// Transform Coordinates
339////////////////////////////////////////////////////////////////////////////
340void bncRtnetDecoder::crdTrafo(int GPSWeek, ColumnVector& xyz,
341 const QString& trafo) {
342
343 bncSettings settings;
344
345 if (trafo == "ETRF2000") {
346 _dx = 0.0541;
347 _dy = 0.0502;
348 _dz = -0.0538;
349 _dxr = -0.0002;
350 _dyr = 0.0001;
351 _dzr = -0.0018;
352 _ox = 0.000891;
353 _oy = 0.005390;
354 _oz = -0.008712;
355 _oxr = 0.000081;
356 _oyr = 0.000490;
357 _ozr = -0.000792;
358 _sc = 0.40;
359 _scr = 0.08;
360 _t0 = 2000.0;
361 }
362 else if (trafo == "NAD83") {
363 _dx = 0.9963;
364 _dy = -1.9024;
365 _dz = -0.5210;
366 _dxr = 0.0005;
367 _dyr = -0.0006;
368 _dzr = -0.0013;
369 _ox = 0.025915;
370 _oy = 0.009426;
371 _oz = 0.011599;
372 _oxr = 0.000067;
373 _oyr = -0.000757;
374 _ozr = -0.000051;
375 _sc = 0.78;
376 _scr = -0.10;
377 _t0 = 1997.0;
378 }
379 else if (trafo == "GDA94") {
380 _dx = -0.07973;
381 _dy = -0.00686;
382 _dz = 0.03803;
383 _dxr = 0.00225;
384 _dyr = -0.00062;
385 _dzr = -0.00056;
386 _ox = 0.0000351;
387 _oy = -0.0021211;
388 _oz = -0.0021411;
389 _oxr = -0.0014707;
390 _oyr = -0.0011443;
391 _ozr = -0.0011701;
392 _sc = 6.636;
393 _scr = 0.294;
394 _t0 = 1994.0;
395 }
396 else if (trafo == "SIRGAS2000") {
397 _dx = -0.0051;
398 _dy = -0.0065;
399 _dz = -0.0099;
400 _dxr = 0.0000;
401 _dyr = 0.0000;
402 _dzr = 0.0000;
403 _ox = 0.000150;
404 _oy = 0.000020;
405 _oz = 0.000021;
406 _oxr = 0.000000;
407 _oyr = 0.000000;
408 _ozr = 0.000000;
409 _sc = 0.000;
410 _scr = 0.000;
411 _t0 = 0000.0;
412 }
413 else if (trafo == "SIRGAS95") {
414 _dx = 0.0077;
415 _dy = 0.0058;
416 _dz = -0.0138;
417 _dxr = 0.0000;
418 _dyr = 0.0000;
419 _dzr = 0.0000;
420 _ox = 0.000000;
421 _oy = 0.000000;
422 _oz = -0.000030;
423 _oxr = 0.000000;
424 _oyr = 0.000000;
425 _ozr = 0.000000;
426 _sc = 1.570;
427 _scr = 0.000;
428 _t0 = 0000.0;
429 }
430 else if (trafo == "Custom") {
431 _dx = settings.value("trafo_dx").toDouble();
432 _dy = settings.value("trafo_dy").toDouble();
433 _dz = settings.value("trafo_dz").toDouble();
434 _dxr = settings.value("trafo_dxr").toDouble();
435 _dyr = settings.value("trafo_dyr").toDouble();
436 _dzr = settings.value("trafo_dzr").toDouble();
437 _ox = settings.value("trafo_ox").toDouble();
438 _oy = settings.value("trafo_oy").toDouble();
439 _oz = settings.value("trafo_oz").toDouble();
440 _oxr = settings.value("trafo_oxr").toDouble();
441 _oyr = settings.value("trafo_oyr").toDouble();
442 _ozr = settings.value("trafo_ozr").toDouble();
443 _sc = settings.value("trafo_sc").toDouble();
444 _scr = settings.value("trafo_scr").toDouble();
445 _t0 = settings.value("trafo_t0").toDouble();
446 }
447
448 // Current epoch minus 2000.0 in years
449 // ------------------------------------
450 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
451
452 ColumnVector dx(3);
453
454 dx(1) = _dx + dt * _dxr;
455 dx(2) = _dy + dt * _dyr;
456 dx(3) = _dz + dt * _dzr;
457
458 static const double arcSec = 180.0 * 3600.0 / M_PI;
459
460 double ox = (_ox + dt * _oxr) / arcSec;
461 double oy = (_oy + dt * _oyr) / arcSec;
462 double oz = (_oz + dt * _ozr) / arcSec;
463
464 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
465
466 Matrix rMat(3,3);
467 rMat(1,1) = 1.0;
468 rMat(1,2) = -oz;
469 rMat(1,3) = oy;
470 rMat(2,1) = oz;
471 rMat(2,2) = 1.0;
472 rMat(2,3) = -ox;
473 rMat(3,1) = -oy;
474 rMat(3,2) = ox;
475 rMat(3,3) = 1.0;
476
477 xyz = sc * rMat * xyz + dx;
478}
Note: See TracBrowser for help on using the repository browser.