source: ntrip/trunk/BNC/src/rinex/reqcedit.cpp@ 4518

Last change on this file since 4518 was 4518, checked in by mervart, 12 years ago
File size: 12.9 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_reqcEdit
30 *
31 * Purpose: Edit/Concatenate RINEX Files
32 *
33 * Author: L. Mervart
34 *
35 * Created: 11-Apr-2012
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42#include "reqcedit.h"
43#include "bncapp.h"
44#include "bncsettings.h"
45#include "bncutils.h"
46
47using namespace std;
48
49const double rnxV2 = 2.11;
50const double rnxV3 = 3.01;
51
52// Constructor
53////////////////////////////////////////////////////////////////////////////
54t_reqcEdit::t_reqcEdit(QObject* parent) : QThread(parent) {
55
56 bncSettings settings;
57
58 _logFileName = settings.value("reqcOutLogFile").toString(); expandEnvVar(_logFileName);
59 _logFile = 0;
60 _log = 0;
61 _obsFileNames = settings.value("reqcObsFile").toString().split(",", QString::SkipEmptyParts);
62 _outObsFileName = settings.value("reqcOutObsFile").toString();
63 _navFileNames = settings.value("reqcNavFile").toString().split(",", QString::SkipEmptyParts);
64 _outNavFileName = settings.value("reqcOutNavFile").toString();
65 int version = settings.value("reqcRnxVersion").toInt();
66 if (version < 3) {
67 _rnxVersion = rnxV2;
68 }
69 else {
70 _rnxVersion = rnxV3;
71 }
72 _samplingRate = settings.value("reqcSampling").toInt();
73 _begTime = bncTime(settings.value("reqcStartDateTime").toString().toAscii().data());
74 _endTime = bncTime(settings.value("reqcEndDateTime").toString().toAscii().data());
75}
76
77// Destructor
78////////////////////////////////////////////////////////////////////////////
79t_reqcEdit::~t_reqcEdit() {
80 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
81 delete _rnxObsFiles[ii];
82 }
83 for (int ii = 0; ii < _ephs.size(); ii++) {
84 delete _ephs[ii];
85 }
86 delete _log; _log = 0;
87 delete _logFile; _logFile = 0;
88}
89
90//
91////////////////////////////////////////////////////////////////////////////
92void t_reqcEdit::run() {
93
94 // Open Log File
95 // -------------
96 _logFile = new QFile(_logFileName);
97 if (_logFile->open(QIODevice::WriteOnly | QIODevice::Text)) {
98 _log = new QTextStream();
99 _log->setDevice(_logFile);
100 }
101
102 // Log File Header
103 // ---------------
104 if (_log) {
105 *_log << "Concatenation of RINEX Observation and/or Navigation Files\n";
106 _log->flush();
107 }
108
109 // Handle Observation Files
110 // ------------------------
111 editObservations();
112
113 // Handle Navigations Files
114 // ------------------------
115 editEphemerides();
116
117 // Exit (thread)
118 // -------------
119 bncApp* app = (bncApp*) qApp;
120 if ( app->mode() != bncApp::interactive) {
121 app->exit(0);
122 }
123 else {
124 emit finished();
125 deleteLater();
126 }
127}
128
129// Initialize input observation files, sort them according to start time
130////////////////////////////////////////////////////////////////////////////
131void t_reqcEdit::initRnxObsFiles(const QStringList& obsFileNames,
132 QVector<t_rnxObsFile*>& rnxObsFiles) {
133
134 QStringListIterator it(obsFileNames);
135 while (it.hasNext()) {
136 QString fileName = it.next();
137 if (fileName.indexOf('*') != -1 || fileName.indexOf('?') != -1) {
138 QFileInfo fileInfo(fileName);
139 QDir dir = fileInfo.dir();
140 QStringList filters; filters << fileInfo.fileName();
141 QListIterator<QFileInfo> it(dir.entryInfoList(filters));
142 while (it.hasNext()) {
143 QString filePath = it.next().filePath();
144 t_rnxObsFile* rnxObsFile = 0;
145 try {
146 rnxObsFile = new t_rnxObsFile(filePath, t_rnxObsFile::input);
147 rnxObsFiles.append(rnxObsFile);
148 }
149 catch (...) {
150 delete rnxObsFile;
151 cerr << "Error in rnxObsFile " << filePath.toAscii().data() << endl;
152 }
153 }
154 }
155 else {
156 t_rnxObsFile* rnxObsFile = 0;
157 try {
158 rnxObsFile = new t_rnxObsFile(fileName, t_rnxObsFile::input);
159 rnxObsFiles.append(rnxObsFile);
160 }
161 catch (...) {
162 cerr << "Error in rnxObsFile " << fileName.toAscii().data() << endl;
163 }
164 }
165 }
166 qStableSort(rnxObsFiles.begin(), rnxObsFiles.end(),
167 t_rnxObsFile::earlierStartTime);
168}
169
170//
171////////////////////////////////////////////////////////////////////////////
172void t_reqcEdit::editObservations() {
173
174 // Easy Exit
175 // ---------
176 if (_obsFileNames.isEmpty() || _outObsFileName.isEmpty()) {
177 return;
178 }
179
180 t_reqcEdit::initRnxObsFiles(_obsFileNames, _rnxObsFiles);
181
182 // Initialize output observation file
183 // ----------------------------------
184 t_rnxObsFile outObsFile(_outObsFileName, t_rnxObsFile::output);
185
186 // Loop over all input observation files
187 // -------------------------------------
188 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
189 t_rnxObsFile* obsFile = _rnxObsFiles[ii];
190 if (ii == 0) {
191 outObsFile.setHeader(obsFile->header(), _rnxVersion);
192 if (_begTime.valid() && _begTime > outObsFile.startTime()) {
193 outObsFile.setStartTime(_begTime);
194 }
195 if (_samplingRate > outObsFile.interval()) {
196 outObsFile.setInterval(_samplingRate);
197 }
198 editRnxObsHeader(outObsFile);
199 bncSettings settings;
200 QMap<QString, QString> txtMap;
201 QString runBy = settings.value("reqcRunBy").toString();
202 if (!runBy.isEmpty()) {
203 txtMap["RUN BY"] = runBy;
204 }
205 QString comment = settings.value("reqcComment").toString();
206 if (!comment.isEmpty()) {
207 txtMap["COMMENT"] = comment;
208 }
209 outObsFile.header().write(outObsFile.stream(), &txtMap);
210 }
211 else {
212 outObsFile.checkNewHeader(obsFile->header());
213 }
214 t_rnxObsFile::t_rnxEpo* epo = 0;
215 while ( (epo = obsFile->nextEpoch()) != 0) {
216 if (_begTime.valid() && epo->tt < _begTime) {
217 continue;
218 }
219 if (_endTime.valid() && epo->tt > _endTime) {
220 break;
221 }
222
223 if (_samplingRate == 0 ||
224 fmod(round(epo->tt.gpssec()), _samplingRate) == 0) {
225 applyLLI(obsFile, epo);
226 outObsFile.writeEpoch(epo);
227 }
228 else {
229 rememberLLI(obsFile, epo);
230 }
231 }
232 }
233}
234
235// Change RINEX Header Content
236////////////////////////////////////////////////////////////////////////////
237void t_reqcEdit::editRnxObsHeader(t_rnxObsFile& obsFile) {
238
239 bncSettings settings;
240
241 QString oldMarkerName = settings.value("reqcOldMarkerName").toString();
242 QString newMarkerName = settings.value("reqcNewMarkerName").toString();
243 if (!newMarkerName.isEmpty()) {
244 if (oldMarkerName.isEmpty() ||
245 QRegExp(oldMarkerName).exactMatch(obsFile.markerName())) {
246 obsFile.setMarkerName(newMarkerName);
247 }
248 }
249
250 QString oldAntennaName = settings.value("reqcOldAntennaName").toString();
251 QString newAntennaName = settings.value("reqcNewAntennaName").toString();
252 if (!newAntennaName.isEmpty()) {
253 if (oldAntennaName.isEmpty() ||
254 QRegExp(oldAntennaName).exactMatch(obsFile.antennaName())) {
255 obsFile.setAntennaName(newAntennaName);
256 }
257 }
258
259 QString oldReceiverType = settings.value("reqcOldReceiverName").toString();
260 QString newReceiverType = settings.value("reqcNewReceiverName").toString();
261 if (!newReceiverType.isEmpty()) {
262 if (oldReceiverType.isEmpty() ||
263 QRegExp(oldReceiverType).exactMatch(obsFile.receiverType())) {
264 obsFile.setReceiverType(newReceiverType);
265 }
266 }
267}
268
269//
270////////////////////////////////////////////////////////////////////////////
271void t_reqcEdit::rememberLLI(const t_rnxObsFile* obsFile,
272 const t_rnxObsFile::t_rnxEpo* epo) {
273
274 if (_samplingRate == 0) {
275 return;
276 }
277
278 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
279 const t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iSat];
280 char sys = rnxSat.satSys;
281 QString prn = QString("%1%2").arg(sys).arg(rnxSat.satNum,2,10,QChar('0'));
282
283 for (int iType = 0; iType < obsFile->nTypes(sys); iType++) {
284 if (!_lli[prn].contains(iType)) {
285 _lli[prn][iType] = 0;
286 }
287 if (rnxSat.lli[iType] & 1) {
288 _lli[prn][iType] |= 1;
289 }
290 }
291 }
292}
293
294//
295////////////////////////////////////////////////////////////////////////////
296void t_reqcEdit::applyLLI(const t_rnxObsFile* obsFile,
297 t_rnxObsFile::t_rnxEpo* epo) {
298
299 if (_samplingRate == 0) {
300 return;
301 }
302
303 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
304 t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iSat];
305 char sys = rnxSat.satSys;
306 QString prn = QString("%1%2").arg(sys).arg(rnxSat.satNum,2,10,QChar('0'));
307
308 for (int iType = 0; iType < obsFile->nTypes(sys); iType++) {
309 if (_lli[prn].contains(iType) && _lli[prn][iType] & 1) {
310 rnxSat.lli[iType] |= 1;
311 }
312 }
313 }
314
315 _lli.clear();
316}
317
318/// Read All Ephemerides
319////////////////////////////////////////////////////////////////////////////
320void t_reqcEdit::readEphemerides(const QStringList& navFileNames,
321 QVector<t_eph*>& ephs) {
322
323 QStringListIterator it(navFileNames);
324 while (it.hasNext()) {
325 QString fileName = it.next();
326 if (fileName.indexOf('*') != -1 || fileName.indexOf('?') != -1) {
327 QFileInfo fileInfo(fileName);
328 QDir dir = fileInfo.dir();
329 QStringList filters; filters << fileInfo.fileName();
330 QListIterator<QFileInfo> it(dir.entryInfoList(filters));
331 while (it.hasNext()) {
332 QString filePath = it.next().filePath();
333 appendEphemerides(filePath, ephs);
334 }
335 }
336 else {
337 appendEphemerides(fileName, ephs);
338 }
339 }
340 qStableSort(ephs.begin(), ephs.end(), t_eph::earlierTime);
341}
342
343//
344////////////////////////////////////////////////////////////////////////////
345void t_reqcEdit::editEphemerides() {
346
347 // Easy Exit
348 // ---------
349 if (_navFileNames.isEmpty() || _outNavFileName.isEmpty()) {
350 return;
351 }
352
353 // Read Ephemerides
354 // ----------------
355 t_reqcEdit::readEphemerides(_navFileNames, _ephs);
356
357 // Check Satellite Systems
358 // -----------------------
359 bool haveGPS = false;
360 bool haveGlonass = false;
361 for (int ii = 0; ii < _ephs.size(); ii++) {
362 const t_eph* eph = _ephs[ii];
363 if (eph->type() == t_eph::GPS) {
364 haveGPS = true;
365 }
366 else if (eph->type() == t_eph::GLONASS) {
367 haveGlonass = true;
368 }
369 }
370
371 // Initialize output navigation file
372 // ---------------------------------
373 t_rnxNavFile outNavFile(_outNavFileName, t_rnxNavFile::output);
374
375 outNavFile.setGlonass(haveGlonass);
376
377 if (haveGPS && haveGlonass) {
378 outNavFile.setVersion(rnxV3);
379 }
380 else {
381 outNavFile.setVersion(_rnxVersion);
382 }
383
384 bncSettings settings;
385 QMap<QString, QString> txtMap;
386 QString runBy = settings.value("reqcRunBy").toString();
387 if (!runBy.isEmpty()) {
388 txtMap["RUN BY"] = runBy;
389 }
390 QString comment = settings.value("reqcComment").toString();
391 if (!comment.isEmpty()) {
392 txtMap["COMMENT"] = comment;
393 }
394
395 outNavFile.writeHeader(&txtMap);
396
397 // Loop over all ephemerides
398 // -------------------------
399 for (int ii = 0; ii < _ephs.size(); ii++) {
400 const t_eph* eph = _ephs[ii];
401 outNavFile.writeEph(eph);
402 }
403}
404
405//
406////////////////////////////////////////////////////////////////////////////
407void t_reqcEdit::appendEphemerides(const QString& fileName,
408 QVector<t_eph*>& ephs) {
409
410 t_rnxNavFile rnxNavFile(fileName, t_rnxNavFile::input);
411 for (unsigned ii = 0; ii < rnxNavFile.ephs().size(); ii++) {
412 t_eph* eph = rnxNavFile.ephs()[ii];
413 bool isNew = true;
414 for (int iOld = 0; iOld < ephs.size(); iOld++) {
415 const t_eph* ephOld = ephs[iOld];
416 if (ephOld->prn() == eph->prn() && ephOld->TOC() == eph->TOC()) {
417 isNew = false;
418 break;
419 }
420 }
421 if (isNew) {
422 if (eph->type() == t_eph::GPS) {
423 ephs.append(new t_ephGPS(*dynamic_cast<t_ephGPS*>(eph)));
424 }
425 else if (eph->type() == t_eph::GLONASS) {
426 ephs.append(new t_ephGlo(*dynamic_cast<t_ephGlo*>(eph)));
427 }
428 else if (eph->type() == t_eph::Galileo) {
429 ephs.append(new t_ephGal(*dynamic_cast<t_ephGal*>(eph)));
430 }
431 }
432 }
433}
Note: See TracBrowser for help on using the repository browser.