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

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