source: ntrip/trunk/BNC/src/orbComp/sp3Comp.cpp@ 11083

Last change on this file since 11083 was 11083, checked in by stuerze, 18 hours ago

further fixes and improvements

File size: 19.2 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: t_sp3Comp
30 *
31 * Purpose: Compare SP3 Files
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Nov-2014
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42#include <iomanip>
43#include "sp3Comp.h"
44#include "bnccore.h"
45#include "bncsettings.h"
46#include "bncutils.h"
47#include "bncsp3.h"
48
49using namespace std;
50
51// Constructor
52////////////////////////////////////////////////////////////////////////////
53t_sp3Comp::t_sp3Comp(QObject* parent) : QThread(parent) {
54
55 bncSettings settings;
56 _sp3FileNames = settings.value("sp3CompFile").toString().split(QRegExp("[ ,]"), Qt::SkipEmptyParts);
57 for (int ii = 0; ii < _sp3FileNames.size(); ii++) {
58 expandEnvVar(_sp3FileNames[ii]);
59 }
60 _logFileName = settings.value("sp3CompOutLogFile").toString(); expandEnvVar(_logFileName);
61 _logFile = 0;
62 _log = 0;
63
64 _excludeSats = settings.value("sp3CompExclude").toString().split(QRegExp("[ ,]"), Qt::SkipEmptyParts);
65
66 _summaryOnly = (Qt::CheckState(settings.value("sp3CompSummaryOnly").toInt()) == Qt::Checked);
67}
68
69// Destructor
70////////////////////////////////////////////////////////////////////////////
71t_sp3Comp::~t_sp3Comp() {
72 delete _log;
73 delete _logFile;
74}
75
76//
77////////////////////////////////////////////////////////////////////////////
78void t_sp3Comp::run() {
79
80 // Open Log File
81 // -------------
82 _logFile = new QFile(_logFileName);
83 if (_logFile->open(QIODevice::WriteOnly | QIODevice::Text)) {
84 _log = new QTextStream();
85 _log->setDevice(_logFile);
86 }
87 if (!_log) {
88 *_log << "ERROR: SP3Comp requires logfile specification" << "\n";
89 goto exit;
90 }
91
92 for (int ii = 0; ii < _sp3FileNames.size(); ii++) {
93 *_log << "! SP3 File " << ii+1 << ": " << _sp3FileNames[ii] << "\n";
94 }
95 if (_sp3FileNames.size() != 2) {
96 *_log << "ERROR: sp3Comp requires two input SP3 files" << "\n";
97 goto end;
98 }
99
100 try {
101 ostringstream msg;
102 compare(msg);
103 *_log << msg.str().c_str();
104 }
105 catch (const string& error) {
106 *_log << "ERROR: " << error.c_str() << "\n";
107 }
108 catch (const char* error) {
109 *_log << "ERROR: " << error << "\n";
110 }
111 catch (Exception& exc) {
112 *_log << "ERROR: " << exc.what() << "\n";
113 }
114 catch (std::exception& exc) {
115 *_log << "ERROR: " << exc.what() << "\n";
116 }
117 catch (QString error) {
118 *_log << "ERROR: " << error << "\n";
119 }
120 catch (...) {
121 *_log << "ERROR: " << "unknown exception" << "\n";
122 }
123
124 // Exit (thread)
125 // -------------
126 end:
127 _log->flush();
128 exit:
129 // do nothing if no logfile available
130
131 if (BNC_CORE->mode() != t_bncCore::interactive) {
132 qApp->exit(7);
133 msleep(100); //sleep 0.1 sec
134 }
135 else {
136 emit finished();
137 deleteLater();
138 }
139}
140
141// Satellite Index in clkSats set
142////////////////////////////////////////////////////////////////////////////////
143int t_sp3Comp::satIndex(const set<t_prn>& clkSats, const t_prn& prn) const {
144 int ret = 0;
145 for (set<t_prn>::const_iterator it = clkSats.begin(); it != clkSats.end(); it++) {
146 if ( *it == prn) {
147 return ret;
148 }
149 ++ret;
150 }
151 return -1;
152}
153
154// Estimate Clock Offsets
155////////////////////////////////////////////////////////////////////////////////
156void t_sp3Comp::processClocks(const set<t_prn>& clkSats, const vector<t_epoch*>& epochsIn,
157 map<string, t_stat>& stat) const {
158
159 if (clkSats.size() == 0) {
160 return;
161 }
162
163 vector<t_epoch*> epochs;
164 for (unsigned ii = 0; ii < epochsIn.size(); ii++) {
165 unsigned numSatOK = 0;
166 std::map<t_prn, double>::const_iterator it;
167 for (it = epochsIn[ii]->_dc.begin(); it != epochsIn[ii]->_dc.end(); it++) {
168 if (satIndex(clkSats, it->first) != -1) {
169 numSatOK += 1;
170 }
171 }
172 if (numSatOK > 0) {
173 epochs.push_back(epochsIn[ii]);
174 }
175 }
176 if (epochs.size() == 0) {
177 return;
178 }
179
180 // Least-squares adjustment of an epoch-specific offset (common to all
181 // satellites) and a satellite-specific offset (common to all epochs), the
182 // sum of the satellite offsets constrained to zero (weight 1). The normal
183 // equations of the epoch offsets are diagonal, so these offsets are
184 // eliminated (Schur complement) and only the satellite offsets are solved
185 // for; the epoch offsets follow from them. The solution is identical to
186 // that of the full system, but the effort grows only linearly with the
187 // number of epochs (before: cubic).
188 // -----------------------------------------------------------------------
189 int nEpo = epochs.size();
190 int nSat = clkSats.size();
191 int nPar = nEpo + nSat;
192
193 vector<vector<int> > satIdx(nEpo); // satellites (index) of each epoch
194 ColumnVector nEp(nEpo); nEp = 0.0; // number of satellites per epoch
195 ColumnVector bEp(nEpo); bEp = 0.0; // sum of the differences per epoch
196 SymmetricMatrix NS(nSat); NS = 1.0; // regularization: sum of satellite offsets
197 ColumnVector bS(nSat); bS = 0.0;
198 for (int ie = 0; ie < nEpo; ie++) {
199 const map<t_prn, double>& dc = epochs[ie]->_dc;
200 for (map<t_prn, double>::const_iterator it = dc.begin(); it != dc.end(); it++) {
201 int is = satIndex(clkSats, it->first);
202 if (is != -1) {
203 satIdx[ie].push_back(is);
204 nEp[ie] += 1.0;
205 bEp[ie] += it->second;
206 NS(is+1, is+1) += 1.0;
207 bS[is] += it->second;
208 }
209 }
210 }
211
212 // Eliminate the epoch offsets
213 // ---------------------------
214 for (int ie = 0; ie < nEpo; ie++) {
215 const vector<int>& idx = satIdx[ie];
216 for (unsigned ia = 0; ia < idx.size(); ia++) {
217 bS[idx[ia]] -= bEp[ie] / nEp[ie];
218 for (unsigned ib = 0; ib <= ia; ib++) {
219 NS(idx[ia]+1, idx[ib]+1) -= 1.0 / nEp[ie];
220 }
221 }
222 }
223
224 // Estimate Parameters (epoch offsets first, then satellite offsets)
225 // ---------------------------------------------------------------
226 ColumnVector yy = NS.i() * bS;
227 ColumnVector xx(nPar);
228 for (int ie = 0; ie < nEpo; ie++) {
229 double sum = bEp[ie];
230 for (unsigned ia = 0; ia < satIdx[ie].size(); ia++) {
231 sum -= yy[satIdx[ie][ia]];
232 }
233 xx[ie] = sum / nEp[ie];
234 }
235 for (int is = 0; is < nSat; is++) {
236 xx[nEpo + is] = yy[is];
237 }
238
239 // Compute clock residuals
240 // -----------------------
241 for (unsigned ie = 0; ie < epochs.size(); ie++) {
242 map<t_prn, double>& dc = epochs[ie]->_dc;
243 map<t_prn, double>& dcRed = epochs[ie]->_dcRed;
244 const map<t_prn, ColumnVector>& dr = epochs[ie]->_dr;
245 for (map<t_prn, double>::iterator it = dc.begin(); it != dc.end(); it++) {
246 const t_prn& prn = it->first;
247 stringstream all; all << prn.system() << 99;
248 if (satIndex(clkSats, prn) != -1) {
249 int index = epochs.size() + satIndex(clkSats, prn);
250 dc[prn] = it->second - xx[ie] - xx[index];
251 stat[prn.toString()]._offset = xx[index];
252 if (dr.find(prn) != dr.end()){
253 // signal-in-space difference: radial minus clock, only the
254 // epoch-wise clock offset (datum) removed, i.e. including the
255 // satellite-specific clock offset
256 epochs[ie]->_sis[prn] = dr.find(prn)->second[0] - (dc[prn] + xx[index]);
257 dcRed[prn] = dc[prn] - dr.find(prn)->second[0]; // clock minus radial component
258 stat[prn.toString()]._dcRedMean += dcRed[prn];
259 stat[all.str() ]._dcRedMean += dcRed[prn];
260 stat[prn.toString()]._nc += 1;
261 stat[all.str() ]._nc += 1;
262 }
263 }
264 }
265 }
266
267 // Compute Clock Mean
268 // ------------------
269 for (map<string, t_stat>::iterator it = stat.begin(); it != stat.end(); it++) {
270 t_stat& stat = it->second;
271 if (stat._nc > 0) {
272 stat._dcRedMean = stat._dcRedMean / stat._nc;
273 }
274 }
275}
276
277// Main Routine
278////////////////////////////////////////////////////////////////////////////////
279void t_sp3Comp::compare(ostringstream& out) const {
280
281 // Synchronize reading of two sp3 files
282 // ------------------------------------
283 bncSP3 in1(_sp3FileNames[0]); in1.nextEpoch();
284 bncSP3 in2(_sp3FileNames[1]); in2.nextEpoch();
285
286 vector<t_epoch*> epochs;
287 while (in1.currEpoch() && in2.currEpoch()) {
288 bncTime t1 = in1.currEpoch()->_tt;
289 bncTime t2 = in2.currEpoch()->_tt;
290 if (t1 < t2) {
291 in1.nextEpoch();
292 }
293 else if (t1 > t2) {
294 in2.nextEpoch();
295 }
296 else if (t1 == t2) {
297 t_epoch* epo = new t_epoch; epo->_tt = t1;
298 bool epochOK = false;
299 for (int i1 = 0; i1 < in1.currEpoch()->_sp3Sat.size(); i1++) {
300 bncSP3::t_sp3Sat* sat1 = in1.currEpoch()->_sp3Sat[i1];
301 for (int i2 = 0; i2 < in2.currEpoch()->_sp3Sat.size(); i2++) {
302 bncSP3::t_sp3Sat* sat2 = in2.currEpoch()->_sp3Sat[i2];
303 if (sat1->_prn == sat2->_prn &&
304 sat1->_clkValid && sat2->_clkValid) {
305 epochOK = true;
306 epo->_dr[sat1->_prn] = sat1->_xyz - sat2->_xyz;
307 epo->_xyz[sat1->_prn] = sat1->_xyz;
308 epo->_dc[sat1->_prn] = sat1->_clk - sat2->_clk;
309 }
310 }
311 }
312 if (epochOK) {
313 epochs.push_back(epo);
314 }
315 else {
316 delete epo;
317 }
318 in1.nextEpoch();
319 in2.nextEpoch();
320 }
321 }
322
323 // Transform xyz into radial, along-track, and out-of-plane
324 // --------------------------------------------------------
325 if (epochs.size() < 2) {
326 throw "t_sp3Comp: not enough common epochs";
327 }
328
329 set<t_prn> clkSatsAll;
330
331 for (unsigned ie = 0; ie < epochs.size(); ie++) {
332 t_epoch* epoch = epochs[ie];
333 t_epoch* epoch2 = 0;
334 if (ie == 0) {
335 epoch2 = epochs[ie+1];
336 }
337 else {
338 epoch2 = epochs[ie-1];
339 }
340 double dt = epoch->_tt - epoch2->_tt;
341 map<t_prn, ColumnVector>& dr = epoch->_dr;
342 map<t_prn, ColumnVector>& xyz = epoch->_xyz;
343 map<t_prn, ColumnVector>& xyz2 = epoch2->_xyz;
344 QList<t_prn> satEraseList;
345 for (map<t_prn, ColumnVector>::const_iterator it = dr.begin(); it != dr.end(); it++) {
346 const t_prn& prn = it->first;
347 if (xyz2.find(prn) != xyz2.end()) {
348 const ColumnVector dx = dr[prn];
349 const ColumnVector& x1 = xyz[prn];
350 const ColumnVector& x2 = xyz2[prn];
351 ColumnVector vel = (x1 - x2) / dt;
352 XYZ_to_RSW(x1, vel, dx, dr[prn]);
353 if (epoch->_dc.find(prn) != epoch->_dc.end()) {
354 clkSatsAll.insert(prn);
355 }
356 }
357 else {
358 satEraseList << it->first;
359 }
360 }
361 for (QList<t_prn>::const_iterator it = satEraseList.begin(); it != satEraseList.end(); it++) {
362 epoch->_dc.erase(*it);
363 epoch->_dr.erase(*it);
364 }
365 }
366
367 map<string, t_stat> stat;
368
369 // Estimate Clock Offsets
370 // ----------------------
371 string systems;
372 for (set<t_prn>::const_iterator it = clkSatsAll.begin(); it != clkSatsAll.end(); it++) {
373 if (systems.find(it->system()) == string::npos) {
374 systems += it->system();
375 }
376 }
377 for (unsigned iSys = 0; iSys < systems.size(); iSys++) {
378 char system = systems[iSys];
379 set<t_prn> clkSats;
380 for (set<t_prn>::const_iterator it = clkSatsAll.begin(); it != clkSatsAll.end(); it++) {
381 if (it->system() == system && !excludeSat(*it)) {
382 clkSats.insert(*it);
383 }
384 }
385 processClocks(clkSats, epochs, stat);
386 }
387
388 // Print epoch-wise Clock Residuals
389 // --------------------------------
390 out.setf(ios::fixed);
391 if (!_summaryOnly) {
392 out << "!\n! Clock residuals and orbit differences in [m]\n"
393 "! ----------------------------------------------------------------------------\n";
394 out << "!\n! Epoch PRN radial along out clk clkRed iPRN SIS"
395 "\n! -------------------------------------------------------------------------------------\n";
396 }
397 for (unsigned ii = 0; ii < epochs.size(); ii++) {
398 const t_epoch* epo = epochs[ii];
399 const map<t_prn, ColumnVector>& dr = epochs[ii]->_dr;
400 const map<t_prn, double>& dc = epochs[ii]->_dc;
401 const map<t_prn, double>& dcRed = epochs[ii]->_dcRed;
402 const map<t_prn, double>& sis = epochs[ii]->_sis;
403 for (map<t_prn, ColumnVector>::const_iterator it = dr.begin(); it != dr.end(); it++) {
404 const t_prn& prn = it->first;
405 stringstream all; all << prn.system() << 99;
406 if (!excludeSat(prn)) {
407 const ColumnVector& rao = it->second;
408 if (!_summaryOnly) {
409 out << setprecision(6) << string(epo->_tt) << ' ' << prn.toString() << ' '
410 << setw(7) << setprecision(4) << rao[0] << ' '
411 << setw(7) << setprecision(4) << rao[1] << ' '
412 << setw(7) << setprecision(4) << rao[2] << " ";
413 }
414 stat[prn.toString()]._rao += SP(rao, rao); // Schur product
415 stat[all.str() ]._rao += SP(rao, rao);
416 stat[prn.toString()]._nr += 1;
417 stat[all.str() ]._nr += 1;
418 if (dc.find(prn) != dc.end() && dcRed.find(prn) != dc.end()) {
419 double clkRes = dc.find(prn)->second;
420 double clkResRed = dcRed.find(prn)->second;
421 if (!_summaryOnly) {
422 out << setw(7) << setprecision(4) << clkRes << ' '
423 << setw(7) << setprecision(4) << clkResRed;
424 }
425 stat[prn.toString()]._dcRMS += clkRes * clkRes;
426 stat[all.str() ]._dcRMS += clkRes * clkRes;
427 stat[prn.toString()]._dcRedRMS += clkResRed * clkResRed;
428 stat[all.str() ]._dcRedRMS += clkResRed * clkResRed;
429 stat[prn.toString()]._dcRedSig += (clkResRed - stat[prn.toString()]._dcRedMean) *
430 (clkResRed - stat[prn.toString()]._dcRedMean);
431 stat[all.str() ]._dcRedSig += (clkResRed - stat[all.str() ]._dcRedMean) *
432 (clkResRed - stat[all.str() ]._dcRedMean);
433 }
434 else {
435 if (!_summaryOnly) {
436 out << " . . ";
437 }
438 }
439 if (!_summaryOnly) {
440 out << " " << setw(2) << int(prn);
441 }
442 if (sis.find(prn) != sis.end()) {
443 double sisVal = sis.find(prn)->second;
444 stat[prn.toString()]._sisSum += sisVal;
445 stat[all.str() ]._sisSum += sisVal;
446 stat[prn.toString()]._sisSqr += sisVal * sisVal;
447 stat[all.str() ]._sisSqr += sisVal * sisVal;
448 stat[prn.toString()]._ns += 1;
449 stat[all.str() ]._ns += 1;
450 if (!_summaryOnly) {
451 out << " " << setw(7) << setprecision(4) << sisVal;
452 }
453 }
454 if (!_summaryOnly) {
455 out << "\n";
456 }
457 }
458 }
459 delete epo;
460 }
461
462 // Print Summary
463 // -------------
464 out << "!\n! Summary";
465 out << "\n! -----------------------------------------------------------------------------------------------------------------\n";
466 out << "!\n! PRN radialRMS alongRMS outRMS 3DRMS nOrb clkRMS clkRedRMS clkRedSig nClk Offset SISRMS SISSig"
467 "\n! [mm] [mm] [mm] [mm] [-] [ns] [ns] [ns] [-] [ns] [mm] [mm]"
468 "\n! -------------------------------------------------------------------------------------------------------------------------------------\n";
469 // SIS standard deviation of a system: pooled about the satellite means
470 map<char, double> sisVarSys;
471 for (map<string, t_stat>::const_iterator it = stat.begin(); it != stat.end(); it++) {
472 const t_stat& st = it->second;
473 stringstream all; all << it->first[0] << 99;
474 if (it->first != all.str() && st._ns > 0) {
475 sisVarSys[it->first[0]] += st._sisSqr - st._sisSum * st._sisSum / st._ns;
476 }
477 }
478 for (map<string, t_stat>::iterator it = stat.begin(); it != stat.end(); it++) {
479 const string& prn = it->first;
480 t_stat& stat = it->second;
481 char sys = prn[0];
482 stringstream all; all << sys << 99;
483 if (stat._nr > 0) {
484 stat._rao[0] = sqrt(stat._rao[0] / stat._nr);
485 stat._rao[1] = sqrt(stat._rao[1] / stat._nr);
486 stat._rao[2] = sqrt(stat._rao[2] / stat._nr);
487 stat._rao3DRMS = stat._rao.NormFrobenius();
488 // orbit values in millimeter
489 if (prn != all.str()) {
490 (_summaryOnly) ? out << " " << prn << ' ':
491 out << "! " << prn << ' ';
492 }
493 else {
494 QString sys; sys = all.str()[0];
495 (_summaryOnly) ? out << " " << QString("%1").arg(sys,3,' ').toStdString().c_str() << " ":
496 out << "! " << QString("%1").arg(sys,3,' ').toStdString().c_str() << " ";
497 }
498 out << setw(10) << setprecision(1) << stat._rao[0] * 1e3 << ' '
499 << setw(10) << setprecision(1) << stat._rao[1] * 1e3 << ' '
500 << setw(10) << setprecision(1) << stat._rao[2] * 1e3 << ' '
501 << setw(10) << setprecision(1) << stat._rao3DRMS * 1e3 << ' '
502 << setw( 7) << stat._nr << " ";
503 // clock values in nano seconds
504 if (stat._nc > 0) {
505 stat._dcRMS = sqrt(stat._dcRMS / stat._nc);
506 stat._dcRedRMS = sqrt(stat._dcRedRMS / stat._nc);
507 stat._dcRedSig = sqrt(stat._dcRedSig / stat._nc);
508 out << setw(10) << setprecision(2) << stat._dcRMS / t_CST::c * 1e9 << ' '
509 << setw(10) << setprecision(2) << stat._dcRedRMS / t_CST::c * 1e9 << ' '
510 << setw(10) << setprecision(2) << stat._dcRedSig / t_CST::c * 1e9 << ' '
511 << setw( 9) << stat._nc << " ";
512 if (prn != "G99" && prn != "R99" && prn != "E99" && prn != "C98" && prn != "C99") {
513 out << setw( 9) << setprecision(2) << stat._offset / t_CST::c * 1e9;
514 }
515 else {
516 out << setw( 9) << " ";
517 }
518 }
519 if (stat._ns > 0) {
520 double var = (prn == all.str()) ? sisVarSys[sys] / stat._ns
521 : stat._sisSqr / stat._ns - pow(stat._sisSum / stat._ns, 2);
522 out << setw(11) << setprecision(1) << sqrt(stat._sisSqr / stat._ns) * 1e3 << ' '
523 << setw( 9) << setprecision(1) << sqrt(max(0.0, var)) * 1e3;
524 }
525 out << "\n";
526 }
527 }
528}
529
530//
531////////////////////////////////////////////////////////////////////////////
532bool t_sp3Comp::excludeSat(const t_prn& prn) const {
533 QStringListIterator it(_excludeSats);
534 while (it.hasNext()) {
535 string prnStr = it.next().toLatin1().data();
536 if (prnStr == prn.toString() || // prn
537 prnStr == prn.toString().substr(0,1)) { // sys
538 return true;
539 }
540 }
541 return false;
542}
543
Note: See TracBrowser for help on using the repository browser.