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

Last change on this file since 3716 was 3716, checked in by mervart, 12 years ago
File size: 12.1 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 string 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(ifstream* stream, int maxLines) {
74 int numLines = 0;
75 while ( stream->good() ) {
76 string line;
77 getline(*stream, line); ++numLines;
78 if (line.empty()) {
79 continue;
80 }
81 if (line.find("END OF FILE") != string::npos) {
82 break;
83 }
84 string value = line.substr(0,60); stripWhiteSpace(value);
85 string key = line.substr(60); stripWhiteSpace(key);
86 if (key == "END OF HEADER") {
87 break;
88 }
89 else if (key == "RINEX VERSION / TYPE") {
90 istringstream in(value);
91 in >> _version;
92 }
93 else if (key == "MARKER NAME") {
94 _markerName = value;
95 }
96 else if (key == "ANT # / TYPE") {
97 _antennaName = line.substr(20,20); stripWhiteSpace(_antennaName);
98 }
99 else if (key == "INTERVAL") {
100 istringstream in(value);
101 in >> _interval;
102 }
103 else if (key == "WAVELENGTH FACT L1/2") {
104 istringstream in(value);
105 int wlFactL1 = 0;
106 int wlFactL2 = 0;
107 int numSat = 0;
108 in >> wlFactL1 >> wlFactL2 >> numSat;
109 if (numSat == 0) {
110 for (unsigned iPrn = 1; iPrn <= MAXPRN_GPS; iPrn++) {
111 _wlFactorsL1[iPrn] = wlFactL1;
112 _wlFactorsL2[iPrn] = wlFactL2;
113 }
114 }
115 else {
116 for (int ii = 0; ii < numSat; ii++) {
117 string prn; in >> prn;
118 if (prn[0] == 'G') {
119 int iPrn;
120 readInt(prn, 1, 2, iPrn);
121 _wlFactorsL1[iPrn] = wlFactL1;
122 _wlFactorsL2[iPrn] = wlFactL2;
123 }
124 }
125 }
126 }
127 else if (key == "APPROX POSITION XYZ") {
128 istringstream in(value);
129 in >> _xyz[0] >> _xyz[1] >> _xyz[2];
130 }
131 else if (key == "ANTENNA: DELTA H/E/N") {
132 istringstream in(value);
133 in >> _antNEU[2] >> _antNEU[1] >> _antNEU[0];
134 }
135 else if (key == "ANTENNA: DELTA X/Y/Z") {
136 istringstream in(value);
137 in >> _antXYZ[0] >> _antXYZ[1] >> _antXYZ[2];
138 }
139 else if (key == "ANTENNA: B.SIGHT XYZ") {
140 istringstream in(value);
141 in >> _antBSG[0] >> _antBSG[1] >> _antBSG[2];
142 }
143 else if (key == "# / TYPES OF OBSERV") {
144 istringstream in(value);
145 int nTypes;
146 in >> nTypes;
147 _obsTypesV2.clear();
148 for (int ii = 0; ii < nTypes; ii++) {
149 string hlp;
150 in >> hlp;
151 _obsTypesV2.push_back(hlp);
152 }
153 }
154 else if (key == "SYS / # / OBS TYPES") {
155 istringstream* in = new istringstream(value);
156 char sys;
157 int nTypes;
158 *in >> sys >> nTypes;
159 _obsTypesV3[sys].clear();
160 for (int ii = 0; ii < nTypes; ii++) {
161 if (ii > 0 && ii % 13 == 0) {
162 getline(*stream, line); ++numLines;
163 delete in;
164 in = new istringstream(line);
165 }
166 string hlp;
167 *in >> hlp;
168 _obsTypesV3[sys].push_back(hlp);
169 }
170 delete in;
171 }
172 if (maxLines > 0 && numLines == maxLines) {
173 break;
174 }
175 }
176
177 return success;
178}
179
180// Number of Observation Types (satellite-system specific)
181////////////////////////////////////////////////////////////////////////////
182int t_rnxObsFile::t_rnxObsHeader::nTypes(char sys) const {
183 if (_version < 3.0) {
184 return _obsTypesV2.size();
185 }
186 else {
187 map<char, vector<string> >::const_iterator it = _obsTypesV3.find(sys);
188 if (it != _obsTypesV3.end()) {
189 return it->second.size();
190 }
191 else {
192 return 0;
193 }
194 }
195}
196
197// Observation Type (satellite-system specific)
198////////////////////////////////////////////////////////////////////////////
199const string& t_rnxObsFile::t_rnxObsHeader::obsType(char sys, int index) const {
200 if (_version < 3.0) {
201 return _obsTypesV2.at(index);
202 }
203 else {
204 map<char, vector<string> >::const_iterator it = _obsTypesV3.find(sys);
205 if (it != _obsTypesV3.end()) {
206 return it->second.at(index);
207 }
208 else {
209 return _emptyStr;
210 }
211 }
212}
213
214// Constructor
215////////////////////////////////////////////////////////////////////////////
216t_rnxObsFile::t_rnxObsFile(const string& fileName) {
217 _stream = 0;
218 _flgPowerFail = false;
219 open(fileName);
220}
221
222// Open
223////////////////////////////////////////////////////////////////////////////
224void t_rnxObsFile::open(const string& fileName) {
225 _stream = new ifstream(expandEnvVar(fileName).c_str());
226 _header.read(_stream);
227
228 // Guess Observation Interval
229 // --------------------------
230 if (_header._interval == 0.0) {
231 bncTime ttPrev;
232 for (int iEpo = 0; iEpo < 10; iEpo++) {
233 const t_rnxEpo* rnxEpo = nextEpoch();
234 if (!rnxEpo) {
235 throw string("t_rnxObsFile: not enough epochs");
236 }
237 if (iEpo > 0) {
238 double dt = rnxEpo->tt - ttPrev;
239 if (_header._interval == 0.0 || dt < _header._interval) {
240 _header._interval = dt;
241 }
242 }
243 ttPrev = rnxEpo->tt;
244 }
245 _stream->clear();
246 _stream->seekg(0, ios::beg);
247 _header.read(_stream);
248 }
249}
250
251// Destructor
252////////////////////////////////////////////////////////////////////////////
253t_rnxObsFile::~t_rnxObsFile() {
254 close();
255}
256
257// Close
258////////////////////////////////////////////////////////////////////////////
259void t_rnxObsFile::close() {
260 delete _stream; _stream = 0;
261}
262
263// Handle Special Epoch Flag
264////////////////////////////////////////////////////////////////////////////
265void t_rnxObsFile::handleEpochFlag(int flag, const string& line) {
266
267 // Power Failure
268 // -------------
269 if (flag == 1) {
270 _flgPowerFail = true;
271 }
272
273 // Start moving antenna
274 // --------------------
275 else if (flag == 2) {
276 // no action
277 }
278
279 // Re-Read Header
280 // --------------
281 else if (flag == 3 || flag == 4) {
282 int numLines = 0;
283 if (version() < 3.0) {
284 readInt(line, 29, 3, numLines);
285 }
286 else {
287 readInt(line, 32, 3, numLines);
288 }
289 _header.read(_stream, numLines);
290 }
291
292 // Unhandled Flag
293 // --------------
294 else {
295 throw string("t_rnxObsFile: unhandled flag\n" + line);
296 }
297}
298
299// Retrieve single Epoch
300////////////////////////////////////////////////////////////////////////////
301const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpoch() {
302
303 _currEpo.clear();
304
305 if (version() < 3.0) {
306 return nextEpochV2();
307 }
308 else {
309 return nextEpochV3();
310 }
311}
312
313// Retrieve single Epoch (RINEX Version 3)
314////////////////////////////////////////////////////////////////////////////
315const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV3() {
316
317 while ( _stream->good() ) {
318
319 string line;
320
321 getline(*_stream, line);
322
323 if (line.empty()) {
324 continue;
325 }
326
327 int flag = 0;
328 readInt(line, 31, 1, flag);
329 if (flag > 0) {
330 handleEpochFlag(flag, line);
331 continue;
332 }
333
334 istringstream in(line.substr(1));
335
336 // Epoch Time
337 // ----------
338 int year, month, day, hour, min;
339 double sec;
340 in >> year >> month >> day >> hour >> min >> sec;
341 _currEpo.tt.set(hour, min, sec, day, month, year);
342
343 // Number of Satellites
344 // --------------------
345 int numSat;
346 readInt(line, 32, 3, numSat);
347
348 _currEpo.rnxSat.resize(numSat);
349
350 // Observations
351 // ------------
352 for (int iSat = 0; iSat < numSat; iSat++) {
353 getline(*_stream, line);
354 _currEpo.rnxSat[iSat].satSys = line[0];
355 readInt(line, 1, 2, _currEpo.rnxSat[iSat].satNum);
356 char sys = line[0];
357 for (int iType = 0; iType < _header.nTypes(sys); iType++) {
358 int pos = 3 + 16*iType;
359 double obsValue = 0.0;
360 int lli = 0;
361 int snr = 0;
362 readDbl(line, pos, 14, obsValue);
363 readInt(line, pos + 14, 1, lli);
364 readInt(line, pos + 15, 1, snr);
365
366 if (_flgPowerFail) {
367 lli |= 1;
368 }
369
370 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
371 _currEpo.rnxSat[iSat].lli.push_back(lli);
372 _currEpo.rnxSat[iSat].snr.push_back(snr);
373 }
374 }
375
376 _flgPowerFail = false;
377
378 return &_currEpo;
379 }
380
381 return 0;
382}
383
384// Retrieve single Epoch (RINEX Version 2)
385////////////////////////////////////////////////////////////////////////////
386const t_rnxObsFile::t_rnxEpo* t_rnxObsFile::nextEpochV2() {
387
388 while ( _stream->good() ) {
389
390 string line;
391
392 getline(*_stream, line);
393
394 if (line.empty()) {
395 continue;
396 }
397
398 int flag = 0;
399 readInt(line, 28, 1, flag);
400 if (flag > 0) {
401 handleEpochFlag(flag, line);
402 continue;
403 }
404
405 istringstream in(line);
406
407 // Epoch Time
408 // ----------
409 int year, month, day, hour, min;
410 double sec;
411 in >> year >> month >> day >> hour >> min >> sec;
412 if (year < 80) {
413 year += 2000;
414 }
415 else if (year < 100) {
416 year += 1900;
417 }
418 _currEpo.tt.set(hour, min, sec, day, month, year);
419
420 // Number of Satellites
421 // --------------------
422 int numSat;
423 readInt(line, 29, 3, numSat);
424
425 _currEpo.rnxSat.resize(numSat);
426
427 // Read Satellite Numbers
428 // ----------------------
429 int pos = 32;
430 for (int iSat = 0; iSat < numSat; iSat++) {
431 if (iSat > 0 && iSat % 12 == 0) {
432 getline(*_stream, line);
433 pos = 32;
434 }
435
436 _currEpo.rnxSat[iSat].satSys = line[pos];
437 readInt(line, pos + 1, 2, _currEpo.rnxSat[iSat].satNum);
438
439 pos += 3;
440 }
441
442 // Read Observation Records
443 // ------------------------
444 for (int iSat = 0; iSat < numSat; iSat++) {
445 getline(*_stream, line);
446 pos = 0;
447 for (int iType = 0; iType < _header.nTypes(_currEpo.rnxSat[iSat].satSys); iType++) {
448 if (iType > 0 && iType % 5 == 0) {
449 getline(*_stream, line);
450 pos = 0;
451 }
452 double obsValue = 0.0;
453 int lli = 0;
454 int snr = 0;
455 readDbl(line, pos, 14, obsValue);
456 readInt(line, pos + 14, 1, lli);
457 readInt(line, pos + 15, 1, snr);
458
459 if (_flgPowerFail) {
460 lli |= 1;
461 }
462
463 _currEpo.rnxSat[iSat].obs.push_back(obsValue);
464 _currEpo.rnxSat[iSat].lli.push_back(lli);
465 _currEpo.rnxSat[iSat].snr.push_back(snr);
466
467 pos += 16;
468 }
469 }
470
471 _flgPowerFail = false;
472
473 return &_currEpo;
474 }
475
476 return 0;
477}
478
Note: See TracBrowser for help on using the repository browser.