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

Last change on this file since 10229 was 9945, checked in by stuerze, 2 years ago

multiple 'run by date' entries for rinex version 4 added

File size: 11.2 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_rnxNavFile
30 *
31 * Purpose: Reads RINEX Navigation File
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Jan-2012
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42#include <newmatio.h>
43#include "rnxnavfile.h"
44#include "bnccore.h"
45#include "bncutils.h"
46#include "ephemeris.h"
47
48using namespace std;
49
50// Constructor
51////////////////////////////////////////////////////////////////////////////
52t_rnxNavFile::t_rnxNavHeader::t_rnxNavHeader() {
53 _version = 0.0;
54 _glonass = false;
55}
56
57// Destructor
58////////////////////////////////////////////////////////////////////////////
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") {
76 QTextStream in(value.toLatin1(), QIODevice::ReadOnly);
77 in >> _version;
78 if (value.indexOf("GLONASS") != -1) {
79 _glonass = true;
80 }
81 }
82 else if (key == "COMMENT") {
83 _comments.append(value.trimmed());
84 }
85 else if (key == "PGM / RUN BY / DATE") {
86 _runByDate.append(value.trimmed());
87 }
88 }
89
90 return success;
91}
92
93// Constructor
94////////////////////////////////////////////////////////////////////////////
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 }
105}
106
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
132// Destructor
133////////////////////////////////////////////////////////////////////////////
134t_rnxNavFile::~t_rnxNavFile() {
135 close();
136 for (unsigned ii = 0; ii < _ephs.size(); ii++) {
137 delete _ephs[ii];
138 }
139}
140
141// Close
142////////////////////////////////////////////////////////////////////////////
143void t_rnxNavFile::close() {
144 delete _stream; _stream = 0;
145 delete _file; _file = 0;
146}
147
148// Read File Content
149////////////////////////////////////////////////////////////////////////////
150void t_rnxNavFile::read(QTextStream* stream) {
151 QString navTypeStr;
152
153 while (stream->status() == QTextStream::Ok && !stream->atEnd()) {
154
155 QString line = stream->readLine();
156 if (line.isEmpty()) {
157 continue;
158 }
159
160 QStringList hlp = line.split(QRegExp("\\s+"), QString::SkipEmptyParts);
161 QString firstStr = hlp.at(0);
162 QString prn;
163
164 if (version() >= 3.0 && firstStr != ">") {
165 prn = firstStr;
166 }
167 else if (version() >= 4.0 && firstStr == ">") {
168 int lines2skip = 0;
169 QString key = hlp.at(1);
170 // EPH is used
171 if (key == "EPH") {
172 navTypeStr = hlp.at(3);
173 }
174 // all others are currently ignored
175 else if (key == "STO") {
176 lines2skip = 2;
177 }
178 else if (key == "EOP" || key == "ION") {
179 lines2skip = 3;
180 }
181 if (lines2skip) {
182 for (int ii = 1; ii < lines2skip; ii++) {
183 stream->readLine();
184 }
185 }
186 continue;
187 }
188 else {
189 if (glonass()) {
190 prn = QString("R%1_0").arg(hlp.at(0).toInt(), 2, 10, QChar('0'));
191 }
192 else {
193 prn = QString("G%1_0").arg(hlp.at(0).toInt(), 2, 10, QChar('0'));
194 }
195 }
196
197 t_eph* eph = 0;
198 QStringList lines; lines << line;
199 if (prn[0] == 'G') {
200 for (int ii = 1; ii < 8; ii++) {
201 lines << stream->readLine();
202 }
203 eph = new t_ephGPS(version(), lines);
204 }
205 else if (prn[0] == 'R') {
206 int num = 4;
207 if (version() >= 3.05) {
208 num += 1;
209 }
210 for (int ii = 1; ii < num; ii++) {
211 lines << stream->readLine();
212 }
213 eph = new t_ephGlo(version(), lines);
214 }
215 else if (prn[0] == 'E') {
216 for (int ii = 1; ii < 8; ii++) {
217 lines << stream->readLine();
218 }
219 eph = new t_ephGal(version(), lines);
220 }
221 else if (prn[0] == 'J') {
222 for (int ii = 1; ii < 8; ii++) {
223 lines << stream->readLine();
224 }
225 eph = new t_ephGPS(version(), lines);
226 }
227 else if (prn[0] == 'S') {
228 for (int ii = 1; ii < 4; ii++) {
229 lines << stream->readLine();
230 }
231 eph = new t_ephSBAS(version(), lines);
232 }
233 else if (prn[0] == 'C') {
234 for (int ii = 1; ii < 8; ii++) {
235 lines << stream->readLine();
236 }
237 eph = new t_ephBDS(version(), lines);
238 }
239 else if (prn[0] == 'I') {
240 for (int ii = 1; ii < 8; ii++) {
241 lines << stream->readLine();
242 }
243 eph = new t_ephGPS(version(), lines);
244 }
245 else {
246 continue;
247 }
248 if (version() >= 4.0) {
249 if (eph->setNavType(navTypeStr) != success) {
250 delete eph;
251 continue;
252 }
253 }
254 _ephs.push_back(eph);
255 }
256}
257
258// Read Next Ephemeris
259////////////////////////////////////////////////////////////////////////////
260t_eph* t_rnxNavFile::getNextEph(const bncTime& tt,
261 const QMap<QString, unsigned int>* corrIODs) {
262
263 // Get Ephemeris according to IOD
264 // ------------------------------
265 if (corrIODs) {
266 QMapIterator<QString, unsigned int> itIOD(*corrIODs);
267 while (itIOD.hasNext()) {
268 itIOD.next();
269 QString prn = itIOD.key();
270 unsigned int iod = itIOD.value();
271 vector<t_eph*>::iterator it = _ephs.begin();
272 while (it != _ephs.end()) {
273 t_eph* eph = *it;
274 double dt = eph->TOC() - tt;
275 if (dt < 8*3600.0 && QString(eph->prn().toInternalString().c_str()) == prn && eph->IOD() == iod) {
276 it = _ephs.erase(it);
277 return eph;
278 }
279 ++it;
280 }
281 }
282 }
283
284 // Get Ephemeris according to time
285 // -------------------------------
286 else {
287 vector<t_eph*>::iterator it = _ephs.begin();
288 while (it != _ephs.end()) {
289 t_eph* eph = *it;
290 double dt = eph->TOC() - tt;
291 if (dt < 2*3600.0) {
292 it = _ephs.erase(it);
293 return eph;
294 }
295 ++it;
296 }
297 }
298
299 return 0;
300}
301
302//
303////////////////////////////////////////////////////////////////////////////
304void t_rnxNavFile::writeHeader(const QMap<QString, QString>* txtMap, int numMergedFiles, int leapSecs) {
305
306 QString runBy = BNC_CORE->userName();
307 QStringList comments;
308 QStringList runByDate;
309
310 if (txtMap) {
311 QMapIterator<QString, QString> it(*txtMap);
312 while (it.hasNext()) {
313 it.next();
314 if (it.key() == "RUN BY") {
315 runBy = it.value();
316 }
317 else if (it.key() == "RUN BY DATE") {
318 runByDate = it.value().split("\\n", QString::SkipEmptyParts);
319 }
320 else if (it.key() == "COMMENT") {
321 comments = it.value().split("\\n", QString::SkipEmptyParts);
322 }
323 }
324 }
325
326 if (version() < 3.0) {
327 const QString fmt = glonass() ? "%1 GLONASS navigation data"
328 : "%1 Navigation data";
329 *_stream << QString(fmt)
330 .arg(_header._version, 9, 'f', 2)
331 .leftJustified(60)
332 << "RINEX VERSION / TYPE\n";
333 }
334 else {
335 QString fmt;
336 t_eph::e_type sys = satSystem();
337 switch(sys) {
338 case t_eph::GPS:
339 fmt.append("%1 N: GNSS NAV DATA G: GPS");
340 break;
341 case t_eph::GLONASS:
342 fmt.append("%1 N: GNSS NAV DATA R: GLONASS");
343 break;
344 case t_eph::Galileo:
345 fmt.append("%1 N: GNSS NAV DATA E: Galileo");
346 break;
347 case t_eph::QZSS:
348 fmt.append("%1 N: GNSS NAV DATA J: QZSS");
349 break;
350 case t_eph::BDS:
351 fmt.append("%1 N: GNSS NAV DATA C: BDS");
352 break;
353 case t_eph::IRNSS:
354 fmt.append("%1 N: GNSS NAV DATA I: IRNSS");
355 break;
356 case t_eph::SBAS:
357 fmt.append("%1 N: GNSS NAV DATA S: SBAS");
358 break;
359 case t_eph::unknown:
360 fmt.append("%1 N: GNSS NAV DATA M: MIXED");
361 break;
362 }
363 *_stream << fmt
364 .arg(_header._version, 9, 'f', 2)
365 .leftJustified(60)
366 << "RINEX VERSION / TYPE\n";
367 }
368
369 const QString fmtDate = (version() < 3.0) ? "dd-MMM-yy hh:mm"
370 : "yyyyMMdd hhmmss UTC";
371 *_stream << QString("%1%2%3")
372 .arg(BNC_CORE->pgmName(), -20)
373 .arg(runBy.trimmed().left(20), -20)
374 .arg(QDateTime::currentDateTime().toUTC().toString(fmtDate), -20)
375 .leftJustified(60)
376 << "PGM / RUN BY / DATE\n";
377
378 if (version() >= 4.0) {
379 QStringListIterator itRunByDt(runByDate);
380 while (itRunByDt.hasNext()) {
381 *_stream << itRunByDt.next().trimmed().left(60).leftJustified(60)
382 << "PGM / RUN BY / DATE\n";
383 }
384 *_stream << QString("%1").arg(leapSecs, 6, 10, QLatin1Char(' ')).left(60).leftJustified(60)
385 << "LEAP SECONDS\n";
386
387 *_stream << QString("%1").arg(numMergedFiles, 19, 10, QLatin1Char(' ')).leftJustified(60)
388 << "MERGED FILE\n";
389 }
390
391 QStringListIterator itCmnt(comments);
392 while (itCmnt.hasNext()) {
393 *_stream << itCmnt.next().trimmed().left(60).leftJustified(60) << "COMMENT\n";
394 }
395
396 *_stream << QString()
397 .leftJustified(60)
398 << "END OF HEADER\n";
399}
400
401//
402////////////////////////////////////////////////////////////////////////////
403void t_rnxNavFile::writeEph(const t_eph* eph) {
404 *_stream << eph->toString(version());
405}
Note: See TracBrowser for help on using the repository browser.