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

Last change on this file since 6739 was 6653, checked in by stuerze, 9 years ago

sinex tro file support added,
troposphere results in overall ppp logfile added

File size: 15.0 KB
RevLine 
[5740]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
[5758]17#include <QThreadStorage>
[5740]18
[5683]19#include <iostream>
20#include <iomanip>
21#include <stdlib.h>
22#include <string.h>
23#include <stdexcept>
24
[5734]25#include "pppClient.h"
[5810]26#include "pppEphPool.h"
27#include "pppObsPool.h"
[5740]28#include "bncconst.h"
29#include "bncutils.h"
[5810]30#include "pppStation.h"
[5740]31#include "bncantex.h"
[5810]32#include "pppFilter.h"
[5683]33
[5814]34using namespace BNC_PPP;
[5683]35using namespace std;
36
[5758]37// Global variable holding thread-specific pointers
[5683]38//////////////////////////////////////////////////////////////////////////////
[5758]39QThreadStorage<t_pppClient*> CLIENTS;
[5683]40
[5758]41// Static function returning thread-specific pointer
42//////////////////////////////////////////////////////////////////////////////
43t_pppClient* t_pppClient::instance() {
44 return CLIENTS.localData();
45}
46
[5683]47// Constructor
48//////////////////////////////////////////////////////////////////////////////
[5810]49t_pppClient::t_pppClient(const t_pppOptions* opt) {
[5683]50 _output = 0;
[5810]51 _opt = new t_pppOptions(*opt);
[5683]52 _log = new ostringstream();
[5810]53 _ephPool = new t_pppEphPool();
[5826]54 _obsPool = new t_pppObsPool();
[5810]55 _staRover = new t_pppStation();
56 _filter = new t_pppFilter();
[5802]57 _tides = new t_tides();
[5761]58
[5914]59 if (!_opt->_antexFileName.empty()) {
60 _antex = new bncAntex(_opt->_antexFileName.c_str());
[5761]61 }
62 else {
63 _antex = 0;
64 }
65
[5759]66 CLIENTS.setLocalData(this); // CLIENTS takes ownership over "this"
[5683]67}
68
69// Destructor
70//////////////////////////////////////////////////////////////////////////////
[5734]71t_pppClient::~t_pppClient() {
[5683]72 delete _log;
73 delete _opt;
74 delete _ephPool;
75 delete _obsPool;
76 delete _staRover;
77 delete _antex;
78 delete _filter;
[5802]79 delete _tides;
[5683]80 clearObs();
81}
82
83//
84//////////////////////////////////////////////////////////////////////////////
[5772]85void t_pppClient::putEphemeris(const t_eph* eph) {
86 const t_ephGPS* ephGPS = dynamic_cast<const t_ephGPS*>(eph);
87 const t_ephGlo* ephGlo = dynamic_cast<const t_ephGlo*>(eph);
88 const t_ephGal* ephGal = dynamic_cast<const t_ephGal*>(eph);
89 if (ephGPS) {
90 _ephPool->putEphemeris(new t_ephGPS(*ephGPS));
91 }
92 else if (ephGlo) {
93 _ephPool->putEphemeris(new t_ephGlo(*ephGlo));
94 }
95 else if (ephGal) {
96 _ephPool->putEphemeris(new t_ephGal(*ephGal));
97 }
[5683]98}
99
100//
101//////////////////////////////////////////////////////////////////////////////
[5779]102void t_pppClient::putOrbCorrections(const vector<t_orbCorr*>& corr) {
[5772]103 for (unsigned ii = 0; ii < corr.size(); ii++) {
[5779]104 _ephPool->putOrbCorrection(new t_orbCorr(*corr[ii]));
[5683]105 }
106}
107
108//
109//////////////////////////////////////////////////////////////////////////////
[5779]110void t_pppClient::putClkCorrections(const vector<t_clkCorr*>& corr) {
[5772]111 for (unsigned ii = 0; ii < corr.size(); ii++) {
[5779]112 _ephPool->putClkCorrection(new t_clkCorr(*corr[ii]));
[5683]113 }
114}
115
116//
117//////////////////////////////////////////////////////////////////////////////
[6463]118void t_pppClient::putCodeBiases(const vector<t_satCodeBias*>& biases) {
[5772]119 for (unsigned ii = 0; ii < biases.size(); ii++) {
[6463]120 _obsPool->putCodeBias(new t_satCodeBias(*biases[ii]));
[5683]121 }
122}
123
124//
125//////////////////////////////////////////////////////////////////////////////
[5823]126t_irc t_pppClient::prepareObs(const vector<t_satObs*>& satObs,
[5819]127 vector<t_pppSatObs*>& obsVector, bncTime& epoTime) {
[5683]128 // Default
129 // -------
130 epoTime.reset();
131
132 // Create vector of valid observations
133 // -----------------------------------
[5823]134 for (unsigned ii = 0; ii < satObs.size(); ii++) {
135 char system = satObs[ii]->_prn.system();
[6035]136 if (OPT->useSystem(system)) {
[5823]137 t_pppSatObs* pppSatObs = new t_pppSatObs(*satObs[ii]);
138 if (pppSatObs->isValid()) {
139 obsVector.push_back(pppSatObs);
[5683]140 }
141 else {
[5823]142 delete pppSatObs;
[5683]143 }
144 }
145 }
146
147 // Check whether data are synchronized, compute epoTime
148 // ----------------------------------------------------
149 const double MAXSYNC = 0.05; // synchronization limit
150 double meanDt = 0.0;
151 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
[5819]152 const t_pppSatObs* satObs = obsVector.at(ii);
[5683]153 if (epoTime.undef()) {
154 epoTime = satObs->time();
155 }
156 else {
157 double dt = satObs->time() - epoTime;
158 if (fabs(dt) > MAXSYNC) {
[5734]159 LOG << "t_pppClient::prepareObs asynchronous observations" << endl;
[5740]160 return failure;
[5683]161 }
162 meanDt += dt;
163 }
164 }
165
[5787]166 if (obsVector.size() > 0) {
167 epoTime += meanDt / obsVector.size();
168 }
169
[5740]170 return success;
[5683]171}
172
173// Compute the Bancroft position, check for blunders
174//////////////////////////////////////////////////////////////////////////////
[5740]175t_irc t_pppClient::cmpBancroft(const bncTime& epoTime,
[5819]176 vector<t_pppSatObs*>& obsVector,
[5683]177 ColumnVector& xyzc, bool print) {
178
[6020]179 t_lc::type tLC = t_lc::dummy;
[5683]180
181 while (true) {
182 Matrix BB(obsVector.size(), 4);
183 int iObs = -1;
184 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
[5819]185 const t_pppSatObs* satObs = obsVector.at(ii);
[6020]186 if (satObs->prn().system() == 'G') {
187 if (tLC == t_lc::dummy) {
188 tLC = satObs->isValid(t_lc::cIF) ? t_lc::cIF : t_lc::c1;
189 }
190 if ( satObs->isValid(tLC) && (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle) ) {
191 ++iObs;
192 BB[iObs][0] = satObs->xc()[0];
193 BB[iObs][1] = satObs->xc()[1];
194 BB[iObs][2] = satObs->xc()[2];
195 BB[iObs][3] = satObs->obsValue(tLC) - satObs->cmpValueForBanc(tLC);
196 }
[5683]197 }
198 }
[5740]199 if (iObs + 1 < OPT->_minObs) {
[5734]200 LOG << "t_pppClient::cmpBancroft not enough observations" << endl;
[5740]201 return failure;
[5683]202 }
203 BB = BB.Rows(1,iObs+1);
204 bancroft(BB, xyzc);
205
[5740]206 xyzc[3] /= t_CST::c;
[5683]207
208 // Check Blunders
209 // --------------
210 const double BLUNDER = 100.0;
211 double maxRes = 0.0;
212 unsigned maxResIndex = 0;
213 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
[5819]214 const t_pppSatObs* satObs = obsVector.at(ii);
[5683]215 if ( satObs->isValid() && satObs->prn().system() == 'G' &&
[5740]216 (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle) ) {
[5683]217 ColumnVector rr = satObs->xc().Rows(1,3) - xyzc.Rows(1,3);
218 double res = rr.norm_Frobenius() - satObs->obsValue(tLC)
[5740]219 - (satObs->xc()[3] - xyzc[3]) * t_CST::c;
[5683]220 if (fabs(res) > maxRes) {
221 maxRes = fabs(res);
222 maxResIndex = ii;
223 }
224 }
225 }
226 if (maxRes < BLUNDER) {
227 if (print) {
228 LOG.setf(ios::fixed);
229 LOG << string(epoTime) << " BANCROFT:" << ' '
230 << setw(14) << setprecision(3) << xyzc[0] << ' '
231 << setw(14) << setprecision(3) << xyzc[1] << ' '
232 << setw(14) << setprecision(3) << xyzc[2] << ' '
[5740]233 << setw(14) << setprecision(3) << xyzc[3] * t_CST::c << endl << endl;
[5683]234 }
235 break;
236 }
237 else {
[5819]238 t_pppSatObs* satObs = obsVector.at(maxResIndex);
[5734]239 LOG << "t_pppClient::cmpBancroft outlier " << satObs->prn().toString()
[5683]240 << " " << maxRes << endl;
241 delete satObs;
242 obsVector.erase(obsVector.begin() + maxResIndex);
243 }
244 }
245
[5740]246 return success;
[5683]247}
248
249// Compute A Priori GPS-Glonass Offset
250//////////////////////////////////////////////////////////////////////////////
[5819]251double t_pppClient::cmpOffGG(vector<t_pppSatObs*>& obsVector) {
[5683]252
[6020]253 t_lc::type tLC = t_lc::dummy;
[5683]254 double offGG = 0.0;
255
[6035]256 if (OPT->useSystem('R')) {
[5857]257
258 while (obsVector.size() > 0) {
[5683]259 offGG = 0.0;
[5857]260 double maxRes = 0.0;
261 int maxResIndex = -1;
[5858]262 t_prn maxResPrn;
[5857]263 unsigned nObs = 0;
[5683]264 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
[5819]265 t_pppSatObs* satObs = obsVector.at(ii);
[6020]266 if (satObs->prn().system() == 'R') {
267 if (tLC == t_lc::dummy) {
268 tLC = satObs->isValid(t_lc::cIF) ? t_lc::cIF : t_lc::c1;
[5683]269 }
[6020]270 if (satObs->isValid(tLC) && (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle)) {
271 double ll = satObs->obsValue(tLC) - satObs->cmpValue(tLC);
272 ++nObs;
273 offGG += ll;
274 if (fabs(ll) > fabs(maxRes)) {
275 maxRes = ll;
276 maxResIndex = ii;
277 maxResPrn = satObs->prn();
278 }
279 }
[5683]280 }
281 }
[5857]282
[5683]283 if (nObs > 0) {
284 offGG = offGG / nObs;
285 }
286 else {
287 offGG = 0.0;
288 }
[5857]289
[5858]290 if (fabs(maxRes) > 1000.0) {
291 LOG << "t_pppClient::cmpOffGG outlier " << maxResPrn.toString() << " " << maxRes << endl;
[5857]292 obsVector.erase(obsVector.begin() + maxResIndex);
293 }
294 else {
[5683]295 break;
296 }
297 }
298 }
299
300 return offGG;
301}
302
303//
304//////////////////////////////////////////////////////////////////////////////
[5744]305void t_pppClient::initOutput(t_output* output) {
[5683]306 _output = output;
[5740]307 _output->_numSat = 0;
308 _output->_pDop = 0.0;
309 _output->_error = false;
[5683]310}
311
312//
313//////////////////////////////////////////////////////////////////////////////
[5734]314void t_pppClient::clearObs() {
[5683]315 for (unsigned ii = 0; ii < _obsRover.size(); ii++) {
316 delete _obsRover.at(ii);
317 }
318 _obsRover.clear();
319}
320
321//
322//////////////////////////////////////////////////////////////////////////////
[5740]323void t_pppClient::finish(t_irc irc) {
[5683]324
325 clearObs();
326
[5740]327 _output->_epoTime = _epoTimeRover;
[5683]328
[5740]329 if (irc == success) {
[5683]330 _output->_xyzRover[0] = _staRover->xyzApr()[0] + _filter->x()[0];
331 _output->_xyzRover[1] = _staRover->xyzApr()[1] + _filter->x()[1];
332 _output->_xyzRover[2] = _staRover->xyzApr()[2] + _filter->x()[2];
[5877]333
334 xyz2neu(_staRover->ellApr().data(), _filter->x().data(), _output->_neu);
335
[5683]336 copy(&_filter->Q().data()[0], &_filter->Q().data()[6], _output->_covMatrix);
[5877]337
[6653]338 _output->_trp0 = t_tropo::delay_saast(_staRover->xyzApr(), M_PI/2.0);
339 _output->_trp = _filter->trp();
340 _output->_trpStdev = _filter->trpStdev();
341
[5683]342 _output->_numSat = _filter->numSat();
343 _output->_pDop = _filter->PDOP();
344 _output->_error = false;
345 }
346 else {
347 _output->_error = true;
348 }
[5740]349 _output->_log = _log->str();
[5683]350 delete _log; _log = new ostringstream();
351}
352
353//
354//////////////////////////////////////////////////////////////////////////////
[5810]355t_irc t_pppClient::cmpModel(t_pppStation* station, const ColumnVector& xyzc,
[5819]356 vector<t_pppSatObs*>& obsVector) {
[5683]357
[5740]358 bncTime time;
359 time = _epoTimeRover;
360 station->setName(OPT->_roverName);
361 station->setAntName(OPT->_antNameRover);
362 if (OPT->xyzAprRoverSet()) {
363 station->setXyzApr(OPT->_xyzAprRover);
[5683]364 }
365 else {
[5740]366 station->setXyzApr(xyzc.Rows(1,3));
[5683]367 }
[5740]368 station->setNeuEcc(OPT->_neuEccRover);
[5683]369
370 // Receiver Clock
371 // --------------
372 station->setDClk(xyzc[3]);
373
374 // Tides
375 // -----
[5802]376 station->setTideDspl( _tides->displacement(time, station->xyzApr()) );
[5683]377
378 // Observation model
379 // -----------------
[5819]380 vector<t_pppSatObs*>::iterator it = obsVector.begin();
[5683]381 while (it != obsVector.end()) {
[5819]382 t_pppSatObs* satObs = *it;
[5831]383 if (satObs->isValid()) {
384 satObs->cmpModel(station);
385 }
[5740]386 if (satObs->isValid() && satObs->eleSat() >= OPT->_minEle) {
[5683]387 ++it;
388 }
389 else {
390 delete satObs;
391 it = obsVector.erase(it);
392 }
393 }
394
[5740]395 return success;
[5683]396}
397
398//
399//////////////////////////////////////////////////////////////////////////////
[5823]400void t_pppClient::processEpoch(const vector<t_satObs*>& satObs, t_output* output) {
[5683]401
402 try {
403 initOutput(output);
404
405 // Prepare Observations of the Rover
406 // ---------------------------------
[5823]407 if (prepareObs(satObs, _obsRover, _epoTimeRover) != success) {
[5740]408 return finish(failure);
[5683]409 }
410
411 LOG << "\nResults of Epoch ";
412 if (!_epoTimeRover.undef()) LOG << string(_epoTimeRover);
413 LOG << "\n--------------------------------------\n";
414
415 for (int iter = 1; iter <= 2; iter++) {
416 ColumnVector xyzc(4); xyzc = 0.0;
417 bool print = (iter == 2);
[5740]418 if (cmpBancroft(_epoTimeRover, _obsRover, xyzc, print) != success) {
419 return finish(failure);
[5683]420 }
[5740]421 if (cmpModel(_staRover, xyzc, _obsRover) != success) {
422 return finish(failure);
[5683]423 }
424 }
425
426 _offGG = cmpOffGG(_obsRover);
427
428 // Store last epoch of data
429 // ------------------------
430 _obsPool->putEpoch(_epoTimeRover, _obsRover);
431
432 // Process Epoch in Filter
433 // -----------------------
[5740]434 if (_filter->processEpoch(_obsPool) != success) {
435 return finish(failure);
[5683]436 }
437 }
438 catch (Exception& exc) {
439 LOG << exc.what() << endl;
[5740]440 return finish(failure);
[5683]441 }
[5825]442 catch (t_except msg) {
[5763]443 LOG << msg.what() << endl;
[5740]444 return finish(failure);
[5683]445 }
[5786]446 catch (const char* msg) {
447 LOG << msg << endl;
448 return finish(failure);
449 }
[5683]450 catch (...) {
451 LOG << "unknown exception" << endl;
[5740]452 return finish(failure);
[5683]453 }
454
[5740]455 return finish(success);
[5683]456}
[5740]457
458//
459////////////////////////////////////////////////////////////////////////////
460double lorentz(const ColumnVector& aa, const ColumnVector& bb) {
461 return aa[0]*bb[0] + aa[1]*bb[1] + aa[2]*bb[2] - aa[3]*bb[3];
462}
463
464//
465////////////////////////////////////////////////////////////////////////////
466void t_pppClient::bancroft(const Matrix& BBpass, ColumnVector& pos) {
467
468 if (pos.Nrows() != 4) {
469 pos.ReSize(4);
470 }
471 pos = 0.0;
472
473 for (int iter = 1; iter <= 2; iter++) {
474 Matrix BB = BBpass;
475 int mm = BB.Nrows();
476 for (int ii = 1; ii <= mm; ii++) {
477 double xx = BB(ii,1);
478 double yy = BB(ii,2);
479 double traveltime = 0.072;
480 if (iter > 1) {
481 double zz = BB(ii,3);
482 double rho = sqrt( (xx-pos(1)) * (xx-pos(1)) +
483 (yy-pos(2)) * (yy-pos(2)) +
484 (zz-pos(3)) * (zz-pos(3)) );
485 traveltime = rho / t_CST::c;
486 }
487 double angle = traveltime * t_CST::omega;
488 double cosa = cos(angle);
489 double sina = sin(angle);
490 BB(ii,1) = cosa * xx + sina * yy;
491 BB(ii,2) = -sina * xx + cosa * yy;
492 }
493
494 Matrix BBB;
495 if (mm > 4) {
496 SymmetricMatrix hlp; hlp << BB.t() * BB;
497 BBB = hlp.i() * BB.t();
498 }
499 else {
500 BBB = BB.i();
501 }
502 ColumnVector ee(mm); ee = 1.0;
503 ColumnVector alpha(mm); alpha = 0.0;
504 for (int ii = 1; ii <= mm; ii++) {
505 alpha(ii) = lorentz(BB.Row(ii).t(),BB.Row(ii).t())/2.0;
506 }
507 ColumnVector BBBe = BBB * ee;
508 ColumnVector BBBalpha = BBB * alpha;
509 double aa = lorentz(BBBe, BBBe);
510 double bb = lorentz(BBBe, BBBalpha)-1;
511 double cc = lorentz(BBBalpha, BBBalpha);
512 double root = sqrt(bb*bb-aa*cc);
513
514 Matrix hlpPos(4,2);
515 hlpPos.Column(1) = (-bb-root)/aa * BBBe + BBBalpha;
516 hlpPos.Column(2) = (-bb+root)/aa * BBBe + BBBalpha;
517
518 ColumnVector omc(2);
519 for (int pp = 1; pp <= 2; pp++) {
520 hlpPos(4,pp) = -hlpPos(4,pp);
521 omc(pp) = BB(1,4) -
522 sqrt( (BB(1,1)-hlpPos(1,pp)) * (BB(1,1)-hlpPos(1,pp)) +
523 (BB(1,2)-hlpPos(2,pp)) * (BB(1,2)-hlpPos(2,pp)) +
524 (BB(1,3)-hlpPos(3,pp)) * (BB(1,3)-hlpPos(3,pp)) ) -
525 hlpPos(4,pp);
526 }
527 if ( fabs(omc(1)) > fabs(omc(2)) ) {
528 pos = hlpPos.Column(2);
529 }
530 else {
531 pos = hlpPos.Column(1);
532 }
533 }
534}
535
Note: See TracBrowser for help on using the repository browser.