source: ntrip/trunk/BNC/src/PPP/pppClient.cpp@ 10251

Last change on this file since 10251 was 10251, checked in by stuerze, 8 months ago

changes regarding PPP: allow single frequency PPP and allow to select the frequency bands that are used

  • Property svn:keywords set to Author Date Id Rev URL;svn:eol-style=native
  • Property svn:mime-type set to text/plain
File size: 17.2 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Client
3 * -------------------------------------------------------------------------
4 *
5 * Class: t_pppClient
6 *
7 * Purpose: PPP Client processing starts here
8 *
9 * Author: L. Mervart
10 *
11 * Created: 29-Jul-2014
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <QThreadStorage>
18
19#include <iostream>
20#include <iomanip>
21#include <cmath>
22#include <stdlib.h>
23#include <string.h>
24#include <stdexcept>
25
26#include "pppClient.h"
27#include "pppEphPool.h"
28#include "pppObsPool.h"
29#include "bncconst.h"
30#include "bncutils.h"
31#include "pppStation.h"
32#include "bncantex.h"
33#include "pppFilter.h"
34
35using namespace BNC_PPP;
36using namespace std;
37
38// Global variable holding thread-specific pointers
39//////////////////////////////////////////////////////////////////////////////
40QThreadStorage<t_pppClient*> CLIENTS;
41
42// Static function returning thread-specific pointer
43//////////////////////////////////////////////////////////////////////////////
44t_pppClient* t_pppClient::instance() {
45 return CLIENTS.localData();
46}
47
48// Constructor
49//////////////////////////////////////////////////////////////////////////////
50t_pppClient::t_pppClient(const t_pppOptions* opt) {
51 _output = 0;
52 _opt = new t_pppOptions(*opt);
53 _log = new ostringstream();
54 _ephPool = new t_pppEphPool();
55 _obsPool = new t_pppObsPool();
56 _staRover = new t_pppStation();
57 _filter = new t_pppFilter();
58 _tides = new t_tides();
59 _antex = 0;
60 if (!_opt->_antexFileName.empty()) {
61 _antex = new bncAntex(_opt->_antexFileName.c_str());
62 }
63 if (!_opt->_blqFileName.empty()) {
64 if (_tides->readBlqFile(_opt->_blqFileName.c_str()) == success) {
65 //_tides->printAllBlqSets();
66 }
67 }
68 _offGlo = 0.0;
69 _offGal = 0.0;
70 _offBds = 0.0;
71
72 CLIENTS.setLocalData(this); // CLIENTS takes ownership over "this"
73}
74
75// Destructor
76//////////////////////////////////////////////////////////////////////////////
77t_pppClient::~t_pppClient() {
78 delete _log;
79 delete _opt;
80 delete _ephPool;
81 delete _obsPool;
82 delete _staRover;
83 if (_antex) {
84 delete _antex;
85 }
86 delete _filter;
87 delete _tides;
88 clearObs();
89}
90
91//
92//////////////////////////////////////////////////////////////////////////////
93void t_pppClient::putEphemeris(const t_eph* eph) {
94 const t_ephGPS* ephGPS = dynamic_cast<const t_ephGPS*>(eph);
95 const t_ephGlo* ephGlo = dynamic_cast<const t_ephGlo*>(eph);
96 const t_ephGal* ephGal = dynamic_cast<const t_ephGal*>(eph);
97 const t_ephBDS* ephBDS = dynamic_cast<const t_ephBDS*>(eph);
98 if (ephGPS) {
99 _ephPool->putEphemeris(new t_ephGPS(*ephGPS));
100 }
101 else if (ephGlo) {
102 _ephPool->putEphemeris(new t_ephGlo(*ephGlo));
103 }
104 else if (ephGal) {
105 _ephPool->putEphemeris(new t_ephGal(*ephGal));
106 }
107 else if (ephBDS) {
108 _ephPool->putEphemeris(new t_ephBDS(*ephBDS));
109 }
110}
111
112//
113//////////////////////////////////////////////////////////////////////////////
114void t_pppClient::putTec(const t_vTec* vTec) {
115 _obsPool->putTec(new t_vTec(*vTec));
116}
117
118//
119//////////////////////////////////////////////////////////////////////////////
120void t_pppClient::putOrbCorrections(const vector<t_orbCorr*>& corr) {
121 for (unsigned ii = 0; ii < corr.size(); ii++) {
122 _ephPool->putOrbCorrection(new t_orbCorr(*corr[ii]));
123 }
124}
125
126//
127//////////////////////////////////////////////////////////////////////////////
128void t_pppClient::putClkCorrections(const vector<t_clkCorr*>& corr) {
129 for (unsigned ii = 0; ii < corr.size(); ii++) {
130 _ephPool->putClkCorrection(new t_clkCorr(*corr[ii]));
131 }
132}
133
134//
135//////////////////////////////////////////////////////////////////////////////
136void t_pppClient::putCodeBiases(const vector<t_satCodeBias*>& biases) {
137 for (unsigned ii = 0; ii < biases.size(); ii++) {
138 _obsPool->putCodeBias(new t_satCodeBias(*biases[ii]));
139 }
140}
141
142//
143//////////////////////////////////////////////////////////////////////////////
144void t_pppClient::putPhaseBiases(const vector<t_satPhaseBias*>& biases) {
145 for (unsigned ii = 0; ii < biases.size(); ii++) {
146 _obsPool->putPhaseBias(new t_satPhaseBias(*biases[ii]));
147 }
148}
149
150//
151//////////////////////////////////////////////////////////////////////////////
152t_irc t_pppClient::prepareObs(const vector<t_satObs*>& satObs,
153 vector<t_pppSatObs*>& obsVector, bncTime& epoTime) {
154
155 // Default
156 // -------
157 epoTime.reset();
158 clearObs();
159
160 // Create vector of valid observations
161 // -----------------------------------
162 for (unsigned ii = 0; ii < satObs.size(); ii++) {
163 char sys = satObs[ii]->_prn.system();
164 if (_opt->useSystem(sys)) {
165 t_pppSatObs* pppSatObs = new t_pppSatObs(*satObs[ii]);
166 if (pppSatObs->isValid()) {
167 obsVector.push_back(pppSatObs);
168 }
169 else {
170 delete pppSatObs;
171 }
172 }
173 }
174
175 // Check whether data are synchronized, compute epoTime
176 // ----------------------------------------------------
177 const double MAXSYNC = 0.05; // synchronization limit
178 double meanDt = 0.0;
179 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
180 const t_pppSatObs* satObs = obsVector.at(ii);
181 if (epoTime.undef()) {
182 epoTime = satObs->time();
183 }
184 else {
185 double dt = satObs->time() - epoTime;
186 if (fabs(dt) > MAXSYNC) {
187 LOG << "t_pppClient::prepareObs asynchronous observations" << endl;
188 return failure;
189 }
190 meanDt += dt;
191 }
192 }
193
194 if (obsVector.size() > 0) {
195 epoTime += meanDt / obsVector.size();
196 }
197
198 return success;
199}
200
201//
202//////////////////////////////////////////////////////////////////////////////
203bool t_pppClient::preparePseudoObs(std::vector<t_pppSatObs*>& obsVector) {
204
205 bool pseudoObsIono = false;
206
207 if (_opt->_pseudoObsIono) {
208 vector<t_pppSatObs*>::iterator it = obsVector.begin();
209 while (it != obsVector.end()) {
210 t_pppSatObs* satObs = *it;
211 pseudoObsIono = satObs->setPseudoObsIono(t_frequency::G1);
212 it++;
213 }
214 }
215 /*
216 vector<t_pppSatObs*>::iterator it = obsVector.begin();
217 while (it != obsVector.end()) {
218 t_pppSatObs* satObs = *it;
219 satObs->printObsMinusComputed();
220 it++;
221 }
222*/
223 return pseudoObsIono;
224}
225
226//
227//////////////////////////////////////////////////////////////////////////////
228void t_pppClient::useObsWithCodeBiasesOnly(std::vector<t_pppSatObs*>& obsVector) {
229
230 vector<t_pppSatObs*>::iterator it = obsVector.begin();
231 while (it != obsVector.end()) {
232 t_pppSatObs* pppSatObs = *it;
233 bool codeBiasesAvailable = false;
234 if (pppSatObs->getCodeBias(pppSatObs->fType1()) &&
235 pppSatObs->getCodeBias(pppSatObs->fType2())) {
236 codeBiasesAvailable = true;
237 }
238 if (codeBiasesAvailable) {
239 ++it;
240 }
241 else {
242 it = obsVector.erase(it);
243 delete pppSatObs;
244 }
245 }
246 return;
247}
248
249
250// Compute the Bancroft position, check for blunders
251//////////////////////////////////////////////////////////////////////////////
252t_irc t_pppClient::cmpBancroft(const bncTime& epoTime,
253 vector<t_pppSatObs*>& obsVector,
254 ColumnVector& xyzc, bool print) {
255
256 t_lc::type tLC = t_lc::dummy;
257
258 while (true) {
259 Matrix BB(obsVector.size(), 4);
260 int iObs = -1;
261 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
262 const t_pppSatObs* satObs = obsVector.at(ii);
263 if (tLC == t_lc::dummy) {
264 if (satObs->isValid(t_lc::cIF)) {
265 tLC = t_lc::cIF;
266 }
267 else if (satObs->isValid(t_lc::c1)) {
268 tLC = t_lc::c1;
269 }
270 else if (satObs->isValid(t_lc::c2)) {
271 tLC = t_lc::c2;
272 }
273 }
274 if ( satObs->isValid(tLC) &&
275 (!satObs->modelSet() || satObs->eleSat() >= _opt->_minEle) ) {
276 ++iObs;
277 BB[iObs][0] = satObs->xc()[0];
278 BB[iObs][1] = satObs->xc()[1];
279 BB[iObs][2] = satObs->xc()[2];
280 BB[iObs][3] = satObs->obsValue(tLC) - satObs->cmpValueForBanc(tLC);
281 }
282 }
283 if (iObs + 1 < _opt->_minObs) {
284 LOG << "t_pppClient::cmpBancroft not enough observations: " << iObs + 1 << endl;
285 return failure;
286 }
287 BB = BB.Rows(1,iObs+1);
288 if (bancroft(BB, xyzc) != success) {
289 return failure;
290 }
291
292 xyzc[3] /= t_CST::c;
293
294 // Check Blunders
295 // --------------
296 const double BLUNDER = 500.0;
297 double maxRes = 0.0;
298 unsigned maxResIndex = 0;
299 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
300 const t_pppSatObs* satObs = obsVector.at(ii);
301 if (satObs->isValid() &&
302 (!satObs->modelSet() || satObs->eleSat() >= _opt->_minEle) ) {
303 ColumnVector rr = satObs->xc().Rows(1,3) - xyzc.Rows(1,3);
304 double res = rr.NormFrobenius() - satObs->obsValue(tLC)
305 - (satObs->xc()[3] - xyzc[3]) * t_CST::c;
306 if (fabs(res) > maxRes) {
307 maxRes = fabs(res);
308 maxResIndex = ii;
309 }
310 }
311 }
312 if (maxRes < BLUNDER) {
313 if (print) {
314 LOG.setf(ios::fixed);
315 LOG << "\nPPP of Epoch ";
316 if (!_epoTimeRover.undef()) LOG << string(_epoTimeRover);
317 LOG << "\n---------------------------------------------------------------\n";
318 LOG << string(epoTime) << " BANCROFT:" << ' '
319 << setw(14) << setprecision(3) << xyzc[0] << ' '
320 << setw(14) << setprecision(3) << xyzc[1] << ' '
321 << setw(14) << setprecision(3) << xyzc[2] << ' '
322 << setw(14) << setprecision(3) << xyzc[3] * t_CST::c << endl << endl;
323 }
324 break;
325 }
326 else {
327 t_pppSatObs* satObs = obsVector.at(maxResIndex);
328 LOG << "t_pppClient::cmpBancroft Outlier " << satObs->prn().toString()
329 << " " << maxRes << endl;
330 delete satObs;
331 obsVector.erase(obsVector.begin() + maxResIndex);
332 }
333 }
334
335 return success;
336}
337
338//
339//////////////////////////////////////////////////////////////////////////////
340void t_pppClient::initOutput(t_output* output) {
341 _output = output;
342 _output->_numSat = 0;
343 _output->_hDop = 0.0;
344 _output->_error = false;
345}
346
347//
348//////////////////////////////////////////////////////////////////////////////
349void t_pppClient::clearObs() {
350 for (unsigned ii = 0; ii < _obsRover.size(); ii++) {
351 delete _obsRover.at(ii);
352 }
353 _obsRover.clear();
354}
355
356//
357//////////////////////////////////////////////////////////////////////////////
358void t_pppClient::finish(t_irc irc, int ind) {
359#ifdef BNC_DEBUG_PPP
360 LOG << "t_pppClient::finish(" << ind << "): " << irc << endl;
361#endif
362
363 clearObs();
364
365 _output->_epoTime = _epoTimeRover;
366
367 if (irc == success) {
368 _output->_xyzRover[0] = _staRover->xyzApr()[0] + _filter->x()[0];
369 _output->_xyzRover[1] = _staRover->xyzApr()[1] + _filter->x()[1];
370 _output->_xyzRover[2] = _staRover->xyzApr()[2] + _filter->x()[2];
371
372 xyz2neu(_staRover->ellApr().data(), _filter->x().data(), _output->_neu);
373
374 copy(&_filter->Q().data()[0], &_filter->Q().data()[6], _output->_covMatrix);
375
376 _output->_trp0 = t_tropo::delay_saast(_staRover->xyzApr(), M_PI/2.0);
377 _output->_trp = _filter->trp();
378 _output->_trpStdev = _filter->trpStdev();
379
380 _output->_numSat = _filter->numSat();
381 _output->_hDop = _filter->HDOP();
382 _output->_error = false;
383 }
384 else {
385 _output->_error = true;
386 }
387
388 _output->_log = _log->str();
389 delete _log; _log = new ostringstream();
390
391 return;
392}
393
394//
395//////////////////////////////////////////////////////////////////////////////
396t_irc t_pppClient::cmpModel(t_pppStation* station, const ColumnVector& xyzc,
397 vector<t_pppSatObs*>& obsVector) {
398 bncTime time;
399 time = _epoTimeRover;
400 station->setName(_opt->_roverName);
401 station->setAntName(_opt->_antNameRover);
402 station->setEpochTime(time);
403
404 if (_opt->xyzAprRoverSet()) {
405 station->setXyzApr(_opt->_xyzAprRover);
406 }
407 else {
408 station->setXyzApr(xyzc.Rows(1,3));
409 }
410 station->setNeuEcc(_opt->_neuEccRover);
411
412 // Receiver Clock
413 // --------------
414 station->setDClk(xyzc[3]);
415
416 // Tides
417 // -----
418 station->setTideDsplEarth(_tides->earth(time, station->xyzApr()));
419 station->setTideDsplOcean(_tides->ocean(time, station->xyzApr(), station->name()));
420
421 // Observation model
422 // -----------------
423 vector<t_pppSatObs*>::iterator it = obsVector.begin();
424 while (it != obsVector.end()) {
425 t_pppSatObs* satObs = *it;
426 t_irc modelSetup;
427 if (satObs->isValid()) {
428 modelSetup = satObs->cmpModel(station);
429 }
430 if (satObs->isValid() &&
431 satObs->eleSat() >= _opt->_minEle &&
432 modelSetup == success) {
433 ++it;
434 }
435 else {
436 delete satObs;
437 it = obsVector.erase(it);
438 }
439 }
440
441 return success;
442}
443
444//
445//////////////////////////////////////////////////////////////////////////////
446void t_pppClient::processEpoch(const vector<t_satObs*>& satObs, t_output* output) {
447
448 try {
449 initOutput(output);
450
451 // Prepare Observations of the Rover
452 // ---------------------------------
453 if (prepareObs(satObs, _obsRover, _epoTimeRover) != success) {
454 return finish(failure, 1);
455 }
456
457 for (int iter = 1; iter <= 2; iter++) {
458 ColumnVector xyzc(4); xyzc = 0.0;
459 bool print = (iter == 2);
460 if (cmpBancroft(_epoTimeRover, _obsRover, xyzc, print) != success) {
461 return finish(failure, 2);
462 }
463 if (cmpModel(_staRover, xyzc, _obsRover) != success) {
464 return finish(failure, 3);
465 }
466 }
467 // use observations only if satellite code biases are available
468 /* ------------------------------------------------------------
469 if (!_opt->_corrMount.empty() {
470 useObsWithCodeBiasesOnly(_obsRover);
471 }*/
472
473 if (int(_obsRover.size()) < _opt->_minObs) {
474 LOG << "t_pppClient::processEpoch not enough observations" << endl;
475 return finish(failure, 4);
476 }
477
478 // Prepare Pseudo Observations of the Rover
479 // ----------------------------------------
480 _pseudoObsIono = preparePseudoObs(_obsRover);
481
482 // Store last epoch of data
483 // ------------------------
484 _obsPool->putEpoch(_epoTimeRover, _obsRover, _pseudoObsIono);
485
486 // Process Epoch in Filter
487 // -----------------------
488 if (_filter->processEpoch(_obsPool) != success) {
489 LOG << "t_pppFilter::processEpoch() != success" << endl;
490 return finish(failure, 5);
491 }
492 }
493 catch (Exception& exc) {
494 LOG << exc.what() << endl;
495 return finish(failure, 6);
496 }
497 catch (t_except msg) {
498 LOG << msg.what() << endl;
499 return finish(failure, 7);
500 }
501 catch (const char* msg) {
502 LOG << msg << endl;
503 return finish(failure, 8);
504 }
505 catch (...) {
506 LOG << "unknown exception" << endl;
507 return finish(failure, 9);
508 }
509 return finish(success, 0);
510}
511
512//
513////////////////////////////////////////////////////////////////////////////
514double lorentz(const ColumnVector& aa, const ColumnVector& bb) {
515 return aa[0]*bb[0] + aa[1]*bb[1] + aa[2]*bb[2] - aa[3]*bb[3];
516}
517
518//
519////////////////////////////////////////////////////////////////////////////
520t_irc t_pppClient::bancroft(const Matrix& BBpass, ColumnVector& pos) {
521
522 if (pos.Nrows() != 4) {
523 pos.ReSize(4);
524 }
525 pos = 0.0;
526
527 for (int iter = 1; iter <= 2; iter++) {
528 Matrix BB = BBpass;
529 int mm = BB.Nrows();
530 for (int ii = 1; ii <= mm; ii++) {
531 double xx = BB(ii,1);
532 double yy = BB(ii,2);
533 double traveltime = 0.072;
534 if (iter > 1) {
535 double zz = BB(ii,3);
536 double rho = sqrt( (xx-pos(1)) * (xx-pos(1)) +
537 (yy-pos(2)) * (yy-pos(2)) +
538 (zz-pos(3)) * (zz-pos(3)) );
539 traveltime = rho / t_CST::c;
540 }
541 double angle = traveltime * t_CST::omega;
542 double cosa = cos(angle);
543 double sina = sin(angle);
544 BB(ii,1) = cosa * xx + sina * yy;
545 BB(ii,2) = -sina * xx + cosa * yy;
546 }
547
548 Matrix BBB;
549 if (mm > 4) {
550 SymmetricMatrix hlp; hlp << BB.t() * BB;
551 BBB = hlp.i() * BB.t();
552 }
553 else {
554 BBB = BB.i();
555 }
556 ColumnVector ee(mm); ee = 1.0;
557 ColumnVector alpha(mm); alpha = 0.0;
558 for (int ii = 1; ii <= mm; ii++) {
559 alpha(ii) = lorentz(BB.Row(ii).t(),BB.Row(ii).t())/2.0;
560 }
561 ColumnVector BBBe = BBB * ee;
562 ColumnVector BBBalpha = BBB * alpha;
563 double aa = lorentz(BBBe, BBBe);
564 double bb = lorentz(BBBe, BBBalpha)-1;
565 double cc = lorentz(BBBalpha, BBBalpha);
566 double root = sqrt(bb*bb-aa*cc);
567
568 Matrix hlpPos(4,2);
569 hlpPos.Column(1) = (-bb-root)/aa * BBBe + BBBalpha;
570 hlpPos.Column(2) = (-bb+root)/aa * BBBe + BBBalpha;
571
572 ColumnVector omc(2);
573 for (int pp = 1; pp <= 2; pp++) {
574 hlpPos(4,pp) = -hlpPos(4,pp);
575 omc(pp) = BB(1,4) -
576 sqrt( (BB(1,1)-hlpPos(1,pp)) * (BB(1,1)-hlpPos(1,pp)) +
577 (BB(1,2)-hlpPos(2,pp)) * (BB(1,2)-hlpPos(2,pp)) +
578 (BB(1,3)-hlpPos(3,pp)) * (BB(1,3)-hlpPos(3,pp)) ) -
579 hlpPos(4,pp);
580 }
581 if ( fabs(omc(1)) > fabs(omc(2)) ) {
582 pos = hlpPos.Column(2);
583 }
584 else {
585 pos = hlpPos.Column(1);
586 }
587 }
588 if (pos.size() != 4 ||
589 std::isnan(pos(1)) ||
590 std::isnan(pos(2)) ||
591 std::isnan(pos(3)) ||
592 std::isnan(pos(4))) {
593 return failure;
594 }
595
596 return success;
597}
598
599//
600//////////////////////////////////////////////////////////////////////////////
601void t_pppClient::reset() {cout << "t_pppClient::reset()" << endl;
602
603 // to delete old orbit and clock corrections
604 delete _ephPool;
605 _ephPool = new t_pppEphPool();
606
607 // to delete old code biases
608 delete _obsPool;
609 _obsPool = new t_pppObsPool();
610
611 // to delete all parameters
612 delete _filter;
613 _filter = new t_pppFilter();
614
615}
Note: See TracBrowser for help on using the repository browser.