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

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