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

Last change on this file since 8449 was 8449, 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 <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 (tLC == t_lc::dummy) {
217 if (satObs->isValid(t_lc::cIF)) {
218 tLC = t_lc::cIF;
219 }
220 else if (satObs->isValid(t_lc::c1)) {
221 tLC = t_lc::c1;
222 }
223 else if (satObs->isValid(t_lc::c2)) {
224 tLC = t_lc::c2;
225 }
226 }
227 if ( satObs->isValid(tLC) && (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle) ) {
228 ++iObs;
229 BB[iObs][0] = satObs->xc()[0];
230 BB[iObs][1] = satObs->xc()[1];
231 BB[iObs][2] = satObs->xc()[2];
232 BB[iObs][3] = satObs->obsValue(tLC) - satObs->cmpValueForBanc(tLC);
233 }
234 }
235 if (iObs + 1 < OPT->_minObs) {
236 LOG << "t_pppClient::cmpBancroft not enough observations" << endl;
237 return failure;
238 }
239 BB = BB.Rows(1,iObs+1);
240 bancroft(BB, xyzc);
241
242 xyzc[3] /= t_CST::c;
243
244 // Check Blunders
245 // --------------
246 const double BLUNDER = 100.0;
247 double maxRes = 0.0;
248 unsigned maxResIndex = 0;
249 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
250 const t_pppSatObs* satObs = obsVector.at(ii);
251 if ( satObs->isValid() && satObs->prn().system() == 'G' &&
252 (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle) ) {
253 ColumnVector rr = satObs->xc().Rows(1,3) - xyzc.Rows(1,3);
254 double res = rr.norm_Frobenius() - satObs->obsValue(tLC)
255 - (satObs->xc()[3] - xyzc[3]) * t_CST::c;
256 if (fabs(res) > maxRes || std::isnan(res)) {
257 maxRes = fabs(res);
258 maxResIndex = ii;
259 }
260 }
261 }
262 if (maxRes < BLUNDER) {
263 if (print) {
264 LOG.setf(ios::fixed);
265 LOG << string(epoTime) << " BANCROFT:" << ' '
266 << setw(14) << setprecision(3) << xyzc[0] << ' '
267 << setw(14) << setprecision(3) << xyzc[1] << ' '
268 << setw(14) << setprecision(3) << xyzc[2] << ' '
269 << setw(14) << setprecision(3) << xyzc[3] * t_CST::c << endl << endl;
270 }
271 break;
272 }
273 else {
274 t_pppSatObs* satObs = obsVector.at(maxResIndex);
275 LOG << "t_pppClient::cmpBancroft outlier " << satObs->prn().toString()
276 << " " << maxRes << endl;
277 delete satObs;
278 obsVector.erase(obsVector.begin() + maxResIndex);
279 }
280 }
281
282 return success;
283}
284
285// Compute A Priori GPS-Glonass Offset
286//////////////////////////////////////////////////////////////////////////////
287double t_pppClient::cmpOffGG(vector<t_pppSatObs*>& obsVector) {
288
289 t_lc::type tLC = t_lc::dummy;
290 double offGG = 0.0;
291
292 if (OPT->useSystem('R')) {
293 while (obsVector.size() > 0) {
294 offGG = 0.0;
295 double maxRes = 0.0;
296 int maxResIndex = -1;
297 t_prn maxResPrn;
298 unsigned nObs = 0;
299 for (unsigned ii = 0; ii < obsVector.size(); ii++) {
300 const t_pppSatObs* satObs = obsVector.at(ii);
301 if (satObs->prn().system() == 'R') {
302 if (tLC == t_lc::dummy) {
303 tLC = satObs->isValid(t_lc::cIF) ? t_lc::cIF : t_lc::c1;
304 }
305 if (satObs->isValid(tLC) && (!satObs->modelSet() || satObs->eleSat() >= OPT->_minEle)) {
306 double ll = satObs->obsValue(tLC) - satObs->cmpValue(tLC);
307 ++nObs;
308 offGG += ll;
309 if (fabs(ll) > fabs(maxRes)) {
310 maxRes = ll;
311 maxResIndex = ii;
312 maxResPrn = satObs->prn();
313 }
314 }
315 }
316 }
317
318 if (nObs > 0) {
319 offGG = offGG / nObs;
320 }
321 else {
322 offGG = 0.0;
323 }
324
325 if (fabs(maxRes) > 1000.0) {
326 LOG << "t_pppClient::cmpOffGG outlier " << maxResPrn.toString() << " " << maxRes << endl;
327 delete obsVector.at(maxResIndex);
328 obsVector.erase(obsVector.begin() + maxResIndex);
329 }
330 else {
331 break;
332 }
333 }
334 }
335 return offGG;
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) {
359
360 clearObs();
361
362 _output->_epoTime = _epoTimeRover;
363
364 if (irc == success) {
365 _output->_xyzRover[0] = _staRover->xyzApr()[0] + _filter->x()[0];
366 _output->_xyzRover[1] = _staRover->xyzApr()[1] + _filter->x()[1];
367 _output->_xyzRover[2] = _staRover->xyzApr()[2] + _filter->x()[2];
368
369 xyz2neu(_staRover->ellApr().data(), _filter->x().data(), _output->_neu);
370
371 copy(&_filter->Q().data()[0], &_filter->Q().data()[6], _output->_covMatrix);
372
373 _output->_trp0 = t_tropo::delay_saast(_staRover->xyzApr(), M_PI/2.0);
374 _output->_trp = _filter->trp();
375 _output->_trpStdev = _filter->trpStdev();
376
377 _output->_numSat = _filter->numSat();
378 _output->_hDop = _filter->HDOP();
379 _output->_error = false;
380 }
381 else {
382 _output->_error = true;
383 }
384 _output->_log = _log->str();
385 delete _log; _log = new ostringstream();
386}
387
388//
389//////////////////////////////////////////////////////////////////////////////
390t_irc t_pppClient::cmpModel(t_pppStation* station, const ColumnVector& xyzc,
391 vector<t_pppSatObs*>& obsVector) {
392
393 bncTime time;
394 time = _epoTimeRover;
395 station->setName(OPT->_roverName);
396 station->setAntName(OPT->_antNameRover);
397 if (OPT->xyzAprRoverSet()) {
398 station->setXyzApr(OPT->_xyzAprRover);
399 }
400 else {
401 station->setXyzApr(xyzc.Rows(1,3));
402 }
403 station->setNeuEcc(OPT->_neuEccRover);
404
405 // Receiver Clock
406 // --------------
407 station->setDClk(xyzc[3]);
408
409 // Tides
410 // -----
411 station->setTideDspl( _tides->displacement(time, station->xyzApr()) );
412
413 // Ionosphere
414 // ----------
415 station->setIonoEpochTime(time);
416
417 // Observation model
418 // -----------------
419 vector<t_pppSatObs*>::iterator it = obsVector.begin();
420 while (it != obsVector.end()) {
421 t_pppSatObs* satObs = *it;
422 t_irc modelSetup;
423 if (satObs->isValid()) {
424 modelSetup = satObs->cmpModel(station);
425 }
426 if (satObs->isValid() &&
427 satObs->eleSat() >= OPT->_minEle &&
428 modelSetup == success) {
429 ++it;
430 }
431 else {
432 delete satObs;
433 it = obsVector.erase(it);
434 }
435 }
436
437 return success;
438}
439
440//
441//////////////////////////////////////////////////////////////////////////////
442void t_pppClient::processEpoch(const vector<t_satObs*>& satObs, t_output* output) {
443
444 try {
445 initOutput(output);
446
447 // Prepare Observations of the Rover
448 // ---------------------------------
449 if (prepareObs(satObs, _obsRover, _epoTimeRover) != success) {
450 return finish(failure);
451 }
452
453 LOG << "\nPPP of Epoch ";
454 if (!_epoTimeRover.undef()) LOG << string(_epoTimeRover);
455 LOG << "\n---------------------------------------------------------------\n";
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);
462 }
463 if (cmpModel(_staRover, xyzc, _obsRover) != success) {
464 return finish(failure);
465 }
466 }
467
468 _offGG = cmpOffGG(_obsRover);
469
470 if (int(_obsRover.size()) < OPT->_minObs) {
471 LOG << "t_pppClient::processEpoch not enough observations" << endl;
472 return finish(failure);
473 }
474
475 // Store last epoch of data
476 // ------------------------
477 _obsPool->putEpoch(_epoTimeRover, _obsRover);
478
479 // Process Epoch in Filter
480 // -----------------------
481 if (_filter->processEpoch(_obsPool) != success) {
482 return finish(failure);
483 }
484 }
485 catch (Exception& exc) {
486 LOG << exc.what() << endl;
487 return finish(failure);
488 }
489 catch (t_except msg) {
490 LOG << msg.what() << endl;
491 return finish(failure);
492 }
493 catch (const char* msg) {
494 LOG << msg << endl;
495 return finish(failure);
496 }
497 catch (...) {
498 LOG << "unknown exception" << endl;
499 return finish(failure);
500 }
501
502 return finish(success);
503}
504
505//
506////////////////////////////////////////////////////////////////////////////
507double lorentz(const ColumnVector& aa, const ColumnVector& bb) {
508 return aa[0]*bb[0] + aa[1]*bb[1] + aa[2]*bb[2] - aa[3]*bb[3];
509}
510
511//
512////////////////////////////////////////////////////////////////////////////
513void t_pppClient::bancroft(const Matrix& BBpass, ColumnVector& pos) {
514
515 if (pos.Nrows() != 4) {
516 pos.ReSize(4);
517 }
518 pos = 0.0;
519
520 for (int iter = 1; iter <= 2; iter++) {
521 Matrix BB = BBpass;
522 int mm = BB.Nrows();
523 for (int ii = 1; ii <= mm; ii++) {
524 double xx = BB(ii,1);
525 double yy = BB(ii,2);
526 double traveltime = 0.072;
527 if (iter > 1) {
528 double zz = BB(ii,3);
529 double rho = sqrt( (xx-pos(1)) * (xx-pos(1)) +
530 (yy-pos(2)) * (yy-pos(2)) +
531 (zz-pos(3)) * (zz-pos(3)) );
532 traveltime = rho / t_CST::c;
533 }
534 double angle = traveltime * t_CST::omega;
535 double cosa = cos(angle);
536 double sina = sin(angle);
537 BB(ii,1) = cosa * xx + sina * yy;
538 BB(ii,2) = -sina * xx + cosa * yy;
539 }
540
541 Matrix BBB;
542 if (mm > 4) {
543 SymmetricMatrix hlp; hlp << BB.t() * BB;
544 BBB = hlp.i() * BB.t();
545 }
546 else {
547 BBB = BB.i();
548 }
549 ColumnVector ee(mm); ee = 1.0;
550 ColumnVector alpha(mm); alpha = 0.0;
551 for (int ii = 1; ii <= mm; ii++) {
552 alpha(ii) = lorentz(BB.Row(ii).t(),BB.Row(ii).t())/2.0;
553 }
554 ColumnVector BBBe = BBB * ee;
555 ColumnVector BBBalpha = BBB * alpha;
556 double aa = lorentz(BBBe, BBBe);
557 double bb = lorentz(BBBe, BBBalpha)-1;
558 double cc = lorentz(BBBalpha, BBBalpha);
559 double root = sqrt(bb*bb-aa*cc);
560
561 Matrix hlpPos(4,2);
562 hlpPos.Column(1) = (-bb-root)/aa * BBBe + BBBalpha;
563 hlpPos.Column(2) = (-bb+root)/aa * BBBe + BBBalpha;
564
565 ColumnVector omc(2);
566 for (int pp = 1; pp <= 2; pp++) {
567 hlpPos(4,pp) = -hlpPos(4,pp);
568 omc(pp) = BB(1,4) -
569 sqrt( (BB(1,1)-hlpPos(1,pp)) * (BB(1,1)-hlpPos(1,pp)) +
570 (BB(1,2)-hlpPos(2,pp)) * (BB(1,2)-hlpPos(2,pp)) +
571 (BB(1,3)-hlpPos(3,pp)) * (BB(1,3)-hlpPos(3,pp)) ) -
572 hlpPos(4,pp);
573 }
574 if ( fabs(omc(1)) > fabs(omc(2)) ) {
575 pos = hlpPos.Column(2);
576 }
577 else {
578 pos = hlpPos.Column(1);
579 }
580 }
581}
582
583//
584//////////////////////////////////////////////////////////////////////////////
585void t_pppClient::reset() {
586
587 // to delete all parameters
588 delete _filter;
589 _filter = new t_pppFilter();
590
591 // to delete old orbit and clock corrections
592 delete _ephPool;
593 _ephPool = new t_pppEphPool();
594
595 // to delete old code biases
596 delete _obsPool;
597 _obsPool = new t_pppObsPool();
598}
Note: See TracBrowser for help on using the repository browser.