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

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

minor changes to prevent nan values in bancroft solution

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