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

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