source: ntrip/branches/BNC_2.12/src/PPP/pppClient.cpp@ 8450

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