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

Last change on this file since 3230 was 3230, checked in by mervart, 13 years ago
File size: 13.7 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 bncUploadCaster(mountpoint, outHost, outPort, password) {
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 if (ephPair->prev &&
269 eph->receptDateTime().secsTo(QDateTime::currentDateTime()) < 60) {
270 eph = ephPair->prev;
271 }
272
273 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5)
274 >> xx(6) >> xx(7) >> xx(8) >> xx(9) >> xx(10)
275 >> xx(11) >> xx(12) >> xx(13) >> xx(14);
276 xx(1) *= 1e3; // x-crd
277 xx(2) *= 1e3; // y-crd
278 xx(3) *= 1e3; // z-crd
279 xx(4) *= 1e-6; // clk
280 xx(5) *= 1e-6; // rel. corr.
281 // xx(6), xx(7), xx(8) ... PhaseCent - CoM
282 // xx(9) ... P1-C1 DCB in meters
283 // xx(10) ... P1-P2 DCB in meters
284 // xx(11) ... dT
285 xx(12) *= 1e3; // x-crd at time + dT
286 xx(13) *= 1e3; // y-crd at time + dT
287 xx(14) *= 1e3; // z-crd at time + dT
288
289 struct ClockOrbit::SatData* sd = 0;
290 if (prn[0] == 'G') {
291 sd = co.Sat + co.NumberOfGPSSat;
292 ++co.NumberOfGPSSat;
293 }
294 else if (prn[0] == 'R') {
295 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
296 ++co.NumberOfGLONASSSat;
297 }
298 if (sd) {
299 QString outLine;
300 processSatellite(eph, epoTime.gpsw(), epoTime.gpssec(), prn,
301 xx, sd, outLine);
302 if (_outFile) {
303 _outFile->write(epoTime.gpsw(), epoTime.gpssec(), outLine);
304 }
305 }
306
307 struct Bias::BiasSat* biasSat = 0;
308 if (prn[0] == 'G') {
309 biasSat = bias.Sat + bias.NumberOfGPSSat;
310 ++bias.NumberOfGPSSat;
311 }
312 else if (prn[0] == 'R') {
313 biasSat = bias.Sat + CLOCKORBIT_NUMGPS + bias.NumberOfGLONASSSat;
314 ++bias.NumberOfGLONASSSat;
315 }
316
317 // Coefficient of Ionosphere-Free LC
318 // ---------------------------------
319 const static double a_L1_GPS = 2.54572778;
320 const static double a_L2_GPS = -1.54572778;
321 const static double a_L1_Glo = 2.53125000;
322 const static double a_L2_Glo = -1.53125000;
323
324 if (biasSat) {
325 biasSat->ID = prn.mid(1).toInt();
326 biasSat->NumberOfCodeBiases = 3;
327 if (prn[0] == 'G') {
328 biasSat->Biases[0].Type = CODETYPEGPS_L1_Z;
329 biasSat->Biases[0].Bias = - a_L2_GPS * xx(10);
330 biasSat->Biases[1].Type = CODETYPEGPS_L1_CA;
331 biasSat->Biases[1].Bias = - a_L2_GPS * xx(10) + xx(9);
332 biasSat->Biases[2].Type = CODETYPEGPS_L2_Z;
333 biasSat->Biases[2].Bias = a_L1_GPS * xx(10);
334 }
335 else if (prn[0] == 'R') {
336 biasSat->Biases[0].Type = CODETYPEGLONASS_L1_P;
337 biasSat->Biases[0].Bias = - a_L2_Glo * xx(10);
338 biasSat->Biases[1].Type = CODETYPEGLONASS_L1_CA;
339 biasSat->Biases[1].Bias = - a_L2_Glo * xx(10) + xx(9);
340 biasSat->Biases[2].Type = CODETYPEGLONASS_L2_P;
341 biasSat->Biases[2].Bias = a_L1_Glo * xx(10);
342 }
343 }
344 }
345 }
346
347 QByteArray hlpBufferCo;
348 if (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) {
349 char obuffer[CLOCKORBIT_BUFFERSIZE];
350
351 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
352 if (len > 0) {
353 hlpBufferCo = QByteArray(obuffer, len);
354 }
355 }
356
357 QByteArray hlpBufferBias;
358 if (bias.NumberOfGPSSat > 0 || bias.NumberOfGLONASSSat > 0) {
359 char obuffer[CLOCKORBIT_BUFFERSIZE];
360 int len = MakeBias(&bias, BTYPE_AUTO, 0, obuffer, sizeof(obuffer));
361 if (len > 0) {
362 hlpBufferBias = QByteArray(obuffer, len);
363 }
364 }
365
366 if (hlpBufferCo.size() > 0) {
367 _outBuffer = hlpBufferCo + hlpBufferBias;
368 }
369}
370
371//
372////////////////////////////////////////////////////////////////////////////
373void bncRtnetUploadCaster::processSatellite(t_eph* eph, int GPSweek,
374 double GPSweeks, const QString& prn,
375 const ColumnVector& xx,
376 struct ClockOrbit::SatData* sd,
377 QString& outLine) {
378
379 const double secPerWeek = 7.0 * 86400.0;
380
381 ColumnVector rsw(3);
382 ColumnVector rsw2(3);
383 double dClk;
384
385 for (int ii = 1; ii <= 2; ++ii) {
386
387 int GPSweek12 = GPSweek;
388 double GPSweeks12 = GPSweeks;
389 if (ii == 2) {
390 GPSweeks12 += xx(11);
391 if (GPSweeks12 > secPerWeek) {
392 GPSweek12 += 1;
393 GPSweeks12 -= secPerWeek;
394 }
395 }
396
397 ColumnVector xB(4);
398 ColumnVector vv(3);
399
400 eph->position(GPSweek12, GPSweeks12, xB.data(), vv.data());
401
402 ColumnVector xyz;
403 if (ii == 1) {
404 xyz = xx.Rows(1,3);
405 }
406 else {
407 xyz = xx.Rows(12,14);
408 }
409
410 // Correction Center of Mass -> Antenna Phase Center
411 // -------------------------------------------------
412 if (! _CoM) {
413 xyz(1) += xx(6);
414 xyz(2) += xx(7);
415 xyz(3) += xx(8);
416 }
417
418 if (_crdTrafo != "IGS05") {
419 crdTrafo(GPSweek12, xyz);
420 }
421
422 ColumnVector dx = xB.Rows(1,3) - xyz ;
423
424 if (ii == 1) {
425 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
426 dClk = (xx(4) + xx(5) - xB(4)) * 299792458.0;
427 }
428 else {
429 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw2);
430 }
431 }
432
433 if (sd) {
434 sd->ID = prn.mid(1).toInt();
435 sd->IOD = eph->IOD();
436 sd->Clock.DeltaA0 = dClk;
437 sd->Orbit.DeltaRadial = rsw(1);
438 sd->Orbit.DeltaAlongTrack = rsw(2);
439 sd->Orbit.DeltaCrossTrack = rsw(3);
440 sd->Orbit.DotDeltaRadial = (rsw2(1) - rsw(1)) / xx(11);
441 sd->Orbit.DotDeltaAlongTrack = (rsw2(2) - rsw(2)) / xx(11);
442 sd->Orbit.DotDeltaCrossTrack = (rsw2(3) - rsw(3)) / xx(11);
443 }
444
445 outLine.sprintf("%d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
446 GPSweek, GPSweeks, eph->prn().c_str(),
447 eph->IOD(), dClk, rsw(1), rsw(2), rsw(3));
448
449 if (_rnx) {
450 _rnx->write(GPSweek, GPSweeks, prn, xx);
451 }
452 if (_sp3) {
453 _sp3->write(GPSweek, GPSweeks, prn, xx);
454 }
455}
456
457// Transform Coordinates
458////////////////////////////////////////////////////////////////////////////
459void bncRtnetUploadCaster::crdTrafo(int GPSWeek, ColumnVector& xyz) {
460
461 // Current epoch minus 2000.0 in years
462 // ------------------------------------
463 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
464
465 ColumnVector dx(3);
466
467 dx(1) = _dx + dt * _dxr;
468 dx(2) = _dy + dt * _dyr;
469 dx(3) = _dz + dt * _dzr;
470
471 static const double arcSec = 180.0 * 3600.0 / M_PI;
472
473 double ox = (_ox + dt * _oxr) / arcSec;
474 double oy = (_oy + dt * _oyr) / arcSec;
475 double oz = (_oz + dt * _ozr) / arcSec;
476
477 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
478
479 Matrix rMat(3,3);
480 rMat(1,1) = 1.0;
481 rMat(1,2) = -oz;
482 rMat(1,3) = oy;
483 rMat(2,1) = oz;
484 rMat(2,2) = 1.0;
485 rMat(2,3) = -ox;
486 rMat(3,1) = -oy;
487 rMat(3,2) = ox;
488 rMat(3,3) = 1.0;
489
490 xyz = sc * rMat * xyz + dx;
491}
492
Note: See TracBrowser for help on using the repository browser.