source: ntrip/trunk/BNC/src/upload/bncrtnetuploadcaster.cpp@ 4753

Last change on this file since 4753 was 4753, checked in by mervart, 12 years ago
File size: 16.6 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Server
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncRtnetUploadCaster
6 *
7 * Purpose: Connection to NTRIP Caster
8 *
9 * Author: L. Mervart
10 *
11 * Created: 29-Mar-2011
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <math.h>
18#include "bncrtnetuploadcaster.h"
19#include "bncsettings.h"
20#include "bncephuser.h"
21#include "bncclockrinex.h"
22#include "bncsp3.h"
23
24using namespace std;
25
26// Constructor
27////////////////////////////////////////////////////////////////////////////
28bncRtnetUploadCaster::bncRtnetUploadCaster(const QString& mountpoint,
29 const QString& outHost, int outPort,
30 const QString& password,
31 const QString& crdTrafo, bool CoM,
32 const QString& sp3FileName,
33 const QString& rnxFileName,
34 int PID, int SID, int IOD, int iRow) :
35 bncUploadCaster(mountpoint, outHost, outPort, password, iRow, 0) {
36
37 _crdTrafo = crdTrafo;
38 _CoM = CoM;
39 _PID = PID;
40 _SID = SID;
41 _IOD = IOD;
42
43 // Member that receives the ephemeris
44 // ----------------------------------
45 _ephUser = new bncEphUser();
46
47 bncSettings settings;
48 QString intr = settings.value("uploadIntr").toString();
49
50 _samplRtcmEphCorr = settings.value("uploadSamplRtcmEphCorr").toDouble();
51 int samplClkRnx = settings.value("uploadSamplClkRnx").toInt();
52 int samplSp3 = settings.value("uploadSamplSp3").toInt() * 60;
53
54 if (_samplRtcmEphCorr == 0.0) {
55 _usedEph = 0;
56 }
57 else {
58 _usedEph = new QMap<QString, t_eph*>;
59 }
60
61 // RINEX writer
62 // ------------
63 if (!rnxFileName.isEmpty()) {
64 _rnx = new bncClockRinex(rnxFileName, intr, samplClkRnx);
65 }
66 else {
67 _rnx = 0;
68 }
69
70 // SP3 writer
71 // ----------
72 if (!sp3FileName.isEmpty()) {
73 _sp3 = new bncSP3(sp3FileName, intr, samplSp3);
74 }
75 else {
76 _sp3 = 0;
77 }
78
79 // Set Transformation Parameters
80 // -----------------------------
81 if (_crdTrafo == "ETRF2000") {
82 _dx = 0.0521;
83 _dy = 0.0493;
84 _dz = -0.0585;
85 _dxr = 0.0001;
86 _dyr = 0.0001;
87 _dzr = -0.0018;
88 _ox = 0.000891;
89 _oy = 0.005390;
90 _oz = -0.008712;
91 _oxr = 0.000081;
92 _oyr = 0.000490;
93 _ozr = -0.000792;
94 _sc = 1.34;
95 _scr = 0.08;
96 _t0 = 2000.0;
97 }
98 else if (_crdTrafo == "NAD83") {
99 _dx = 0.9963;
100 _dy = -1.9024;
101 _dz = -0.5210;
102 _dxr = 0.0005;
103 _dyr = -0.0006;
104 _dzr = -0.0013;
105 _ox = -0.025915;
106 _oy = -0.009426;
107 _oz = -0.011599;
108 _oxr = -0.000067;
109 _oyr = 0.000757;
110 _ozr = 0.000051;
111 _sc = 0.78;
112 _scr = -0.10;
113 _t0 = 1997.0;
114 }
115 else if (_crdTrafo == "GDA94") {
116 _dx = -0.07973;
117 _dy = -0.00686;
118 _dz = 0.03803;
119 _dxr = 0.00225;
120 _dyr = -0.00062;
121 _dzr = -0.00056;
122 _ox = 0.0000351;
123 _oy = -0.0021211;
124 _oz = -0.0021411;
125 _oxr = -0.0014707;
126 _oyr = -0.0011443;
127 _ozr = -0.0011701;
128 _sc = 6.636;
129 _scr = 0.294;
130 _t0 = 1994.0;
131 }
132 else if (_crdTrafo == "SIRGAS2000") {
133 _dx = -0.0051;
134 _dy = -0.0065;
135 _dz = -0.0099;
136 _dxr = 0.0000;
137 _dyr = 0.0000;
138 _dzr = 0.0000;
139 _ox = 0.000150;
140 _oy = 0.000020;
141 _oz = 0.000021;
142 _oxr = 0.000000;
143 _oyr = 0.000000;
144 _ozr = 0.000000;
145 _sc = 0.000;
146 _scr = 0.000;
147 _t0 = 0000.0;
148 }
149 else if (_crdTrafo == "SIRGAS95") {
150 _dx = 0.0077;
151 _dy = 0.0058;
152 _dz = -0.0138;
153 _dxr = 0.0000;
154 _dyr = 0.0000;
155 _dzr = 0.0000;
156 _ox = 0.000000;
157 _oy = 0.000000;
158 _oz = -0.000030;
159 _oxr = 0.000000;
160 _oyr = 0.000000;
161 _ozr = 0.000000;
162 _sc = 1.570;
163 _scr = 0.000;
164 _t0 = 0000.0;
165 }
166 else if (_crdTrafo == "Custom") {
167 _dx = settings.value("trafo_dx").toDouble();
168 _dy = settings.value("trafo_dy").toDouble();
169 _dz = settings.value("trafo_dz").toDouble();
170 _dxr = settings.value("trafo_dxr").toDouble();
171 _dyr = settings.value("trafo_dyr").toDouble();
172 _dzr = settings.value("trafo_dzr").toDouble();
173 _ox = settings.value("trafo_ox").toDouble();
174 _oy = settings.value("trafo_oy").toDouble();
175 _oz = settings.value("trafo_oz").toDouble();
176 _oxr = settings.value("trafo_oxr").toDouble();
177 _oyr = settings.value("trafo_oyr").toDouble();
178 _ozr = settings.value("trafo_ozr").toDouble();
179 _sc = settings.value("trafo_sc").toDouble();
180 _scr = settings.value("trafo_scr").toDouble();
181 _t0 = settings.value("trafo_t0").toDouble();
182 }
183}
184
185// Destructor
186////////////////////////////////////////////////////////////////////////////
187bncRtnetUploadCaster::~bncRtnetUploadCaster() {
188 if (isRunning()) {
189 wait();
190 }
191 delete _rnx;
192 delete _sp3;
193 delete _ephUser;
194 delete _usedEph;
195}
196
197//
198////////////////////////////////////////////////////////////////////////////
199void bncRtnetUploadCaster::decodeRtnetStream(char* buffer, int bufLen) {
200
201 QMutexLocker locker(&_mutex);
202
203 // Append to internal buffer
204 // -------------------------
205 _rtnetStreamBuffer.append(QByteArray(buffer, bufLen));
206
207 // Select buffer part that contains last epoch
208 // -------------------------------------------
209 QStringList lines;
210 int iEpoBeg = _rtnetStreamBuffer.lastIndexOf('*'); // begin of last epoch
211 if (iEpoBeg == -1) {
212 _rtnetStreamBuffer.clear();
213 return;
214 }
215 _rtnetStreamBuffer = _rtnetStreamBuffer.mid(iEpoBeg);
216
217 int iEpoEnd = _rtnetStreamBuffer.lastIndexOf("EOE"); // end of last epoch
218 if (iEpoEnd == -1) {
219 return;
220 }
221 else {
222 lines = _rtnetStreamBuffer.left(iEpoEnd).split('\n', QString::SkipEmptyParts);
223 _rtnetStreamBuffer = _rtnetStreamBuffer.mid(iEpoEnd+3);
224 }
225
226 if (lines.size() < 2) {
227 return;
228 }
229
230 // Keep the last unfinished line in buffer
231 // ---------------------------------------
232 int iLastEOL = _rtnetStreamBuffer.lastIndexOf('\n');
233 if (iLastEOL != -1) {
234 _rtnetStreamBuffer = _rtnetStreamBuffer.mid(iLastEOL+1);
235 }
236
237 // Read first line (with epoch time)
238 // ---------------------------------
239 QTextStream in(lines[0].toAscii());
240 QString hlp;
241 int year, month, day, hour, min;
242 double sec;
243 in >> hlp >> year >> month >> day >> hour >> min >> sec;
244 bncTime epoTime; epoTime.set( year, month, day, hour, min, sec);
245
246 struct ClockOrbit co;
247 memset(&co, 0, sizeof(co));
248 co.GPSEpochTime = static_cast<int>(epoTime.gpssec());
249 co.GLONASSEpochTime = static_cast<int>(fmod(epoTime.gpssec(), 86400.0))
250 + 3 * 3600 - gnumleap(year, month, day);
251 co.ClockDataSupplied = 1;
252 co.OrbitDataSupplied = 1;
253 co.SatRefDatum = DATUM_ITRF;
254 co.SSRIOD = _IOD;
255 co.SSRProviderID = _PID; // 256 .. BKG, 257 ... EUREF
256 co.SSRSolutionID = _SID;
257
258 struct Bias bias;
259 memset(&bias, 0, sizeof(bias));
260 bias.GPSEpochTime = co.GPSEpochTime;
261 bias.GLONASSEpochTime = co.GLONASSEpochTime;
262
263 // Default Update Interval
264 // -----------------------
265 int clkUpdInterval = 5;
266 int ephUpdInterval = (_samplRtcmEphCorr != 0.0 ? int(_samplRtcmEphCorr) : 5);
267
268 co.UpdateInterval = clkUpdInterval;
269 bias.UpdateInterval = clkUpdInterval;
270
271 for (int ii = 1; ii < lines.size(); ii++) {
272
273 QString prn;
274 ColumnVector xx(14); xx = 0.0;
275
276 QTextStream in(lines[ii].toAscii());
277
278 in >> prn;
279 if (prn[0] == 'P') {
280 prn.remove(0,1);
281 }
282
283 t_eph* eph = 0;
284 const bncEphUser::t_ephPair* ephPair = _ephUser->ephPair(prn);
285 if (ephPair) {
286
287 eph = ephPair->last;
288
289 // Use previous ephemeris if the last one is too recent
290 // ----------------------------------------------------
291 const int MINAGE = 60; // seconds
292 if (ephPair->prev && eph->receptDateTime().isValid() &&
293 eph->receptDateTime().secsTo(currentDateAndTimeGPS()) < MINAGE) {
294 eph = ephPair->prev;
295 }
296
297 // Make sure the clock messages refer to same IOD as orbit messages
298 // ----------------------------------------------------------------
299 if (_usedEph) {
300 if (fmod(epoTime.gpssec(), _samplRtcmEphCorr) == 0.0) {
301 (*_usedEph)[prn] = eph;
302 }
303 else {
304 eph = 0;
305 if (_usedEph->contains(prn)) {
306 t_eph* usedEph = _usedEph->value(prn);
307 if (usedEph == ephPair->last) {
308 eph = ephPair->last;
309 }
310 else if (usedEph == ephPair->prev) {
311 eph = ephPair->prev;
312 }
313 }
314 }
315 }
316 }
317
318 if (eph) {
319
320 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5)
321 >> xx(6) >> xx(7) >> xx(8) >> xx(9) >> xx(10)
322 >> xx(11) >> xx(12) >> xx(13) >> xx(14);
323 xx(1) *= 1e3; // x-crd
324 xx(2) *= 1e3; // y-crd
325 xx(3) *= 1e3; // z-crd
326 xx(4) *= 1e-6; // clk
327 xx(5) *= 1e-6; // rel. corr.
328 // xx(6), xx(7), xx(8) ... PhaseCent - CoM
329 // xx(9) ... P1-C1 DCB in meters
330 // xx(10) ... P1-P2 DCB in meters
331 // xx(11) ... dT
332 xx(12) *= 1e3; // x-crd at time + dT
333 xx(13) *= 1e3; // y-crd at time + dT
334 xx(14) *= 1e3; // z-crd at time + dT
335
336 struct ClockOrbit::SatData* sd = 0;
337 if (prn[0] == 'G') {
338 sd = co.Sat + co.NumberOfGPSSat;
339 ++co.NumberOfGPSSat;
340 }
341 else if (prn[0] == 'R') {
342 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
343 ++co.NumberOfGLONASSSat;
344 }
345 if (sd) {
346 QString outLine;
347 processSatellite(eph, epoTime.gpsw(), epoTime.gpssec(), prn,
348 xx, sd, outLine);
349 }
350
351 struct Bias::BiasSat* biasSat = 0;
352 if (prn[0] == 'G') {
353 biasSat = bias.Sat + bias.NumberOfGPSSat;
354 ++bias.NumberOfGPSSat;
355 }
356 else if (prn[0] == 'R') {
357 biasSat = bias.Sat + CLOCKORBIT_NUMGPS + bias.NumberOfGLONASSSat;
358 ++bias.NumberOfGLONASSSat;
359 }
360
361 // Coefficient of Ionosphere-Free LC
362 // ---------------------------------
363 const static double a_L1_GPS = 2.54572778;
364 const static double a_L2_GPS = -1.54572778;
365 const static double a_L1_Glo = 2.53125000;
366 const static double a_L2_Glo = -1.53125000;
367
368 if (biasSat) {
369 biasSat->ID = prn.mid(1).toInt();
370 biasSat->NumberOfCodeBiases = 3;
371 if (prn[0] == 'G') {
372 biasSat->Biases[0].Type = CODETYPEGPS_L1_Z;
373 biasSat->Biases[0].Bias = - a_L2_GPS * xx(10);
374 biasSat->Biases[1].Type = CODETYPEGPS_L1_CA;
375 biasSat->Biases[1].Bias = - a_L2_GPS * xx(10) + xx(9);
376 biasSat->Biases[2].Type = CODETYPEGPS_L2_Z;
377 biasSat->Biases[2].Bias = a_L1_GPS * xx(10);
378 }
379 else if (prn[0] == 'R') {
380 biasSat->Biases[0].Type = CODETYPEGLONASS_L1_P;
381 biasSat->Biases[0].Bias = - a_L2_Glo * xx(10);
382 biasSat->Biases[1].Type = CODETYPEGLONASS_L1_CA;
383 biasSat->Biases[1].Bias = - a_L2_Glo * xx(10) + xx(9);
384 biasSat->Biases[2].Type = CODETYPEGLONASS_L2_P;
385 biasSat->Biases[2].Bias = a_L1_Glo * xx(10);
386 }
387 }
388 }
389 }
390
391 QByteArray hlpBufferCo;
392
393 // Orbit and Clock Corrections together
394 // ------------------------------------
395 if (_samplRtcmEphCorr == 0.0) {
396 if (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) {
397 char obuffer[CLOCKORBIT_BUFFERSIZE];
398 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
399 if (len > 0) {
400 hlpBufferCo = QByteArray(obuffer, len);
401 }
402 }
403 }
404
405 // Orbit and Clock Corrections separately
406 // --------------------------------------
407 else {
408 if (co.NumberOfGPSSat > 0) {
409 char obuffer[CLOCKORBIT_BUFFERSIZE];
410 if (fmod(epoTime.gpssec(), _samplRtcmEphCorr) == 0.0) {
411 co.UpdateInterval = ephUpdInterval;
412 int len1 = MakeClockOrbit(&co, COTYPE_GPSORBIT, 1, obuffer, sizeof(obuffer));
413 co.UpdateInterval = clkUpdInterval;
414 if (len1 > 0) {
415 hlpBufferCo += QByteArray(obuffer, len1);
416 }
417 }
418 int mmsg = (co.NumberOfGLONASSSat > 0) ? 1 : 0;
419 int len2 = MakeClockOrbit(&co, COTYPE_GPSCLOCK, mmsg, obuffer, sizeof(obuffer));
420 if (len2 > 0) {
421 hlpBufferCo += QByteArray(obuffer, len2);
422 }
423 }
424 if (co.NumberOfGLONASSSat > 0) {
425 char obuffer[CLOCKORBIT_BUFFERSIZE];
426 if (fmod(epoTime.gpssec(), _samplRtcmEphCorr) == 0.0) {
427 co.UpdateInterval = ephUpdInterval;
428 int len1 = MakeClockOrbit(&co, COTYPE_GLONASSORBIT, 1, obuffer, sizeof(obuffer));
429 co.UpdateInterval = clkUpdInterval;
430 if (len1 > 0) {
431 hlpBufferCo += QByteArray(obuffer, len1);
432 }
433 }
434 int len2 = MakeClockOrbit(&co, COTYPE_GLONASSCLOCK, 0, obuffer, sizeof(obuffer));
435 if (len2 > 0) {
436 hlpBufferCo += QByteArray(obuffer, len2);
437 }
438 }
439 }
440
441 // Biases
442 // ------
443 QByteArray hlpBufferBias;
444 if (bias.NumberOfGPSSat > 0 || bias.NumberOfGLONASSSat > 0) {
445 char obuffer[CLOCKORBIT_BUFFERSIZE];
446 int len = MakeBias(&bias, BTYPE_AUTO, 0, obuffer, sizeof(obuffer));
447 if (len > 0) {
448 hlpBufferBias = QByteArray(obuffer, len);
449 }
450 }
451
452 if (hlpBufferCo.size() > 0) {
453 _outBuffer = hlpBufferCo + hlpBufferBias;
454 }
455}
456
457//
458////////////////////////////////////////////////////////////////////////////
459void bncRtnetUploadCaster::processSatellite(t_eph* eph, int GPSweek,
460 double GPSweeks, const QString& prn,
461 const ColumnVector& xx,
462 struct ClockOrbit::SatData* sd,
463 QString& outLine) {
464
465 const double secPerWeek = 7.0 * 86400.0;
466
467 ColumnVector rsw(3);
468 ColumnVector rsw2(3);
469 double dClk;
470
471 for (int ii = 1; ii <= 2; ++ii) {
472
473 int GPSweek12 = GPSweek;
474 double GPSweeks12 = GPSweeks;
475 if (ii == 2) {
476 GPSweeks12 += xx(11);
477 if (GPSweeks12 > secPerWeek) {
478 GPSweek12 += 1;
479 GPSweeks12 -= secPerWeek;
480 }
481 }
482
483 ColumnVector xB(4);
484 ColumnVector vv(3);
485
486 eph->position(GPSweek12, GPSweeks12, xB.data(), vv.data());
487
488 ColumnVector xyz;
489 if (ii == 1) {
490 xyz = xx.Rows(1,3);
491 }
492 else {
493 xyz = xx.Rows(12,14);
494 }
495
496 // Correction Center of Mass -> Antenna Phase Center
497 // -------------------------------------------------
498 if (! _CoM) {
499 xyz(1) += xx(6);
500 xyz(2) += xx(7);
501 xyz(3) += xx(8);
502 }
503
504 if (_crdTrafo != "IGS08") {
505 crdTrafo(GPSweek12, xyz);
506 }
507
508 ColumnVector dx = xB.Rows(1,3) - xyz ;
509
510 if (ii == 1) {
511 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
512 dClk = (xx(4) + xx(5) - xB(4)) * 299792458.0;
513 }
514 else {
515 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw2);
516 }
517 }
518
519 if (sd) {
520 sd->ID = prn.mid(1).toInt();
521 sd->IOD = eph->IOD();
522 sd->Clock.DeltaA0 = dClk;
523 sd->Orbit.DeltaRadial = rsw(1);
524 sd->Orbit.DeltaAlongTrack = rsw(2);
525 sd->Orbit.DeltaCrossTrack = rsw(3);
526 sd->Orbit.DotDeltaRadial = (rsw2(1) - rsw(1)) / xx(11);
527 sd->Orbit.DotDeltaAlongTrack = (rsw2(2) - rsw(2)) / xx(11);
528 sd->Orbit.DotDeltaCrossTrack = (rsw2(3) - rsw(3)) / xx(11);
529 }
530
531 outLine.sprintf("%d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
532 GPSweek, GPSweeks, eph->prn().toAscii().data(),
533 eph->IOD(), dClk, rsw(1), rsw(2), rsw(3));
534
535 if (_rnx) {
536 _rnx->write(GPSweek, GPSweeks, prn, xx);
537 }
538 if (_sp3) {
539 _sp3->write(GPSweek, GPSweeks, prn, xx);
540 }
541}
542
543// Transform Coordinates
544////////////////////////////////////////////////////////////////////////////
545void bncRtnetUploadCaster::crdTrafo(int GPSWeek, ColumnVector& xyz) {
546
547 // Current epoch minus 2000.0 in years
548 // ------------------------------------
549 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
550
551 ColumnVector dx(3);
552
553 dx(1) = _dx + dt * _dxr;
554 dx(2) = _dy + dt * _dyr;
555 dx(3) = _dz + dt * _dzr;
556
557 static const double arcSec = 180.0 * 3600.0 / M_PI;
558
559 double ox = (_ox + dt * _oxr) / arcSec;
560 double oy = (_oy + dt * _oyr) / arcSec;
561 double oz = (_oz + dt * _ozr) / arcSec;
562
563 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
564
565 Matrix rMat(3,3);
566 rMat(1,1) = 1.0;
567 rMat(1,2) = -oz;
568 rMat(1,3) = oy;
569 rMat(2,1) = oz;
570 rMat(2,2) = 1.0;
571 rMat(2,3) = -ox;
572 rMat(3,1) = -oy;
573 rMat(3,2) = ox;
574 rMat(3,3) = 1.0;
575
576 xyz = sc * rMat * xyz + dx;
577}
578
Note: See TracBrowser for help on using the repository browser.