source: ntrip/trunk/BNC/upload/bncrtnetuploadcaster.cpp@ 4174

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