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

Last change on this file since 7170 was 6954, checked in by stuerze, 9 years ago

minor changes regarding windowing for RINEX navigation data

File size: 19.8 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
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 // Select observation types
220 // ------------------------
221 bncSettings settings;
222 QStringList useObsTypes = settings.value("reqcUseObsTypes").toString().split(" ", QString::SkipEmptyParts);
223
224 // Put together all observation types
225 // ----------------------------------
226 if (_rnxObsFiles.size() > 1 && useObsTypes.size() == 0) {
227 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
228 t_rnxObsFile* obsFile = _rnxObsFiles[ii];
229 for (int iSys = 0; iSys < obsFile->numSys(); iSys++) {
230 char sys = obsFile->system(iSys);
231 if (sys != ' ') {
232 for (int iType = 0; iType < obsFile->nTypes(sys); iType++) {
233 QString type = obsFile->obsType(sys, iType);
234 if (_rnxVersion < 3.0) {
235 useObsTypes << type;
236 }
237 else {
238 useObsTypes << QString(sys) + ":" + type;
239 }
240 }
241 }
242 }
243 }
244 useObsTypes.removeDuplicates();
245 }
246
247 // Put together all phase shifts
248 // -----------------------------
249 QStringList phaseShifts;
250 if (_rnxVersion >= 3.0 && _rnxObsFiles.size() > 1) {
251 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
252 t_rnxObsFile* obsFile = _rnxObsFiles[ii];
253 phaseShifts << obsFile->phaseShifts();
254 }
255 phaseShifts.removeDuplicates();
256 }
257
258 // Put together all GLONASS biases
259 // -------------------------------
260 QStringList gloBiases;
261 if (_rnxVersion >= 3.0 && _rnxObsFiles.size() > 1) {
262 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
263 t_rnxObsFile* obsFile = _rnxObsFiles[ii];
264 if (ii == 0 && obsFile->numGloBiases() == 4) {
265 break;
266 }
267 else {
268 gloBiases << obsFile->gloBiases();
269 }
270 }
271 gloBiases.removeDuplicates();
272 }
273
274 // Put together all GLONASS slots
275 // -----------------------------
276 QStringList gloSlots;
277 if (_rnxVersion >= 3.0 && _rnxObsFiles.size() > 1) {
278 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
279 t_rnxObsFile* obsFile = _rnxObsFiles[ii];
280 if (ii == 0 &&
281 obsFile->numGloSlots() == signed(t_prn::MAXPRN_GLONASS)) {
282 break;
283 }
284 else {
285 gloSlots << obsFile->gloSlots();
286 }
287 }
288 gloSlots.removeDuplicates();
289 }
290
291 // Loop over all input observation files
292 // -------------------------------------
293 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
294 t_rnxObsFile* obsFile = _rnxObsFiles[ii];
295 if (_log) {
296 *_log << "Processing File: " << obsFile->fileName() << " start: "
297 << obsFile->startTime().datestr().c_str() << ' '
298 << obsFile->startTime().timestr(0).c_str() << endl;
299 }
300 if (ii == 0) {
301 outObsFile.setHeader(obsFile->header(), int(_rnxVersion), &useObsTypes,
302 &phaseShifts, &gloBiases, &gloSlots);
303 if (_begTime.valid() && _begTime > outObsFile.startTime()) {
304 outObsFile.setStartTime(_begTime);
305 }
306 if (_samplingRate > outObsFile.interval()) {
307 outObsFile.setInterval(_samplingRate);
308 }
309 editRnxObsHeader(outObsFile);
310 bncSettings settings;
311 QMap<QString, QString> txtMap;
312 QString runBy = settings.value("reqcRunBy").toString();
313 if (!runBy.isEmpty()) {
314 txtMap["RUN BY"] = runBy;
315 }
316 QString comment = settings.value("reqcComment").toString();
317 if (!comment.isEmpty()) {
318 txtMap["COMMENT"] = comment;
319 }
320 outObsFile.header().write(outObsFile.stream(), &txtMap);
321 }
322 t_rnxObsFile::t_rnxEpo* epo = 0;
323 try {
324 while ( (epo = obsFile->nextEpoch()) != 0) {
325 if (_begTime.valid() && epo->tt < _begTime) {
326 continue;
327 }
328 if (_endTime.valid() && epo->tt > _endTime) {
329 break;
330 }
331
332 if (_samplingRate == 0 ||
333 fmod(round(epo->tt.gpssec()), _samplingRate) == 0) {
334 applyLLI(obsFile, epo);
335 outObsFile.writeEpoch(epo);
336 }
337 else {
338 rememberLLI(obsFile, epo);
339 }
340 }
341 }
342 catch (QString str) {
343 if (_log) {
344 *_log << "Exception " << str << endl;
345 }
346 else {
347 qDebug() << str;
348 }
349 return;
350 }
351 catch (...) {
352 if (_log) {
353 *_log << "Exception unknown" << endl;
354 }
355 else {
356 qDebug() << "Exception unknown";
357 }
358 return;
359 }
360 }
361}
362
363// Change RINEX Header Content
364////////////////////////////////////////////////////////////////////////////
365void t_reqcEdit::editRnxObsHeader(t_rnxObsFile& obsFile) {
366
367 bncSettings settings;
368
369 QString oldMarkerName = settings.value("reqcOldMarkerName").toString();
370 QString newMarkerName = settings.value("reqcNewMarkerName").toString();
371 if (!newMarkerName.isEmpty()) {
372 if (oldMarkerName.isEmpty() ||
373 QRegExp(oldMarkerName).exactMatch(obsFile.markerName())) {
374 obsFile.setMarkerName(newMarkerName);
375 }
376 }
377
378 QString oldAntennaName = settings.value("reqcOldAntennaName").toString();
379 QString newAntennaName = settings.value("reqcNewAntennaName").toString();
380 if (!newAntennaName.isEmpty()) {
381 if (oldAntennaName.isEmpty() ||
382 QRegExp(oldAntennaName).exactMatch(obsFile.antennaName())) {
383 obsFile.setAntennaName(newAntennaName);
384 }
385 }
386
387 QString oldAntennaNumber = settings.value("reqcOldAntennaNumber").toString();
388 QString newAntennaNumber = settings.value("reqcNewAntennaNumber").toString();
389 if (!newAntennaNumber.isEmpty()) {
390 if (oldAntennaNumber.isEmpty() ||
391 QRegExp(oldAntennaNumber).exactMatch(obsFile.antennaNumber())) {
392 obsFile.setAntennaNumber(newAntennaNumber);
393 }
394 }
395
396
397 const ColumnVector& obsFileAntNEU = obsFile.antNEU();
398 QString oldAntennadN = settings.value("reqcOldAntennadN").toString();
399 QString newAntennadN = settings.value("reqcNewAntennadN").toString();
400 if(!newAntennadN.isEmpty()) {
401 if (oldAntennadN.isEmpty() ||
402 oldAntennadN.toDouble() == obsFileAntNEU(1)) {
403 obsFile.setAntennaN(newAntennadN.toDouble());
404 }
405 }
406 QString oldAntennadE = settings.value("reqcOldAntennadE").toString();
407 QString newAntennadE = settings.value("reqcNewAntennadE").toString();
408 if(!newAntennadE.isEmpty()) {
409 if (oldAntennadE.isEmpty() ||
410 oldAntennadE.toDouble() == obsFileAntNEU(2)) {
411 obsFile.setAntennaE(newAntennadE.toDouble());
412 }
413 }
414 QString oldAntennadU = settings.value("reqcOldAntennadU").toString();
415 QString newAntennadU = settings.value("reqcNewAntennadU").toString();
416 if(!newAntennadU.isEmpty()) {
417 if (oldAntennadU.isEmpty() ||
418 oldAntennadU.toDouble() == obsFileAntNEU(3)) {
419 obsFile.setAntennaU(newAntennadU.toDouble());
420 }
421 }
422
423 QString oldReceiverType = settings.value("reqcOldReceiverName").toString();
424 QString newReceiverType = settings.value("reqcNewReceiverName").toString();
425 if (!newReceiverType.isEmpty()) {
426 if (oldReceiverType.isEmpty() ||
427 QRegExp(oldReceiverType).exactMatch(obsFile.receiverType())) {
428 obsFile.setReceiverType(newReceiverType);
429 }
430 }
431
432 QString oldReceiverNumber = settings.value("reqcOldReceiverNumber").toString();
433 QString newReceiverNumber = settings.value("reqcNewReceiverNumber").toString();
434 if (!newReceiverNumber.isEmpty()) {
435 if (oldReceiverNumber.isEmpty() ||
436 QRegExp(oldReceiverNumber).exactMatch(obsFile.receiverNumber())) {
437 obsFile.setReceiverNumber(newReceiverNumber);
438 }
439 }
440}
441
442//
443////////////////////////////////////////////////////////////////////////////
444void t_reqcEdit::rememberLLI(const t_rnxObsFile* obsFile,
445 const t_rnxObsFile::t_rnxEpo* epo) {
446
447 if (_samplingRate == 0) {
448 return;
449 }
450
451 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
452 const t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iSat];
453 char sys = rnxSat.prn.system();
454 QString prn(rnxSat.prn.toString().c_str());
455
456 for (int iType = 0; iType < obsFile->nTypes(sys); iType++) {
457 QString type = obsFile->obsType(sys, iType);
458 if (!_lli[prn].contains(iType)) {
459 _lli[prn][iType] = 0;
460 }
461 if (rnxSat.obs.contains(type) && rnxSat.obs[type].lli & 1) {
462 _lli[prn][iType] |= 1;
463 }
464 }
465 }
466}
467
468//
469////////////////////////////////////////////////////////////////////////////
470void t_reqcEdit::applyLLI(const t_rnxObsFile* obsFile,
471 t_rnxObsFile::t_rnxEpo* epo) {
472
473 if (_samplingRate == 0) {
474 return;
475 }
476
477 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
478 t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iSat];
479 char sys = rnxSat.prn.system();
480 QString prn(rnxSat.prn.toString().c_str());
481
482 for (int iType = 0; iType < obsFile->nTypes(sys); iType++) {
483 QString type = obsFile->obsType(sys, iType);
484 if (_lli[prn].contains(iType) && _lli[prn][iType] & 1) {
485 if (rnxSat.obs.contains(type)) {
486 rnxSat.obs[type].lli |= 1;
487 }
488 }
489 }
490 }
491
492 _lli.clear();
493}
494
495/// Read All Ephemerides
496////////////////////////////////////////////////////////////////////////////
497void t_reqcEdit::readEphemerides(const QStringList& navFileNames,
498 QVector<t_eph*>& ephs) {
499
500 QStringListIterator it(navFileNames);
501 while (it.hasNext()) {
502 QString fileName = it.next();
503 if (fileName.indexOf('*') != -1 || fileName.indexOf('?') != -1) {
504 QFileInfo fileInfo(fileName);
505 QDir dir = fileInfo.dir();
506 QStringList filters; filters << fileInfo.fileName();
507 QListIterator<QFileInfo> it(dir.entryInfoList(filters));
508 while (it.hasNext()) {
509 QString filePath = it.next().filePath();
510 appendEphemerides(filePath, ephs);
511 }
512 }
513 else {
514 appendEphemerides(fileName, ephs);
515 }
516 }
517 qStableSort(ephs.begin(), ephs.end(), t_eph::earlierTime);
518}
519
520//
521////////////////////////////////////////////////////////////////////////////
522void t_reqcEdit::editEphemerides() {
523
524 // Easy Exit
525 // ---------
526 if (_navFileNames.isEmpty() || _outNavFileName.isEmpty()) {
527 return;
528 }
529
530 // Read Ephemerides
531 // ----------------
532 t_reqcEdit::readEphemerides(_navFileNames, _ephs);
533
534 // Check Satellite Systems
535 // -----------------------
536 bool haveGPS = false;
537 bool haveGlonass = false;
538 for (int ii = 0; ii < _ephs.size(); ii++) {
539 const t_eph* eph = _ephs[ii];
540 if (eph->type() == t_eph::GPS) {
541 haveGPS = true;
542 }
543 else if (eph->type() == t_eph::GLONASS) {
544 haveGlonass = true;
545 }
546 }
547
548 // Initialize output navigation file
549 // ---------------------------------
550 t_rnxNavFile outNavFile(_outNavFileName, t_rnxNavFile::output);
551
552 outNavFile.setGlonass(haveGlonass);
553
554 if ( (haveGPS && haveGlonass) || _rnxVersion >= 3.0) {
555 outNavFile.setVersion(t_rnxNavFile::defaultRnxNavVersion3);
556 }
557 else {
558 outNavFile.setVersion(t_rnxNavFile::defaultRnxNavVersion2);
559 }
560
561 bncSettings settings;
562 QMap<QString, QString> txtMap;
563 QString runBy = settings.value("reqcRunBy").toString();
564 if (!runBy.isEmpty()) {
565 txtMap["RUN BY"] = runBy;
566 }
567 QString comment = settings.value("reqcComment").toString();
568 if (!comment.isEmpty()) {
569 txtMap["COMMENT"] = comment;
570 }
571
572 outNavFile.writeHeader(&txtMap);
573
574 // Loop over all ephemerides
575 // -------------------------
576 for (int ii = 0; ii < _ephs.size(); ii++) {
577 const t_eph* eph = _ephs[ii];
578 bncTime begTime = _begTime;
579 bncTime endTime = _endTime;
580 if (eph->type() == t_eph::BDS) {
581 begTime += 14;
582 endTime += 14;
583 }
584 if (begTime.valid() && eph->TOC() < begTime) {
585 continue;
586 }
587 if (endTime.valid() && eph->TOC() > endTime) {
588 break;
589 }
590 outNavFile.writeEph(eph);
591 }
592}
593
594//
595////////////////////////////////////////////////////////////////////////////
596void t_reqcEdit::appendEphemerides(const QString& fileName,
597 QVector<t_eph*>& ephs) {
598
599 t_rnxNavFile rnxNavFile(fileName, t_rnxNavFile::input);
600 for (unsigned ii = 0; ii < rnxNavFile.ephs().size(); ii++) {
601 t_eph* eph = rnxNavFile.ephs()[ii];
602 bool isNew = true;
603 for (int iOld = 0; iOld < ephs.size(); iOld++) {
604 const t_eph* ephOld = ephs[iOld];
605 if (ephOld->prn() == eph->prn() &&
606 ephOld->TOC() == eph->TOC()) {
607 isNew = false;
608 break;
609 }
610 }
611 if (isNew) {
612 if (eph->type() == t_eph::GPS) {
613 ephs.append(new t_ephGPS(*dynamic_cast<t_ephGPS*>(eph)));
614 }
615 else if (eph->type() == t_eph::GLONASS) {
616 ephs.append(new t_ephGlo(*dynamic_cast<t_ephGlo*>(eph)));
617 }
618 else if (eph->type() == t_eph::Galileo) {
619 ephs.append(new t_ephGal(*dynamic_cast<t_ephGal*>(eph)));
620 }
621 else if (eph->type() == t_eph::QZSS) {
622 ephs.append(new t_ephGPS(*dynamic_cast<t_ephGPS*>(eph)));
623 }
624 else if (eph->type() == t_eph::SBAS) {
625 ephs.append(new t_ephSBAS(*dynamic_cast<t_ephSBAS*>(eph)));
626 }
627 else if (eph->type() == t_eph::BDS) {
628 ephs.append(new t_ephBDS(*dynamic_cast<t_ephBDS*>(eph)));
629 }
630 }
631 }
632}
Note: See TracBrowser for help on using the repository browser.