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

Last change on this file since 6483 was 6439, checked in by mervart, 10 years ago
File size: 12.8 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("[ ,]"), QString::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("[ ,]"), QString::SkipEmptyParts);
65}
66
67// Destructor
68////////////////////////////////////////////////////////////////////////////
69t_sp3Comp::~t_sp3Comp() {
70 delete _log;
71 delete _logFile;
72}
73
74//
75////////////////////////////////////////////////////////////////////////////
76void t_sp3Comp::run() {
77
78 // Open Log File
79 // -------------
80 _logFile = new QFile(_logFileName);
81 if (_logFile->open(QIODevice::WriteOnly | QIODevice::Text)) {
82 _log = new QTextStream();
83 _log->setDevice(_logFile);
84 }
85 if (!_log) {
86 goto end;
87 }
88
89 for (int ii = 0; ii < _sp3FileNames.size(); ii++) {
90 *_log << "! SP3 File " << ii+1 << ": " << _sp3FileNames[ii] << endl;
91 }
92 if (_sp3FileNames.size() != 2) {
93 *_log << "ERROR: sp3Comp requires two input SP3 files" << endl;
94 goto end;
95 }
96
97 try {
98 ostringstream msg;
99 compare(msg);
100 *_log << msg.str().c_str();
101 }
102 catch (const string& error) {
103 *_log << "ERROR: " << error.c_str() << endl;
104 }
105 catch (const char* error) {
106 *_log << "ERROR: " << error << endl;
107 }
108 catch (Exception& exc) {
109 *_log << "ERROR: " << exc.what() << endl;
110 }
111 catch (std::exception& exc) {
112 *_log << "ERROR: " << exc.what() << endl;
113 }
114 catch (QString error) {
115 *_log << "ERROR: " << error << endl;
116 }
117 catch (...) {
118 *_log << "ERROR: " << "unknown exception" << endl;
119 }
120
121 // Exit (thread)
122 // -------------
123 end:
124 if (BNC_CORE->mode() != t_bncCore::interactive) {
125 qApp->exit(0);
126 }
127 else {
128 emit finished();
129 deleteLater();
130 }
131}
132
133// Satellite Index in clkSats set
134////////////////////////////////////////////////////////////////////////////////
135int t_sp3Comp::satIndex(const set<t_prn>& clkSats, const t_prn& prn) const {
136 int ret = 0;
137 for (set<t_prn>::const_iterator it = clkSats.begin(); it != clkSats.end(); it++) {
138 if ( *it == prn) {
139 return ret;
140 }
141 ++ret;
142 }
143 return -1;
144}
145
146// Estimate Clock Offsets
147////////////////////////////////////////////////////////////////////////////////
148void t_sp3Comp::processClocks(const set<t_prn>& clkSats, const vector<t_epoch*>& epochsIn,
149 map<string, t_stat>& stat) const {
150
151 if (clkSats.size() == 0) {
152 return;
153 }
154
155 vector<t_epoch*> epochs;
156 for (unsigned ii = 0; ii < epochsIn.size(); ii++) {
157 if (epochsIn[ii]->_dc.size() > 0) {
158 epochs.push_back(epochsIn[ii]);
159 }
160 }
161 if (epochs.size() == 0) {
162 return;
163 }
164
165 int nPar = epochs.size() + clkSats.size();
166 SymmetricMatrix NN(nPar); NN = 0.0;
167 ColumnVector bb(nPar); bb = 0.0;
168
169 // Create Matrix A'A and vector b
170 // ------------------------------
171 for (unsigned ie = 0; ie < epochs.size(); ie++) {
172 const map<t_prn, double>& dc = epochs[ie]->_dc;
173 Matrix AA(dc.size(), nPar); AA = 0.0;
174 ColumnVector ll(dc.size()); ll = 0.0;
175 map<t_prn, double>::const_iterator it;
176 int ii = -1;
177 for (it = dc.begin(); it != dc.end(); it++) {
178 const t_prn& prn = it->first;
179 if (satIndex(clkSats, prn) != -1) {
180 ++ii;
181 int index = epochs.size() + satIndex(clkSats, prn);
182 AA[ii][ie] = 1.0; // epoch-specfic offset (common for all satellites)
183 AA[ii][index] = 1.0; // satellite-specific offset (common for all epochs)
184 ll[ii] = it->second;
185 }
186 }
187 SymmetricMatrix dN; dN << AA.t() * AA;
188 NN += dN;
189 bb += AA.t() * ll;
190 }
191
192 // Regularize NN
193 // -------------
194 RowVector HH(nPar);
195 HH.columns(1, epochs.size()) = 0.0;
196 HH.columns(epochs.size()+1, nPar) = 1.0;
197 SymmetricMatrix dN; dN << HH.t() * HH;
198 NN += dN;
199
200 // Estimate Parameters
201 // -------------------
202 ColumnVector xx = NN.i() * bb;
203
204 // Compute clock residuals
205 // -----------------------
206 for (unsigned ie = 0; ie < epochs.size(); ie++) {
207 map<t_prn, double>& dc = epochs[ie]->_dc;
208 for (map<t_prn, double>::iterator it = dc.begin(); it != dc.end(); it++) {
209 const t_prn& prn = it->first;
210 if (satIndex(clkSats, prn) != -1) {
211 int index = epochs.size() + satIndex(clkSats, prn);
212 dc[prn] = it->second - xx[ie] - xx[index];
213 stat[prn.toString()]._offset = xx[index];
214 }
215 }
216 }
217}
218
219// Main Routine
220////////////////////////////////////////////////////////////////////////////////
221void t_sp3Comp::compare(ostringstream& out) const {
222
223 // Synchronize reading of two sp3 files
224 // ------------------------------------
225 bncSP3 in1(_sp3FileNames[0]); in1.nextEpoch();
226 bncSP3 in2(_sp3FileNames[1]); in2.nextEpoch();
227
228 vector<t_epoch*> epochs;
229 while (in1.currEpoch() && in2.currEpoch()) {
230 bncTime t1 = in1.currEpoch()->_tt;
231 bncTime t2 = in2.currEpoch()->_tt;
232 if (t1 < t2) {
233 in1.nextEpoch();
234 }
235 else if (t1 > t2) {
236 in2.nextEpoch();
237 }
238 else if (t1 == t2) {
239 t_epoch* epo = new t_epoch; epo->_tt = t1;
240 bool epochOK = false;
241 for (int i1 = 0; i1 < in1.currEpoch()->_sp3Sat.size(); i1++) {
242 bncSP3::t_sp3Sat* sat1 = in1.currEpoch()->_sp3Sat[i1];
243 for (int i2 = 0; i2 < in2.currEpoch()->_sp3Sat.size(); i2++) {
244 bncSP3::t_sp3Sat* sat2 = in2.currEpoch()->_sp3Sat[i2];
245 if (sat1->_prn == sat2->_prn) {
246 epochOK = true;
247 epo->_dr[sat1->_prn] = sat1->_xyz - sat2->_xyz;
248 epo->_xyz[sat1->_prn] = sat1->_xyz;
249 if (sat1->_clkValid && sat2->_clkValid) {
250 epo->_dc[sat1->_prn] = sat1->_clk - sat2->_clk;
251 }
252 }
253 }
254 }
255 if (epochOK) {
256 epochs.push_back(epo);
257 }
258 else {
259 delete epo;
260 }
261 in1.nextEpoch();
262 in2.nextEpoch();
263 }
264 }
265
266 // Transform xyz into radial, along-track, and out-of-plane
267 // --------------------------------------------------------
268 if (epochs.size() < 2) {
269 throw "t_sp3Comp: not enough common epochs";
270 }
271
272 set<t_prn> clkSatsAll;
273
274 for (unsigned ie = 0; ie < epochs.size(); ie++) {
275 t_epoch* epoch = epochs[ie];
276 t_epoch* epoch2 = 0;
277 if (ie == 0) {
278 epoch2 = epochs[ie+1];
279 }
280 else {
281 epoch2 = epochs[ie-1];
282 }
283 double dt = epoch->_tt - epoch2->_tt;
284 map<t_prn, ColumnVector>& dr = epoch->_dr;
285 map<t_prn, ColumnVector>& xyz = epoch->_xyz;
286 map<t_prn, ColumnVector>& xyz2 = epoch2->_xyz;
287 for (map<t_prn, ColumnVector>::const_iterator it = dr.begin(); it != dr.end(); it++) {
288 const t_prn& prn = it->first;
289 if (xyz2.find(prn) != xyz2.end()) {
290 const ColumnVector dx = dr[prn];
291 const ColumnVector& x1 = xyz[prn];
292 const ColumnVector& x2 = xyz2[prn];
293 ColumnVector vel = (x1 - x2) / dt;
294 XYZ_to_RSW(x1, vel, dx, dr[prn]);
295 if (epoch->_dc.find(prn) != epoch->_dc.end()) {
296 clkSatsAll.insert(prn);
297 }
298 }
299 else {
300 epoch->_dr.erase(prn);
301 epoch->_xyz.erase(prn);
302 epoch->_dc.erase(prn);
303 }
304 }
305 }
306
307 map<string, t_stat> stat;
308
309 // Estimate Clock Offsets
310 // ----------------------
311 string systems;
312 for (set<t_prn>::const_iterator it = clkSatsAll.begin(); it != clkSatsAll.end(); it++) {
313 if (systems.find(it->system()) == string::npos) {
314 systems += it->system();
315 }
316 }
317 for (unsigned iSys = 0; iSys < systems.size(); iSys++) {
318 char system = systems[iSys];
319 set<t_prn> clkSats;
320 for (set<t_prn>::const_iterator it = clkSatsAll.begin(); it != clkSatsAll.end(); it++) {
321 if (it->system() == system && !excludeSat(*it)) {
322 clkSats.insert(*it);
323 }
324 }
325 processClocks(clkSats, epochs, stat);
326 }
327
328 // Print Residuals
329 // ---------------
330 const string all = "ZZZ";
331
332 out.setf(ios::fixed);
333 out << "!\n! MJD PRN radial along out clk clkRed iPRN"
334 "\n! ----------------------------------------------------------------\n";
335 for (unsigned ii = 0; ii < epochs.size(); ii++) {
336 const t_epoch* epo = epochs[ii];
337 const map<t_prn, ColumnVector>& dr = epochs[ii]->_dr;
338 const map<t_prn, double>& dc = epochs[ii]->_dc;
339 for (map<t_prn, ColumnVector>::const_iterator it = dr.begin(); it != dr.end(); it++) {
340 const t_prn& prn = it->first;
341 if (!excludeSat(prn)) {
342 const ColumnVector& rao = it->second;
343 out << setprecision(6) << epo->_tt.mjddec() << ' ' << prn.toString() << ' '
344 << setw(7) << setprecision(4) << rao[0] << ' '
345 << setw(7) << setprecision(4) << rao[1] << ' '
346 << setw(7) << setprecision(4) << rao[2] << " ";
347 stat[prn.toString()]._rao += SP(rao, rao); // Schur product
348 stat[prn.toString()]._nr += 1;
349 stat[all]._rao += SP(rao, rao);
350 stat[all]._nr += 1;
351 if (dc.find(prn) != dc.end()) {
352 double clkRes = dc.find(prn)->second;
353 double clkResRed = clkRes - it->second[0]; // clock minus radial component
354 out << setw(7) << setprecision(4) << clkRes << ' '
355 << setw(7) << setprecision(4) << clkResRed;
356 stat[prn.toString()]._dc += clkRes * clkRes;
357 stat[prn.toString()]._dcRed += clkResRed * clkResRed;
358 stat[prn.toString()]._nc += 1;
359 stat[all]._dc += clkRes * clkRes;
360 stat[all]._dcRed += clkResRed * clkResRed;
361 stat[all]._nc += 1;
362 }
363 else {
364 out << " . . ";
365 }
366 out << " " << setw(2) << int(prn) << endl;
367 }
368 }
369 delete epo;
370 }
371
372 // Print Summary
373 // -------------
374 out << "!\n! RMS:\n";
375 for (map<string, t_stat>::iterator it = stat.begin(); it != stat.end(); it++) {
376 const string& prn = it->first;
377 t_stat& stat = it->second;
378 if (stat._nr > 0) {
379 stat._rao[0] = sqrt(stat._rao[0] / stat._nr);
380 stat._rao[1] = sqrt(stat._rao[1] / stat._nr);
381 stat._rao[2] = sqrt(stat._rao[2] / stat._nr);
382 if (prn == all) {
383 out << "!\n! Total ";
384 }
385 else {
386 out << "! " << prn << ' ';
387 }
388 out << setw(7) << setprecision(4) << stat._rao[0] << ' '
389 << setw(7) << setprecision(4) << stat._rao[1] << ' '
390 << setw(7) << setprecision(4) << stat._rao[2] << " ";
391 if (stat._nc > 0) {
392 stat._dc = sqrt(stat._dc / stat._nc);
393 stat._dcRed = sqrt(stat._dcRed / stat._nc);
394 out << setw(7) << setprecision(4) << stat._dc << ' '
395 << setw(7) << setprecision(4) << stat._dcRed;
396 if (prn != all) {
397 out << " offset " << setw(9) << setprecision(4) << stat._offset;
398 }
399 }
400 else {
401 out << " . . ";
402 }
403 out << endl;
404 }
405 }
406}
407
408//
409////////////////////////////////////////////////////////////////////////////
410bool t_sp3Comp::excludeSat(const t_prn& prn) const {
411 QStringListIterator it(_excludeSats);
412 while (it.hasNext()) {
413 string prnStr = it.next().toAscii().data();
414 if (prnStr == prn.toString() || prnStr == prn.toString().substr(0,1)) {
415 return true;
416 }
417 }
418 return false;
419}
420
Note: See TracBrowser for help on using the repository browser.