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

Last change on this file since 10583 was 10583, checked in by stuerze, 5 days ago

changed in PPP mode from common receiver clock + ISBs to system specific receiver clocks

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