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

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