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

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