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

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