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

Last change on this file since 10673 was 10673, checked in by stuerze, 9 days ago

minor changes

File size: 17.3 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://www.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 int nPar = epochs.size() + clkSats.size();
181 SymmetricMatrix NN(nPar); NN = 0.0;
182 ColumnVector bb(nPar); bb = 0.0;
183
184 // Create Matrix A'A and vector b
185 // ------------------------------
186 for (unsigned ie = 0; ie < epochs.size(); ie++) {
187 const map<t_prn, double>& dc = epochs[ie]->_dc;
188 Matrix AA(dc.size(), nPar); AA = 0.0;
189 ColumnVector ll(dc.size()); ll = 0.0;
190 map<t_prn, double>::const_iterator it;
191 int ii = -1;
192 for (it = dc.begin(); it != dc.end(); it++) {
193 const t_prn& prn = it->first;
194 if (satIndex(clkSats, prn) != -1) {
195 ++ii;
196 int index = epochs.size() + satIndex(clkSats, prn);
197 AA[ii][ie] = 1.0; // epoch-specfic offset (common for all satellites)
198 AA[ii][index] = 1.0; // satellite-specific offset (common for all epochs)
199 ll[ii] = it->second;
200 }
201 }
202 SymmetricMatrix dN; dN << AA.t() * AA;
203 NN += dN;
204 bb += AA.t() * ll;
205 }
206
207 // Regularize NN
208 // -------------
209 RowVector HH(nPar);
210 HH.columns(1, epochs.size()) = 0.0;
211 HH.columns(epochs.size()+1, nPar) = 1.0;
212 SymmetricMatrix dN; dN << HH.t() * HH;
213 NN += dN;
214
215 // Estimate Parameters
216 // -------------------
217 ColumnVector xx = NN.i() * bb;
218
219 // Compute clock residuals
220 // -----------------------
221 for (unsigned ie = 0; ie < epochs.size(); ie++) {
222 map<t_prn, double>& dc = epochs[ie]->_dc;
223 map<t_prn, double>& dcRed = epochs[ie]->_dcRed;
224 const map<t_prn, ColumnVector>& dr = epochs[ie]->_dr;
225 for (map<t_prn, double>::iterator it = dc.begin(); it != dc.end(); it++) {
226 const t_prn& prn = it->first;
227 stringstream all; all << prn.system() << 99;
228 if (satIndex(clkSats, prn) != -1) {
229 int index = epochs.size() + satIndex(clkSats, prn);
230 dc[prn] = it->second - xx[ie] - xx[index];
231 stat[prn.toString()]._offset = xx[index];
232 if (dr.find(prn) != dr.end()){
233 dcRed[prn] = dc[prn] - dr.find(prn)->second[0]; // clock minus radial component
234 stat[prn.toString()]._dcRedMean += dcRed[prn];
235 stat[all.str() ]._dcRedMean += dcRed[prn];
236 stat[prn.toString()]._nc += 1;
237 stat[all.str() ]._nc += 1;
238 }
239 }
240 }
241 }
242
243 // Compute Clock Mean
244 // ------------------
245 for (map<string, t_stat>::iterator it = stat.begin(); it != stat.end(); it++) {
246 t_stat& stat = it->second;
247 if (stat._nc > 0) {
248 stat._dcRedMean = stat._dcRedMean / stat._nc;
249 }
250 }
251}
252
253// Main Routine
254////////////////////////////////////////////////////////////////////////////////
255void t_sp3Comp::compare(ostringstream& out) const {
256
257 // Synchronize reading of two sp3 files
258 // ------------------------------------
259 bncSP3 in1(_sp3FileNames[0]); in1.nextEpoch();
260 bncSP3 in2(_sp3FileNames[1]); in2.nextEpoch();
261
262 vector<t_epoch*> epochs;
263 while (in1.currEpoch() && in2.currEpoch()) {
264 bncTime t1 = in1.currEpoch()->_tt;
265 bncTime t2 = in2.currEpoch()->_tt;
266 if (t1 < t2) {
267 in1.nextEpoch();
268 }
269 else if (t1 > t2) {
270 in2.nextEpoch();
271 }
272 else if (t1 == t2) {
273 t_epoch* epo = new t_epoch; epo->_tt = t1;
274 bool epochOK = false;
275 for (int i1 = 0; i1 < in1.currEpoch()->_sp3Sat.size(); i1++) {
276 bncSP3::t_sp3Sat* sat1 = in1.currEpoch()->_sp3Sat[i1];
277 for (int i2 = 0; i2 < in2.currEpoch()->_sp3Sat.size(); i2++) {
278 bncSP3::t_sp3Sat* sat2 = in2.currEpoch()->_sp3Sat[i2];
279 if (sat1->_prn == sat2->_prn &&
280 sat1->_clkValid && sat2->_clkValid) {
281 epochOK = true;
282 epo->_dr[sat1->_prn] = sat1->_xyz - sat2->_xyz;
283 // temporarily included START
284 if (epo->_dr[sat1->_prn].NormFrobenius() > 1.0) {
285 epochOK = false;
286 out << "! " << epo->_tt.timestr().c_str() << " " << sat1->_prn.toString().c_str() << "excluded \n"
287 << QString("! sat 1: %1%2%3\n").arg(sat1->_xyz[0], 19, 'e', 12)
288 .arg(sat1->_xyz[1], 19, 'e', 12)
289 .arg(sat1->_xyz[2], 19, 'e', 12).toStdString().c_str()
290 << QString("! sat 2: %1%2%3\n").arg(sat2->_xyz[0], 19, 'e', 12)
291 .arg(sat2->_xyz[1], 19, 'e', 12)
292 .arg(sat2->_xyz[2], 19, 'e', 12).toStdString().c_str()
293 << QString("! diff : %1%2%3\n").arg(epo->_dr[sat1->_prn][0], 19, 'e', 12)
294 .arg(epo->_dr[sat1->_prn][1], 19, 'e', 12)
295 .arg(epo->_dr[sat1->_prn][2], 19, 'e', 12).toStdString().c_str()
296 << endl;
297 }
298 // temporarily included END
299
300 epo->_xyz[sat1->_prn] = sat1->_xyz;
301 epo->_dc[sat1->_prn] = sat1->_clk - sat2->_clk;
302 }
303 }
304 }
305 if (epochOK) {
306 epochs.push_back(epo);
307 }
308 else {
309 delete epo;
310 }
311 in1.nextEpoch();
312 in2.nextEpoch();
313 }
314 }
315
316 // Transform xyz into radial, along-track, and out-of-plane
317 // --------------------------------------------------------
318 if (epochs.size() < 2) {
319 throw "t_sp3Comp: not enough common epochs";
320 }
321
322 set<t_prn> clkSatsAll;
323
324 for (unsigned ie = 0; ie < epochs.size(); ie++) {
325 t_epoch* epoch = epochs[ie];
326 t_epoch* epoch2 = 0;
327 if (ie == 0) {
328 epoch2 = epochs[ie+1];
329 }
330 else {
331 epoch2 = epochs[ie-1];
332 }
333 double dt = epoch->_tt - epoch2->_tt;
334 map<t_prn, ColumnVector>& dr = epoch->_dr;
335 map<t_prn, ColumnVector>& xyz = epoch->_xyz;
336 map<t_prn, ColumnVector>& xyz2 = epoch2->_xyz;
337 QList<t_prn> satEraseList;
338 for (map<t_prn, ColumnVector>::const_iterator it = dr.begin(); it != dr.end(); it++) {
339 const t_prn& prn = it->first;
340 if (xyz2.find(prn) != xyz2.end()) {
341 const ColumnVector dx = dr[prn];
342 const ColumnVector& x1 = xyz[prn];
343 const ColumnVector& x2 = xyz2[prn];
344 ColumnVector vel = (x1 - x2) / dt;
345 XYZ_to_RSW(x1, vel, dx, dr[prn]);
346 if (epoch->_dc.find(prn) != epoch->_dc.end()) {
347 clkSatsAll.insert(prn);
348 }
349 }
350 else {
351 satEraseList << it->first;
352 }
353 }
354 for (QList<t_prn>::const_iterator it = satEraseList.begin(); it != satEraseList.end(); it++) {
355 epoch->_dc.erase(*it);
356 epoch->_dr.erase(*it);
357 }
358 }
359
360 map<string, t_stat> stat;
361
362 // Estimate Clock Offsets
363 // ----------------------
364 string systems;
365 for (set<t_prn>::const_iterator it = clkSatsAll.begin(); it != clkSatsAll.end(); it++) {
366 if (systems.find(it->system()) == string::npos) {
367 systems += it->system();
368 }
369 }
370 for (unsigned iSys = 0; iSys < systems.size(); iSys++) {
371 char system = systems[iSys];
372 set<t_prn> clkSats;
373 for (set<t_prn>::const_iterator it = clkSatsAll.begin(); it != clkSatsAll.end(); it++) {
374 if (it->system() == system && !excludeSat(*it)) {
375 clkSats.insert(*it);
376 }
377 }
378 processClocks(clkSats, epochs, stat);
379 }
380
381 // Print epoch-wise Clock Residuals
382 // --------------------------------
383 out.setf(ios::fixed);
384 if (!_summaryOnly) {
385 out << "!\n! Clock residuals and orbit differences in [m]\n"
386 "! ----------------------------------------------------------------------------\n";
387 out << "!\n! Epoch PRN radial along out clk clkRed iPRN"
388 "\n! ----------------------------------------------------------------------------\n";
389 }
390 for (unsigned ii = 0; ii < epochs.size(); ii++) {
391 const t_epoch* epo = epochs[ii];
392 const map<t_prn, ColumnVector>& dr = epochs[ii]->_dr;
393 const map<t_prn, double>& dc = epochs[ii]->_dc;
394 const map<t_prn, double>& dcRed = epochs[ii]->_dcRed;
395 for (map<t_prn, ColumnVector>::const_iterator it = dr.begin(); it != dr.end(); it++) {
396 const t_prn& prn = it->first;
397 stringstream all; all << prn.system() << 99;
398 if (!excludeSat(prn)) {
399 const ColumnVector& rao = it->second;
400 if (!_summaryOnly) {
401 out << setprecision(6) << string(epo->_tt) << ' ' << prn.toString() << ' '
402 << setw(7) << setprecision(4) << rao[0] << ' '
403 << setw(7) << setprecision(4) << rao[1] << ' '
404 << setw(7) << setprecision(4) << rao[2] << " ";
405 }
406 stat[prn.toString()]._rao += SP(rao, rao); // Schur product
407 stat[all.str() ]._rao += SP(rao, rao);
408 stat[prn.toString()]._nr += 1;
409 stat[all.str() ]._nr += 1;
410 if (dc.find(prn) != dc.end() && dcRed.find(prn) != dc.end()) {
411 double clkRes = dc.find(prn)->second;
412 double clkResRed = dcRed.find(prn)->second;
413 if (!_summaryOnly) {
414 out << setw(7) << setprecision(4) << clkRes << ' '
415 << setw(7) << setprecision(4) << clkResRed;
416 }
417 stat[prn.toString()]._dcRMS += clkRes * clkRes;
418 stat[all.str() ]._dcRMS += clkRes * clkRes;
419 stat[prn.toString()]._dcRedRMS += clkResRed * clkResRed;
420 stat[all.str() ]._dcRedRMS += clkResRed * clkResRed;
421 stat[prn.toString()]._dcRedSig += (clkResRed - stat[prn.toString()]._dcRedMean) *
422 (clkResRed - stat[prn.toString()]._dcRedMean);
423 stat[all.str() ]._dcRedSig += (clkResRed - stat[all.str() ]._dcRedMean) *
424 (clkResRed - stat[all.str() ]._dcRedMean);
425 }
426 else {
427 if (!_summaryOnly) {
428 out << " . . ";
429 }
430 }
431 if (!_summaryOnly) {
432 out << " " << setw(2) << int(prn) << "\n";
433 }
434 }
435 }
436 delete epo;
437 }
438
439 // Print Summary
440 // -------------
441 out << "!\n! Summary";
442 out << "\n! -----------------------------------------------------------------------------------------------------------------\n";
443 out << "!\n! PRN radialRMS alongRMS outRMS 3DRMS nOrb clkRMS clkRedRMS clkRedSig nClk Offset "
444 "\n! [mm] [mm] [mm] [mm] [-] [ns] [ns] [ns] [-] [ns] "
445 "\n! -----------------------------------------------------------------------------------------------------------------\n";
446 for (map<string, t_stat>::iterator it = stat.begin(); it != stat.end(); it++) {
447 const string& prn = it->first;
448 t_stat& stat = it->second;
449 stringstream all; all << prn[0] << 99;
450 if (stat._nr > 0) {
451 stat._rao[0] = sqrt(stat._rao[0] / stat._nr);
452 stat._rao[1] = sqrt(stat._rao[1] / stat._nr);
453 stat._rao[2] = sqrt(stat._rao[2] / stat._nr);
454 stat._rao3DRMS = stat._rao.NormFrobenius();
455 // orbit values in millimeter
456 if (prn != all.str()) {
457 (_summaryOnly) ? out << " " << prn << ' ':
458 out << "! " << prn << ' ';
459 }
460 else {
461 (_summaryOnly) ? out << " " << QString("%1 ").arg(all.str()[0]).toStdString() << " ":
462 out << "! " << QString("%1 ").arg(all.str()[0]).toStdString() << " ";
463 }
464 out << setw(10) << setprecision(1) << stat._rao[0] * 1e3 << ' '
465 << setw(10) << setprecision(1) << stat._rao[1] * 1e3 << ' '
466 << setw(10) << setprecision(1) << stat._rao[2] * 1e3 << ' '
467 << setw(10) << setprecision(1) << stat._rao3DRMS * 1e3 << ' '
468 << setw( 7) << stat._nr << " ";
469 // clock values in nano seconds
470 if (stat._nc > 0) {
471 stat._dcRMS = sqrt(stat._dcRMS / stat._nc);
472 stat._dcRedRMS = sqrt(stat._dcRedRMS / stat._nc);
473 stat._dcRedSig = sqrt(stat._dcRedSig / stat._nc);
474 out << setw(10) << setprecision(2) << stat._dcRMS / t_CST::c * 1e9 << ' '
475 << setw(10) << setprecision(2) << stat._dcRedRMS / t_CST::c * 1e9 << ' '
476 << setw(10) << setprecision(2) << stat._dcRedSig / t_CST::c * 1e9 << ' '
477 << setw( 9) << stat._nc << " ";
478 if (prn != all.str()) {
479 out << setw( 9) << setprecision(2) << stat._offset / t_CST::c * 1e9;
480 }
481 }
482 out << "\n";
483 }
484 }
485}
486
487//
488////////////////////////////////////////////////////////////////////////////
489bool t_sp3Comp::excludeSat(const t_prn& prn) const {
490 QStringListIterator it(_excludeSats);
491 while (it.hasNext()) {
492 string prnStr = it.next().toLatin1().data();
493 if (prnStr == prn.toString() || // prn
494 prnStr == prn.toString().substr(0,1)) { // sys
495 return true;
496 }
497 }
498 return false;
499}
500
Note: See TracBrowser for help on using the repository browser.