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

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