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

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