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

Last change on this file since 3272 was 3272, checked in by mervart, 13 years ago
File size: 14.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) {
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 if (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) {
355 char obuffer[CLOCKORBIT_BUFFERSIZE];
356
357 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
358 if (len > 0) {
359 hlpBufferCo = QByteArray(obuffer, len);
360 }
361 }
362
363 QByteArray hlpBufferBias;
364 if (bias.NumberOfGPSSat > 0 || bias.NumberOfGLONASSSat > 0) {
365 char obuffer[CLOCKORBIT_BUFFERSIZE];
366 int len = MakeBias(&bias, BTYPE_AUTO, 0, obuffer, sizeof(obuffer));
367 if (len > 0) {
368 hlpBufferBias = QByteArray(obuffer, len);
369 }
370 }
371
372 if (hlpBufferCo.size() > 0) {
373 _outBuffer = hlpBufferCo + hlpBufferBias;
374 }
375}
376
377//
378////////////////////////////////////////////////////////////////////////////
379void bncRtnetUploadCaster::processSatellite(t_eph* eph, int GPSweek,
380 double GPSweeks, const QString& prn,
381 const ColumnVector& xx,
382 struct ClockOrbit::SatData* sd,
383 QString& outLine) {
384
385 const double secPerWeek = 7.0 * 86400.0;
386
387 ColumnVector rsw(3);
388 ColumnVector rsw2(3);
389 double dClk;
390
391 for (int ii = 1; ii <= 2; ++ii) {
392
393 int GPSweek12 = GPSweek;
394 double GPSweeks12 = GPSweeks;
395 if (ii == 2) {
396 GPSweeks12 += xx(11);
397 if (GPSweeks12 > secPerWeek) {
398 GPSweek12 += 1;
399 GPSweeks12 -= secPerWeek;
400 }
401 }
402
403 ColumnVector xB(4);
404 ColumnVector vv(3);
405
406 eph->position(GPSweek12, GPSweeks12, xB.data(), vv.data());
407
408 ColumnVector xyz;
409 if (ii == 1) {
410 xyz = xx.Rows(1,3);
411 }
412 else {
413 xyz = xx.Rows(12,14);
414 }
415
416 // Correction Center of Mass -> Antenna Phase Center
417 // -------------------------------------------------
418 if (! _CoM) {
419 xyz(1) += xx(6);
420 xyz(2) += xx(7);
421 xyz(3) += xx(8);
422 }
423
424 if (_crdTrafo != "IGS05") {
425 crdTrafo(GPSweek12, xyz);
426 }
427
428 ColumnVector dx = xB.Rows(1,3) - xyz ;
429
430 if (ii == 1) {
431 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
432 dClk = (xx(4) + xx(5) - xB(4)) * 299792458.0;
433 }
434 else {
435 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw2);
436 }
437 }
438
439 if (sd) {
440 sd->ID = prn.mid(1).toInt();
441 sd->IOD = eph->IOD();
442 sd->Clock.DeltaA0 = dClk;
443 sd->Orbit.DeltaRadial = rsw(1);
444 sd->Orbit.DeltaAlongTrack = rsw(2);
445 sd->Orbit.DeltaCrossTrack = rsw(3);
446 sd->Orbit.DotDeltaRadial = (rsw2(1) - rsw(1)) / xx(11);
447 sd->Orbit.DotDeltaAlongTrack = (rsw2(2) - rsw(2)) / xx(11);
448 sd->Orbit.DotDeltaCrossTrack = (rsw2(3) - rsw(3)) / xx(11);
449 }
450
451 outLine.sprintf("%d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
452 GPSweek, GPSweeks, eph->prn().toAscii().data(),
453 eph->IOD(), dClk, rsw(1), rsw(2), rsw(3));
454
455 if (_rnx) {
456 _rnx->write(GPSweek, GPSweeks, prn, xx);
457 }
458 if (_sp3) {
459 _sp3->write(GPSweek, GPSweeks, prn, xx);
460 }
461}
462
463// Transform Coordinates
464////////////////////////////////////////////////////////////////////////////
465void bncRtnetUploadCaster::crdTrafo(int GPSWeek, ColumnVector& xyz) {
466
467 // Current epoch minus 2000.0 in years
468 // ------------------------------------
469 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
470
471 ColumnVector dx(3);
472
473 dx(1) = _dx + dt * _dxr;
474 dx(2) = _dy + dt * _dyr;
475 dx(3) = _dz + dt * _dzr;
476
477 static const double arcSec = 180.0 * 3600.0 / M_PI;
478
479 double ox = (_ox + dt * _oxr) / arcSec;
480 double oy = (_oy + dt * _oyr) / arcSec;
481 double oz = (_oz + dt * _ozr) / arcSec;
482
483 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
484
485 Matrix rMat(3,3);
486 rMat(1,1) = 1.0;
487 rMat(1,2) = -oz;
488 rMat(1,3) = oy;
489 rMat(2,1) = oz;
490 rMat(2,2) = 1.0;
491 rMat(2,3) = -ox;
492 rMat(3,1) = -oy;
493 rMat(3,2) = ox;
494 rMat(3,3) = 1.0;
495
496 xyz = sc * rMat * xyz + dx;
497}
498
Note: See TracBrowser for help on using the repository browser.