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

Last change on this file since 7237 was 7237, checked in by stuerze, 9 years ago

some renaming regarding PPP

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