source: ntrip/trunk/BNC/rinex/rnxobsfile.cpp@ 3848

Last change on this file since 3848 was 3848, checked in by mervart, 12 years ago
File size: 15.6 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_rnxObsFile
30 *
31 * Purpose: Reads RINEX Observation File
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Jan-2012
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <iostream>
42#include <iomanip>
43#include <sstream>
44#include "rnxobsfile.h"
45#include "bncutils.h"
46
47using namespace std;
48
49const QString t_rnxObsFile::t_rnxObsHeader::_emptyStr;
50
51// Constructor
52////////////////////////////////////////////////////////////////////////////
53t_rnxObsFile::t_rnxObsHeader::t_rnxObsHeader() {
54 _antNEU.ReSize(3); _antNEU = 0.0;
55 _antXYZ.ReSize(3); _antXYZ = 0.0;
56 _antBSG.ReSize(3); _antBSG = 0.0;
57 _xyz.ReSize(3); _xyz = 0.0;
58 _version = 0.0;
59 _interval = 0.0;
60 for (unsigned iPrn = 1; iPrn <= MAXPRN_GPS; iPrn++) {
61 _wlFactorsL1[iPrn] = 1;
62 _wlFactorsL2[iPrn] = 1;
63 }
64}
65
66// Destructor
67////////////////////////////////////////////////////////////////////////////
68t_rnxObsFile::t_rnxObsHeader::~t_rnxObsHeader() {
69}
70
71// Read Header
72////////////////////////////////////////////////////////////////////////////
73t_irc t_rnxObsFile::t_rnxObsHeader::read(QTextStream* stream, int maxLines) {
74 int numLines = 0;
75 while ( stream->status() == QTextStream::Ok && !stream->atEnd() ) {
76 QString line = stream->readLine(); ++ numLines;
77 if (line.isEmpty()) {
78 continue;
79 }
80 if (line.indexOf("END OF FILE") != -1) {
81 break;
82 }
83 QString value = line.mid(0,60).trimmed();
84 QString key = line.mid(60).trimmed();
85 if (key == "END OF HEADER") {
86 break;
87 }
88 else if (key == "RINEX VERSION / TYPE") {
89 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
90 in >> _version;
91 }
92 else if (key == "MARKER NAME") {
93 _markerName = value;
94 }
95 else if (key == "ANT # / TYPE") {
96 _antennaName = line.mid(20,20).trimmed();
97 }
98 else if (key == "INTERVAL") {
99 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
100 in >> _interval;
101 }
102 else if (key == "WAVELENGTH FACT L1/2") {
103 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
104 int wlFactL1 = 0;
105 int wlFactL2 = 0;
106 int numSat = 0;
107 in >> wlFactL1 >> wlFactL2 >> numSat;
108 if (numSat == 0) {
109 for (unsigned iPrn = 1; iPrn <= MAXPRN_GPS; iPrn++) {
110 _wlFactorsL1[iPrn] = wlFactL1;
111 _wlFactorsL2[iPrn] = wlFactL2;
112 }
113 }
114 else {
115 for (int ii = 0; ii < numSat; ii++) {
116 QString prn; in >> prn;
117 if (prn[0] == 'G') {
118 int iPrn;
119 readInt(prn, 1, 2, iPrn);
120 _wlFactorsL1[iPrn] = wlFactL1;
121 _wlFactorsL2[iPrn] = wlFactL2;
122 }
123 }
124 }
125 }
126 else if (key == "APPROX POSITION XYZ") {
127 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
128 in >> _xyz[0] >> _xyz[1] >> _xyz[2];
129 }
130 else if (key == "ANTENNA: DELTA H/E/N") {
131 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
132 in >> _antNEU[2] >> _antNEU[1] >> _antNEU[0];
133 }
134 else if (key == "ANTENNA: DELTA X/Y/Z") {
135 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
136 in >> _antXYZ[0] >> _antXYZ[1] >> _antXYZ[2];
137 }
138 else if (key == "ANTENNA: B.SIGHT XYZ") {
139 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
140 in >> _antBSG[0] >> _antBSG[1] >> _antBSG[2];
141 }
142 else if (key == "# / TYPES OF OBSERV") {
143 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
144 int nTypes;
145 in >> nTypes;
146 _obsTypesV2.clear();
147 for (int ii = 0; ii < nTypes; ii++) {
148 QString hlp;
149 in >> hlp;
150 _obsTypesV2.push_back(hlp);
151 }
152 }
153 else if (key == "SYS / # / OBS TYPES") {
154 QTextStream* in = new QTextStream(value.toAscii(), QIODevice::ReadOnly);
155 char sys;
156 int nTypes;
157 *in >> sys >> nTypes;
158 _obsTypesV3[sys].clear();
159 for (int ii = 0; ii < nTypes; ii++) {
160 if (ii > 0 && ii % 13 == 0) {
161 line = stream->readLine(); ++numLines;
162 delete in;
163 in = new QTextStream(line.toAscii(), QIODevice::ReadOnly);
164 }
165 QString hlp;
166 *in >> hlp;
167 _obsTypesV3[sys].push_back(hlp);
168 }
169 delete in;
170 }
171 else if (key == "TIME OF FIRST OBS") {
172 QTextStream in(value.toAscii(), QIODevice::ReadOnly);
173 int year, month, day, hour, min;
174 double sec;
175 in >> year >> month >> day >> hour >> min >> sec;
176 _startTime.set(year, month, day, hour, min, sec);
177 }
178 if (maxLines > 0 && numLines == maxLines) {
179 break;
180 }
181 }
182
183 return success;
184}
185
186// Number of Observation Types (satellite-system specific)
187////////////////////////////////////////////////////////////////////////////
188int t_rnxObsFile::t_rnxObsHeader::nTypes(char sys) const {
189 if (_version < 3.0) {
190 return _obsTypesV2.size();
191 }
192 else {
193 map<char, vector<QString> >::const_iterator it = _obsTypesV3.find(sys);
194 if (it != _obsTypesV3.end()) {
195 return it->second.size();
196 }
197 else {
198 return 0;
199 }
200 }
201}
202
203// Observation Type (satellite-system specific)
204////////////////////////////////////////////////////////////////////////////
205const QString& t_rnxObsFile::t_rnxObsHeader::obsType(char sys, int index) const {
206 if (_version < 3.0) {
207 return _obsTypesV2.at(index);
208 }
209 else {
210 map<char, vector<QString> >::const_iterator it = _obsTypesV3.find(sys);
211 if (it != _obsTypesV3.end()) {
212 return it->second.at(index);
213 }
214 else {
215 return _emptyStr;
216 }
217 }
218}
219
220// Constructor
221////////////////////////////////////////////////////////////////////////////
222t_rnxObsFile::t_rnxObsFile(const QString& fileName, e_inpOut inpOut) {
223 _inpOut = inpOut;
224 _stream = 0;
225 _flgPowerFail = false;
226 if (_inpOut == input) {
227 openRead(fileName);
228 }
229 else {
230 openWrite(fileName);
231 }
232}
233
234// Open for input
235////////////////////////////////////////////////////////////////////////////
236void t_rnxObsFile::openRead(const QString& fileName) {
237
238 _fileName = fileName; expandEnvVar(_fileName);
239 _file = new QFile(_fileName);
240 _file->open(QIODevice::ReadOnly | QIODevice::Text);
241 _stream = new QTextStream();
242 _stream->setDevice(_file);
243
244 _header.read(_stream);
245
246 // Guess Observation Interval
247 // --------------------------
248 if (_header._interval == 0.0) {
249 bncTime ttPrev;
250 for (int iEpo = 0; iEpo < 10; iEpo++) {
251 const t_rnxEpo* rnxEpo = nextEpoch();
252 if (!rnxEpo) {
253 throw QString("t_rnxObsFile: not enough epochs");
254 }
255 if (iEpo > 0) {
256 double dt = rnxEpo->tt - ttPrev;
257 if (_header._interval == 0.0 || dt < _header._interval) {
258 _header._interval = dt;
259 }
260 }
261 ttPrev = rnxEpo->tt;
262 }
263 _stream->seek(0);
264 _header.read(_stream);
265 }
266
267 // Time of first observation
268 // -------------------------
269 if (!_header._startTime.valid()) {
270 const t_rnxEpo* rnxEpo = nextEpoch();
271 if (!rnxEpo) {
272 throw QString("t_rnxObsFile: not enough epochs");
273 }
274 _header._startTime = rnxEpo->tt;
275 _stream->seek(0);
276 _header.read(_stream);
277 }
278}
279
280// Open for output
281////////////////////////////////////////////////////////////////////////////
282void t_rnxObsFile::openWrite(const QString& fileName) {
283
284 _fileName = fileName; expandEnvVar(_fileName);
285 _file = new QFile(_fileName);
286 _file->open(QIODevice::WriteOnly | QIODevice::Text);
287 _stream = new QTextStream();
288 _stream->setDevice(_file);
289}
290
291// Destructor
292////////////////////////////////////////////////////////////////////////////
293t_rnxObsFile::~t_rnxObsFile() {
294 close();
295}
296
297// Close
298////////////////////////////////////////////////////////////////////////////
299void t_rnxObsFile::close() {
300 delete _stream; _stream = 0;
301 delete _file; _file = 0;
302}
303
304// Handle Special Epoch Flag
305////////////////////////////////////////////////////////////////////////////
306void t_rnxObsFile::handleEpochFlag(int flag, const QString& line) {
307
308 // Power Failure
309 // -------------
310 if (flag == 1) {
311 _flgPowerFail = true;
312 }
313
314 // Start moving antenna
315 // --------------------
316 else if (flag == 2) {
317 // no action
318 }
319
320 // Re-Read Header
321 // --------------
322 else if (flag == 3 || flag == 4) {
323 int numLines = 0;
324 if (version() < 3.0) {
325 readInt(line, 29, 3, numLines);
326 }
327 else {
328 readInt(line, 32, 3, numLines);
329 }
330 _header.read(_stream, numLines);
331 }
332
333 // Unhandled Flag
334 // --------------
335 else {
336 throw QString("t_rnxObsFile: unhandled flag\n" + line);
337 }
338}
339
340// Retrieve single Epoch
341////////////////////////////////////////////////////////////////////////////
342const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpoch() {
343
344 _currEpo.clear();
345
346 if (version() < 3.0) {
347 return nextEpochV2();
348 }
349 else {
350 return nextEpochV3();
351 }
352}
353
354// Retrieve single Epoch (RINEX Version 3)
355////////////////////////////////////////////////////////////////////////////
356const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV3() {
357
358 while ( _stream->status() == QTextStream::Ok && !_stream->atEnd() ) {
359
360 QString line = _stream->readLine();
361
362 if (line.isEmpty()) {
363 continue;
364 }
365
366 int flag = 0;
367 readInt(line, 31, 1, flag);
368 if (flag > 0) {
369 handleEpochFlag(flag, line);
370 continue;
371 }
372
373 QTextStream in(line.mid(1).toAscii(), QIODevice::ReadOnly);
374
375 // Epoch Time
376 // ----------
377 int year, month, day, hour, min;
378 double sec;
379 in >> year >> month >> day >> hour >> min >> sec;
380 _currEpo.tt.set(year, month, day, hour, min, sec);
381
382 // Number of Satellites
383 // --------------------
384 int numSat;
385 readInt(line, 32, 3, numSat);
386
387 _currEpo.rnxSat.resize(numSat);
388
389 // Observations
390 // ------------
391 for (int iSat = 0; iSat < numSat; iSat++) {
392 line = _stream->readLine();
393 _currEpo.rnxSat[iSat].satSys = line.toAscii()[0];
394 readInt(line, 1, 2, _currEpo.rnxSat[iSat].satNum);
395 char sys = line.toAscii()[0];
396 for (int iType = 0; iType < _header.nTypes(sys); iType++) {
397 int pos = 3 + 16*iType;
398 double obsValue = 0.0;
399 int lli = 0;
400 int snr = 0;
401 readDbl(line, pos, 14, obsValue);
402 readInt(line, pos + 14, 1, lli);
403 readInt(line, pos + 15, 1, snr);
404
405 if (_flgPowerFail) {
406 lli |= 1;
407 }
408
409 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
410 _currEpo.rnxSat[iSat].lli.push_back(lli);
411 _currEpo.rnxSat[iSat].snr.push_back(snr);
412 }
413 }
414
415 _flgPowerFail = false;
416
417 return &_currEpo;
418 }
419
420 return 0;
421}
422
423// Retrieve single Epoch (RINEX Version 2)
424////////////////////////////////////////////////////////////////////////////
425const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV2() {
426
427 while ( _stream->status() == QTextStream::Ok && !_stream->atEnd() ) {
428
429 QString line = _stream->readLine();
430
431 if (line.isEmpty()) {
432 continue;
433 }
434
435 int flag = 0;
436 readInt(line, 28, 1, flag);
437 if (flag > 0) {
438 handleEpochFlag(flag, line);
439 continue;
440 }
441
442 QTextStream in(line.toAscii(), QIODevice::ReadOnly);
443
444 // Epoch Time
445 // ----------
446 int year, month, day, hour, min;
447 double sec;
448 in >> year >> month >> day >> hour >> min >> sec;
449 if (year < 80) {
450 year += 2000;
451 }
452 else if (year < 100) {
453 year += 1900;
454 }
455 _currEpo.tt.set(year, month, day, hour, min, sec);
456
457 // Number of Satellites
458 // --------------------
459 int numSat;
460 readInt(line, 29, 3, numSat);
461
462 _currEpo.rnxSat.resize(numSat);
463
464 // Read Satellite Numbers
465 // ----------------------
466 int pos = 32;
467 for (int iSat = 0; iSat < numSat; iSat++) {
468 if (iSat > 0 && iSat % 12 == 0) {
469 line = _stream->readLine();
470 pos = 32;
471 }
472
473 _currEpo.rnxSat[iSat].satSys = line.toAscii()[pos];
474 readInt(line, pos + 1, 2, _currEpo.rnxSat[iSat].satNum);
475
476 pos += 3;
477 }
478
479 // Read Observation Records
480 // ------------------------
481 for (int iSat = 0; iSat < numSat; iSat++) {
482 line = _stream->readLine();
483 pos = 0;
484 for (int iType = 0; iType < _header.nTypes(_currEpo.rnxSat[iSat].satSys); iType++) {
485 if (iType > 0 && iType % 5 == 0) {
486 line = _stream->readLine();
487 pos = 0;
488 }
489 double obsValue = 0.0;
490 int lli = 0;
491 int snr = 0;
492 readDbl(line, pos, 14, obsValue);
493 readInt(line, pos + 14, 1, lli);
494 readInt(line, pos + 15, 1, snr);
495
496 if (_flgPowerFail) {
497 lli |= 1;
498 }
499
500 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
501 _currEpo.rnxSat[iSat].lli.push_back(lli);
502 _currEpo.rnxSat[iSat].snr.push_back(snr);
503
504 pos += 16;
505 }
506 }
507
508 _flgPowerFail = false;
509
510 return &_currEpo;
511 }
512
513 return 0;
514}
515
516// Set Header Information
517////////////////////////////////////////////////////////////////////////////
518void t_rnxObsFile::setHeader(const t_rnxObsHeader& header) {
519 _header._version = header._version;
520 _header._interval = header._interval;
521 _header._antennaName = header._antennaName;
522 _header._markerName = header._markerName;
523 _header._antNEU = header._antNEU;
524 _header._antXYZ = header._antXYZ;
525 _header._antBSG = header._antBSG;
526 _header._xyz = header._xyz;
527 for (unsigned iPrn = 1; iPrn <= MAXPRN_GPS; iPrn++) {
528 _header._wlFactorsL1[iPrn] = header._wlFactorsL1[iPrn];
529 _header._wlFactorsL2[iPrn] = header._wlFactorsL2[iPrn];
530 }
531 _header._startTime = header._startTime;
532 for (unsigned ii = 0; ii < header._obsTypesV2.size(); ii++) {
533 _header._obsTypesV2.push_back(header._obsTypesV2[ii]);
534 }
535 map<char, vector<QString> >::const_iterator it;
536 for (it = header._obsTypesV3.begin(); it != header._obsTypesV3.end(); it++) {
537 char sys = it->first;
538 const vector<QString>& typesV3 = it->second;
539 for (unsigned ii = 0; ii < typesV3.size(); ii++) {
540 _header._obsTypesV3[sys].push_back(typesV3[ii]);
541 }
542 }
543}
544
545// Write Header
546////////////////////////////////////////////////////////////////////////////
547void t_rnxObsFile::writeHeader() {
548 *_stream << QString("%1 OBSERVATION DATA M (MIXED) ")
549 .arg(_header._version, 9, 'f', 2)
550 << "RINEX VERSION / TYPE\n";
551
552 *_stream << QString("%1%2%3")
553 .arg("BNC", -20)
554 .arg("BKG", -20)
555 .arg(currentDateAndTimeGPS().date().toString("dd-MMM-yyyy"), -20)
556 << "PGM / RUN BY / DATE\n";
557}
558
559// Write Data Epoch
560////////////////////////////////////////////////////////////////////////////
561void t_rnxObsFile::writeEpoch(const t_rnxEpo* epo) {
562 *_stream << "Epoch " << epo->tt.datestr().c_str() << " "
563 << epo->tt.timestr().c_str() << endl;
564}
Note: See TracBrowser for help on using the repository browser.