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

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