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

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