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

Last change on this file since 5268 was 5072, checked in by mervart, 12 years ago
File size: 14.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 "bnccore.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 << QByteArray(78, '-') << endl;
106 *_log << "Concatenation of RINEX Observation and/or Navigation Files\n";
107 *_log << QByteArray(78, '-') << endl;
108
109 *_log << QByteArray("Program").leftJustified(15) << ": "
110 << BNC_CORE->pgmName() << endl;
111 *_log << QByteArray("Run by").leftJustified(15) << ": "
112 << BNC_CORE->userName() << endl;
113 *_log << QByteArray("Date").leftJustified(15) << ": "
114 << QDateTime::currentDateTime().toUTC().toString("yyyy-MM-dd hh:mm:ss") << endl;
115 *_log << QByteArray("RINEX Version").leftJustified(15) << ": "
116 << _rnxVersion << endl;
117 *_log << QByteArray("Sampling").leftJustified(15) << ": "
118 << _samplingRate << endl;
119 *_log << QByteArray("Start time").leftJustified(15) << ": "
120 << _begTime.datestr().c_str() << ' '
121 << _begTime.timestr(0).c_str() << endl;
122 *_log << QByteArray("End time").leftJustified(15) << ": "
123 << _endTime.datestr().c_str() << ' '
124 << _endTime.timestr(0).c_str() << endl;
125 *_log << QByteArray("Input Obs Files").leftJustified(15) << ": "
126 << _obsFileNames.join(",") << endl;
127 *_log << QByteArray("Input Nav Files").leftJustified(15) << ": "
128 << _navFileNames.join(",") << endl;
129 *_log << QByteArray("Output Obs File").leftJustified(15) << ": "
130 << _outObsFileName << endl;
131 *_log << QByteArray("Output Nav File").leftJustified(15) << ": "
132 << _outNavFileName << endl;
133
134 *_log << QByteArray(78, '-') << endl;
135 _log->flush();
136 }
137
138 // Handle Observation Files
139 // ------------------------
140 editObservations();
141
142 // Handle Navigations Files
143 // ------------------------
144 editEphemerides();
145
146 // Exit (thread)
147 // -------------
148 if (BNC_CORE->mode() != t_bncCore::interactive) {
149 qApp->exit(0);
150 }
151 else {
152 emit finished();
153 deleteLater();
154 }
155}
156
157// Initialize input observation files, sort them according to start time
158////////////////////////////////////////////////////////////////////////////
159void t_reqcEdit::initRnxObsFiles(const QStringList& obsFileNames,
160 QVector<t_rnxObsFile*>& rnxObsFiles,
161 QTextStream* log) {
162
163 QStringListIterator it(obsFileNames);
164 while (it.hasNext()) {
165 QString fileName = it.next();
166 if (fileName.indexOf('*') != -1 || fileName.indexOf('?') != -1) {
167 QFileInfo fileInfo(fileName);
168 QDir dir = fileInfo.dir();
169 QStringList filters; filters << fileInfo.fileName();
170 QListIterator<QFileInfo> it(dir.entryInfoList(filters));
171 while (it.hasNext()) {
172 QString filePath = it.next().filePath();
173 t_rnxObsFile* rnxObsFile = 0;
174 try {
175 rnxObsFile = new t_rnxObsFile(filePath, t_rnxObsFile::input);
176 rnxObsFiles.append(rnxObsFile);
177 }
178 catch (...) {
179 delete rnxObsFile;
180 if (log) {
181 *log << "Error in rnxObsFile " << filePath.toAscii().data() << endl;
182 }
183 }
184 }
185 }
186 else {
187 t_rnxObsFile* rnxObsFile = 0;
188 try {
189 rnxObsFile = new t_rnxObsFile(fileName, t_rnxObsFile::input);
190 rnxObsFiles.append(rnxObsFile);
191 }
192 catch (...) {
193 if (log) {
194 *log << "Error in rnxObsFile " << fileName.toAscii().data() << endl;
195 }
196 }
197 }
198 }
199 qStableSort(rnxObsFiles.begin(), rnxObsFiles.end(),
200 t_rnxObsFile::earlierStartTime);
201}
202
203//
204////////////////////////////////////////////////////////////////////////////
205void t_reqcEdit::editObservations() {
206
207 // Easy Exit
208 // ---------
209 if (_obsFileNames.isEmpty() || _outObsFileName.isEmpty()) {
210 return;
211 }
212
213 t_reqcEdit::initRnxObsFiles(_obsFileNames, _rnxObsFiles, _log);
214
215 // Initialize output observation file
216 // ----------------------------------
217 t_rnxObsFile outObsFile(_outObsFileName, t_rnxObsFile::output);
218
219 // Loop over all input observation files
220 // -------------------------------------
221 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
222 t_rnxObsFile* obsFile = _rnxObsFiles[ii];
223 if (_log) {
224 *_log << "Processing File: " << obsFile->fileName() << " start: "
225 << obsFile->startTime().datestr().c_str() << ' '
226 << obsFile->startTime().timestr(0).c_str() << endl;
227 }
228 if (ii == 0) {
229 outObsFile.setHeader(obsFile->header(), _rnxVersion);
230 if (_begTime.valid() && _begTime > outObsFile.startTime()) {
231 outObsFile.setStartTime(_begTime);
232 }
233 if (_samplingRate > outObsFile.interval()) {
234 outObsFile.setInterval(_samplingRate);
235 }
236 editRnxObsHeader(outObsFile);
237 bncSettings settings;
238 QMap<QString, QString> txtMap;
239 QString runBy = settings.value("reqcRunBy").toString();
240 if (!runBy.isEmpty()) {
241 txtMap["RUN BY"] = runBy;
242 }
243 QString comment = settings.value("reqcComment").toString();
244 if (!comment.isEmpty()) {
245 txtMap["COMMENT"] = comment;
246 }
247 outObsFile.header().write(outObsFile.stream(), &txtMap);
248 }
249 else {
250 outObsFile.checkNewHeader(obsFile->header());
251 }
252 t_rnxObsFile::t_rnxEpo* epo = 0;
253 try {
254 while ( (epo = obsFile->nextEpoch()) != 0) {
255 if (_begTime.valid() && epo->tt < _begTime) {
256 continue;
257 }
258 if (_endTime.valid() && epo->tt > _endTime) {
259 break;
260 }
261
262 if (_samplingRate == 0 ||
263 fmod(round(epo->tt.gpssec()), _samplingRate) == 0) {
264 applyLLI(obsFile, epo);
265 outObsFile.writeEpoch(epo);
266 }
267 else {
268 rememberLLI(obsFile, epo);
269 }
270 }
271 }
272 catch (QString str) {
273 if (_log) {
274 *_log << "Exception " << str << endl;
275 }
276 else {
277 qDebug() << str;
278 }
279 return;
280 }
281 }
282}
283
284// Change RINEX Header Content
285////////////////////////////////////////////////////////////////////////////
286void t_reqcEdit::editRnxObsHeader(t_rnxObsFile& obsFile) {
287
288 bncSettings settings;
289
290 QString oldMarkerName = settings.value("reqcOldMarkerName").toString();
291 QString newMarkerName = settings.value("reqcNewMarkerName").toString();
292 if (!newMarkerName.isEmpty()) {
293 if (oldMarkerName.isEmpty() ||
294 QRegExp(oldMarkerName).exactMatch(obsFile.markerName())) {
295 obsFile.setMarkerName(newMarkerName);
296 }
297 }
298
299 QString oldAntennaName = settings.value("reqcOldAntennaName").toString();
300 QString newAntennaName = settings.value("reqcNewAntennaName").toString();
301 if (!newAntennaName.isEmpty()) {
302 if (oldAntennaName.isEmpty() ||
303 QRegExp(oldAntennaName).exactMatch(obsFile.antennaName())) {
304 obsFile.setAntennaName(newAntennaName);
305 }
306 }
307
308 QString oldReceiverType = settings.value("reqcOldReceiverName").toString();
309 QString newReceiverType = settings.value("reqcNewReceiverName").toString();
310 if (!newReceiverType.isEmpty()) {
311 if (oldReceiverType.isEmpty() ||
312 QRegExp(oldReceiverType).exactMatch(obsFile.receiverType())) {
313 obsFile.setReceiverType(newReceiverType);
314 }
315 }
316}
317
318//
319////////////////////////////////////////////////////////////////////////////
320void t_reqcEdit::rememberLLI(const t_rnxObsFile* obsFile,
321 const t_rnxObsFile::t_rnxEpo* epo) {
322
323 if (_samplingRate == 0) {
324 return;
325 }
326
327 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
328 const t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iSat];
329 char sys = rnxSat.satSys;
330 QString prn = QString("%1%2").arg(sys).arg(rnxSat.satNum,2,10,QChar('0'));
331
332 for (int iType = 0; iType < obsFile->nTypes(sys); iType++) {
333 if (!_lli[prn].contains(iType)) {
334 _lli[prn][iType] = 0;
335 }
336 if (rnxSat.lli[iType] & 1) {
337 _lli[prn][iType] |= 1;
338 }
339 }
340 }
341}
342
343//
344////////////////////////////////////////////////////////////////////////////
345void t_reqcEdit::applyLLI(const t_rnxObsFile* obsFile,
346 t_rnxObsFile::t_rnxEpo* epo) {
347
348 if (_samplingRate == 0) {
349 return;
350 }
351
352 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
353 t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iSat];
354 char sys = rnxSat.satSys;
355 QString prn = QString("%1%2").arg(sys).arg(rnxSat.satNum,2,10,QChar('0'));
356
357 for (int iType = 0; iType < obsFile->nTypes(sys); iType++) {
358 if (_lli[prn].contains(iType) && _lli[prn][iType] & 1) {
359 rnxSat.lli[iType] |= 1;
360 }
361 }
362 }
363
364 _lli.clear();
365}
366
367/// Read All Ephemerides
368////////////////////////////////////////////////////////////////////////////
369void t_reqcEdit::readEphemerides(const QStringList& navFileNames,
370 QVector<t_eph*>& ephs) {
371
372 QStringListIterator it(navFileNames);
373 while (it.hasNext()) {
374 QString fileName = it.next();
375 if (fileName.indexOf('*') != -1 || fileName.indexOf('?') != -1) {
376 QFileInfo fileInfo(fileName);
377 QDir dir = fileInfo.dir();
378 QStringList filters; filters << fileInfo.fileName();
379 QListIterator<QFileInfo> it(dir.entryInfoList(filters));
380 while (it.hasNext()) {
381 QString filePath = it.next().filePath();
382 appendEphemerides(filePath, ephs);
383 }
384 }
385 else {
386 appendEphemerides(fileName, ephs);
387 }
388 }
389 qStableSort(ephs.begin(), ephs.end(), t_eph::earlierTime);
390}
391
392//
393////////////////////////////////////////////////////////////////////////////
394void t_reqcEdit::editEphemerides() {
395
396 // Easy Exit
397 // ---------
398 if (_navFileNames.isEmpty() || _outNavFileName.isEmpty()) {
399 return;
400 }
401
402 // Read Ephemerides
403 // ----------------
404 t_reqcEdit::readEphemerides(_navFileNames, _ephs);
405
406 // Check Satellite Systems
407 // -----------------------
408 bool haveGPS = false;
409 bool haveGlonass = false;
410 for (int ii = 0; ii < _ephs.size(); ii++) {
411 const t_eph* eph = _ephs[ii];
412 if (eph->type() == t_eph::GPS) {
413 haveGPS = true;
414 }
415 else if (eph->type() == t_eph::GLONASS) {
416 haveGlonass = true;
417 }
418 }
419
420 // Initialize output navigation file
421 // ---------------------------------
422 t_rnxNavFile outNavFile(_outNavFileName, t_rnxNavFile::output);
423
424 outNavFile.setGlonass(haveGlonass);
425
426 if (haveGPS && haveGlonass) {
427 outNavFile.setVersion(rnxV3);
428 }
429 else {
430 outNavFile.setVersion(_rnxVersion);
431 }
432
433 bncSettings settings;
434 QMap<QString, QString> txtMap;
435 QString runBy = settings.value("reqcRunBy").toString();
436 if (!runBy.isEmpty()) {
437 txtMap["RUN BY"] = runBy;
438 }
439 QString comment = settings.value("reqcComment").toString();
440 if (!comment.isEmpty()) {
441 txtMap["COMMENT"] = comment;
442 }
443
444 outNavFile.writeHeader(&txtMap);
445
446 // Loop over all ephemerides
447 // -------------------------
448 for (int ii = 0; ii < _ephs.size(); ii++) {
449 const t_eph* eph = _ephs[ii];
450 outNavFile.writeEph(eph);
451 }
452}
453
454//
455////////////////////////////////////////////////////////////////////////////
456void t_reqcEdit::appendEphemerides(const QString& fileName,
457 QVector<t_eph*>& ephs) {
458
459 t_rnxNavFile rnxNavFile(fileName, t_rnxNavFile::input);
460 for (unsigned ii = 0; ii < rnxNavFile.ephs().size(); ii++) {
461 t_eph* eph = rnxNavFile.ephs()[ii];
462 bool isNew = true;
463 for (int iOld = 0; iOld < ephs.size(); iOld++) {
464 const t_eph* ephOld = ephs[iOld];
465 if (ephOld->prn() == eph->prn() && ephOld->TOC() == eph->TOC()) {
466 isNew = false;
467 break;
468 }
469 }
470 if (isNew) {
471 if (eph->type() == t_eph::GPS) {
472 ephs.append(new t_ephGPS(*dynamic_cast<t_ephGPS*>(eph)));
473 }
474 else if (eph->type() == t_eph::GLONASS) {
475 ephs.append(new t_ephGlo(*dynamic_cast<t_ephGlo*>(eph)));
476 }
477 else if (eph->type() == t_eph::Galileo) {
478 ephs.append(new t_ephGal(*dynamic_cast<t_ephGal*>(eph)));
479 }
480 }
481 }
482}
Note: See TracBrowser for help on using the repository browser.