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

Last change on this file since 4524 was 4524, checked in by mervart, 12 years ago
File size: 14.6 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
165 QStringListIterator it(obsFileNames);
166 while (it.hasNext()) {
167 QString fileName = it.next();
168 if (fileName.indexOf('*') != -1 || fileName.indexOf('?') != -1) {
169 QFileInfo fileInfo(fileName);
170 QDir dir = fileInfo.dir();
171 QStringList filters; filters << fileInfo.fileName();
172 QListIterator<QFileInfo> it(dir.entryInfoList(filters));
173 while (it.hasNext()) {
174 QString filePath = it.next().filePath();
175 t_rnxObsFile* rnxObsFile = 0;
176 try {
177 rnxObsFile = new t_rnxObsFile(filePath, t_rnxObsFile::input);
178 rnxObsFiles.append(rnxObsFile);
179 }
180 catch (...) {
181 delete rnxObsFile;
182 cerr << "Error in rnxObsFile " << filePath.toAscii().data() << endl;
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 cerr << "Error in rnxObsFile " << fileName.toAscii().data() << endl;
194 }
195 }
196 }
197 qStableSort(rnxObsFiles.begin(), rnxObsFiles.end(),
198 t_rnxObsFile::earlierStartTime);
199}
200
201//
202////////////////////////////////////////////////////////////////////////////
203void t_reqcEdit::editObservations() {
204
205 // Easy Exit
206 // ---------
207 if (_obsFileNames.isEmpty() || _outObsFileName.isEmpty()) {
208 return;
209 }
210
211 t_reqcEdit::initRnxObsFiles(_obsFileNames, _rnxObsFiles);
212
213 // Initialize output observation file
214 // ----------------------------------
215 t_rnxObsFile outObsFile(_outObsFileName, t_rnxObsFile::output);
216
217 // Loop over all input observation files
218 // -------------------------------------
219 for (int ii = 0; ii < _rnxObsFiles.size(); ii++) {
220 t_rnxObsFile* obsFile = _rnxObsFiles[ii];
221 if (_log) {
222 *_log << "Processing File: " << obsFile->fileName() << " start: "
223 << obsFile->startTime().datestr().c_str() << ' '
224 << obsFile->startTime().timestr(0).c_str() << endl;
225 }
226 if (ii == 0) {
227 outObsFile.setHeader(obsFile->header(), _rnxVersion);
228 if (_begTime.valid() && _begTime > outObsFile.startTime()) {
229 outObsFile.setStartTime(_begTime);
230 }
231 if (_samplingRate > outObsFile.interval()) {
232 outObsFile.setInterval(_samplingRate);
233 }
234 editRnxObsHeader(outObsFile);
235 bncSettings settings;
236 QMap<QString, QString> txtMap;
237 QString runBy = settings.value("reqcRunBy").toString();
238 if (!runBy.isEmpty()) {
239 txtMap["RUN BY"] = runBy;
240 }
241 QString comment = settings.value("reqcComment").toString();
242 if (!comment.isEmpty()) {
243 txtMap["COMMENT"] = comment;
244 }
245 outObsFile.header().write(outObsFile.stream(), &txtMap);
246 }
247 else {
248 outObsFile.checkNewHeader(obsFile->header());
249 }
250 t_rnxObsFile::t_rnxEpo* epo = 0;
251 while ( (epo = obsFile->nextEpoch()) != 0) {
252 if (_begTime.valid() && epo->tt < _begTime) {
253 continue;
254 }
255 if (_endTime.valid() && epo->tt > _endTime) {
256 break;
257 }
258
259 if (_samplingRate == 0 ||
260 fmod(round(epo->tt.gpssec()), _samplingRate) == 0) {
261 applyLLI(obsFile, epo);
262 outObsFile.writeEpoch(epo);
263 }
264 else {
265 rememberLLI(obsFile, epo);
266 }
267 }
268 }
269}
270
271// Change RINEX Header Content
272////////////////////////////////////////////////////////////////////////////
273void t_reqcEdit::editRnxObsHeader(t_rnxObsFile& obsFile) {
274
275 bncSettings settings;
276
277 QString oldMarkerName = settings.value("reqcOldMarkerName").toString();
278 QString newMarkerName = settings.value("reqcNewMarkerName").toString();
279 if (!newMarkerName.isEmpty()) {
280 if (oldMarkerName.isEmpty() ||
281 QRegExp(oldMarkerName).exactMatch(obsFile.markerName())) {
282 obsFile.setMarkerName(newMarkerName);
283 }
284 }
285
286 QString oldAntennaName = settings.value("reqcOldAntennaName").toString();
287 QString newAntennaName = settings.value("reqcNewAntennaName").toString();
288 if (!newAntennaName.isEmpty()) {
289 if (oldAntennaName.isEmpty() ||
290 QRegExp(oldAntennaName).exactMatch(obsFile.antennaName())) {
291 obsFile.setAntennaName(newAntennaName);
292 }
293 }
294
295 QString oldReceiverType = settings.value("reqcOldReceiverName").toString();
296 QString newReceiverType = settings.value("reqcNewReceiverName").toString();
297 if (!newReceiverType.isEmpty()) {
298 if (oldReceiverType.isEmpty() ||
299 QRegExp(oldReceiverType).exactMatch(obsFile.receiverType())) {
300 obsFile.setReceiverType(newReceiverType);
301 }
302 }
303}
304
305//
306////////////////////////////////////////////////////////////////////////////
307void t_reqcEdit::rememberLLI(const t_rnxObsFile* obsFile,
308 const t_rnxObsFile::t_rnxEpo* epo) {
309
310 if (_samplingRate == 0) {
311 return;
312 }
313
314 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
315 const t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iSat];
316 char sys = rnxSat.satSys;
317 QString prn = QString("%1%2").arg(sys).arg(rnxSat.satNum,2,10,QChar('0'));
318
319 for (int iType = 0; iType < obsFile->nTypes(sys); iType++) {
320 if (!_lli[prn].contains(iType)) {
321 _lli[prn][iType] = 0;
322 }
323 if (rnxSat.lli[iType] & 1) {
324 _lli[prn][iType] |= 1;
325 }
326 }
327 }
328}
329
330//
331////////////////////////////////////////////////////////////////////////////
332void t_reqcEdit::applyLLI(const t_rnxObsFile* obsFile,
333 t_rnxObsFile::t_rnxEpo* epo) {
334
335 if (_samplingRate == 0) {
336 return;
337 }
338
339 for (unsigned iSat = 0; iSat < epo->rnxSat.size(); iSat++) {
340 t_rnxObsFile::t_rnxSat& rnxSat = epo->rnxSat[iSat];
341 char sys = rnxSat.satSys;
342 QString prn = QString("%1%2").arg(sys).arg(rnxSat.satNum,2,10,QChar('0'));
343
344 for (int iType = 0; iType < obsFile->nTypes(sys); iType++) {
345 if (_lli[prn].contains(iType) && _lli[prn][iType] & 1) {
346 rnxSat.lli[iType] |= 1;
347 }
348 }
349 }
350
351 _lli.clear();
352}
353
354/// Read All Ephemerides
355////////////////////////////////////////////////////////////////////////////
356void t_reqcEdit::readEphemerides(const QStringList& navFileNames,
357 QVector<t_eph*>& ephs) {
358
359 QStringListIterator it(navFileNames);
360 while (it.hasNext()) {
361 QString fileName = it.next();
362 if (fileName.indexOf('*') != -1 || fileName.indexOf('?') != -1) {
363 QFileInfo fileInfo(fileName);
364 QDir dir = fileInfo.dir();
365 QStringList filters; filters << fileInfo.fileName();
366 QListIterator<QFileInfo> it(dir.entryInfoList(filters));
367 while (it.hasNext()) {
368 QString filePath = it.next().filePath();
369 appendEphemerides(filePath, ephs);
370 }
371 }
372 else {
373 appendEphemerides(fileName, ephs);
374 }
375 }
376 qStableSort(ephs.begin(), ephs.end(), t_eph::earlierTime);
377}
378
379//
380////////////////////////////////////////////////////////////////////////////
381void t_reqcEdit::editEphemerides() {
382
383 // Easy Exit
384 // ---------
385 if (_navFileNames.isEmpty() || _outNavFileName.isEmpty()) {
386 return;
387 }
388
389 // Read Ephemerides
390 // ----------------
391 t_reqcEdit::readEphemerides(_navFileNames, _ephs);
392
393 // Check Satellite Systems
394 // -----------------------
395 bool haveGPS = false;
396 bool haveGlonass = false;
397 for (int ii = 0; ii < _ephs.size(); ii++) {
398 const t_eph* eph = _ephs[ii];
399 if (eph->type() == t_eph::GPS) {
400 haveGPS = true;
401 }
402 else if (eph->type() == t_eph::GLONASS) {
403 haveGlonass = true;
404 }
405 }
406
407 // Initialize output navigation file
408 // ---------------------------------
409 t_rnxNavFile outNavFile(_outNavFileName, t_rnxNavFile::output);
410
411 outNavFile.setGlonass(haveGlonass);
412
413 if (haveGPS && haveGlonass) {
414 outNavFile.setVersion(rnxV3);
415 }
416 else {
417 outNavFile.setVersion(_rnxVersion);
418 }
419
420 bncSettings settings;
421 QMap<QString, QString> txtMap;
422 QString runBy = settings.value("reqcRunBy").toString();
423 if (!runBy.isEmpty()) {
424 txtMap["RUN BY"] = runBy;
425 }
426 QString comment = settings.value("reqcComment").toString();
427 if (!comment.isEmpty()) {
428 txtMap["COMMENT"] = comment;
429 }
430
431 outNavFile.writeHeader(&txtMap);
432
433 // Loop over all ephemerides
434 // -------------------------
435 for (int ii = 0; ii < _ephs.size(); ii++) {
436 const t_eph* eph = _ephs[ii];
437 outNavFile.writeEph(eph);
438 }
439}
440
441//
442////////////////////////////////////////////////////////////////////////////
443void t_reqcEdit::appendEphemerides(const QString& fileName,
444 QVector<t_eph*>& ephs) {
445
446 t_rnxNavFile rnxNavFile(fileName, t_rnxNavFile::input);
447 for (unsigned ii = 0; ii < rnxNavFile.ephs().size(); ii++) {
448 t_eph* eph = rnxNavFile.ephs()[ii];
449 bool isNew = true;
450 for (int iOld = 0; iOld < ephs.size(); iOld++) {
451 const t_eph* ephOld = ephs[iOld];
452 if (ephOld->prn() == eph->prn() && ephOld->TOC() == eph->TOC()) {
453 isNew = false;
454 break;
455 }
456 }
457 if (isNew) {
458 if (eph->type() == t_eph::GPS) {
459 ephs.append(new t_ephGPS(*dynamic_cast<t_ephGPS*>(eph)));
460 }
461 else if (eph->type() == t_eph::GLONASS) {
462 ephs.append(new t_ephGlo(*dynamic_cast<t_ephGlo*>(eph)));
463 }
464 else if (eph->type() == t_eph::Galileo) {
465 ephs.append(new t_ephGal(*dynamic_cast<t_ephGal*>(eph)));
466 }
467 }
468 }
469}
Note: See TracBrowser for help on using the repository browser.