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

Last change on this file since 4806 was 4803, checked in by mervart, 12 years ago
File size: 17.2 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 clkUpdInd = 2; // 5 sec
266 int ephUpdInd = clkUpdInd; // default
267 if (_samplRtcmEphCorr == 10.0) {
268 ephUpdInd = 3;
269 }
270 else if (_samplRtcmEphCorr == 15.0) {
271 ephUpdInd = 4;
272 }
273 else if (_samplRtcmEphCorr == 30.0) {
274 ephUpdInd = 5;
275 }
276 else if (_samplRtcmEphCorr == 60.0) {
277 ephUpdInd = 6;
278 }
279 else if (_samplRtcmEphCorr == 120.0) {
280 ephUpdInd = 7;
281 }
282 else if (_samplRtcmEphCorr == 240.0) {
283 ephUpdInd = 8;
284 }
285 else if (_samplRtcmEphCorr == 300.0) {
286 ephUpdInd = 9;
287 }
288 else if (_samplRtcmEphCorr == 600.0) {
289 ephUpdInd = 10;
290 }
291 else if (_samplRtcmEphCorr == 900.0) {
292 ephUpdInd = 11;
293 }
294
295 co.UpdateInterval = clkUpdInd;
296 bias.UpdateInterval = clkUpdInd;
297
298 for (int ii = 1; ii < lines.size(); ii++) {
299
300 QString prn;
301 ColumnVector xx(14); xx = 0.0;
302
303 QTextStream in(lines[ii].toAscii());
304
305 in >> prn;
306 if (prn[0] == 'P') {
307 prn.remove(0,1);
308 }
309
310 t_eph* eph = 0;
311 const bncEphUser::t_ephPair* ephPair = _ephUser->ephPair(prn);
312 if (ephPair) {
313
314 eph = ephPair->last;
315
316 // Use previous ephemeris if the last one is too recent
317 // ----------------------------------------------------
318 const int MINAGE = 60; // seconds
319 if (ephPair->prev && eph->receptDateTime().isValid() &&
320 eph->receptDateTime().secsTo(currentDateAndTimeGPS()) < MINAGE) {
321 eph = ephPair->prev;
322 }
323
324 // Make sure the clock messages refer to same IOD as orbit messages
325 // ----------------------------------------------------------------
326 if (_usedEph) {
327 if (fmod(epoTime.gpssec(), _samplRtcmEphCorr) == 0.0) {
328 (*_usedEph)[prn] = eph;
329 }
330 else {
331 eph = 0;
332 if (_usedEph->contains(prn)) {
333 t_eph* usedEph = _usedEph->value(prn);
334 if (usedEph == ephPair->last) {
335 eph = ephPair->last;
336 }
337 else if (usedEph == ephPair->prev) {
338 eph = ephPair->prev;
339 }
340 }
341 }
342 }
343 }
344
345 if (eph) {
346
347 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5)
348 >> xx(6) >> xx(7) >> xx(8) >> xx(9) >> xx(10)
349 >> xx(11) >> xx(12) >> xx(13) >> xx(14);
350 xx(1) *= 1e3; // x-crd
351 xx(2) *= 1e3; // y-crd
352 xx(3) *= 1e3; // z-crd
353 xx(4) *= 1e-6; // clk
354 xx(5) *= 1e-6; // rel. corr.
355 // xx(6), xx(7), xx(8) ... PhaseCent - CoM
356 // xx(9) ... P1-C1 DCB in meters
357 // xx(10) ... P1-P2 DCB in meters
358 // xx(11) ... dT
359 xx(12) *= 1e3; // x-crd at time + dT
360 xx(13) *= 1e3; // y-crd at time + dT
361 xx(14) *= 1e3; // z-crd at time + dT
362
363 struct ClockOrbit::SatData* sd = 0;
364 if (prn[0] == 'G') {
365 sd = co.Sat + co.NumberOfGPSSat;
366 ++co.NumberOfGPSSat;
367 }
368 else if (prn[0] == 'R') {
369 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
370 ++co.NumberOfGLONASSSat;
371 }
372 if (sd) {
373 QString outLine;
374 processSatellite(eph, epoTime.gpsw(), epoTime.gpssec(), prn,
375 xx, sd, outLine);
376 }
377
378 struct Bias::BiasSat* biasSat = 0;
379 if (prn[0] == 'G') {
380 biasSat = bias.Sat + bias.NumberOfGPSSat;
381 ++bias.NumberOfGPSSat;
382 }
383 else if (prn[0] == 'R') {
384 biasSat = bias.Sat + CLOCKORBIT_NUMGPS + bias.NumberOfGLONASSSat;
385 ++bias.NumberOfGLONASSSat;
386 }
387
388 // Coefficient of Ionosphere-Free LC
389 // ---------------------------------
390 const static double a_L1_GPS = 2.54572778;
391 const static double a_L2_GPS = -1.54572778;
392 const static double a_L1_Glo = 2.53125000;
393 const static double a_L2_Glo = -1.53125000;
394
395 if (biasSat) {
396 biasSat->ID = prn.mid(1).toInt();
397 biasSat->NumberOfCodeBiases = 3;
398 if (prn[0] == 'G') {
399 biasSat->Biases[0].Type = CODETYPEGPS_L1_Z;
400 biasSat->Biases[0].Bias = - a_L2_GPS * xx(10);
401 biasSat->Biases[1].Type = CODETYPEGPS_L1_CA;
402 biasSat->Biases[1].Bias = - a_L2_GPS * xx(10) + xx(9);
403 biasSat->Biases[2].Type = CODETYPEGPS_L2_Z;
404 biasSat->Biases[2].Bias = a_L1_GPS * xx(10);
405 }
406 else if (prn[0] == 'R') {
407 biasSat->Biases[0].Type = CODETYPEGLONASS_L1_P;
408 biasSat->Biases[0].Bias = - a_L2_Glo * xx(10);
409 biasSat->Biases[1].Type = CODETYPEGLONASS_L1_CA;
410 biasSat->Biases[1].Bias = - a_L2_Glo * xx(10) + xx(9);
411 biasSat->Biases[2].Type = CODETYPEGLONASS_L2_P;
412 biasSat->Biases[2].Bias = a_L1_Glo * xx(10);
413 }
414 }
415 }
416 }
417
418 QByteArray hlpBufferCo;
419
420 // Orbit and Clock Corrections together
421 // ------------------------------------
422 if (_samplRtcmEphCorr == 0.0) {
423 if (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) {
424 char obuffer[CLOCKORBIT_BUFFERSIZE];
425 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
426 if (len > 0) {
427 hlpBufferCo = QByteArray(obuffer, len);
428 }
429 }
430 }
431
432 // Orbit and Clock Corrections separately
433 // --------------------------------------
434 else {
435 if (co.NumberOfGPSSat > 0) {
436 char obuffer[CLOCKORBIT_BUFFERSIZE];
437 if (fmod(epoTime.gpssec(), _samplRtcmEphCorr) == 0.0) {
438 co.UpdateInterval = ephUpdInd;
439 int len1 = MakeClockOrbit(&co, COTYPE_GPSORBIT, 1, obuffer, sizeof(obuffer));
440 co.UpdateInterval = clkUpdInd;
441 if (len1 > 0) {
442 hlpBufferCo += QByteArray(obuffer, len1);
443 }
444 }
445 int mmsg = (co.NumberOfGLONASSSat > 0) ? 1 : 0;
446 int len2 = MakeClockOrbit(&co, COTYPE_GPSCLOCK, mmsg, obuffer, sizeof(obuffer));
447 if (len2 > 0) {
448 hlpBufferCo += QByteArray(obuffer, len2);
449 }
450 }
451 if (co.NumberOfGLONASSSat > 0) {
452 char obuffer[CLOCKORBIT_BUFFERSIZE];
453 if (fmod(epoTime.gpssec(), _samplRtcmEphCorr) == 0.0) {
454 co.UpdateInterval = ephUpdInd;
455 int len1 = MakeClockOrbit(&co, COTYPE_GLONASSORBIT, 1, obuffer, sizeof(obuffer));
456 co.UpdateInterval = clkUpdInd;
457 if (len1 > 0) {
458 hlpBufferCo += QByteArray(obuffer, len1);
459 }
460 }
461 int len2 = MakeClockOrbit(&co, COTYPE_GLONASSCLOCK, 0, obuffer, sizeof(obuffer));
462 if (len2 > 0) {
463 hlpBufferCo += QByteArray(obuffer, len2);
464 }
465 }
466 }
467
468 // Biases
469 // ------
470 QByteArray hlpBufferBias;
471 if (bias.NumberOfGPSSat > 0 || bias.NumberOfGLONASSSat > 0) {
472 char obuffer[CLOCKORBIT_BUFFERSIZE];
473 int len = MakeBias(&bias, BTYPE_AUTO, 0, obuffer, sizeof(obuffer));
474 if (len > 0) {
475 hlpBufferBias = QByteArray(obuffer, len);
476 }
477 }
478
479 if (hlpBufferCo.size() > 0) {
480 _outBuffer = hlpBufferCo + hlpBufferBias;
481 }
482}
483
484//
485////////////////////////////////////////////////////////////////////////////
486void bncRtnetUploadCaster::processSatellite(t_eph* eph, int GPSweek,
487 double GPSweeks, const QString& prn,
488 const ColumnVector& xx,
489 struct ClockOrbit::SatData* sd,
490 QString& outLine) {
491
492 const double secPerWeek = 7.0 * 86400.0;
493
494 ColumnVector rsw(3);
495 ColumnVector rsw2(3);
496 double dClk;
497
498 for (int ii = 1; ii <= 2; ++ii) {
499
500 int GPSweek12 = GPSweek;
501 double GPSweeks12 = GPSweeks;
502 if (ii == 2) {
503 GPSweeks12 += xx(11);
504 if (GPSweeks12 > secPerWeek) {
505 GPSweek12 += 1;
506 GPSweeks12 -= secPerWeek;
507 }
508 }
509
510 ColumnVector xB(4);
511 ColumnVector vv(3);
512
513 eph->position(GPSweek12, GPSweeks12, xB.data(), vv.data());
514
515 ColumnVector xyz;
516 if (ii == 1) {
517 xyz = xx.Rows(1,3);
518 }
519 else {
520 xyz = xx.Rows(12,14);
521 }
522
523 // Correction Center of Mass -> Antenna Phase Center
524 // -------------------------------------------------
525 if (! _CoM) {
526 xyz(1) += xx(6);
527 xyz(2) += xx(7);
528 xyz(3) += xx(8);
529 }
530
531 double dc = 0.0;
532 if (_crdTrafo != "IGS08") {
533 crdTrafo(GPSweek12, xyz, dc);
534 }
535
536 ColumnVector dx = xB.Rows(1,3) - xyz ;
537
538 if (ii == 1) {
539 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
540 dClk = (xx(4) + xx(5) - xB(4) + dc) * t_CST::c;
541 }
542 else {
543 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw2);
544 }
545 }
546
547 if (sd) {
548 sd->ID = prn.mid(1).toInt();
549 sd->IOD = eph->IOD();
550 sd->Clock.DeltaA0 = dClk;
551 sd->Orbit.DeltaRadial = rsw(1);
552 sd->Orbit.DeltaAlongTrack = rsw(2);
553 sd->Orbit.DeltaCrossTrack = rsw(3);
554 sd->Orbit.DotDeltaRadial = (rsw2(1) - rsw(1)) / xx(11);
555 sd->Orbit.DotDeltaAlongTrack = (rsw2(2) - rsw(2)) / xx(11);
556 sd->Orbit.DotDeltaCrossTrack = (rsw2(3) - rsw(3)) / xx(11);
557 }
558
559 outLine.sprintf("%d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
560 GPSweek, GPSweeks, eph->prn().toAscii().data(),
561 eph->IOD(), dClk, rsw(1), rsw(2), rsw(3));
562
563 if (_rnx) {
564 _rnx->write(GPSweek, GPSweeks, prn, xx);
565 }
566 if (_sp3) {
567 _sp3->write(GPSweek, GPSweeks, prn, xx);
568 }
569}
570
571// Transform Coordinates
572////////////////////////////////////////////////////////////////////////////
573void bncRtnetUploadCaster::crdTrafo(int GPSWeek, ColumnVector& xyz,
574 double& dc) {
575
576 // Clock Correction
577 // ----------------
578 dc = 0.0; // TODO
579
580 // Current epoch minus 2000.0 in years
581 // ------------------------------------
582 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
583
584 ColumnVector dx(3);
585
586 dx(1) = _dx + dt * _dxr;
587 dx(2) = _dy + dt * _dyr;
588 dx(3) = _dz + dt * _dzr;
589
590 static const double arcSec = 180.0 * 3600.0 / M_PI;
591
592 double ox = (_ox + dt * _oxr) / arcSec;
593 double oy = (_oy + dt * _oyr) / arcSec;
594 double oz = (_oz + dt * _ozr) / arcSec;
595
596 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
597
598 Matrix rMat(3,3);
599 rMat(1,1) = 1.0;
600 rMat(1,2) = -oz;
601 rMat(1,3) = oy;
602 rMat(2,1) = oz;
603 rMat(2,2) = 1.0;
604 rMat(2,3) = -ox;
605 rMat(3,1) = -oy;
606 rMat(3,2) = ox;
607 rMat(3,3) = 1.0;
608
609 xyz = sc * rMat * xyz + dx;
610}
611
Note: See TracBrowser for help on using the repository browser.