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

Last change on this file since 8451 was 8451, checked in by stuerze, 6 years ago

minor changes

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