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

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