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

Last change on this file since 7904 was 7904, checked in by stuerze, 8 years ago

memeory leak fixed

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