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

Last change on this file since 3229 was 3227, checked in by mervart, 15 years ago
File size: 13.6 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 buffer
197 // ----------------
198 _rtnetStreamBuffer.append(QByteArray(buffer, bufLen));
199 int iLastAsterix = _rtnetStreamBuffer.lastIndexOf('*'); // begin of last epoch
200 if (iLastAsterix == -1) {
201 _rtnetStreamBuffer.clear();
202 return;
203 }
204 else {
205 _rtnetStreamBuffer = _rtnetStreamBuffer.mid(iLastAsterix);
206 }
207
208 // Prepare list of lines with satellite positions in SP3-like format
209 // -----------------------------------------------------------------
210 QStringList lines = _rtnetStreamBuffer.split('\n', QString::SkipEmptyParts);
211
212 if (lines.size() < 2) {
213 return;
214 }
215
216 // Keep the last unfinished line in buffer
217 // ---------------------------------------
218 int iLastEOL = _rtnetStreamBuffer.lastIndexOf('\n');
219 if (iLastEOL != -1) {
220 _rtnetStreamBuffer = _rtnetStreamBuffer.mid(iLastEOL+1);
221 }
222
223 // Read first line (with epoch time)
224 // ---------------------------------
225 QTextStream in(lines[0].toAscii());
226 QString hlp;
227 int year, month, day, hour, min;
228 double sec;
229 in >> hlp >> year >> month >> day >> hour >> min >> sec;
230 bncTime epoTime; epoTime.set( year, month, day, hour, min, sec);
231
232 struct ClockOrbit co;
233 memset(&co, 0, sizeof(co));
234 co.GPSEpochTime = static_cast<int>(epoTime.gpssec());
235 co.GLONASSEpochTime = static_cast<int>(fmod(epoTime.gpssec(), 86400.0))
236 + 3 * 3600 - gnumleap(year, month, day);
237 co.ClockDataSupplied = 1;
238 co.OrbitDataSupplied = 1;
239 co.SatRefDatum = DATUM_ITRF;
240
241 struct Bias bias;
242 memset(&bias, 0, sizeof(bias));
243 bias.GPSEpochTime = co.GPSEpochTime;
244 bias.GLONASSEpochTime = co.GLONASSEpochTime;
245
246 for (int ii = 1; ii < lines.size(); ii++) {
247
248 QString prn;
249 ColumnVector xx(14); xx = 0.0;
250
251 QTextStream in(lines[ii].toAscii());
252
253 in >> prn;
254 if (prn[0] == 'P') {
255 prn.remove(0,1);
256 }
257
258 const bncEphUser::t_ephPair* ephPair = _ephUser->ephPair(prn);
259 if (ephPair) {
260 t_eph* eph = ephPair->last;
261 if (ephPair->prev &&
262 eph->receptDateTime().secsTo(QDateTime::currentDateTime()) < 60) {
263 eph = ephPair->prev;
264 }
265
266 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5)
267 >> xx(6) >> xx(7) >> xx(8) >> xx(9) >> xx(10)
268 >> xx(11) >> xx(12) >> xx(13) >> xx(14);
269 xx(1) *= 1e3; // x-crd
270 xx(2) *= 1e3; // y-crd
271 xx(3) *= 1e3; // z-crd
272 xx(4) *= 1e-6; // clk
273 xx(5) *= 1e-6; // rel. corr.
274 // xx(6), xx(7), xx(8) ... PhaseCent - CoM
275 // xx(9) ... P1-C1 DCB in meters
276 // xx(10) ... P1-P2 DCB in meters
277 // xx(11) ... dT
278 xx(12) *= 1e3; // x-crd at time + dT
279 xx(13) *= 1e3; // y-crd at time + dT
280 xx(14) *= 1e3; // z-crd at time + dT
281
282 struct ClockOrbit::SatData* sd = 0;
283 if (prn[0] == 'G') {
284 sd = co.Sat + co.NumberOfGPSSat;
285 ++co.NumberOfGPSSat;
286 }
287 else if (prn[0] == 'R') {
288 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
289 ++co.NumberOfGLONASSSat;
290 }
291 if (sd) {
292 QString outLine;
293 processSatellite(eph, epoTime.gpsw(), epoTime.gpssec(), prn,
294 xx, sd, outLine);
295 if (_outFile) {
296 _outFile->write(epoTime.gpsw(), epoTime.gpssec(), outLine);
297 }
298 }
299
300 struct Bias::BiasSat* biasSat = 0;
301 if (prn[0] == 'G') {
302 biasSat = bias.Sat + bias.NumberOfGPSSat;
303 ++bias.NumberOfGPSSat;
304 }
305 else if (prn[0] == 'R') {
306 biasSat = bias.Sat + CLOCKORBIT_NUMGPS + bias.NumberOfGLONASSSat;
307 ++bias.NumberOfGLONASSSat;
308 }
309
310 // Coefficient of Ionosphere-Free LC
311 // ---------------------------------
312 const static double a_L1_GPS = 2.54572778;
313 const static double a_L2_GPS = -1.54572778;
314 const static double a_L1_Glo = 2.53125000;
315 const static double a_L2_Glo = -1.53125000;
316
317 if (biasSat) {
318 biasSat->ID = prn.mid(1).toInt();
319 biasSat->NumberOfCodeBiases = 3;
320 if (prn[0] == 'G') {
321 biasSat->Biases[0].Type = CODETYPEGPS_L1_Z;
322 biasSat->Biases[0].Bias = - a_L2_GPS * xx(10);
323 biasSat->Biases[1].Type = CODETYPEGPS_L1_CA;
324 biasSat->Biases[1].Bias = - a_L2_GPS * xx(10) + xx(9);
325 biasSat->Biases[2].Type = CODETYPEGPS_L2_Z;
326 biasSat->Biases[2].Bias = a_L1_GPS * xx(10);
327 }
328 else if (prn[0] == 'R') {
329 biasSat->Biases[0].Type = CODETYPEGLONASS_L1_P;
330 biasSat->Biases[0].Bias = - a_L2_Glo * xx(10);
331 biasSat->Biases[1].Type = CODETYPEGLONASS_L1_CA;
332 biasSat->Biases[1].Bias = - a_L2_Glo * xx(10) + xx(9);
333 biasSat->Biases[2].Type = CODETYPEGLONASS_L2_P;
334 biasSat->Biases[2].Bias = a_L1_Glo * xx(10);
335 }
336 }
337 }
338 }
339
340 QByteArray hlpBufferCo;
341 if (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) {
342 char obuffer[CLOCKORBIT_BUFFERSIZE];
343
344 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
345 if (len > 0) {
346 hlpBufferCo = QByteArray(obuffer, len);
347 }
348 }
349
350 QByteArray hlpBufferBias;
351 if (bias.NumberOfGPSSat > 0 || bias.NumberOfGLONASSSat > 0) {
352 char obuffer[CLOCKORBIT_BUFFERSIZE];
353 int len = MakeBias(&bias, BTYPE_AUTO, 0, obuffer, sizeof(obuffer));
354 if (len > 0) {
355 hlpBufferBias = QByteArray(obuffer, len);
356 }
357 }
358
359 if (hlpBufferCo.size() > 0) {
360 _outBuffer = hlpBufferCo + hlpBufferBias;
361 }
362}
363
364//
365////////////////////////////////////////////////////////////////////////////
366void bncRtnetUploadCaster::processSatellite(t_eph* eph, int GPSweek,
367 double GPSweeks, const QString& prn,
368 const ColumnVector& xx,
369 struct ClockOrbit::SatData* sd,
370 QString& outLine) {
371
372 const double secPerWeek = 7.0 * 86400.0;
373
374 ColumnVector rsw(3);
375 ColumnVector rsw2(3);
376 double dClk;
377
378 for (int ii = 1; ii <= 2; ++ii) {
379
380 int GPSweek12 = GPSweek;
381 double GPSweeks12 = GPSweeks;
382 if (ii == 2) {
383 GPSweeks12 += xx(11);
384 if (GPSweeks12 > secPerWeek) {
385 GPSweek12 += 1;
386 GPSweeks12 -= secPerWeek;
387 }
388 }
389
390 ColumnVector xB(4);
391 ColumnVector vv(3);
392
393 eph->position(GPSweek12, GPSweeks12, xB.data(), vv.data());
394
395 ColumnVector xyz;
396 if (ii == 1) {
397 xyz = xx.Rows(1,3);
398 }
399 else {
400 xyz = xx.Rows(12,14);
401 }
402
403 // Correction Center of Mass -> Antenna Phase Center
404 // -------------------------------------------------
405 if (! _CoM) {
406 xyz(1) += xx(6);
407 xyz(2) += xx(7);
408 xyz(3) += xx(8);
409 }
410
411 if (_crdTrafo != "IGS05") {
412 crdTrafo(GPSweek12, xyz);
413 }
414
415 ColumnVector dx = xB.Rows(1,3) - xyz ;
416
417 if (ii == 1) {
418 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
419 dClk = (xx(4) + xx(5) - xB(4)) * 299792458.0;
420 }
421 else {
422 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw2);
423 }
424 }
425
426 if (sd) {
427 sd->ID = prn.mid(1).toInt();
428 sd->IOD = eph->IOD();
429 sd->Clock.DeltaA0 = dClk;
430 sd->Orbit.DeltaRadial = rsw(1);
431 sd->Orbit.DeltaAlongTrack = rsw(2);
432 sd->Orbit.DeltaCrossTrack = rsw(3);
433 sd->Orbit.DotDeltaRadial = (rsw2(1) - rsw(1)) / xx(11);
434 sd->Orbit.DotDeltaAlongTrack = (rsw2(2) - rsw(2)) / xx(11);
435 sd->Orbit.DotDeltaCrossTrack = (rsw2(3) - rsw(3)) / xx(11);
436 }
437
438 outLine.sprintf("%d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
439 GPSweek, GPSweeks, eph->prn().c_str(),
440 eph->IOD(), dClk, rsw(1), rsw(2), rsw(3));
441
442 if (_rnx) {
443 _rnx->write(GPSweek, GPSweeks, prn, xx);
444 }
445 if (_sp3) {
446 _sp3->write(GPSweek, GPSweeks, prn, xx);
447 }
448}
449
450// Transform Coordinates
451////////////////////////////////////////////////////////////////////////////
452void bncRtnetUploadCaster::crdTrafo(int GPSWeek, ColumnVector& xyz) {
453
454 // Current epoch minus 2000.0 in years
455 // ------------------------------------
456 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
457
458 ColumnVector dx(3);
459
460 dx(1) = _dx + dt * _dxr;
461 dx(2) = _dy + dt * _dyr;
462 dx(3) = _dz + dt * _dzr;
463
464 static const double arcSec = 180.0 * 3600.0 / M_PI;
465
466 double ox = (_ox + dt * _oxr) / arcSec;
467 double oy = (_oy + dt * _oyr) / arcSec;
468 double oz = (_oz + dt * _ozr) / arcSec;
469
470 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
471
472 Matrix rMat(3,3);
473 rMat(1,1) = 1.0;
474 rMat(1,2) = -oz;
475 rMat(1,3) = oy;
476 rMat(2,1) = oz;
477 rMat(2,2) = 1.0;
478 rMat(2,3) = -ox;
479 rMat(3,1) = -oy;
480 rMat(3,2) = ox;
481 rMat(3,3) = 1.0;
482
483 xyz = sc * rMat * xyz + dx;
484}
485
Note: See TracBrowser for help on using the repository browser.