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

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