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

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

* empty log message *

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