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

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