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

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