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

Last change on this file since 4973 was 4973, checked in by weber, 9 years ago

Centers of transformation areas changed back to points on surface

File size: 18.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.08468;
117 _dy = -0.01942;
118 _dz = 0.03201;
119 _dxr = 0.00142;
120 _dyr = 0.00134;
121 _dzr = 0.00090;
122 _ox = 0.0004254;
123 _oy = -0.0022578;
124 _oz = -0.0024015;
125 _oxr = -0.0015461;
126 _oyr = -0.0011820;
127 _ozr = -0.0011551;
128 _sc = 9.710;
129 _scr = 0.109;
130 _t0 = 1994.0;
131 }
132 else if (_crdTrafo == "SIRGAS2000") {
133 _dx = 0.0020;
134 _dy = 0.0041;
135 _dz = 0.0039;
136 _dxr = 0.0000;
137 _dyr = 0.0000;
138 _dzr = 0.0000;
139 _ox = 0.000170;
140 _oy = -0.000030;
141 _oz = 0.000070;
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 emit(newMessage("decodeRtnetStream: " + lines[0].toAscii(), false));
247
248 struct ClockOrbit co;
249 memset(&co, 0, sizeof(co));
250 co.GPSEpochTime = static_cast<int>(epoTime.gpssec());
251 co.GLONASSEpochTime = static_cast<int>(fmod(epoTime.gpssec(), 86400.0))
252 + 3 * 3600 - gnumleap(year, month, day);
253 co.ClockDataSupplied = 1;
254 co.OrbitDataSupplied = 1;
255 co.SatRefDatum = DATUM_ITRF;
256 co.SSRIOD = _IOD;
257 co.SSRProviderID = _PID; // 256 .. BKG, 257 ... EUREF
258 co.SSRSolutionID = _SID;
259
260 struct Bias bias;
261 memset(&bias, 0, sizeof(bias));
262 bias.GPSEpochTime = co.GPSEpochTime;
263 bias.GLONASSEpochTime = co.GLONASSEpochTime;
264
265 // Default Update Interval
266 // -----------------------
267 int clkUpdInd = 2; // 5 sec
268 int ephUpdInd = clkUpdInd; // default
269 if (_samplRtcmEphCorr == 10.0) {
270 ephUpdInd = 3;
271 }
272 else if (_samplRtcmEphCorr == 15.0) {
273 ephUpdInd = 4;
274 }
275 else if (_samplRtcmEphCorr == 30.0) {
276 ephUpdInd = 5;
277 }
278 else if (_samplRtcmEphCorr == 60.0) {
279 ephUpdInd = 6;
280 }
281 else if (_samplRtcmEphCorr == 120.0) {
282 ephUpdInd = 7;
283 }
284 else if (_samplRtcmEphCorr == 240.0) {
285 ephUpdInd = 8;
286 }
287 else if (_samplRtcmEphCorr == 300.0) {
288 ephUpdInd = 9;
289 }
290 else if (_samplRtcmEphCorr == 600.0) {
291 ephUpdInd = 10;
292 }
293 else if (_samplRtcmEphCorr == 900.0) {
294 ephUpdInd = 11;
295 }
296
297 co.UpdateInterval = clkUpdInd;
298 bias.UpdateInterval = clkUpdInd;
299
300 for (int ii = 1; ii < lines.size(); ii++) {
301
302 QString prn;
303 ColumnVector xx(14); xx = 0.0;
304
305 QTextStream in(lines[ii].toAscii());
306
307 in >> prn;
308 if (prn[0] == 'P') {
309 prn.remove(0,1);
310 }
311
312 t_eph* eph = 0;
313 const bncEphUser::t_ephPair* ephPair = _ephUser->ephPair(prn);
314 if (ephPair) {
315
316 eph = ephPair->last;
317
318 // Use previous ephemeris if the last one is too recent
319 // ----------------------------------------------------
320 const int MINAGE = 60; // seconds
321 if (ephPair->prev && eph->receptDateTime().isValid() &&
322 eph->receptDateTime().secsTo(currentDateAndTimeGPS()) < MINAGE) {
323 eph = ephPair->prev;
324 }
325
326 // Make sure the clock messages refer to same IOD as orbit messages
327 // ----------------------------------------------------------------
328 if (_usedEph) {
329 if (fmod(epoTime.gpssec(), _samplRtcmEphCorr) == 0.0) {
330 (*_usedEph)[prn] = eph;
331 }
332 else {
333 eph = 0;
334 if (_usedEph->contains(prn)) {
335 t_eph* usedEph = _usedEph->value(prn);
336 if (usedEph == ephPair->last) {
337 eph = ephPair->last;
338 }
339 else if (usedEph == ephPair->prev) {
340 eph = ephPair->prev;
341 }
342 }
343 }
344 }
345 }
346
347 if (eph) {
348
349 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5)
350 >> xx(6) >> xx(7) >> xx(8) >> xx(9) >> xx(10)
351 >> xx(11) >> xx(12) >> xx(13) >> xx(14);
352 xx(1) *= 1e3; // x-crd
353 xx(2) *= 1e3; // y-crd
354 xx(3) *= 1e3; // z-crd
355 xx(4) *= 1e-6; // clk
356 xx(5) *= 1e-6; // rel. corr.
357 // xx(6), xx(7), xx(8) ... PhaseCent - CoM
358 // xx(9) ... P1-C1 DCB in meters
359 // xx(10) ... P1-P2 DCB in meters
360 // xx(11) ... dT
361 xx(12) *= 1e3; // x-crd at time + dT
362 xx(13) *= 1e3; // y-crd at time + dT
363 xx(14) *= 1e3; // z-crd at time + dT
364
365 struct ClockOrbit::SatData* sd = 0;
366 if (prn[0] == 'G') {
367 sd = co.Sat + co.NumberOfGPSSat;
368 ++co.NumberOfGPSSat;
369 }
370 else if (prn[0] == 'R') {
371 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
372 ++co.NumberOfGLONASSSat;
373 }
374 if (sd) {
375 QString outLine;
376 processSatellite(eph, epoTime.gpsw(), epoTime.gpssec(), prn,
377 xx, sd, outLine);
378 }
379
380 struct Bias::BiasSat* biasSat = 0;
381 if (prn[0] == 'G') {
382 biasSat = bias.Sat + bias.NumberOfGPSSat;
383 ++bias.NumberOfGPSSat;
384 }
385 else if (prn[0] == 'R') {
386 biasSat = bias.Sat + CLOCKORBIT_NUMGPS + bias.NumberOfGLONASSSat;
387 ++bias.NumberOfGLONASSSat;
388 }
389
390 // Coefficient of Ionosphere-Free LC
391 // ---------------------------------
392 const static double a_L1_GPS = 2.54572778;
393 const static double a_L2_GPS = -1.54572778;
394 const static double a_L1_Glo = 2.53125000;
395 const static double a_L2_Glo = -1.53125000;
396
397 if (biasSat) {
398 biasSat->ID = prn.mid(1).toInt();
399 biasSat->NumberOfCodeBiases = 3;
400 if (prn[0] == 'G') {
401 biasSat->Biases[0].Type = CODETYPEGPS_L1_Z;
402 biasSat->Biases[0].Bias = - a_L2_GPS * xx(10);
403 biasSat->Biases[1].Type = CODETYPEGPS_L1_CA;
404 biasSat->Biases[1].Bias = - a_L2_GPS * xx(10) + xx(9);
405 biasSat->Biases[2].Type = CODETYPEGPS_L2_Z;
406 biasSat->Biases[2].Bias = a_L1_GPS * xx(10);
407 }
408 else if (prn[0] == 'R') {
409 biasSat->Biases[0].Type = CODETYPEGLONASS_L1_P;
410 biasSat->Biases[0].Bias = - a_L2_Glo * xx(10);
411 biasSat->Biases[1].Type = CODETYPEGLONASS_L1_CA;
412 biasSat->Biases[1].Bias = - a_L2_Glo * xx(10) + xx(9);
413 biasSat->Biases[2].Type = CODETYPEGLONASS_L2_P;
414 biasSat->Biases[2].Bias = a_L1_Glo * xx(10);
415 }
416 }
417 }
418 }
419
420 QByteArray hlpBufferCo;
421
422 // Orbit and Clock Corrections together
423 // ------------------------------------
424 if (_samplRtcmEphCorr == 0.0) {
425 if (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) {
426 char obuffer[CLOCKORBIT_BUFFERSIZE];
427 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
428 if (len > 0) {
429 hlpBufferCo = QByteArray(obuffer, len);
430 }
431 }
432 }
433
434 // Orbit and Clock Corrections separately
435 // --------------------------------------
436 else {
437 if (co.NumberOfGPSSat > 0) {
438 char obuffer[CLOCKORBIT_BUFFERSIZE];
439 if (fmod(epoTime.gpssec(), _samplRtcmEphCorr) == 0.0) {
440 co.UpdateInterval = ephUpdInd;
441 int len1 = MakeClockOrbit(&co, COTYPE_GPSORBIT, 1, obuffer, sizeof(obuffer));
442 co.UpdateInterval = clkUpdInd;
443 if (len1 > 0) {
444 hlpBufferCo += QByteArray(obuffer, len1);
445 }
446 }
447 int mmsg = (co.NumberOfGLONASSSat > 0) ? 1 : 0;
448 int len2 = MakeClockOrbit(&co, COTYPE_GPSCLOCK, mmsg, obuffer, sizeof(obuffer));
449 if (len2 > 0) {
450 hlpBufferCo += QByteArray(obuffer, len2);
451 }
452 }
453 if (co.NumberOfGLONASSSat > 0) {
454 char obuffer[CLOCKORBIT_BUFFERSIZE];
455 if (fmod(epoTime.gpssec(), _samplRtcmEphCorr) == 0.0) {
456 co.UpdateInterval = ephUpdInd;
457 int len1 = MakeClockOrbit(&co, COTYPE_GLONASSORBIT, 1, obuffer, sizeof(obuffer));
458 co.UpdateInterval = clkUpdInd;
459 if (len1 > 0) {
460 hlpBufferCo += QByteArray(obuffer, len1);
461 }
462 }
463 int len2 = MakeClockOrbit(&co, COTYPE_GLONASSCLOCK, 0, obuffer, sizeof(obuffer));
464 if (len2 > 0) {
465 hlpBufferCo += QByteArray(obuffer, len2);
466 }
467 }
468 }
469
470 // Biases
471 // ------
472 QByteArray hlpBufferBias;
473 if (bias.NumberOfGPSSat > 0 || bias.NumberOfGLONASSSat > 0) {
474 char obuffer[CLOCKORBIT_BUFFERSIZE];
475 int len = MakeBias(&bias, BTYPE_AUTO, 0, obuffer, sizeof(obuffer));
476 if (len > 0) {
477 hlpBufferBias = QByteArray(obuffer, len);
478 }
479 }
480
481 _outBuffer += hlpBufferCo + hlpBufferBias;
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 // Broadcast Position and Velocity
493 // -------------------------------
494 ColumnVector xB(4);
495 ColumnVector vB(3);
496 eph->position(GPSweek, GPSweeks, xB.data(), vB.data());
497
498 // Precise Position and Velocity
499 // -----------------------------
500 ColumnVector xP = xx.Rows(1,3);
501 ColumnVector vP = (xx.Rows(12,14) - xx.Rows(1,3)) / xx(11);
502
503 // Correction Center of Mass -> Antenna Phase Center
504 // -------------------------------------------------
505 if (! _CoM) {
506 xP(1) += xx(6);
507 xP(2) += xx(7);
508 xP(3) += xx(8);
509 }
510
511 double dc = 0.0;
512 if (_crdTrafo != "IGS08") {
513 crdTrafo(GPSweek, xP, dc);
514 }
515
516 // Difference in xyz
517 // -----------------
518 ColumnVector dx = xB.Rows(1,3) - xP;
519 ColumnVector dv = vB - vP;
520
521 // Difference in RSW
522 // -----------------
523 ColumnVector rsw(3);
524 XYZ_to_RSW(xB.Rows(1,3), vB, dx, rsw);
525
526 ColumnVector dotRsw(3);
527 XYZ_to_RSW(xB.Rows(1,3), vB, dv, dotRsw);
528
529 // Clock Correction
530 // ----------------
531 double dClk = (xx(4) + xx(5) - xB(4) + dc) * t_CST::c;
532
533 if (sd) {
534 sd->ID = prn.mid(1).toInt();
535 sd->IOD = eph->IOD();
536 sd->Clock.DeltaA0 = dClk;
537 sd->Orbit.DeltaRadial = rsw(1);
538 sd->Orbit.DeltaAlongTrack = rsw(2);
539 sd->Orbit.DeltaCrossTrack = rsw(3);
540 sd->Orbit.DotDeltaRadial = dotRsw(1);
541 sd->Orbit.DotDeltaAlongTrack = dotRsw(2);
542 sd->Orbit.DotDeltaCrossTrack = dotRsw(3);
543 }
544
545 outLine.sprintf("%d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
546 GPSweek, GPSweeks, eph->prn().toAscii().data(),
547 eph->IOD(), dClk, rsw(1), rsw(2), rsw(3));
548
549 if (_rnx) {
550 _rnx->write(GPSweek, GPSweeks, prn, xx);
551 }
552 if (_sp3) {
553 _sp3->write(GPSweek, GPSweeks, prn, xx);
554 }
555}
556
557// Transform Coordinates
558////////////////////////////////////////////////////////////////////////////
559void bncRtnetUploadCaster::crdTrafo(int GPSWeek, ColumnVector& xyz,
560 double& dc) {
561
562 // Current epoch minus 2000.0 in years
563 // ------------------------------------
564 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
565
566 ColumnVector dx(3);
567
568 dx(1) = _dx + dt * _dxr;
569 dx(2) = _dy + dt * _dyr;
570 dx(3) = _dz + dt * _dzr;
571
572 static const double arcSec = 180.0 * 3600.0 / M_PI;
573
574 double ox = (_ox + dt * _oxr) / arcSec;
575 double oy = (_oy + dt * _oyr) / arcSec;
576 double oz = (_oz + dt * _ozr) / arcSec;
577
578 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
579
580 // Specify approximate center of area
581 // ----------------------------------
582 ColumnVector meanSta(3);
583
584 if (_crdTrafo == "ETRF2000") {
585 meanSta(1) = 3661090.0;
586 meanSta(2) = 845230.0;
587 meanSta(3) = 5136850.0;
588 }
589 else if (_crdTrafo == "NAD83") {
590 meanSta(1) = -1092950.0;
591 meanSta(2) = -4383600.0;
592 meanSta(3) = 4487420.0;
593 }
594 else if (_crdTrafo == "GDA94") {
595 meanSta(1) = -4052050.0;
596 meanSta(2) = 4212840.0;
597 meanSta(3) = -2545110.0;
598 }
599 else if (_crdTrafo == "SIRGAS2000") {
600 meanSta(1) = 3740860.0;
601 meanSta(2) = -4964290.0;
602 meanSta(3) = -1425420.0;
603 }
604 else if (_crdTrafo == "SIRGAS95") {
605 meanSta(1) = 3135390.0;
606 meanSta(2) = -5017670.0;
607 meanSta(3) = -2374440.0;
608 }
609 else if (_crdTrafo == "Custom") {
610 meanSta(1) = 0.0; // TODO
611 meanSta(2) = 0.0; // TODO
612 meanSta(3) = 0.0; // TODO
613 }
614
615 // Clock correction proportional to topocentric distance to satellites
616 // -------------------------------------------------------------------
617 double rho = (xyz - meanSta).norm_Frobenius();
618 dc = rho * (sc - 1.0) / sc / t_CST::c;
619
620 Matrix rMat(3,3);
621 rMat(1,1) = 1.0;
622 rMat(1,2) = -oz;
623 rMat(1,3) = oy;
624 rMat(2,1) = oz;
625 rMat(2,2) = 1.0;
626 rMat(2,3) = -ox;
627 rMat(3,1) = -oy;
628 rMat(3,2) = ox;
629 rMat(3,3) = 1.0;
630
631 xyz = sc * rMat * xyz + dx;
632}
633
Note: See TracBrowser for help on using the repository browser.