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

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

* empty log message *

File size: 16.3 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: bncRinex
30 *
31 * Purpose: writes RINEX files
32 *
33 * Author: L. Mervart
34 *
35 * Created: 27-Aug-2006
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <stdlib.h>
42#include <iostream>
43#include <iomanip>
44#include <math.h>
45
46#include <QtCore>
47#include <QUrl>
48#include <QString>
49
50#include "bncrinex.h"
51#include "bncapp.h"
52#include "bncutils.h"
53#include "bncconst.h"
54#include "bnctabledlg.h"
55#include "bncgetthread.h"
56#include "RTCM3/rtcm3torinex.h"
57
58using namespace std;
59
60// Constructor
61////////////////////////////////////////////////////////////////////////////
62bncRinex::bncRinex(const QByteArray& statID, const QUrl& mountPoint,
63 const QByteArray& format, const QByteArray& latitude,
64 const QByteArray& longitude, const QByteArray& nmea) {
65 _statID = statID;
66 _mountPoint = mountPoint;
67 _format = format.left(6);
68 _latitude = latitude;
69 _longitude = longitude;
70 _nmea = nmea;
71 _headerWritten = false;
72 _reconnectFlag = false;
73
74 QSettings settings;
75 _rnxScriptName = settings.value("rnxScript").toString();
76 expandEnvVar(_rnxScriptName);
77
78 _pgmName = ((bncApp*)qApp)->bncVersion().leftJustified(20, ' ', true);
79#ifdef WIN32
80 _userName = QString("${USERNAME}");
81#else
82 _userName = QString("${USER}");
83#endif
84 expandEnvVar(_userName);
85 _userName = _userName.leftJustified(20, ' ', true);
86
87 if ( Qt::CheckState(settings.value("rnxV3").toInt()) == Qt::Checked) {
88 _rinexVers = 3;
89 }
90 else {
91 _rinexVers = 2;
92 }
93}
94
95// Destructor
96////////////////////////////////////////////////////////////////////////////
97bncRinex::~bncRinex() {
98 _out.close();
99}
100
101// Download Skeleton Header File
102////////////////////////////////////////////////////////////////////////////
103t_irc bncRinex::downloadSkeleton() {
104
105 t_irc irc = failure;
106
107 QStringList table;
108 bncTableDlg::getFullTable(_mountPoint.host(), _mountPoint.port(),
109 table, false);
110 QString net;
111 QStringListIterator it(table);
112 while (it.hasNext()) {
113 QString line = it.next();
114 if (line.indexOf("STR") == 0) {
115 QStringList tags = line.split(";");
116 if (tags.at(1) == _mountPoint.path().mid(1).toAscii()) {
117 net = tags.at(7);
118 break;
119 }
120 }
121 }
122 QString sklDir;
123 it.toFront();
124 while (it.hasNext()) {
125 QString line = it.next();
126 if (line.indexOf("NET") == 0) {
127 QStringList tags = line.split(";");
128 if (tags.at(1) == net) {
129 sklDir = tags.at(6).trimmed();
130 break;
131 }
132 }
133 }
134 if (!sklDir.isEmpty() && sklDir != "none") {
135 QUrl url(sklDir + "/" + _mountPoint.path().mid(1,4).toLower() + ".skl");
136 if (url.port() == -1) {
137 url.setPort(80);
138 }
139
140 const int timeOut = 10*1000;
141 QString msg;
142 QByteArray _latitude;
143 QByteArray _longitude;
144 QByteArray _nmea;
145 QTcpSocket* socket = bncGetThread::request(url, _latitude, _longitude, _nmea, timeOut, msg);
146
147 if (socket) {
148 _headerLines.clear();
149 bool firstLineRead = false;
150 while (true) {
151 if (socket->canReadLine()) {
152 irc = success;
153 QString line = socket->readLine();
154 line.chop(1);
155 if (line.indexOf("RINEX VERSION") != -1) {
156 _headerLines.append(" 2.11 OBSERVATION DATA"
157 " M (MIXED)"
158 " RINEX VERSION / TYPE");
159 _headerLines.append("PGM / RUN BY / DATE");
160 firstLineRead = true;
161 }
162 else if (firstLineRead) {
163 if (line.indexOf("END OF HEADER") != -1) {
164 _headerLines.append("# / TYPES OF OBSERV");
165 _headerLines.append(
166 QString(" 1 1").leftJustified(60, ' ', true) +
167 "WAVELENGTH FACT L1/2");
168 _headerLines.append("TIME OF FIRST OBS");
169 _headerLines.append( line );
170 break;
171 }
172 else {
173 _headerLines.append( line );
174 }
175 }
176 }
177 else {
178 socket->waitForReadyRead(timeOut);
179 if (socket->bytesAvailable() > 0) {
180 continue;
181 }
182 else {
183 break;
184 }
185 }
186 }
187 delete socket;
188 }
189 }
190 return irc;
191}
192
193// Read Skeleton Header File
194////////////////////////////////////////////////////////////////////////////
195void bncRinex::readSkeleton() {
196
197 // Read the local file
198 // -------------------
199 QFile skl(_sklName);
200 if ( skl.exists() && skl.open(QIODevice::ReadOnly) ) {
201 _headerLines.clear();
202 QTextStream in(&skl);
203 while ( !in.atEnd() ) {
204 _headerLines.append( in.readLine() );
205 if (_headerLines.last().indexOf("END OF HEADER") != -1) {
206 break;
207 }
208 }
209 }
210
211 // Read downloaded file
212 // --------------------
213 else {
214 QDate currDate = QDate::currentDate();
215 if ( !_skeletonDate.isValid() || _skeletonDate != currDate ) {
216 if ( downloadSkeleton() == success) {
217 _skeletonDate = currDate;
218 }
219 }
220 }
221}
222
223// File Name according to RINEX Standards
224////////////////////////////////////////////////////////////////////////////
225void bncRinex::resolveFileName(const QDateTime& datTim) {
226
227 QSettings settings;
228 QString path = settings.value("rnxPath").toString();
229 expandEnvVar(path);
230
231 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
232 path += QDir::separator();
233 }
234
235 QString intStr = settings.value("rnxIntr").toString();
236 QString hlpStr;
237
238 QTime nextTime;
239 QDate nextDate;
240
241 int indHlp = intStr.indexOf("min");
242
243 if ( indHlp != -1) {
244 int step = intStr.left(indHlp-1).toInt();
245 char ch = 'A' + datTim.time().hour();
246 hlpStr = ch;
247 if (datTim.time().minute() >= 60-step) {
248 hlpStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
249 if (datTim.time().hour() < 23) {
250 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
251 nextDate = datTim.date();
252 }
253 else {
254 nextTime.setHMS(0, 0, 0);
255 nextDate = datTim.date().addDays(1);
256 }
257 }
258 else {
259 for (int limit = step; limit <= 60-step; limit += step) {
260 if (datTim.time().minute() < limit) {
261 hlpStr += QString("%1").arg(limit-step, 2, 10, QChar('0'));
262 nextTime.setHMS(datTim.time().hour(), limit, 0);
263 nextDate = datTim.date();
264 break;
265 }
266 }
267 }
268 }
269 else if (intStr == "1 hour") {
270 char ch = 'A' + datTim.time().hour();
271 hlpStr = ch;
272 if (datTim.time().hour() < 23) {
273 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
274 nextDate = datTim.date();
275 }
276 else {
277 nextTime.setHMS(0, 0, 0);
278 nextDate = datTim.date().addDays(1);
279 }
280 }
281 else {
282 hlpStr = "0";
283 nextTime.setHMS(0, 0, 0);
284 nextDate = datTim.date().addDays(1);
285 }
286 _nextCloseEpoch = QDateTime(nextDate, nextTime);
287
288 QString ID4 = _statID.left(4);
289
290 // Check name conflict
291 // -------------------
292 QString distStr;
293 int num = 0;
294 QListIterator<QString> it(settings.value("mountPoints").toStringList());
295 while (it.hasNext()) {
296 QString mp = it.next();
297 if (mp.indexOf(ID4) != -1) {
298 ++num;
299 }
300 }
301 if (num > 1) {
302 distStr = "_" + _statID.mid(4);
303 }
304
305 QString sklExt = settings.value("rnxSkel").toString();
306 if (!sklExt.isEmpty()) {
307 _sklName = path + ID4 + distStr + "." + sklExt;
308 }
309
310 path += ID4 +
311 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
312 hlpStr + distStr + datTim.toString(".yyO");
313
314 _fName = path.toAscii();
315}
316
317// Write RINEX Header
318////////////////////////////////////////////////////////////////////////////
319void bncRinex::writeHeader(const QDateTime& datTim,
320 const QDateTime& datTimNom) {
321
322 QSettings settings;
323
324 // Open the Output File
325 // --------------------
326 resolveFileName(datTimNom);
327
328 // Append to existing file and return
329 // ----------------------------------
330 if ( QFile::exists(_fName) ) {
331 if (_reconnectFlag ||
332 Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
333 _out.open(_fName.data(), ios::app);
334 _out.setf(ios::showpoint | ios::fixed);
335 _headerWritten = true;
336 _reconnectFlag = false;
337 return;
338 }
339 }
340
341 _out.open(_fName.data());
342 _out.setf(ios::showpoint | ios::fixed);
343
344 // Copy Skeleton Header
345 // --------------------
346 readSkeleton();
347 if (_headerLines.size() > 0) {
348 QStringListIterator it(_headerLines);
349 while (it.hasNext()) {
350 QString line = it.next();
351 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
352 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
353 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
354 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
355 }
356 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
357 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2"
358 " # / TYPES OF OBSERV" << endl;
359 }
360 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
361 _out << datTim.toString(" yyyy MM dd"
362 " hh mm ss.zzz0000").toAscii().data();
363 _out << " TIME OF FIRST OBS" << endl;
364 QString hlp = (_format + QString(" %1").arg(_mountPoint.host() +
365 _mountPoint.path())).leftJustified(60, ' ', true);
366 _out << hlp.toAscii().data() << "COMMENT" << endl;
367 }
368 else {
369 _out << line.toAscii().data() << endl;
370 }
371 }
372 }
373
374 // Write Dummy Header
375 // ------------------
376 else {
377 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
378 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
379
380 _out << " 2.11 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
381 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
382 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
383 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
384 _out.setf(ios::left);
385 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
386 _out << setw(60) << "unknown unknown" << "OBSERVER / AGENCY" << endl;
387 _out << setw(20) << "unknown"
388 << setw(20) << "unknown"
389 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
390 _out << setw(20) << "unknown"
391 << setw(20) << "unknown"
392 << setw(20) << " " << "ANT # / TYPE" << endl;
393 _out.unsetf(ios::left);
394 _out << setw(14) << setprecision(4) << approxPos[0]
395 << setw(14) << setprecision(4) << approxPos[1]
396 << setw(14) << setprecision(4) << approxPos[2]
397 << " " << "APPROX POSITION XYZ" << endl;
398 _out << setw(14) << setprecision(4) << antennaNEU[0]
399 << setw(14) << setprecision(4) << antennaNEU[1]
400 << setw(14) << setprecision(4) << antennaNEU[2]
401 << " " << "ANTENNA: DELTA H/E/N" << endl;
402 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
403 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2 # / TYPES OF OBSERV" << endl;
404 _out << datTim.toString(" yyyy MM dd"
405 " hh mm ss.zzz0000").toAscii().data();
406 _out << " " << "TIME OF FIRST OBS" << endl;
407 hlp = (_format + QString(" %1").arg(_mountPoint.host() +
408 _mountPoint.path())).leftJustified(60, ' ', true);
409 _out << hlp.toAscii().data() << "COMMENT" << endl;
410
411 if (_nmea == "yes") {
412 hlp = ("NMEA LAT=" + _latitude + " " + "LONG=" + _longitude).leftJustified(60, ' ',true);
413 _out << hlp.toAscii().data() << "COMMENT" << endl; }
414
415 _out << " END OF HEADER" << endl;
416 }
417
418 _headerWritten = true;
419}
420
421// Stores Observation into Internal Array
422////////////////////////////////////////////////////////////////////////////
423void bncRinex::deepCopy(const Observation* obs) {
424 Observation* newObs = new Observation();
425 memcpy(newObs, obs, sizeof(*obs));
426 _obs.push_back(newObs);
427}
428
429// Write One Epoch into the RINEX File
430////////////////////////////////////////////////////////////////////////////
431void bncRinex::dumpEpoch(long maxTime) {
432
433 // Select observations older than maxTime
434 // --------------------------------------
435 QList<Observation*> dumpList;
436 QMutableListIterator<Observation*> mIt(_obs);
437 while (mIt.hasNext()) {
438 Observation* ob = mIt.next();
439 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime - 0.05) {
440 dumpList.push_back(ob);
441 mIt.remove();
442 }
443 }
444
445 // Easy Return
446 // -----------
447 if (dumpList.isEmpty()) {
448 return;
449 }
450
451 // Time of Epoch
452 // -------------
453 Observation* fObs = *dumpList.begin();
454 QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
455 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs->GPSWeek,
456 floor(fObs->GPSWeeks+0.5));
457
458 // Close the file
459 // --------------
460 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
461 closeFile();
462 _headerWritten = false;
463 }
464
465 // Write RINEX Header
466 // ------------------
467 if (!_headerWritten) {
468 writeHeader(datTim, datTimNom);
469 }
470
471 double sec = double(datTim.time().second()) + fmod(fObs->GPSWeeks,1.0);
472 _out << datTim.toString(" yy MM dd hh mm ").toAscii().data()
473 << setw(10) << setprecision(7) << sec
474 << " " << 0 << setw(3) << dumpList.size();
475
476 QSettings settings;
477
478 QListIterator<Observation*> it(dumpList); int iSat = 0;
479 while (it.hasNext()) {
480 iSat++;
481 Observation* ob = it.next();
482 _out << ob->satSys << setw(2) << ob->satNum;
483 if (iSat == 12 && it.hasNext()) {
484 _out << endl << " ";
485 iSat = 0;
486 }
487 }
488 _out << endl;
489
490 it.toFront();
491 while (it.hasNext()) {
492 Observation* ob = it.next();
493
494 char lli = ' ';
495 char snr = ' ';
496 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
497 _out << setw(14) << setprecision(3) << ob->C2 << lli << snr;
498 _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
499 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
500 _out << setw(14) << setprecision(3) << ob->L1 << lli
501 << setw(1) << ob->SNR1 << endl;
502 _out << setw(14) << setprecision(3) << ob->L2 << lli
503 << setw(1) << ob->SNR2;
504 _out << setw(14) << setprecision(3) << ob->S1 ;
505 _out << setw(16) << setprecision(3) << ob->S2 ;
506 _out << endl;
507
508 delete ob;
509 }
510
511 _out.flush();
512}
513
514// Close the Old RINEX File
515////////////////////////////////////////////////////////////////////////////
516void bncRinex::closeFile() {
517 QMutexLocker locker(&_mutex);
518 _out.close();
519 if (!_rnxScriptName.isEmpty()) {
520
521#ifdef WIN32
522 QProcess::startDetached(_rnxScriptName, QStringList() << _fName) ;
523#else
524 QProcess::startDetached("nohup", QStringList() << _rnxScriptName << _fName) ;
525#endif
526
527 }
528}
Note: See TracBrowser for help on using the repository browser.