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

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

* empty log message *

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