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

Last change on this file since 3720 was 3720, checked in by mervart, 12 years ago
File size: 12.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 "rinex/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 if (maxLines > 0 && numLines == maxLines) {
172 break;
173 }
174 }
175
176 return success;
177}
178
179// Number of Observation Types (satellite-system specific)
180////////////////////////////////////////////////////////////////////////////
181int t_rnxObsFile::t_rnxObsHeader::nTypes(char sys) const {
182 if (_version < 3.0) {
183 return _obsTypesV2.size();
184 }
185 else {
186 map<char, vector<QString> >::const_iterator it = _obsTypesV3.find(sys);
187 if (it != _obsTypesV3.end()) {
188 return it->second.size();
189 }
190 else {
191 return 0;
192 }
193 }
194}
195
196// Observation Type (satellite-system specific)
197////////////////////////////////////////////////////////////////////////////
198const QString& t_rnxObsFile::t_rnxObsHeader::obsType(char sys, int index) const {
199 if (_version < 3.0) {
200 return _obsTypesV2.at(index);
201 }
202 else {
203 map<char, vector<QString> >::const_iterator it = _obsTypesV3.find(sys);
204 if (it != _obsTypesV3.end()) {
205 return it->second.at(index);
206 }
207 else {
208 return _emptyStr;
209 }
210 }
211}
212
213// Constructor
214////////////////////////////////////////////////////////////////////////////
215t_rnxObsFile::t_rnxObsFile(const QString& fileName) {
216 _stream = 0;
217 _flgPowerFail = false;
218 open(fileName);
219}
220
221// Open
222////////////////////////////////////////////////////////////////////////////
223void t_rnxObsFile::open(const QString& fileName) {
224
225 _fileName = fileName; expandEnvVar(_fileName);
226 _file = new QFile(_fileName);
227 _file->open(QIODevice::ReadOnly | QIODevice::Text);
228 _stream = new QTextStream();
229 _stream->setDevice(_file);
230
231 _header.read(_stream);
232
233 // Guess Observation Interval
234 // --------------------------
235 if (_header._interval == 0.0) {
236 bncTime ttPrev;
237 for (int iEpo = 0; iEpo < 10; iEpo++) {
238 const t_rnxEpo* rnxEpo = nextEpoch();
239 if (!rnxEpo) {
240 throw QString("t_rnxObsFile: not enough epochs");
241 }
242 if (iEpo > 0) {
243 double dt = rnxEpo->tt - ttPrev;
244 if (_header._interval == 0.0 || dt < _header._interval) {
245 _header._interval = dt;
246 }
247 }
248 ttPrev = rnxEpo->tt;
249 }
250 _stream->seek(0);
251 _header.read(_stream);
252 }
253}
254
255// Destructor
256////////////////////////////////////////////////////////////////////////////
257t_rnxObsFile::~t_rnxObsFile() {
258 close();
259}
260
261// Close
262////////////////////////////////////////////////////////////////////////////
263void t_rnxObsFile::close() {
264 delete _stream; _stream = 0;
265 delete _file; _file = 0;
266}
267
268// Handle Special Epoch Flag
269////////////////////////////////////////////////////////////////////////////
270void t_rnxObsFile::handleEpochFlag(int flag, const QString& line) {
271
272 // Power Failure
273 // -------------
274 if (flag == 1) {
275 _flgPowerFail = true;
276 }
277
278 // Start moving antenna
279 // --------------------
280 else if (flag == 2) {
281 // no action
282 }
283
284 // Re-Read Header
285 // --------------
286 else if (flag == 3 || flag == 4) {
287 int numLines = 0;
288 if (version() < 3.0) {
289 readInt(line, 29, 3, numLines);
290 }
291 else {
292 readInt(line, 32, 3, numLines);
293 }
294 _header.read(_stream, numLines);
295 }
296
297 // Unhandled Flag
298 // --------------
299 else {
300 throw QString("t_rnxObsFile: unhandled flag\n" + line);
301 }
302}
303
304// Retrieve single Epoch
305////////////////////////////////////////////////////////////////////////////
306const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpoch() {
307
308 _currEpo.clear();
309
310 if (version() < 3.0) {
311 return nextEpochV2();
312 }
313 else {
314 return nextEpochV3();
315 }
316}
317
318// Retrieve single Epoch (RINEX Version 3)
319////////////////////////////////////////////////////////////////////////////
320const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV3() {
321
322 while ( _stream->status() == QTextStream::Ok && !_stream->atEnd() ) {
323
324 QString line = _stream->readLine();
325
326 if (line.isEmpty()) {
327 continue;
328 }
329
330 int flag = 0;
331 readInt(line, 31, 1, flag);
332 if (flag > 0) {
333 handleEpochFlag(flag, line);
334 continue;
335 }
336
337 QTextStream in(line.mid(1).toAscii(), QIODevice::ReadOnly);
338
339 // Epoch Time
340 // ----------
341 int year, month, day, hour, min;
342 double sec;
343 in >> year >> month >> day >> hour >> min >> sec;
344 _currEpo.tt.set(year, month, day, hour, min, sec);
345
346 // Number of Satellites
347 // --------------------
348 int numSat;
349 readInt(line, 32, 3, numSat);
350
351 _currEpo.rnxSat.resize(numSat);
352
353 // Observations
354 // ------------
355 for (int iSat = 0; iSat < numSat; iSat++) {
356 line = _stream->readLine();
357 _currEpo.rnxSat[iSat].satSys = line.toAscii()[0];
358 readInt(line, 1, 2, _currEpo.rnxSat[iSat].satNum);
359 char sys = line.toAscii()[0];
360 for (int iType = 0; iType < _header.nTypes(sys); iType++) {
361 int pos = 3 + 16*iType;
362 double obsValue = 0.0;
363 int lli = 0;
364 int snr = 0;
365 readDbl(line, pos, 14, obsValue);
366 readInt(line, pos + 14, 1, lli);
367 readInt(line, pos + 15, 1, snr);
368
369 if (_flgPowerFail) {
370 lli |= 1;
371 }
372
373 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
374 _currEpo.rnxSat[iSat].lli.push_back(lli);
375 _currEpo.rnxSat[iSat].snr.push_back(snr);
376 }
377 }
378
379 _flgPowerFail = false;
380
381 return &_currEpo;
382 }
383
384 return 0;
385}
386
387// Retrieve single Epoch (RINEX Version 2)
388////////////////////////////////////////////////////////////////////////////
389const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV2() {
390
391 while ( _stream->status() == QTextStream::Ok && !_stream->atEnd() ) {
392
393 QString line = _stream->readLine();
394
395 if (line.isEmpty()) {
396 continue;
397 }
398
399 int flag = 0;
400 readInt(line, 28, 1, flag);
401 if (flag > 0) {
402 handleEpochFlag(flag, line);
403 continue;
404 }
405
406 QTextStream in(line.toAscii(), QIODevice::ReadOnly);
407
408 // Epoch Time
409 // ----------
410 int year, month, day, hour, min;
411 double sec;
412 in >> year >> month >> day >> hour >> min >> sec;
413 if (year < 80) {
414 year += 2000;
415 }
416 else if (year < 100) {
417 year += 1900;
418 }
419 _currEpo.tt.set(year, month, day, hour, min, sec);
420
421 // Number of Satellites
422 // --------------------
423 int numSat;
424 readInt(line, 29, 3, numSat);
425
426 _currEpo.rnxSat.resize(numSat);
427
428 // Read Satellite Numbers
429 // ----------------------
430 int pos = 32;
431 for (int iSat = 0; iSat < numSat; iSat++) {
432 if (iSat > 0 && iSat % 12 == 0) {
433 line = _stream->readLine();
434 pos = 32;
435 }
436
437 _currEpo.rnxSat[iSat].satSys = line.toAscii()[pos];
438 readInt(line, pos + 1, 2, _currEpo.rnxSat[iSat].satNum);
439
440 pos += 3;
441 }
442
443 // Read Observation Records
444 // ------------------------
445 for (int iSat = 0; iSat < numSat; iSat++) {
446 line = _stream->readLine();
447 pos = 0;
448 for (int iType = 0; iType < _header.nTypes(_currEpo.rnxSat[iSat].satSys); iType++) {
449 if (iType > 0 && iType % 5 == 0) {
450 line = _stream->readLine();
451 pos = 0;
452 }
453 double obsValue = 0.0;
454 int lli = 0;
455 int snr = 0;
456 readDbl(line, pos, 14, obsValue);
457 readInt(line, pos + 14, 1, lli);
458 readInt(line, pos + 15, 1, snr);
459
460 if (_flgPowerFail) {
461 lli |= 1;
462 }
463
464 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
465 _currEpo.rnxSat[iSat].lli.push_back(lli);
466 _currEpo.rnxSat[iSat].snr.push_back(snr);
467
468 pos += 16;
469 }
470 }
471
472 _flgPowerFail = false;
473
474 return &_currEpo;
475 }
476
477 return 0;
478}
479
Note: See TracBrowser for help on using the repository browser.