source: ntrip/trunk/BNC/bncrinex.cpp@ 300

Last change on this file since 300 was 300, checked in by mervart, 17 years ago

* empty log message *

File size: 13.5 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters,
3// written by Leos Mervart.
4//
5// Copyright (C) 2006
6// German Federal Agency for Cartography and Geodesy (BKG)
7// http://www.bkg.bund.de
8// Czech Technical University Prague, Department of Advanced Geodesy
9// http://www.fsv.cvut.cz
10//
11// Email: euref-ip@bkg.bund.de
12//
13// This program is free software; you can redistribute it and/or
14// modify it under the terms of the GNU General Public License
15// as published by the Free Software Foundation, version 2.
16//
17// This program is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with this program; if not, write to the Free Software
24// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26/* -------------------------------------------------------------------------
27 * BKG NTRIP Client
28 * -------------------------------------------------------------------------
29 *
30 * Class: bncRinex
31 *
32 * Purpose: writes RINEX files
33 *
34 * Author: L. Mervart
35 *
36 * Created: 27-Aug-2006
37 *
38 * Changes:
39 *
40 * -----------------------------------------------------------------------*/
41
42#include <stdlib.h>
43#include <iostream>
44#include <iomanip>
45#include <math.h>
46
47#include <QtCore>
48#include <QUrl>
49#include <QString>
50
51#include "bncrinex.h"
52#include "bncapp.h"
53#include "bncutils.h"
54#include "bncconst.h"
55#include "bnctabledlg.h"
56#include "RTCM3/rtcm3torinex.h"
57
58using namespace std;
59
60// Constructor
61////////////////////////////////////////////////////////////////////////////
62bncRinex::bncRinex(const char* StatID, const QUrl& mountPoint) {
63 _statID = StatID;
64 _mountPoint = mountPoint;
65 _headerWritten = false;
66
67 QSettings settings;
68 _rnxScriptName = settings.value("rnxScript").toString();
69 expandEnvVar(_rnxScriptName);
70
71 _pgmName = ((bncApp*)qApp)->bncVersion().leftJustified(20, ' ', true);
72 _userName = QString("${USER}");
73 expandEnvVar(_userName);
74 _userName = _userName.leftJustified(20, ' ', true);
75}
76
77// Destructor
78////////////////////////////////////////////////////////////////////////////
79bncRinex::~bncRinex() {
80 _out.close();
81}
82
83// Read Skeleton Header File
84////////////////////////////////////////////////////////////////////////////
85void bncRinex::readSkeleton() {
86
87 _headerLines.clear();
88
89 // Read the File
90 // -------------
91 QFile skl(_sklName);
92 if ( skl.exists() && skl.open(QIODevice::ReadOnly) ) {
93 QTextStream in(&skl);
94 while ( !in.atEnd() ) {
95 _headerLines.append( in.readLine() );
96 if (_headerLines.last().indexOf("END OF HEADER") != -1) {
97 break;
98 }
99 }
100 }
101
102 // Try to download the skeleton file
103 // ---------------------------------
104 else {
105 QStringList table;
106 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(),
107 table, false);
108 QString net;
109 QStringListIterator it(table);
110 while (it.hasNext()) {
111 QString line = it.next();
112 if (line.indexOf("STR") == 0) {
113 QStringList tags = line.split(";");
114 if (tags.at(1) == _mountPoint.path().mid(1).toAscii()) {
115 net = tags.at(7);
116 break;
117 }
118 }
119 }
120 QString sklDir;
121 it.toFront();
122 while (it.hasNext()) {
123 QString line = it.next();
124 if (line.indexOf("NET") == 0) {
125 QStringList tags = line.split(";");
126 if (tags.at(1) == net) {
127 sklDir = tags.at(6).trimmed();
128 break;
129 }
130 }
131 }
132 if (!sklDir.isEmpty() && sklDir != "none") {
133 cout << _mountPoint.path().mid(1).toAscii().data() << " "
134 << sklDir.toAscii().data() << endl;
135 }
136 }
137}
138
139// File Name according to RINEX Standards
140////////////////////////////////////////////////////////////////////////////
141void bncRinex::resolveFileName(const QDateTime& datTim) {
142
143 QSettings settings;
144 QString path = settings.value("rnxPath").toString();
145 expandEnvVar(path);
146
147 if ( path[path.length()-1] != QDir::separator() ) {
148 path += QDir::separator();
149 }
150
151 QString intStr = settings.value("rnxIntr").toString();
152 QString hlpStr;
153
154 QTime nextTime;
155 QDate nextDate;
156
157 int indHlp = intStr.indexOf("min");
158
159 if ( indHlp != -1) {
160 int step = intStr.left(indHlp-1).toInt();
161 char ch = 'A' + datTim.time().hour();
162 hlpStr = ch;
163 if (datTim.time().minute() >= 60-step) {
164 hlpStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
165 if (datTim.time().hour() < 23) {
166 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
167 nextDate = datTim.date();
168 }
169 else {
170 nextTime.setHMS(0, 0, 0);
171 nextDate = datTim.date().addDays(1);
172 }
173 }
174 else {
175 for (int limit = step; limit <= 60-step; limit += step) {
176 if (datTim.time().minute() < limit) {
177 hlpStr += QString("%1").arg(limit-step, 2, 10, QChar('0'));
178 nextTime.setHMS(datTim.time().hour(), limit, 0);
179 nextDate = datTim.date();
180 break;
181 }
182 }
183 }
184 }
185 else if (intStr == "1 hour") {
186 char ch = 'A' + datTim.time().hour();
187 hlpStr = ch;
188 if (datTim.time().hour() < 23) {
189 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
190 nextDate = datTim.date();
191 }
192 else {
193 nextTime.setHMS(0, 0, 0);
194 nextDate = datTim.date().addDays(1);
195 }
196 }
197 else {
198 hlpStr = "0";
199 nextTime.setHMS(0, 0, 0);
200 nextDate = datTim.date().addDays(1);
201 }
202 _nextCloseEpoch = QDateTime(nextDate, nextTime);
203
204 QString ID4 = _statID.left(4);
205
206 // Check name conflict
207 // -------------------
208 QString distStr;
209 int num = 0;
210 QListIterator<QString> it(settings.value("mountPoints").toStringList());
211 while (it.hasNext()) {
212 QString mp = it.next();
213 if (mp.indexOf(ID4) != -1) {
214 ++num;
215 }
216 }
217 if (num > 1) {
218 distStr = "_" + _statID.mid(4);
219 }
220
221 QString sklExt = settings.value("rnxSkel").toString();
222 if (!sklExt.isEmpty()) {
223 _sklName = path + ID4 + distStr + "." + sklExt;
224 }
225
226 path += ID4 +
227 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
228 hlpStr + distStr + datTim.toString(".yyO");
229
230 _fName = path.toAscii();
231}
232
233// Write RINEX Header
234////////////////////////////////////////////////////////////////////////////
235void bncRinex::writeHeader(const QDateTime& datTim,
236 const QDateTime& datTimNom) {
237
238 // Open the Output File
239 // --------------------
240 resolveFileName(datTimNom);
241
242 // Append to existing file and return
243 // ----------------------------------
244 if ( QFile::exists(_fName) ) {
245 QSettings settings;
246 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
247 _out.open(_fName.data(), ios::app);
248 _out.setf(ios::showpoint | ios::fixed);
249 _headerWritten = true;
250 return;
251 }
252 }
253
254 _out.open(_fName.data());
255 _out.setf(ios::showpoint | ios::fixed);
256
257 // Copy Skeleton Header
258 // --------------------
259 readSkeleton();
260 if (_headerLines.size() > 0) {
261 QStringListIterator it(_headerLines);
262 while (it.hasNext()) {
263 QString line = it.next();
264 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
265 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
266 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
267 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
268 }
269 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
270 _out << " 5 C1 P1 P2 L1 L2"
271 " # / TYPES OF OBSERV" << endl;
272 }
273 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
274 _out << datTim.toString(" yyyy MM dd"
275 " hh mm ss.zzz0000").toAscii().data();
276 _out << " TIME OF FIRST OBS" << endl;
277 QString hlp = QString("STREAM %1").arg(_mountPoint.host() + _mountPoint.path())
278 .leftJustified(60, ' ', true);
279 _out << hlp.toAscii().data() << "COMMENT" << endl;
280 }
281 else {
282 _out << line.toAscii().data() << endl;
283 }
284 }
285 }
286
287 // Write Dummy Header
288 // ------------------
289 else {
290 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
291 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
292
293 _out << " 2.10 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
294 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
295 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
296 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
297 _out.setf(ios::left);
298 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
299 _out << setw(60) << "BKG" << "OBSERVER / AGENCY" << endl;
300 _out << setw(20) << "unknown"
301 << setw(20) << "unknown"
302 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
303 _out << setw(20) << "unknown"
304 << setw(20) << "unknown"
305 << setw(20) << " " << "ANT # / TYPE" << endl;
306 _out.unsetf(ios::left);
307 _out << setw(14) << setprecision(4) << approxPos[0]
308 << setw(14) << setprecision(4) << approxPos[1]
309 << setw(14) << setprecision(4) << approxPos[2]
310 << " " << "APPROX POSITION XYZ" << endl;
311 _out << setw(14) << setprecision(4) << antennaNEU[0]
312 << setw(14) << setprecision(4) << antennaNEU[1]
313 << setw(14) << setprecision(4) << antennaNEU[2]
314 << " " << "ANTENNA: DELTA H/E/N" << endl;
315 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
316 _out << " 5 C1 P1 P2 L1 L2 # / TYPES OF OBSERV" << endl;
317 _out << datTim.toString(" yyyy MM dd"
318 " hh mm ss.zzz0000").toAscii().data();
319 _out << " " << "TIME OF FIRST OBS" << endl;
320 hlp = QString("STREAM %1").arg(_mountPoint.host() + _mountPoint.path())
321 .leftJustified(60, ' ', true);
322 _out << hlp.toAscii().data() << "COMMENT" << endl;
323 _out << " END OF HEADER" << endl;
324 }
325
326 _headerWritten = true;
327}
328
329// Stores Observation into Internal Array
330////////////////////////////////////////////////////////////////////////////
331void bncRinex::deepCopy(const Observation* obs) {
332 Observation* newObs = new Observation();
333 memcpy(newObs, obs, sizeof(*obs));
334 _obs.push_back(newObs);
335}
336
337// Write One Epoch into the RINEX File
338////////////////////////////////////////////////////////////////////////////
339void bncRinex::dumpEpoch(long maxTime) {
340
341 // Select observations older than maxTime
342 // --------------------------------------
343 QList<Observation*> dumpList;
344 QMutableListIterator<Observation*> mIt(_obs);
345 while (mIt.hasNext()) {
346 Observation* ob = mIt.next();
347 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime - 0.05) {
348 dumpList.push_back(ob);
349 mIt.remove();
350 }
351 }
352
353 // Easy Return
354 // -----------
355 if (dumpList.isEmpty()) {
356 return;
357 }
358
359 // Time of Epoch
360 // -------------
361 Observation* fObs = *dumpList.begin();
362 QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
363 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs->GPSWeek,
364 floor(fObs->GPSWeeks+0.5));
365
366 // Close the file
367 // --------------
368 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
369 closeFile();
370 _headerWritten = false;
371 }
372
373 // Write RINEX Header
374 // ------------------
375 if (!_headerWritten) {
376 writeHeader(datTim, datTimNom);
377 }
378
379 _out << datTim.toString(" yy MM dd hh mm ss.zzz0000").toAscii().data()
380 << " " << 0 << setw(3) << dumpList.size();
381
382 QListIterator<Observation*> it(dumpList); int iSat = 0;
383 while (it.hasNext()) {
384 iSat++;
385 Observation* ob = it.next();
386 int prn = ob->SVPRN;
387 if (prn <= PRN_GPS_END) {
388 _out << "G" << setw(2) << prn;
389 }
390 else if (prn >= PRN_GLONASS_START && prn <= PRN_GLONASS_END) {
391 _out << "R" << setw(2) << prn - PRN_GLONASS_START + 1;
392 }
393 else {
394 _out << "R" << setw(2) << prn % 100;
395 }
396 if (iSat == 12 && it.hasNext()) {
397 _out << endl << " ";
398 iSat = 0;
399 }
400 }
401 _out << endl;
402
403 it.toFront();
404 while (it.hasNext()) {
405 Observation* ob = it.next();
406
407 char lli = ' ';
408 char snr = ' ';
409 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
410 _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
411 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
412 _out << setw(14) << setprecision(3) << ob->L1 << lli << snr;
413 _out << setw(14) << setprecision(3) << ob->L2 << lli << snr;
414 _out << endl;
415
416 delete ob;
417 }
418
419 _out.flush();
420}
421
422// Close the Old RINEX File
423////////////////////////////////////////////////////////////////////////////
424void bncRinex::closeFile() {
425 _out.close();
426 if (!_rnxScriptName.isEmpty()) {
427 system( QString(_rnxScriptName + " " + _fName + " &").toAscii().data() );
428 }
429}
Note: See TracBrowser for help on using the repository browser.