source: ntrip/trunk/BNC/src/rinex/rnxnavfile.cpp@ 10902

Last change on this file since 10902 was 10662, checked in by stuerze, 12 months ago

minor changes

File size: 12.8 KB
RevLine 
[3645]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_rnxNavFile
30 *
31 * Purpose: Reads RINEX Navigation File
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Jan-2012
36 *
[7638]37 * Changes:
[3645]38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
[3666]42#include <newmatio.h>
[3645]43#include "rnxnavfile.h"
[5070]44#include "bnccore.h"
[3657]45#include "bncutils.h"
[5738]46#include "ephemeris.h"
[3645]47
48using namespace std;
49
50// Constructor
51////////////////////////////////////////////////////////////////////////////
[3657]52t_rnxNavFile::t_rnxNavHeader::t_rnxNavHeader() {
53 _version = 0.0;
[3659]54 _glonass = false;
[3645]55}
56
57// Destructor
58////////////////////////////////////////////////////////////////////////////
[3657]59t_rnxNavFile::t_rnxNavHeader::~t_rnxNavHeader() {
60}
61
62// Read Header
63////////////////////////////////////////////////////////////////////////////
64t_irc t_rnxNavFile::t_rnxNavHeader::read(QTextStream* stream) {
65 while (stream->status() == QTextStream::Ok && !stream->atEnd()) {
66 QString line = stream->readLine();
67 if (line.isEmpty()) {
68 continue;
69 }
70 QString value = line.left(60).trimmed();
71 QString key = line.mid(60).trimmed();
72 if (key == "END OF HEADER") {
73 break;
74 }
75 else if (key == "RINEX VERSION / TYPE") {
[8204]76 QTextStream in(value.toLatin1(), QIODevice::ReadOnly);
[3657]77 in >> _version;
[3659]78 if (value.indexOf("GLONASS") != -1) {
79 _glonass = true;
80 }
[3657]81 }
[7999]82 else if (key == "COMMENT") {
83 _comments.append(value.trimmed());
84 }
[9945]85 else if (key == "PGM / RUN BY / DATE") {
86 _runByDate.append(value.trimmed());
87 }
[3657]88 }
89
90 return success;
91}
92
93// Constructor
94////////////////////////////////////////////////////////////////////////////
[3999]95t_rnxNavFile::t_rnxNavFile(const QString& fileName, e_inpOut inpOut) {
96 _inpOut = inpOut;
97 _stream = 0;
98 _file = 0;
99 if (_inpOut == input) {
100 openRead(fileName);
101 }
102 else {
103 openWrite(fileName);
104 }
[3657]105}
106
[3999]107// Open for input
108////////////////////////////////////////////////////////////////////////////
109void t_rnxNavFile::openRead(const QString& fileName) {
110
111 _fileName = fileName; expandEnvVar(_fileName);
112 _file = new QFile(_fileName);
113 _file->open(QIODevice::ReadOnly | QIODevice::Text);
114 _stream = new QTextStream();
115 _stream->setDevice(_file);
116
117 _header.read(_stream);
118 this->read(_stream);
119}
120
121// Open for output
122////////////////////////////////////////////////////////////////////////////
123void t_rnxNavFile::openWrite(const QString& fileName) {
124
125 _fileName = fileName; expandEnvVar(_fileName);
126 _file = new QFile(_fileName);
127 _file->open(QIODevice::WriteOnly | QIODevice::Text);
128 _stream = new QTextStream();
129 _stream->setDevice(_file);
130}
131
[3657]132// Destructor
133////////////////////////////////////////////////////////////////////////////
[3645]134t_rnxNavFile::~t_rnxNavFile() {
[3999]135 close();
[3758]136 for (unsigned ii = 0; ii < _ephs.size(); ii++) {
137 delete _ephs[ii];
[7638]138 }
[3645]139}
140
[3999]141// Close
142////////////////////////////////////////////////////////////////////////////
143void t_rnxNavFile::close() {
144 delete _stream; _stream = 0;
145 delete _file; _file = 0;
146}
147
[3746]148// Read File Content
[3659]149////////////////////////////////////////////////////////////////////////////
[3746]150void t_rnxNavFile::read(QTextStream* stream) {
[10603]151 QString navType;
152 QString prn;
[3659]153
[3746]154 while (stream->status() == QTextStream::Ok && !stream->atEnd()) {
[10603]155 char sys;
[3746]156 QString line = stream->readLine();
[3659]157 if (line.isEmpty()) {
158 continue;
159 }
[10603]160
[10548]161 QStringList hlp = line.split(QRegExp("\\s+"), Qt::SkipEmptyParts);
[10587]162 QString firstStr = hlp.at(0);
[10574]163
[10662]164 // RINEX version 2
165 if (version() < 3.0 && firstStr != ">") {
166 glonass() ? sys = 'R' : sys = 'G';
167 navType = "";
168 prn = QString("%1").arg(hlp.at(0).toInt(), 2, 10, QChar('0'));
169 }
[10587]170 // RINEX version 3
[10662]171 else if (version() >= 3.0 &&
[10587]172 version() < 4.0 && firstStr != ">") {
[9765]173 prn = firstStr;
[10574]174 sys = prn[0].toLatin1();
[10587]175 navType = "";
[3659]176 }
[10587]177 // RINEX version 4
[9765]178 else if (version() >= 4.0 && firstStr == ">") {
[10587]179 QString key = hlp.at(1);
[10574]180 prn = hlp.at(2);
181 sys = prn[0].toLatin1();
[10587]182 navType = hlp.at(3);
[10574]183
184 // ALL Non-EPH messages are currently ignored
[9765]185 int lines2skip = 0;
[10574]186 // STO
187 if (key == "STO") {
[9765]188 lines2skip = 2;
189 }
[10574]190 // EOP
191 else if (key == "EOP") {
[9765]192 lines2skip = 3;
193 }
[10574]194 // ION
195 else if (key == "ION") {
196 if (sys == 'R') {
197 lines2skip = 1; //if (navType == "LXOC") {}
198 }
199 else if (sys == 'E') {
200 lines2skip = 2;
201 }
202 else if (sys == 'G' || sys == 'C' ||
203 sys == 'J' || sys == 'I') {
204 lines2skip = 3;
[10587]205 if ((sys == 'I' && navType == "L1NV") || // I: KLOB, NEQN
206 (sys == 'J' && navType == "CNVX")) { // J: WIDE, JAPN
207 QString navSubType = hlp.at(4);
[10574]208 if (navSubType == "KLOB") {
209 lines2skip += 1;
210 }
211 else if (navSubType == "NEQN") {
212 lines2skip += 4;
213 }
214 }
215 }
216 }
[9765]217 if (lines2skip) {
[10603]218 for (int ii = 0; ii < lines2skip; ii++) {
[9765]219 stream->readLine();
220 }
221 }
222 continue;
223 }
[7638]224
[3746]225 t_eph* eph = 0;
[10574]226 QStringList lines;
227 lines << line;
228 if (sys == 'G') {
[10587]229 int nLines = 8;
230 if (navType == "CNAV") {
231 nLines += 1;
232 }
233 if (navType == "CNV2") {
234 nLines += 2;
235 }
236 for (int ii = 1; ii < nLines; ii++) {
[3746]237 lines << stream->readLine();
[3659]238 }
[10603]239 eph = new t_ephGPS(version(), lines, navType);
[3659]240 }
[10574]241 else if (sys == 'R') {
[10587]242 int nLines = 4;
[9366]243 if (version() >= 3.05) {
[10587]244 nLines += 1;
[9366]245 }
[10587]246 for (int ii = 1; ii < nLines; ii++) {
[3746]247 lines << stream->readLine();
[3659]248 }
[10603]249 eph = new t_ephGlo(version(), lines, navType);
[3659]250 }
[10574]251 else if (sys == 'E') {
[3659]252 for (int ii = 1; ii < 8; ii++) {
[3746]253 lines << stream->readLine();
[3659]254 }
[10603]255 eph = new t_ephGal(version(), lines, navType);
[3659]256 }
[10574]257 else if (sys == 'J') {
[10587]258 int nLines = 8;
259 if (navType == "CNAV") {
260 nLines += 1;
261 }
262 if (navType == "CNV2") {
263 nLines += 2;
264 }
265 for (int ii = 1; ii < nLines; ii++) {
[6377]266 lines << stream->readLine();
267 }
[10603]268 eph = new t_ephGPS(version(), lines, navType);
[6377]269 }
[10574]270 else if (sys== 'S') {
[6391]271 for (int ii = 1; ii < 4; ii++) {
272 lines << stream->readLine();
273 }
[10603]274 eph = new t_ephSBAS(version(), lines, navType);
[6391]275 }
[10574]276 else if (sys == 'C') {
[10587]277 int nLines = 8;
278 if (navType == "CNV1" ||
279 navType == "CNV2") {
280 nLines += 2;
281 }
282 if (navType == "CNV3") {
283 nLines += 1;
284 }
285 for (int ii = 1; ii < nLines; ii++) {
[6399]286 lines << stream->readLine();
287 }
[10603]288 eph = new t_ephBDS(version(), lines, navType);
[6399]289 }
[10574]290 else if (sys == 'I') {
[10587]291 int nLines = 8;
292 if (navType == "L1NV") {
293 nLines += 1;
294 }
295 for (int ii = 1; ii < nLines; ii++) {
[8168]296 lines << stream->readLine();
297 }
[10603]298 eph = new t_ephGPS(version(), lines, navType);
[8168]299 }
[10525]300
[10318]301 if (eph) {
302 _ephs.push_back(eph);
303 }
[10587]304
[3659]305 }
[3746]306}
[3659]307
[3746]308// Read Next Ephemeris
309////////////////////////////////////////////////////////////////////////////
[7638]310t_eph* t_rnxNavFile::getNextEph(const bncTime& tt,
[7169]311 const QMap<QString, unsigned int>* corrIODs) {
[3758]312
[3764]313 // Get Ephemeris according to IOD
314 // ------------------------------
[3758]315 if (corrIODs) {
[7169]316 QMapIterator<QString, unsigned int> itIOD(*corrIODs);
[3758]317 while (itIOD.hasNext()) {
318 itIOD.next();
[10599]319 QString corrPrn = itIOD.key();
320 unsigned int corrIod = itIOD.value();
[3758]321 vector<t_eph*>::iterator it = _ephs.begin();
322 while (it != _ephs.end()) {
323 t_eph* eph = *it;
[4150]324 double dt = eph->TOC() - tt;
[10599]325 QString ephPrn = QString(eph->prn().toInternalString().c_str());
326 unsigned int ephIod = eph->IOD();
327 if (dt < 8*3600.0 &&
328 ephPrn == corrPrn &&
329 ephIod == corrIod) {
[3758]330 it = _ephs.erase(it);
331 return eph;
332 }
333 ++it;
334 }
[3748]335 }
[3746]336 }
[3758]337
[3764]338 // Get Ephemeris according to time
339 // -------------------------------
340 else {
341 vector<t_eph*>::iterator it = _ephs.begin();
342 while (it != _ephs.end()) {
343 t_eph* eph = *it;
[4018]344 double dt = eph->TOC() - tt;
[10599]345 char sys = eph->prn().system();
346 int num = eph->prn().number();
347 int ssrNavType = t_corrSSR::getSsrNavTypeFlag(sys, num);
348 if (dt < 2*3600.0 && eph->type() == ssrNavType) {
[3764]349 it = _ephs.erase(it);
350 return eph;
351 }
352 ++it;
353 }
354 }
355
[3683]356 return 0;
[3659]357}
[4004]358
[7638]359//
[4004]360////////////////////////////////////////////////////////////////////////////
[9765]361void t_rnxNavFile::writeHeader(const QMap<QString, QString>* txtMap, int numMergedFiles, int leapSecs) {
[4010]362
[5068]363 QString runBy = BNC_CORE->userName();
[4219]364 QStringList comments;
[9945]365 QStringList runByDate;
[4219]366
367 if (txtMap) {
368 QMapIterator<QString, QString> it(*txtMap);
369 while (it.hasNext()) {
370 it.next();
371 if (it.key() == "RUN BY") {
372 runBy = it.value();
373 }
[9945]374 else if (it.key() == "RUN BY DATE") {
[10548]375 runByDate = it.value().split("\\n", Qt::SkipEmptyParts);
[9945]376 }
[4219]377 else if (it.key() == "COMMENT") {
[10548]378 comments = it.value().split("\\n", Qt::SkipEmptyParts);
[4219]379 }
380 }
381 }
382
[4010]383 if (version() < 3.0) {
[7638]384 const QString fmt = glonass() ? "%1 GLONASS navigation data"
[4230]385 : "%1 Navigation data";
386 *_stream << QString(fmt)
[4010]387 .arg(_header._version, 9, 'f', 2)
388 .leftJustified(60)
389 << "RINEX VERSION / TYPE\n";
390 }
391 else {
[8354]392 QString fmt;
[10587]393 t_eph::e_system sys = satSystem();
[8354]394 switch(sys) {
395 case t_eph::GPS:
396 fmt.append("%1 N: GNSS NAV DATA G: GPS");
397 break;
398 case t_eph::GLONASS:
399 fmt.append("%1 N: GNSS NAV DATA R: GLONASS");
400 break;
401 case t_eph::Galileo:
402 fmt.append("%1 N: GNSS NAV DATA E: Galileo");
403 break;
404 case t_eph::QZSS:
405 fmt.append("%1 N: GNSS NAV DATA J: QZSS");
406 break;
407 case t_eph::BDS:
408 fmt.append("%1 N: GNSS NAV DATA C: BDS");
409 break;
[10619]410 case t_eph::NavIC:
411 fmt.append("%1 N: GNSS NAV DATA I: NavIC");
[8354]412 break;
413 case t_eph::SBAS:
414 fmt.append("%1 N: GNSS NAV DATA S: SBAS");
415 break;
416 case t_eph::unknown:
417 fmt.append("%1 N: GNSS NAV DATA M: MIXED");
418 break;
419 }
420 *_stream << fmt
[4010]421 .arg(_header._version, 9, 'f', 2)
422 .leftJustified(60)
423 << "RINEX VERSION / TYPE\n";
424 }
425
[4232]426 const QString fmtDate = (version() < 3.0) ? "dd-MMM-yy hh:mm"
[4231]427 : "yyyyMMdd hhmmss UTC";
[4010]428 *_stream << QString("%1%2%3")
[5068]429 .arg(BNC_CORE->pgmName(), -20)
[4219]430 .arg(runBy.trimmed().left(20), -20)
[4231]431 .arg(QDateTime::currentDateTime().toUTC().toString(fmtDate), -20)
[4010]432 .leftJustified(60)
433 << "PGM / RUN BY / DATE\n";
434
[9765]435 if (version() >= 4.0) {
[9945]436 QStringListIterator itRunByDt(runByDate);
437 while (itRunByDt.hasNext()) {
438 *_stream << itRunByDt.next().trimmed().left(60).leftJustified(60)
439 << "PGM / RUN BY / DATE\n";
440 }
[9765]441 *_stream << QString("%1").arg(leapSecs, 6, 10, QLatin1Char(' ')).left(60).leftJustified(60)
442 << "LEAP SECONDS\n";
443
444 *_stream << QString("%1").arg(numMergedFiles, 19, 10, QLatin1Char(' ')).leftJustified(60)
445 << "MERGED FILE\n";
446 }
447
[4222]448 QStringListIterator itCmnt(comments);
449 while (itCmnt.hasNext()) {
450 *_stream << itCmnt.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
451 }
452
[4010]453 *_stream << QString()
454 .leftJustified(60)
455 << "END OF HEADER\n";
[4004]456}
457
[7638]458//
[4004]459////////////////////////////////////////////////////////////////////////////
460void t_rnxNavFile::writeEph(const t_eph* eph) {
[4013]461 *_stream << eph->toString(version());
[4004]462}
Note: See TracBrowser for help on using the repository browser.