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

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