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

Last change on this file since 7866 was 7866, checked in by stuerze, 8 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: 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 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 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 obsVector.erase(obsVector.begin() + maxResIndex);
312 }
313 else {
314 break;
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.