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

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