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

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