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

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