source: ntrip/branches/BNC_LM/upload/bncrtnetuploadcaster.cpp@ 4515

Last change on this file since 4515 was 3498, checked in by neumaier, 13 years ago

Changed sign of NAD83 rotation and rotation rate

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