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

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