source: ntrip/trunk/BNC/src/ephemeris.cpp@ 7888

Last change on this file since 7888 was 7888, checked in by stuerze, 8 years ago

a try to fix a persistent memory leak

File size: 46.3 KB
Line 
1#include <sstream>
2#include <iostream>
3#include <iomanip>
4#include <cstring>
5
6#include <newmatio.h>
7
8#include "ephemeris.h"
9#include "bncutils.h"
10#include "bnctime.h"
11#include "bnccore.h"
12#include "bncutils.h"
13#include "satObs.h"
14#include "pppInclude.h"
15#include "pppModel.h"
16
17using namespace std;
18
19// Constructor
20////////////////////////////////////////////////////////////////////////////
21t_eph::t_eph() {
22 _checkState = unchecked;
23 _orbCorr = 0;
24 _clkCorr = 0;
25}
26// Destructor
27////////////////////////////////////////////////////////////////////////////
28t_eph::~t_eph() {
29 if (_orbCorr)
30 delete _orbCorr;
31 if (_clkCorr)
32 delete _clkCorr;
33}
34
35//
36////////////////////////////////////////////////////////////////////////////
37void t_eph::setOrbCorr(const t_orbCorr* orbCorr) {
38 if (_orbCorr) {
39 delete _orbCorr;
40 _orbCorr = 0;
41 }
42 _orbCorr = new t_orbCorr(*orbCorr);
43}
44
45//
46////////////////////////////////////////////////////////////////////////////
47void t_eph::setClkCorr(const t_clkCorr* clkCorr) {
48 if (_clkCorr) {
49 delete _clkCorr;
50 _clkCorr = 0;
51 }
52 _clkCorr = new t_clkCorr(*clkCorr);
53}
54
55//
56////////////////////////////////////////////////////////////////////////////
57t_irc t_eph::getCrd(const bncTime& tt, ColumnVector& xc, ColumnVector& vv, bool useCorr) const {
58
59 if (_checkState == bad) {
60 return failure;
61 }
62 const QVector<int> updateInt = QVector<int>() << 1 << 2 << 5 << 10 << 15 << 30
63 << 60 << 120 << 240 << 300 << 600
64 << 900 << 1800 << 3600 << 7200
65 << 10800;
66 xc.ReSize(4);
67 vv.ReSize(3);
68 if (position(tt.gpsw(), tt.gpssec(), xc.data(), vv.data()) != success) {
69 return failure;
70 }
71 if (useCorr) {
72 if (_orbCorr && _clkCorr) {
73 double dtO = tt - _orbCorr->_time;
74 if (_orbCorr->_updateInt) {
75 dtO -= (0.5 * updateInt[_orbCorr->_updateInt]);
76 }
77 ColumnVector dx(3);
78 dx[0] = _orbCorr->_xr[0] + _orbCorr->_dotXr[0] * dtO;
79 dx[1] = _orbCorr->_xr[1] + _orbCorr->_dotXr[1] * dtO;
80 dx[2] = _orbCorr->_xr[2] + _orbCorr->_dotXr[2] * dtO;
81
82 RSW_to_XYZ(xc.Rows(1,3), vv.Rows(1,3), dx, dx);
83
84 xc[0] -= dx[0];
85 xc[1] -= dx[1];
86 xc[2] -= dx[2];
87
88 ColumnVector dv(3);
89 RSW_to_XYZ(xc.Rows(1,3), vv.Rows(1,3), _orbCorr->_dotXr, dv);
90
91 vv[0] -= dv[0];
92 vv[1] -= dv[1];
93 vv[2] -= dv[2];
94
95 double dtC = tt - _clkCorr->_time;
96 if (_clkCorr->_updateInt) {
97 dtC -= (0.5 * updateInt[_clkCorr->_updateInt]);
98 }
99 xc[3] += _clkCorr->_dClk + _clkCorr->_dotDClk * dtC + _clkCorr->_dotDotDClk * dtC * dtC;
100 }
101 else {
102 return failure;
103 }
104 }
105 return success;
106}
107
108//
109//////////////////////////////////////////////////////////////////////////////
110QString t_eph::rinexDateStr(const bncTime& tt, const t_prn& prn, double version) {
111 QString prnStr(prn.toString().c_str());
112 return rinexDateStr(tt, prnStr, version);
113}
114
115//
116//////////////////////////////////////////////////////////////////////////////
117QString t_eph::rinexDateStr(const bncTime& tt, const QString& prnStr, double version) {
118
119 QString datStr;
120
121 unsigned year, month, day, hour, min;
122 double sec;
123 tt.civil_date(year, month, day);
124 tt.civil_time(hour, min, sec);
125
126 QTextStream out(&datStr);
127
128 if (version < 3.0) {
129 QString prnHlp = prnStr.mid(1,2); if (prnHlp[0] == '0') prnHlp[0] = ' ';
130 out << prnHlp << QString(" %1 %2 %3 %4 %5%6")
131 .arg(year % 100, 2, 10, QChar('0'))
132 .arg(month, 2)
133 .arg(day, 2)
134 .arg(hour, 2)
135 .arg(min, 2)
136 .arg(sec, 5, 'f',1);
137 }
138 else {
139 out << prnStr << QString(" %1 %2 %3 %4 %5 %6")
140 .arg(year, 4)
141 .arg(month, 2, 10, QChar('0'))
142 .arg(day, 2, 10, QChar('0'))
143 .arg(hour, 2, 10, QChar('0'))
144 .arg(min, 2, 10, QChar('0'))
145 .arg(int(sec), 2, 10, QChar('0'));
146 }
147
148 return datStr;
149}
150
151// Constructor
152//////////////////////////////////////////////////////////////////////////////
153t_ephGPS::t_ephGPS(float rnxVersion, const QStringList& lines) {
154
155 const int nLines = 8;
156
157 if (lines.size() != nLines) {
158 _checkState = bad;
159 return;
160 }
161
162 // RINEX Format
163 // ------------
164 int fieldLen = 19;
165
166 int pos[4];
167 pos[0] = (rnxVersion <= 2.12) ? 3 : 4;
168 pos[1] = pos[0] + fieldLen;
169 pos[2] = pos[1] + fieldLen;
170 pos[3] = pos[2] + fieldLen;
171
172 // Read eight lines
173 // ----------------
174 for (int iLine = 0; iLine < nLines; iLine++) {
175 QString line = lines[iLine];
176
177 if ( iLine == 0 ) {
178 QTextStream in(line.left(pos[1]).toAscii());
179 int year, month, day, hour, min;
180 double sec;
181
182 QString prnStr, n;
183 in >> prnStr;
184
185 if (prnStr.size() == 1 &&
186 (prnStr[0] == 'G' || prnStr[0] == 'J')) {
187 in >> n;
188 prnStr.append(n);
189 }
190
191 in >> year >> month >> day >> hour >> min >> sec;
192 if (prnStr.at(0) == 'G') {
193 _prn.set('G', prnStr.mid(1).toInt());
194 }
195 else if (prnStr.at(0) == 'J') {
196 _prn.set('J', prnStr.mid(1).toInt());
197 }
198 else {
199 _prn.set('G', prnStr.toInt());
200 }
201
202 if (year < 80) {
203 year += 2000;
204 }
205 else if (year < 100) {
206 year += 1900;
207 }
208
209 _TOC.set(year, month, day, hour, min, sec);
210
211 if ( readDbl(line, pos[1], fieldLen, _clock_bias ) ||
212 readDbl(line, pos[2], fieldLen, _clock_drift ) ||
213 readDbl(line, pos[3], fieldLen, _clock_driftrate) ) {
214 _checkState = bad;
215 return;
216 }
217 }
218
219 else if ( iLine == 1 ) {
220 if ( readDbl(line, pos[0], fieldLen, _IODE ) ||
221 readDbl(line, pos[1], fieldLen, _Crs ) ||
222 readDbl(line, pos[2], fieldLen, _Delta_n) ||
223 readDbl(line, pos[3], fieldLen, _M0 ) ) {
224 _checkState = bad;
225 return;
226 }
227 }
228
229 else if ( iLine == 2 ) {
230 if ( readDbl(line, pos[0], fieldLen, _Cuc ) ||
231 readDbl(line, pos[1], fieldLen, _e ) ||
232 readDbl(line, pos[2], fieldLen, _Cus ) ||
233 readDbl(line, pos[3], fieldLen, _sqrt_A) ) {
234 _checkState = bad;
235 return;
236 }
237 }
238
239 else if ( iLine == 3 ) {
240 if ( readDbl(line, pos[0], fieldLen, _TOEsec) ||
241 readDbl(line, pos[1], fieldLen, _Cic ) ||
242 readDbl(line, pos[2], fieldLen, _OMEGA0) ||
243 readDbl(line, pos[3], fieldLen, _Cis ) ) {
244 _checkState = bad;
245 return;
246 }
247 }
248
249 else if ( iLine == 4 ) {
250 if ( readDbl(line, pos[0], fieldLen, _i0 ) ||
251 readDbl(line, pos[1], fieldLen, _Crc ) ||
252 readDbl(line, pos[2], fieldLen, _omega ) ||
253 readDbl(line, pos[3], fieldLen, _OMEGADOT) ) {
254 _checkState = bad;
255 return;
256 }
257 }
258
259 else if ( iLine == 5 ) {
260 if ( readDbl(line, pos[0], fieldLen, _IDOT ) ||
261 readDbl(line, pos[1], fieldLen, _L2Codes) ||
262 readDbl(line, pos[2], fieldLen, _TOEweek ) ||
263 readDbl(line, pos[3], fieldLen, _L2PFlag) ) {
264 _checkState = bad;
265 return;
266 }
267 }
268
269 else if ( iLine == 6 ) {
270 if ( readDbl(line, pos[0], fieldLen, _ura ) ||
271 readDbl(line, pos[1], fieldLen, _health) ||
272 readDbl(line, pos[2], fieldLen, _TGD ) ||
273 readDbl(line, pos[3], fieldLen, _IODC ) ) {
274 _checkState = bad;
275 return;
276 }
277 }
278
279 else if ( iLine == 7 ) {
280 if ( readDbl(line, pos[0], fieldLen, _TOT) ) {
281 _checkState = bad;
282 return;
283 }
284 readDbl(line, pos[1], fieldLen, _fitInterval); // _fitInterval optional
285 }
286 }
287}
288
289// Compute GPS Satellite Position (virtual)
290////////////////////////////////////////////////////////////////////////////
291t_irc t_ephGPS::position(int GPSweek, double GPSweeks, double* xc, double* vv) const {
292
293 if (_checkState == bad) {
294 return failure;
295 }
296
297 static const double omegaEarth = 7292115.1467e-11;
298 static const double gmGRS = 398.6005e12;
299
300 memset(xc, 0, 4*sizeof(double));
301 memset(vv, 0, 3*sizeof(double));
302
303 double a0 = _sqrt_A * _sqrt_A;
304 if (a0 == 0) {
305 return failure;
306 }
307
308 double n0 = sqrt(gmGRS/(a0*a0*a0));
309
310 bncTime tt(GPSweek, GPSweeks);
311 double tk = tt - bncTime(int(_TOEweek), _TOEsec);
312
313 double n = n0 + _Delta_n;
314 double M = _M0 + n*tk;
315 double E = M;
316 double E_last;
317 do {
318 E_last = E;
319 E = M + _e*sin(E);
320 } while ( fabs(E-E_last)*a0 > 0.001 );
321 double v = 2.0*atan( sqrt( (1.0 + _e)/(1.0 - _e) )*tan( E/2 ) );
322 double u0 = v + _omega;
323 double sin2u0 = sin(2*u0);
324 double cos2u0 = cos(2*u0);
325 double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
326 double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
327 double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
328 double xp = r*cos(u);
329 double yp = r*sin(u);
330 double OM = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk -
331 omegaEarth*_TOEsec;
332
333 double sinom = sin(OM);
334 double cosom = cos(OM);
335 double sini = sin(i);
336 double cosi = cos(i);
337 xc[0] = xp*cosom - yp*cosi*sinom;
338 xc[1] = xp*sinom + yp*cosi*cosom;
339 xc[2] = yp*sini;
340
341 double tc = tt - _TOC;
342 xc[3] = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
343
344 // Velocity
345 // --------
346 double tanv2 = tan(v/2);
347 double dEdM = 1 / (1 - _e*cos(E));
348 double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
349 * dEdM * n;
350 double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
351 double dotom = _OMEGADOT - omegaEarth;
352 double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
353 double dotr = a0 * _e*sin(E) * dEdM * n
354 + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
355 double dotx = dotr*cos(u) - r*sin(u)*dotu;
356 double doty = dotr*sin(u) + r*cos(u)*dotu;
357
358 vv[0] = cosom *dotx - cosi*sinom *doty // dX / dr
359 - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
360 + yp*sini*sinom*doti; // dX / di
361
362 vv[1] = sinom *dotx + cosi*cosom *doty
363 + xp*cosom*dotom - yp*cosi*sinom*dotom
364 - yp*sini*cosom*doti;
365
366 vv[2] = sini *doty + yp*cosi *doti;
367
368 // Relativistic Correction
369 // -----------------------
370 // correspondent to IGS convention and GPS ICD (and SSR standard)
371 xc[3] -= 2.0 * (xc[0]*vv[0] + xc[1]*vv[1] + xc[2]*vv[2]) / t_CST::c / t_CST::c;
372
373 return success;
374}
375
376// RINEX Format String
377//////////////////////////////////////////////////////////////////////////////
378QString t_ephGPS::toString(double version) const {
379
380 QString rnxStr = rinexDateStr(_TOC, _prn, version);
381
382 QTextStream out(&rnxStr);
383
384 out << QString("%1%2%3\n")
385 .arg(_clock_bias, 19, 'e', 12)
386 .arg(_clock_drift, 19, 'e', 12)
387 .arg(_clock_driftrate, 19, 'e', 12);
388
389 QString fmt = version < 3.0 ? " %1%2%3%4\n" : " %1%2%3%4\n";
390
391 out << QString(fmt)
392 .arg(_IODE, 19, 'e', 12)
393 .arg(_Crs, 19, 'e', 12)
394 .arg(_Delta_n, 19, 'e', 12)
395 .arg(_M0, 19, 'e', 12);
396
397 out << QString(fmt)
398 .arg(_Cuc, 19, 'e', 12)
399 .arg(_e, 19, 'e', 12)
400 .arg(_Cus, 19, 'e', 12)
401 .arg(_sqrt_A, 19, 'e', 12);
402
403 out << QString(fmt)
404 .arg(_TOEsec, 19, 'e', 12)
405 .arg(_Cic, 19, 'e', 12)
406 .arg(_OMEGA0, 19, 'e', 12)
407 .arg(_Cis, 19, 'e', 12);
408
409 out << QString(fmt)
410 .arg(_i0, 19, 'e', 12)
411 .arg(_Crc, 19, 'e', 12)
412 .arg(_omega, 19, 'e', 12)
413 .arg(_OMEGADOT, 19, 'e', 12);
414
415 out << QString(fmt)
416 .arg(_IDOT, 19, 'e', 12)
417 .arg(_L2Codes, 19, 'e', 12)
418 .arg(_TOEweek, 19, 'e', 12)
419 .arg(_L2PFlag, 19, 'e', 12);
420
421 out << QString(fmt)
422 .arg(_ura, 19, 'e', 12)
423 .arg(_health, 19, 'e', 12)
424 .arg(_TGD, 19, 'e', 12)
425 .arg(_IODC, 19, 'e', 12);
426
427 out << QString(fmt)
428 .arg(_TOT, 19, 'e', 12)
429 .arg(_fitInterval, 19, 'e', 12)
430 .arg("", 19, QChar(' '))
431 .arg("", 19, QChar(' '));
432
433 return rnxStr;
434}
435
436// Constructor
437//////////////////////////////////////////////////////////////////////////////
438t_ephGlo::t_ephGlo(float rnxVersion, const QStringList& lines) {
439
440 const int nLines = 4;
441
442 if (lines.size() != nLines) {
443 _checkState = bad;
444 return;
445 }
446
447 // RINEX Format
448 // ------------
449 int fieldLen = 19;
450
451 int pos[4];
452 pos[0] = (rnxVersion <= 2.12) ? 3 : 4;
453 pos[1] = pos[0] + fieldLen;
454 pos[2] = pos[1] + fieldLen;
455 pos[3] = pos[2] + fieldLen;
456
457 // Read four lines
458 // ---------------
459 for (int iLine = 0; iLine < nLines; iLine++) {
460 QString line = lines[iLine];
461
462 if ( iLine == 0 ) {
463 QTextStream in(line.left(pos[1]).toAscii());
464
465 int year, month, day, hour, min;
466 double sec;
467
468 QString prnStr, n;
469 in >> prnStr;
470 if (prnStr.size() == 1 && prnStr[0] == 'R') {
471 in >> n;
472 prnStr.append(n);
473 }
474 in >> year >> month >> day >> hour >> min >> sec;
475 if (prnStr.at(0) == 'R') {
476 _prn.set('R', prnStr.mid(1).toInt());
477 }
478 else {
479 _prn.set('R', prnStr.toInt());
480 }
481
482 if (year < 80) {
483 year += 2000;
484 }
485 else if (year < 100) {
486 year += 1900;
487 }
488
489 _gps_utc = gnumleap(year, month, day);
490
491 _TOC.set(year, month, day, hour, min, sec);
492 _TOC = _TOC + _gps_utc;
493
494 if ( readDbl(line, pos[1], fieldLen, _tau ) ||
495 readDbl(line, pos[2], fieldLen, _gamma) ||
496 readDbl(line, pos[3], fieldLen, _tki ) ) {
497 _checkState = bad;
498 return;
499 }
500
501 _tau = -_tau;
502 }
503
504 else if ( iLine == 1 ) {
505 if ( readDbl(line, pos[0], fieldLen, _x_pos ) ||
506 readDbl(line, pos[1], fieldLen, _x_velocity ) ||
507 readDbl(line, pos[2], fieldLen, _x_acceleration) ||
508 readDbl(line, pos[3], fieldLen, _health ) ) {
509 _checkState = bad;
510 return;
511 }
512 }
513
514 else if ( iLine == 2 ) {
515 if ( readDbl(line, pos[0], fieldLen, _y_pos ) ||
516 readDbl(line, pos[1], fieldLen, _y_velocity ) ||
517 readDbl(line, pos[2], fieldLen, _y_acceleration ) ||
518 readDbl(line, pos[3], fieldLen, _frequency_number) ) {
519 _checkState = bad;
520 return;
521 }
522 }
523
524 else if ( iLine == 3 ) {
525 if ( readDbl(line, pos[0], fieldLen, _z_pos ) ||
526 readDbl(line, pos[1], fieldLen, _z_velocity ) ||
527 readDbl(line, pos[2], fieldLen, _z_acceleration) ||
528 readDbl(line, pos[3], fieldLen, _E ) ) {
529 _checkState = bad;
530 return;
531 }
532 }
533 }
534
535 // Initialize status vector
536 // ------------------------
537 _tt = _TOC;
538 _xv.ReSize(6);
539 _xv(1) = _x_pos * 1.e3;
540 _xv(2) = _y_pos * 1.e3;
541 _xv(3) = _z_pos * 1.e3;
542 _xv(4) = _x_velocity * 1.e3;
543 _xv(5) = _y_velocity * 1.e3;
544 _xv(6) = _z_velocity * 1.e3;
545}
546
547// Compute Glonass Satellite Position (virtual)
548////////////////////////////////////////////////////////////////////////////
549t_irc t_ephGlo::position(int GPSweek, double GPSweeks, double* xc, double* vv) const {
550
551 if (_checkState == bad) {
552 return failure;
553 }
554
555 static const double nominalStep = 10.0;
556
557 memset(xc, 0, 4*sizeof(double));
558 memset(vv, 0, 3*sizeof(double));
559
560 double dtPos = bncTime(GPSweek, GPSweeks) - _tt;
561
562 if (fabs(dtPos) > 24*3600.0) {
563 return failure;
564 }
565
566 int nSteps = int(fabs(dtPos) / nominalStep) + 1;
567 double step = dtPos / nSteps;
568
569 double acc[3];
570 acc[0] = _x_acceleration * 1.e3;
571 acc[1] = _y_acceleration * 1.e3;
572 acc[2] = _z_acceleration * 1.e3;
573 for (int ii = 1; ii <= nSteps; ii++) {
574 _xv = rungeKutta4(_tt.gpssec(), _xv, step, acc, glo_deriv);
575 _tt = _tt + step;
576 }
577
578 // Position and Velocity
579 // ---------------------
580 xc[0] = _xv(1);
581 xc[1] = _xv(2);
582 xc[2] = _xv(3);
583
584 vv[0] = _xv(4);
585 vv[1] = _xv(5);
586 vv[2] = _xv(6);
587
588 // Clock Correction
589 // ----------------
590 double dtClk = bncTime(GPSweek, GPSweeks) - _TOC;
591 xc[3] = -_tau + _gamma * dtClk;
592
593 return success;
594}
595
596// RINEX Format String
597//////////////////////////////////////////////////////////////////////////////
598QString t_ephGlo::toString(double version) const {
599
600 QString rnxStr = rinexDateStr(_TOC-_gps_utc, _prn, version);
601
602 QTextStream out(&rnxStr);
603
604 out << QString("%1%2%3\n")
605 .arg(-_tau, 19, 'e', 12)
606 .arg(_gamma, 19, 'e', 12)
607 .arg(_tki, 19, 'e', 12);
608
609 QString fmt = version < 3.0 ? " %1%2%3%4\n" : " %1%2%3%4\n";
610
611 out << QString(fmt)
612 .arg(_x_pos, 19, 'e', 12)
613 .arg(_x_velocity, 19, 'e', 12)
614 .arg(_x_acceleration, 19, 'e', 12)
615 .arg(_health, 19, 'e', 12);
616
617 out << QString(fmt)
618 .arg(_y_pos, 19, 'e', 12)
619 .arg(_y_velocity, 19, 'e', 12)
620 .arg(_y_acceleration, 19, 'e', 12)
621 .arg(_frequency_number, 19, 'e', 12);
622
623 out << QString(fmt)
624 .arg(_z_pos, 19, 'e', 12)
625 .arg(_z_velocity, 19, 'e', 12)
626 .arg(_z_acceleration, 19, 'e', 12)
627 .arg(_E, 19, 'e', 12);
628
629 return rnxStr;
630}
631
632// Derivative of the state vector using a simple force model (static)
633////////////////////////////////////////////////////////////////////////////
634ColumnVector t_ephGlo::glo_deriv(double /* tt */, const ColumnVector& xv,
635 double* acc) {
636
637 // State vector components
638 // -----------------------
639 ColumnVector rr = xv.rows(1,3);
640 ColumnVector vv = xv.rows(4,6);
641
642 // Acceleration
643 // ------------
644 static const double gmWGS = 398.60044e12;
645 static const double AE = 6378136.0;
646 static const double OMEGA = 7292115.e-11;
647 static const double C20 = -1082.6257e-6;
648
649 double rho = rr.norm_Frobenius();
650 double t1 = -gmWGS/(rho*rho*rho);
651 double t2 = 3.0/2.0 * C20 * (gmWGS*AE*AE) / (rho*rho*rho*rho*rho);
652 double t3 = OMEGA * OMEGA;
653 double t4 = 2.0 * OMEGA;
654 double z2 = rr(3) * rr(3);
655
656 // Vector of derivatives
657 // ---------------------
658 ColumnVector va(6);
659 va(1) = vv(1);
660 va(2) = vv(2);
661 va(3) = vv(3);
662 va(4) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(1) + t4*vv(2) + acc[0];
663 va(5) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(2) - t4*vv(1) + acc[1];
664 va(6) = (t1 + t2*(3.0-5.0*z2/(rho*rho)) ) * rr(3) + acc[2];
665
666 return va;
667}
668
669// IOD of Glonass Ephemeris (virtual)
670////////////////////////////////////////////////////////////////////////////
671unsigned int t_ephGlo::IOD() const {
672 bncTime tMoscow = _TOC - _gps_utc + 3 * 3600.0;
673 return (unsigned long)tMoscow.daysec() / 900;
674}
675
676// Constructor
677//////////////////////////////////////////////////////////////////////////////
678t_ephGal::t_ephGal(float rnxVersion, const QStringList& lines) {
679 int year, month, day, hour, min;
680 double sec;
681 QString prnStr;
682 const int nLines = 8;
683 if (lines.size() != nLines) {
684 _checkState = bad;
685 return;
686 }
687
688 // RINEX Format
689 // ------------
690 int fieldLen = 19;
691 double SVhealth = 0.0;
692 double datasource = 0.0;
693
694 int pos[4];
695 pos[0] = (rnxVersion <= 2.12) ? 3 : 4;
696 pos[1] = pos[0] + fieldLen;
697 pos[2] = pos[1] + fieldLen;
698 pos[3] = pos[2] + fieldLen;
699
700 // Read eight lines
701 // ----------------
702 for (int iLine = 0; iLine < nLines; iLine++) {
703 QString line = lines[iLine];
704
705 if ( iLine == 0 ) {
706 QTextStream in(line.left(pos[1]).toAscii());
707 QString n;
708 in >> prnStr;
709 if (prnStr.size() == 1 && prnStr[0] == 'E') {
710 in >> n;
711 prnStr.append(n);
712 }
713 in >> year >> month >> day >> hour >> min >> sec;
714 if (year < 80) {
715 year += 2000;
716 }
717 else if (year < 100) {
718 year += 1900;
719 }
720
721 _TOC.set(year, month, day, hour, min, sec);
722
723 if ( readDbl(line, pos[1], fieldLen, _clock_bias ) ||
724 readDbl(line, pos[2], fieldLen, _clock_drift ) ||
725 readDbl(line, pos[3], fieldLen, _clock_driftrate) ) {
726 _checkState = bad;
727 return;
728 }
729 }
730
731 else if ( iLine == 1 ) {
732 if ( readDbl(line, pos[0], fieldLen, _IODnav ) ||
733 readDbl(line, pos[1], fieldLen, _Crs ) ||
734 readDbl(line, pos[2], fieldLen, _Delta_n) ||
735 readDbl(line, pos[3], fieldLen, _M0 ) ) {
736 _checkState = bad;
737 return;
738 }
739 }
740
741 else if ( iLine == 2 ) {
742 if ( readDbl(line, pos[0], fieldLen, _Cuc ) ||
743 readDbl(line, pos[1], fieldLen, _e ) ||
744 readDbl(line, pos[2], fieldLen, _Cus ) ||
745 readDbl(line, pos[3], fieldLen, _sqrt_A) ) {
746 _checkState = bad;
747 return;
748 }
749 }
750
751 else if ( iLine == 3 ) {
752 if ( readDbl(line, pos[0], fieldLen, _TOEsec) ||
753 readDbl(line, pos[1], fieldLen, _Cic ) ||
754 readDbl(line, pos[2], fieldLen, _OMEGA0) ||
755 readDbl(line, pos[3], fieldLen, _Cis ) ) {
756 _checkState = bad;
757 return;
758 }
759 }
760
761 else if ( iLine == 4 ) {
762 if ( readDbl(line, pos[0], fieldLen, _i0 ) ||
763 readDbl(line, pos[1], fieldLen, _Crc ) ||
764 readDbl(line, pos[2], fieldLen, _omega ) ||
765 readDbl(line, pos[3], fieldLen, _OMEGADOT) ) {
766 _checkState = bad;
767 return;
768 }
769 }
770
771 else if ( iLine == 5 ) {
772 if ( readDbl(line, pos[0], fieldLen, _IDOT ) ||
773 readDbl(line, pos[1], fieldLen, datasource) ||
774 readDbl(line, pos[2], fieldLen, _TOEweek ) ) {
775 _checkState = bad;
776 return;
777 } else {
778 if (int(datasource) & (1<<8)) {
779 _fnav = true;
780 _inav = false;
781 } else if (int(datasource) & (1<<9)) {
782 _fnav = false;
783 _inav = true;
784 }
785 _TOEweek -= 1024.0;
786 }
787 }
788
789 else if ( iLine == 6 ) {
790 if ( readDbl(line, pos[0], fieldLen, _SISA ) ||
791 readDbl(line, pos[1], fieldLen, SVhealth) ||
792 readDbl(line, pos[2], fieldLen, _BGD_1_5A) ||
793 readDbl(line, pos[3], fieldLen, _BGD_1_5B) ) {
794 _checkState = bad;
795 return;
796 } else {
797 // Bit 0
798 _e1DataInValid = (int(SVhealth) & (1<<0));
799 // Bit 1-2
800 _E1_bHS = double((int(SVhealth) >> 1) & 0x3);
801 // Bit 3
802 _e5aDataInValid = (int(SVhealth) & (1<<3));
803 // Bit 4-5
804 _E5aHS = double((int(SVhealth) >> 4) & 0x3);
805 // Bit 6
806 _e5bDataInValid = (int(SVhealth) & (1<<6));
807 // Bit 7-8
808 _E5bHS = double((int(SVhealth) >> 7) & 0x3);
809
810 if (prnStr.at(0) == 'E') {
811 _prn.set('E', prnStr.mid(1).toInt(), _inav ? 1 : 0);
812 }
813 }
814 }
815
816 else if ( iLine == 7 ) {
817 if ( readDbl(line, pos[0], fieldLen, _TOT) ) {
818 _checkState = bad;
819 return;
820 }
821 }
822 }
823}
824
825// Compute Galileo Satellite Position (virtual)
826////////////////////////////////////////////////////////////////////////////
827t_irc t_ephGal::position(int GPSweek, double GPSweeks, double* xc, double* vv) const {
828
829 if (_checkState == bad) {
830 return failure;
831 }
832
833 static const double omegaEarth = 7292115.1467e-11;
834 static const double gmWGS = 398.60044e12;
835
836 memset(xc, 0, 4*sizeof(double));
837 memset(vv, 0, 3*sizeof(double));
838
839 double a0 = _sqrt_A * _sqrt_A;
840 if (a0 == 0) {
841 return failure;
842 }
843
844 double n0 = sqrt(gmWGS/(a0*a0*a0));
845
846 bncTime tt(GPSweek, GPSweeks);
847 double tk = tt - bncTime(_TOC.gpsw(), _TOEsec);
848
849 double n = n0 + _Delta_n;
850 double M = _M0 + n*tk;
851 double E = M;
852 double E_last;
853 do {
854 E_last = E;
855 E = M + _e*sin(E);
856 } while ( fabs(E-E_last)*a0 > 0.001 );
857 double v = 2.0*atan( sqrt( (1.0 + _e)/(1.0 - _e) )*tan( E/2 ) );
858 double u0 = v + _omega;
859 double sin2u0 = sin(2*u0);
860 double cos2u0 = cos(2*u0);
861 double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
862 double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
863 double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
864 double xp = r*cos(u);
865 double yp = r*sin(u);
866 double OM = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk -
867 omegaEarth*_TOEsec;
868
869 double sinom = sin(OM);
870 double cosom = cos(OM);
871 double sini = sin(i);
872 double cosi = cos(i);
873 xc[0] = xp*cosom - yp*cosi*sinom;
874 xc[1] = xp*sinom + yp*cosi*cosom;
875 xc[2] = yp*sini;
876
877 double tc = tt - _TOC;
878 xc[3] = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
879
880 // Velocity
881 // --------
882 double tanv2 = tan(v/2);
883 double dEdM = 1 / (1 - _e*cos(E));
884 double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
885 * dEdM * n;
886 double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
887 double dotom = _OMEGADOT - omegaEarth;
888 double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
889 double dotr = a0 * _e*sin(E) * dEdM * n
890 + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
891 double dotx = dotr*cos(u) - r*sin(u)*dotu;
892 double doty = dotr*sin(u) + r*cos(u)*dotu;
893
894 vv[0] = cosom *dotx - cosi*sinom *doty // dX / dr
895 - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
896 + yp*sini*sinom*doti; // dX / di
897
898 vv[1] = sinom *dotx + cosi*cosom *doty
899 + xp*cosom*dotom - yp*cosi*sinom*dotom
900 - yp*sini*cosom*doti;
901
902 vv[2] = sini *doty + yp*cosi *doti;
903
904 // Relativistic Correction
905 // -----------------------
906 // correspondent to Galileo ICD and to SSR standard
907 xc[3] -= 4.442807633e-10 * _e * sqrt(a0) *sin(E);
908 // correspondent to IGS convention
909 //xc[3] -= 2.0 * (xc[0]*vv[0] + xc[1]*vv[1] + xc[2]*vv[2]) / t_CST::c / t_CST::c;
910
911 return success;
912}
913
914// RINEX Format String
915//////////////////////////////////////////////////////////////////////////////
916QString t_ephGal::toString(double version) const {
917
918 QString rnxStr = rinexDateStr(_TOC, _prn, version);
919
920 QTextStream out(&rnxStr);
921
922 out << QString("%1%2%3\n")
923 .arg(_clock_bias, 19, 'e', 12)
924 .arg(_clock_drift, 19, 'e', 12)
925 .arg(_clock_driftrate, 19, 'e', 12);
926
927 QString fmt = version < 3.0 ? " %1%2%3%4\n" : " %1%2%3%4\n";
928
929 out << QString(fmt)
930 .arg(_IODnav, 19, 'e', 12)
931 .arg(_Crs, 19, 'e', 12)
932 .arg(_Delta_n, 19, 'e', 12)
933 .arg(_M0, 19, 'e', 12);
934
935 out << QString(fmt)
936 .arg(_Cuc, 19, 'e', 12)
937 .arg(_e, 19, 'e', 12)
938 .arg(_Cus, 19, 'e', 12)
939 .arg(_sqrt_A, 19, 'e', 12);
940
941 out << QString(fmt)
942 .arg(_TOEsec, 19, 'e', 12)
943 .arg(_Cic, 19, 'e', 12)
944 .arg(_OMEGA0, 19, 'e', 12)
945 .arg(_Cis, 19, 'e', 12);
946
947 out << QString(fmt)
948 .arg(_i0, 19, 'e', 12)
949 .arg(_Crc, 19, 'e', 12)
950 .arg(_omega, 19, 'e', 12)
951 .arg(_OMEGADOT, 19, 'e', 12);
952
953 int dataSource = 0;
954 int SVhealth = 0;
955 double BGD_1_5A = _BGD_1_5A;
956 double BGD_1_5B = _BGD_1_5B;
957 if (_fnav) {
958 dataSource |= (1<<1);
959 dataSource |= (1<<8);
960 BGD_1_5B = 0.0;
961 // SVhealth
962 // Bit 3 : E5a DVS
963 if (_e5aDataInValid) {
964 SVhealth |= (1<<3);
965 }
966 // Bit 4-5: E5a HS
967 if (_E5aHS == 1.0) {
968 SVhealth |= (1<<4);
969 }
970 else if (_E5aHS == 2.0) {
971 SVhealth |= (1<<5);
972 }
973 else if (_E5aHS == 3.0) {
974 SVhealth |= (1<<4);
975 SVhealth |= (1<<5);
976 }
977 }
978 else if(_inav) {
979 // Bit 2 and 0 are set because from MT1046 the data source cannot be determined
980 // and RNXv3.03 says both can be set if the navigation messages were merged
981 dataSource |= (1<<0);
982 dataSource |= (1<<2);
983 dataSource |= (1<<9);
984 // SVhealth
985 // Bit 0 : E1-B DVS
986 if (_e1DataInValid) {
987 SVhealth |= (1<<0);
988 }
989 // Bit 1-2: E1-B HS
990 if (_E1_bHS == 1.0) {
991 SVhealth |= (1<<1);
992 }
993 else if (_E1_bHS == 2.0) {
994 SVhealth |= (1<<2);
995 }
996 else if (_E1_bHS == 3.0) {
997 SVhealth |= (1<<1);
998 SVhealth |= (1<<2);
999 }
1000 // Bit 3 : E5a DVS
1001 if (_e5aDataInValid) {
1002 SVhealth |= (1<<3);
1003 }
1004 // Bit 4-5: E5a HS
1005 if (_E5aHS == 1.0) {
1006 SVhealth |= (1<<4);
1007 }
1008 else if (_E5aHS == 2.0) {
1009 SVhealth |= (1<<5);
1010 }
1011 else if (_E5aHS == 3.0) {
1012 SVhealth |= (1<<4);
1013 SVhealth |= (1<<5);
1014 }
1015 // Bit 6 : E5b DVS
1016 if (_e5bDataInValid) {
1017 SVhealth |= (1<<6);
1018 }
1019 // Bit 7-8: E5b HS
1020 if (_E5bHS == 1.0) {
1021 SVhealth |= (1<<7);
1022 }
1023 else if (_E5bHS == 2.0) {
1024 SVhealth |= (1<<8);
1025 }
1026 else if (_E5bHS == 3.0) {
1027 SVhealth |= (1<<7);
1028 SVhealth |= (1<<8);
1029 }
1030 }
1031
1032 out << QString(fmt)
1033 .arg(_IDOT, 19, 'e', 12)
1034 .arg(double(dataSource), 19, 'e', 12)
1035 .arg(_TOEweek + 1024.0, 19, 'e', 12)
1036 .arg(0.0, 19, 'e', 12);
1037
1038 out << QString(fmt)
1039 .arg(_SISA, 19, 'e', 12)
1040 .arg(double(SVhealth), 19, 'e', 12)
1041 .arg(BGD_1_5A, 19, 'e', 12)
1042 .arg(BGD_1_5B, 19, 'e', 12);
1043
1044 out << QString(fmt)
1045 .arg(_TOT, 19, 'e', 12)
1046 .arg("", 19, QChar(' '))
1047 .arg("", 19, QChar(' '))
1048 .arg("", 19, QChar(' '));
1049
1050 return rnxStr;
1051}
1052
1053// Constructor
1054//////////////////////////////////////////////////////////////////////////////
1055t_ephSBAS::t_ephSBAS(float rnxVersion, const QStringList& lines) {
1056
1057 const int nLines = 4;
1058
1059 if (lines.size() != nLines) {
1060 _checkState = bad;
1061 return;
1062 }
1063
1064 // RINEX Format
1065 // ------------
1066 int fieldLen = 19;
1067
1068 int pos[4];
1069 pos[0] = (rnxVersion <= 2.12) ? 3 : 4;
1070 pos[1] = pos[0] + fieldLen;
1071 pos[2] = pos[1] + fieldLen;
1072 pos[3] = pos[2] + fieldLen;
1073
1074 // Read four lines
1075 // ---------------
1076 for (int iLine = 0; iLine < nLines; iLine++) {
1077 QString line = lines[iLine];
1078
1079 if ( iLine == 0 ) {
1080 QTextStream in(line.left(pos[1]).toAscii());
1081
1082 int year, month, day, hour, min;
1083 double sec;
1084
1085 QString prnStr, n;
1086 in >> prnStr;
1087 if (prnStr.size() == 1 && prnStr[0] == 'S') {
1088 in >> n;
1089 prnStr.append(n);
1090 }
1091 in >> year >> month >> day >> hour >> min >> sec;
1092 if (prnStr.at(0) == 'S') {
1093 _prn.set('S', prnStr.mid(1).toInt());
1094 }
1095 else {
1096 _prn.set('S', prnStr.toInt());
1097 }
1098
1099 if (year < 80) {
1100 year += 2000;
1101 }
1102 else if (year < 100) {
1103 year += 1900;
1104 }
1105
1106 _TOC.set(year, month, day, hour, min, sec);
1107
1108 if ( readDbl(line, pos[1], fieldLen, _agf0 ) ||
1109 readDbl(line, pos[2], fieldLen, _agf1 ) ||
1110 readDbl(line, pos[3], fieldLen, _TOW ) ) {
1111 _checkState = bad;
1112 return;
1113 }
1114 }
1115
1116 else if ( iLine == 1 ) {
1117 if ( readDbl(line, pos[0], fieldLen, _x_pos ) ||
1118 readDbl(line, pos[1], fieldLen, _x_velocity ) ||
1119 readDbl(line, pos[2], fieldLen, _x_acceleration) ||
1120 readDbl(line, pos[3], fieldLen, _health ) ) {
1121 _checkState = bad;
1122 return;
1123 }
1124 }
1125
1126 else if ( iLine == 2 ) {
1127 if ( readDbl(line, pos[0], fieldLen, _y_pos ) ||
1128 readDbl(line, pos[1], fieldLen, _y_velocity ) ||
1129 readDbl(line, pos[2], fieldLen, _y_acceleration ) ||
1130 readDbl(line, pos[3], fieldLen, _ura ) ) {
1131 _checkState = bad;
1132 return;
1133 }
1134 }
1135
1136 else if ( iLine == 3 ) {
1137 double iodn;
1138 if ( readDbl(line, pos[0], fieldLen, _z_pos ) ||
1139 readDbl(line, pos[1], fieldLen, _z_velocity ) ||
1140 readDbl(line, pos[2], fieldLen, _z_acceleration) ||
1141 readDbl(line, pos[3], fieldLen, iodn ) ) {
1142 _checkState = bad;
1143 return;
1144 } else {
1145 _IODN = int(iodn);
1146 }
1147 }
1148 }
1149
1150 _x_pos *= 1.e3;
1151 _y_pos *= 1.e3;
1152 _z_pos *= 1.e3;
1153 _x_velocity *= 1.e3;
1154 _y_velocity *= 1.e3;
1155 _z_velocity *= 1.e3;
1156 _x_acceleration *= 1.e3;
1157 _y_acceleration *= 1.e3;
1158 _z_acceleration *= 1.e3;
1159}
1160
1161// IOD of SBAS Ephemeris (virtual)
1162////////////////////////////////////////////////////////////////////////////
1163
1164unsigned int t_ephSBAS::IOD() const {
1165 unsigned char buffer[80];
1166 int size = 0;
1167 int numbits = 0;
1168 long long bitbuffer = 0;
1169 unsigned char *startbuffer = buffer;
1170
1171 SBASADDBITSFLOAT(30, this->_x_pos, 0.08)
1172 SBASADDBITSFLOAT(30, this->_y_pos, 0.08)
1173 SBASADDBITSFLOAT(25, this->_z_pos, 0.4)
1174 SBASADDBITSFLOAT(17, this->_x_velocity, 0.000625)
1175 SBASADDBITSFLOAT(17, this->_y_velocity, 0.000625)
1176 SBASADDBITSFLOAT(18, this->_z_velocity, 0.004)
1177 SBASADDBITSFLOAT(10, this->_x_acceleration, 0.0000125)
1178 SBASADDBITSFLOAT(10, this->_y_acceleration, 0.0000125)
1179 SBASADDBITSFLOAT(10, this->_z_acceleration, 0.0000625)
1180 SBASADDBITSFLOAT(12, this->_agf0, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<1))
1181 SBASADDBITSFLOAT(8, this->_agf1, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<10))
1182 SBASADDBITS(5,0); // the last byte is filled by 0-bits to obtain a length of an integer multiple of 8
1183
1184 return CRC24(size, startbuffer);
1185}
1186
1187// Compute SBAS Satellite Position (virtual)
1188////////////////////////////////////////////////////////////////////////////
1189t_irc t_ephSBAS::position(int GPSweek, double GPSweeks, double* xc, double* vv) const {
1190
1191 if (_checkState == bad) {
1192 return failure;
1193 }
1194
1195 bncTime tt(GPSweek, GPSweeks);
1196 double dt = tt - _TOC;
1197
1198 xc[0] = _x_pos + _x_velocity * dt + _x_acceleration * dt * dt / 2.0;
1199 xc[1] = _y_pos + _y_velocity * dt + _y_acceleration * dt * dt / 2.0;
1200 xc[2] = _z_pos + _z_velocity * dt + _z_acceleration * dt * dt / 2.0;
1201
1202 vv[0] = _x_velocity + _x_acceleration * dt;
1203 vv[1] = _y_velocity + _y_acceleration * dt;
1204 vv[2] = _z_velocity + _z_acceleration * dt;
1205
1206 xc[3] = _agf0 + _agf1 * dt;
1207
1208 return success;
1209}
1210
1211// RINEX Format String
1212//////////////////////////////////////////////////////////////////////////////
1213QString t_ephSBAS::toString(double version) const {
1214
1215 QString rnxStr = rinexDateStr(_TOC, _prn, version);
1216
1217 QTextStream out(&rnxStr);
1218
1219 out << QString("%1%2%3\n")
1220 .arg(_agf0, 19, 'e', 12)
1221 .arg(_agf1, 19, 'e', 12)
1222 .arg(_TOW, 19, 'e', 12);
1223
1224 QString fmt = version < 3.0 ? " %1%2%3%4\n" : " %1%2%3%4\n";
1225
1226 out << QString(fmt)
1227 .arg(1.e-3*_x_pos, 19, 'e', 12)
1228 .arg(1.e-3*_x_velocity, 19, 'e', 12)
1229 .arg(1.e-3*_x_acceleration, 19, 'e', 12)
1230 .arg(_health, 19, 'e', 12);
1231
1232 out << QString(fmt)
1233 .arg(1.e-3*_y_pos, 19, 'e', 12)
1234 .arg(1.e-3*_y_velocity, 19, 'e', 12)
1235 .arg(1.e-3*_y_acceleration, 19, 'e', 12)
1236 .arg(_ura, 19, 'e', 12);
1237
1238 out << QString(fmt)
1239 .arg(1.e-3*_z_pos, 19, 'e', 12)
1240 .arg(1.e-3*_z_velocity, 19, 'e', 12)
1241 .arg(1.e-3*_z_acceleration, 19, 'e', 12)
1242 .arg(double(_IODN), 19, 'e', 12);
1243
1244 return rnxStr;
1245}
1246
1247// Constructor
1248//////////////////////////////////////////////////////////////////////////////
1249t_ephBDS::t_ephBDS(float rnxVersion, const QStringList& lines) {
1250
1251 const int nLines = 8;
1252
1253 if (lines.size() != nLines) {
1254 _checkState = bad;
1255 return;
1256 }
1257
1258 // RINEX Format
1259 // ------------
1260 int fieldLen = 19;
1261
1262 int pos[4];
1263 pos[0] = (rnxVersion <= 2.12) ? 3 : 4;
1264 pos[1] = pos[0] + fieldLen;
1265 pos[2] = pos[1] + fieldLen;
1266 pos[3] = pos[2] + fieldLen;
1267
1268 // Read eight lines
1269 // ----------------
1270 for (int iLine = 0; iLine < nLines; iLine++) {
1271 QString line = lines[iLine];
1272
1273 if ( iLine == 0 ) {
1274 QTextStream in(line.left(pos[1]).toAscii());
1275
1276 int year, month, day, hour, min;
1277 double sec;
1278
1279 QString prnStr, n;
1280 in >> prnStr;
1281 if (prnStr.size() == 1 && prnStr[0] == 'C') {
1282 in >> n;
1283 prnStr.append(n);
1284 }
1285 in >> year >> month >> day >> hour >> min >> sec;
1286 if (prnStr.at(0) == 'C') {
1287 _prn.set('C', prnStr.mid(1).toInt());
1288 }
1289 else {
1290 _prn.set('C', prnStr.toInt());
1291 }
1292
1293 if (year < 80) {
1294 year += 2000;
1295 }
1296 else if (year < 100) {
1297 year += 1900;
1298 }
1299
1300 _TOC.setBDS(year, month, day, hour, min, sec);
1301
1302 if ( readDbl(line, pos[1], fieldLen, _clock_bias ) ||
1303 readDbl(line, pos[2], fieldLen, _clock_drift ) ||
1304 readDbl(line, pos[3], fieldLen, _clock_driftrate) ) {
1305 _checkState = bad;
1306 return;
1307 }
1308 }
1309
1310 else if ( iLine == 1 ) {
1311 double aode;
1312 if ( readDbl(line, pos[0], fieldLen, aode ) ||
1313 readDbl(line, pos[1], fieldLen, _Crs ) ||
1314 readDbl(line, pos[2], fieldLen, _Delta_n) ||
1315 readDbl(line, pos[3], fieldLen, _M0 ) ) {
1316 _checkState = bad;
1317 return;
1318 }
1319 _AODE = int(aode);
1320 }
1321
1322 else if ( iLine == 2 ) {
1323 if ( readDbl(line, pos[0], fieldLen, _Cuc ) ||
1324 readDbl(line, pos[1], fieldLen, _e ) ||
1325 readDbl(line, pos[2], fieldLen, _Cus ) ||
1326 readDbl(line, pos[3], fieldLen, _sqrt_A) ) {
1327 _checkState = bad;
1328 return;
1329 }
1330 }
1331
1332 else if ( iLine == 3 ) {
1333 if ( readDbl(line, pos[0], fieldLen, _TOEsec ) ||
1334 readDbl(line, pos[1], fieldLen, _Cic ) ||
1335 readDbl(line, pos[2], fieldLen, _OMEGA0) ||
1336 readDbl(line, pos[3], fieldLen, _Cis ) ) {
1337 _checkState = bad;
1338 return;
1339 }
1340 }
1341
1342 else if ( iLine == 4 ) {
1343 if ( readDbl(line, pos[0], fieldLen, _i0 ) ||
1344 readDbl(line, pos[1], fieldLen, _Crc ) ||
1345 readDbl(line, pos[2], fieldLen, _omega ) ||
1346 readDbl(line, pos[3], fieldLen, _OMEGADOT) ) {
1347 _checkState = bad;
1348 return;
1349 }
1350 }
1351
1352 else if ( iLine == 5 ) {
1353 if ( readDbl(line, pos[0], fieldLen, _IDOT ) ||
1354 readDbl(line, pos[2], fieldLen, _TOEweek)) {
1355 _checkState = bad;
1356 return;
1357 }
1358 }
1359
1360 else if ( iLine == 6 ) {
1361 double SatH1;
1362 if ( readDbl(line, pos[0], fieldLen, _URA ) ||
1363 readDbl(line, pos[1], fieldLen, SatH1) ||
1364 readDbl(line, pos[2], fieldLen, _TGD1) ||
1365 readDbl(line, pos[3], fieldLen, _TGD2) ) {
1366 _checkState = bad;
1367 return;
1368 }
1369 _SatH1 = int(SatH1);
1370 }
1371
1372 else if ( iLine == 7 ) {
1373 double aodc;
1374 if ( readDbl(line, pos[0], fieldLen, _TOT) ||
1375 readDbl(line, pos[1], fieldLen, aodc) ) {
1376 _checkState = bad;
1377 return;
1378 }
1379 if (_TOT == 0.9999e9) { // 0.9999e9 means not known (RINEX standard)
1380 _TOT = _TOEsec;
1381 }
1382 _AODC = int(aodc);
1383 }
1384 }
1385
1386 _TOE.setBDS(int(_TOEweek), _TOEsec);
1387
1388 // remark: actually should be computed from second_tot
1389 // but it seems to be unreliable in RINEX files
1390 //_TOT = _TOC.bdssec();
1391}
1392
1393// IOD of BDS Ephemeris (virtual)
1394////////////////////////////////////////////////////////////////////////////
1395unsigned int t_ephBDS::IOD() const {
1396 unsigned char buffer[80];
1397 int size = 0;
1398 int numbits = 0;
1399 long long bitbuffer = 0;
1400 unsigned char *startbuffer = buffer;
1401
1402 BDSADDBITSFLOAT(14, this->_IDOT, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<13))
1403 BDSADDBITSFLOAT(11, this->_clock_driftrate, 1.0/static_cast<double>(1<<30)
1404 /static_cast<double>(1<<30)/static_cast<double>(1<<6))
1405 BDSADDBITSFLOAT(22, this->_clock_drift, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<20))
1406 BDSADDBITSFLOAT(24, this->_clock_bias, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<3))
1407 BDSADDBITSFLOAT(18, this->_Crs, 1.0/static_cast<double>(1<<6))
1408 BDSADDBITSFLOAT(16, this->_Delta_n, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<13))
1409 BDSADDBITSFLOAT(32, this->_M0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
1410 BDSADDBITSFLOAT(18, this->_Cuc, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<1))
1411 BDSADDBITSFLOAT(32, this->_e, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<3))
1412 BDSADDBITSFLOAT(18, this->_Cus, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<1))
1413 BDSADDBITSFLOAT(32, this->_sqrt_A, 1.0/static_cast<double>(1<<19))
1414 BDSADDBITSFLOAT(18, this->_Cic, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<1))
1415 BDSADDBITSFLOAT(32, this->_OMEGA0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
1416 BDSADDBITSFLOAT(18, this->_Cis, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<1))
1417 BDSADDBITSFLOAT(32, this->_i0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
1418 BDSADDBITSFLOAT(18, this->_Crc, 1.0/static_cast<double>(1<<6))
1419 BDSADDBITSFLOAT(32, this->_omega, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
1420 BDSADDBITSFLOAT(24, this->_OMEGADOT, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<13))
1421 BDSADDBITS(5, 0) // the last byte is filled by 0-bits to obtain a length of an integer multiple of 8
1422
1423 return CRC24(size, startbuffer);
1424}
1425
1426// Compute BDS Satellite Position (virtual)
1427//////////////////////////////////////////////////////////////////////////////
1428t_irc t_ephBDS::position(int GPSweek, double GPSweeks, double* xc, double* vv) const {
1429
1430 if (_checkState == bad) {
1431 return failure;
1432 }
1433
1434 static const double gmBDS = 398.6004418e12;
1435 static const double omegaBDS = 7292115.0000e-11;
1436
1437 xc[0] = xc[1] = xc[2] = xc[3] = 0.0;
1438 vv[0] = vv[1] = vv[2] = 0.0;
1439
1440 bncTime tt(GPSweek, GPSweeks);
1441
1442 if (_sqrt_A == 0) {
1443 return failure;
1444 }
1445 double a0 = _sqrt_A * _sqrt_A;
1446
1447 double n0 = sqrt(gmBDS/(a0*a0*a0));
1448 double tk = tt - _TOE;
1449 double n = n0 + _Delta_n;
1450 double M = _M0 + n*tk;
1451 double E = M;
1452 double E_last;
1453 int nLoop = 0;
1454 do {
1455 E_last = E;
1456 E = M + _e*sin(E);
1457
1458 if (++nLoop == 100) {
1459 return failure;
1460 }
1461 } while ( fabs(E-E_last)*a0 > 0.001 );
1462
1463 double v = atan2(sqrt(1-_e*_e) * sin(E), cos(E) - _e);
1464 double u0 = v + _omega;
1465 double sin2u0 = sin(2*u0);
1466 double cos2u0 = cos(2*u0);
1467 double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
1468 double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
1469 double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
1470 double xp = r*cos(u);
1471 double yp = r*sin(u);
1472 double toesec = (_TOE.gpssec() - 14.0);
1473 double sinom = 0;
1474 double cosom = 0;
1475 double sini = 0;
1476 double cosi = 0;
1477
1478 // Velocity
1479 // --------
1480 double tanv2 = tan(v/2);
1481 double dEdM = 1 / (1 - _e*cos(E));
1482 double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2)
1483 / (1 + tanv2*tanv2) * dEdM * n;
1484 double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
1485 double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
1486 double dotr = a0 * _e*sin(E) * dEdM * n
1487 + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
1488
1489 double dotx = dotr*cos(u) - r*sin(u)*dotu;
1490 double doty = dotr*sin(u) + r*cos(u)*dotu;
1491
1492 const double iMaxGEO = 10.0 / 180.0 * M_PI;
1493
1494 // MEO/IGSO satellite
1495 // ------------------
1496 if (_i0 > iMaxGEO) {
1497 double OM = _OMEGA0 + (_OMEGADOT - omegaBDS)*tk - omegaBDS*toesec;
1498
1499 sinom = sin(OM);
1500 cosom = cos(OM);
1501 sini = sin(i);
1502 cosi = cos(i);
1503
1504 xc[0] = xp*cosom - yp*cosi*sinom;
1505 xc[1] = xp*sinom + yp*cosi*cosom;
1506 xc[2] = yp*sini;
1507
1508 // Velocity
1509 // --------
1510
1511 double dotom = _OMEGADOT - t_CST::omega;
1512
1513 vv[0] = cosom *dotx - cosi*sinom *doty // dX / dr
1514 - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
1515 + yp*sini*sinom*doti; // dX / di
1516
1517 vv[1] = sinom *dotx + cosi*cosom *doty
1518 + xp*cosom*dotom - yp*cosi*sinom*dotom
1519 - yp*sini*cosom*doti;
1520
1521 vv[2] = sini *doty + yp*cosi *doti;
1522
1523 }
1524
1525 // GEO satellite
1526 // -------------
1527 else {
1528 double OM = _OMEGA0 + _OMEGADOT*tk - omegaBDS*toesec;
1529 double ll = omegaBDS*tk;
1530
1531 sinom = sin(OM);
1532 cosom = cos(OM);
1533 sini = sin(i);
1534 cosi = cos(i);
1535
1536 double xx = xp*cosom - yp*cosi*sinom;
1537 double yy = xp*sinom + yp*cosi*cosom;
1538 double zz = yp*sini;
1539
1540 Matrix RX = BNC_PPP::t_astro::rotX(-5.0 / 180.0 * M_PI);
1541 Matrix RZ = BNC_PPP::t_astro::rotZ(ll);
1542
1543 ColumnVector X1(3); X1 << xx << yy << zz;
1544 ColumnVector X2 = RZ*RX*X1;
1545
1546 xc[0] = X2(1);
1547 xc[1] = X2(2);
1548 xc[2] = X2(3);
1549
1550 double dotom = _OMEGADOT;
1551
1552 double vx = cosom *dotx - cosi*sinom *doty
1553 - xp*sinom*dotom - yp*cosi*cosom*dotom
1554 + yp*sini*sinom*doti;
1555
1556 double vy = sinom *dotx + cosi*cosom *doty
1557 + xp*cosom*dotom - yp*cosi*sinom*dotom
1558 - yp*sini*cosom*doti;
1559
1560 double vz = sini *doty + yp*cosi *doti;
1561
1562 ColumnVector V(3); V << vx << vy << vz;
1563
1564 Matrix RdotZ(3,3);
1565 double C = cos(ll);
1566 double S = sin(ll);
1567 Matrix UU(3,3);
1568 UU[0][0] = -S; UU[0][1] = +C; UU[0][2] = 0.0;
1569 UU[1][0] = -C; UU[1][1] = -S; UU[1][2] = 0.0;
1570 UU[2][0] = 0.0; UU[2][1] = 0.0; UU[2][2] = 0.0;
1571 RdotZ = omegaBDS * UU;
1572
1573 ColumnVector VV(3);
1574 VV = RZ*RX*V + RdotZ*RX*X1;
1575
1576 vv[0] = VV(1);
1577 vv[1] = VV(2);
1578 vv[2] = VV(3);
1579 }
1580
1581 double tc = tt - _TOC;
1582 xc[3] = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
1583
1584 // dotC = _clock_drift + _clock_driftrate*tc
1585 // - 4.442807633e-10*_e*sqrt(a0)*cos(E) * dEdM * n;
1586
1587 // Relativistic Correction
1588 // -----------------------
1589 // correspondent to BDS ICD and to SSR standard
1590 xc[3] -= 4.442807633e-10 * _e * sqrt(a0) *sin(E);
1591 // correspondent to IGS convention
1592 // xc[3] -= 2.0 * (xc[0]*vv[0] + xc[1]*vv[1] + xc[2]*vv[2]) / t_CST::c / t_CST::c;
1593
1594 return success;
1595}
1596
1597// RINEX Format String
1598//////////////////////////////////////////////////////////////////////////////
1599QString t_ephBDS::toString(double version) const {
1600
1601 QString rnxStr = rinexDateStr(_TOC-14.0, _prn, version);
1602
1603 QTextStream out(&rnxStr);
1604
1605 out << QString("%1%2%3\n")
1606 .arg(_clock_bias, 19, 'e', 12)
1607 .arg(_clock_drift, 19, 'e', 12)
1608 .arg(_clock_driftrate, 19, 'e', 12);
1609
1610 QString fmt = version < 3.0 ? " %1%2%3%4\n" : " %1%2%3%4\n";
1611
1612 out << QString(fmt)
1613 .arg(double(_AODE), 19, 'e', 12)
1614 .arg(_Crs, 19, 'e', 12)
1615 .arg(_Delta_n, 19, 'e', 12)
1616 .arg(_M0, 19, 'e', 12);
1617
1618 out << QString(fmt)
1619 .arg(_Cuc, 19, 'e', 12)
1620 .arg(_e, 19, 'e', 12)
1621 .arg(_Cus, 19, 'e', 12)
1622 .arg(_sqrt_A, 19, 'e', 12);
1623
1624 double toes = 0.0;
1625 if (_TOEweek > -1.0) {// RINEX input
1626 toes = _TOEsec;
1627 }
1628 else {// RTCM stream input
1629 toes = _TOE.bdssec();
1630 }
1631 out << QString(fmt)
1632 .arg(toes, 19, 'e', 12)
1633 .arg(_Cic, 19, 'e', 12)
1634 .arg(_OMEGA0, 19, 'e', 12)
1635 .arg(_Cis, 19, 'e', 12);
1636
1637 out << QString(fmt)
1638 .arg(_i0, 19, 'e', 12)
1639 .arg(_Crc, 19, 'e', 12)
1640 .arg(_omega, 19, 'e', 12)
1641 .arg(_OMEGADOT, 19, 'e', 12);
1642
1643 double toew = 0.0;
1644 if (_TOEweek > -1.0) {// RINEX input
1645 toew = _TOEweek;
1646 }
1647 else {// RTCM stream input
1648 toew = double(_TOE.bdsw());
1649 }
1650 out << QString(fmt)
1651 .arg(_IDOT, 19, 'e', 12)
1652 .arg(0.0, 19, 'e', 12)
1653 .arg(toew, 19, 'e', 12)
1654 .arg(0.0, 19, 'e', 12);
1655
1656 out << QString(fmt)
1657 .arg(_URA, 19, 'e', 12)
1658 .arg(double(_SatH1), 19, 'e', 12)
1659 .arg(_TGD1, 19, 'e', 12)
1660 .arg(_TGD2, 19, 'e', 12);
1661
1662 double tots = 0.0;
1663 if (_TOEweek > -1.0) {// RINEX input
1664 tots = _TOT;
1665 }
1666 else {// RTCM stream input
1667 tots = _TOE.bdssec();
1668 }
1669 out << QString(fmt)
1670 .arg(tots, 19, 'e', 12)
1671 .arg(double(_AODC), 19, 'e', 12)
1672 .arg("", 19, QChar(' '))
1673 .arg("", 19, QChar(' '));
1674 return rnxStr;
1675}
Note: See TracBrowser for help on using the repository browser.