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

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