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

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