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

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

* empty log message *

File size: 16.2 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 _headerLines.clear();
211 if ( downloadSkeleton() == success) {
212 _skeletonDate = currDate;
213 }
214 }
215 }
216}
217
218// File Name according to RINEX Standards
219////////////////////////////////////////////////////////////////////////////
220void bncRinex::resolveFileName(const QDateTime& datTim) {
221
222 QSettings settings;
223 QString path = settings.value("rnxPath").toString();
224 expandEnvVar(path);
225
226 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
227 path += QDir::separator();
228 }
229
230 QString intStr = settings.value("rnxIntr").toString();
231 QString hlpStr;
232
233 QTime nextTime;
234 QDate nextDate;
235
236 int indHlp = intStr.indexOf("min");
237
238 if ( indHlp != -1) {
239 int step = intStr.left(indHlp-1).toInt();
240 char ch = 'A' + datTim.time().hour();
241 hlpStr = ch;
242 if (datTim.time().minute() >= 60-step) {
243 hlpStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
244 if (datTim.time().hour() < 23) {
245 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
246 nextDate = datTim.date();
247 }
248 else {
249 nextTime.setHMS(0, 0, 0);
250 nextDate = datTim.date().addDays(1);
251 }
252 }
253 else {
254 for (int limit = step; limit <= 60-step; limit += step) {
255 if (datTim.time().minute() < limit) {
256 hlpStr += QString("%1").arg(limit-step, 2, 10, QChar('0'));
257 nextTime.setHMS(datTim.time().hour(), limit, 0);
258 nextDate = datTim.date();
259 break;
260 }
261 }
262 }
263 }
264 else if (intStr == "1 hour") {
265 char ch = 'A' + datTim.time().hour();
266 hlpStr = ch;
267 if (datTim.time().hour() < 23) {
268 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
269 nextDate = datTim.date();
270 }
271 else {
272 nextTime.setHMS(0, 0, 0);
273 nextDate = datTim.date().addDays(1);
274 }
275 }
276 else {
277 hlpStr = "0";
278 nextTime.setHMS(0, 0, 0);
279 nextDate = datTim.date().addDays(1);
280 }
281 _nextCloseEpoch = QDateTime(nextDate, nextTime);
282
283 QString ID4 = _statID.left(4);
284
285 // Check name conflict
286 // -------------------
287 QString distStr;
288 int num = 0;
289 QListIterator<QString> it(settings.value("mountPoints").toStringList());
290 while (it.hasNext()) {
291 QString mp = it.next();
292 if (mp.indexOf(ID4) != -1) {
293 ++num;
294 }
295 }
296 if (num > 1) {
297 distStr = "_" + _statID.mid(4);
298 }
299
300 QString sklExt = settings.value("rnxSkel").toString();
301 if (!sklExt.isEmpty()) {
302 _sklName = path + ID4 + distStr + "." + sklExt;
303 }
304
305 path += ID4 +
306 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
307 hlpStr + distStr + datTim.toString(".yyO");
308
309 _fName = path.toAscii();
310}
311
312// Write RINEX Header
313////////////////////////////////////////////////////////////////////////////
314void bncRinex::writeHeader(const QDateTime& datTim,
315 const QDateTime& datTimNom) {
316
317 QSettings settings;
318
319 // Open the Output File
320 // --------------------
321 resolveFileName(datTimNom);
322
323 // Append to existing file and return
324 // ----------------------------------
325 if ( QFile::exists(_fName) ) {
326 if (_reconnectFlag ||
327 Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
328 _out.open(_fName.data(), ios::app);
329 _out.setf(ios::showpoint | ios::fixed);
330 _headerWritten = true;
331 _reconnectFlag = false;
332 return;
333 }
334 }
335
336 _out.open(_fName.data());
337 _out.setf(ios::showpoint | ios::fixed);
338
339 // Copy Skeleton Header
340 // --------------------
341 readSkeleton();
342 if (_headerLines.size() > 0) {
343 QStringListIterator it(_headerLines);
344 while (it.hasNext()) {
345 QString line = it.next();
346 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
347 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
348 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
349 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
350 }
351 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
352 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2"
353 " # / TYPES OF OBSERV" << endl;
354 }
355 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
356 _out << datTim.toString(" yyyy MM dd"
357 " hh mm ss.zzz0000").toAscii().data();
358 _out << " TIME OF FIRST OBS" << endl;
359 QString hlp = (_format + QString(" %1").arg(_mountPoint.host() +
360 _mountPoint.path())).leftJustified(60, ' ', true);
361 _out << hlp.toAscii().data() << "COMMENT" << endl;
362 }
363 else {
364 _out << line.toAscii().data() << endl;
365 }
366 }
367 }
368
369 // Write Dummy Header
370 // ------------------
371 else {
372 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
373 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
374
375 _out << " 2.11 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
376 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
377 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
378 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
379 _out.setf(ios::left);
380 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
381 _out << setw(60) << "unknown unknown" << "OBSERVER / AGENCY" << endl;
382 _out << setw(20) << "unknown"
383 << setw(20) << "unknown"
384 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
385 _out << setw(20) << "unknown"
386 << setw(20) << "unknown"
387 << setw(20) << " " << "ANT # / TYPE" << endl;
388 _out.unsetf(ios::left);
389 _out << setw(14) << setprecision(4) << approxPos[0]
390 << setw(14) << setprecision(4) << approxPos[1]
391 << setw(14) << setprecision(4) << approxPos[2]
392 << " " << "APPROX POSITION XYZ" << endl;
393 _out << setw(14) << setprecision(4) << antennaNEU[0]
394 << setw(14) << setprecision(4) << antennaNEU[1]
395 << setw(14) << setprecision(4) << antennaNEU[2]
396 << " " << "ANTENNA: DELTA H/E/N" << endl;
397 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
398 _out << " 8 C1 C2 P1 P2 L1 L2 S1 S2 # / TYPES OF OBSERV" << endl;
399 _out << datTim.toString(" yyyy MM dd"
400 " hh mm ss.zzz0000").toAscii().data();
401 _out << " " << "TIME OF FIRST OBS" << endl;
402 hlp = (_format + QString(" %1").arg(_mountPoint.host() +
403 _mountPoint.path())).leftJustified(60, ' ', true);
404 _out << hlp.toAscii().data() << "COMMENT" << endl;
405
406 if (_nmea == "yes") {
407 hlp = ("NMEA LAT=" + _latitude + " " + "LONG=" + _longitude).leftJustified(60, ' ',true);
408 _out << hlp.toAscii().data() << "COMMENT" << endl; }
409
410 _out << " END OF HEADER" << endl;
411 }
412
413 _headerWritten = true;
414}
415
416// Stores Observation into Internal Array
417////////////////////////////////////////////////////////////////////////////
418void bncRinex::deepCopy(const Observation* obs) {
419 Observation* newObs = new Observation();
420 memcpy(newObs, obs, sizeof(*obs));
421 _obs.push_back(newObs);
422}
423
424// Write One Epoch into the RINEX File
425////////////////////////////////////////////////////////////////////////////
426void bncRinex::dumpEpoch(long maxTime) {
427
428 // Select observations older than maxTime
429 // --------------------------------------
430 QList<Observation*> dumpList;
431 QMutableListIterator<Observation*> mIt(_obs);
432 while (mIt.hasNext()) {
433 Observation* ob = mIt.next();
434 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime - 0.05) {
435 dumpList.push_back(ob);
436 mIt.remove();
437 }
438 }
439
440 // Easy Return
441 // -----------
442 if (dumpList.isEmpty()) {
443 return;
444 }
445
446 // Time of Epoch
447 // -------------
448 Observation* fObs = *dumpList.begin();
449 QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
450 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs->GPSWeek,
451 floor(fObs->GPSWeeks+0.5));
452
453 // Close the file
454 // --------------
455 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
456 closeFile();
457 _headerWritten = false;
458 }
459
460 // Write RINEX Header
461 // ------------------
462 if (!_headerWritten) {
463 writeHeader(datTim, datTimNom);
464 }
465
466 double sec = double(datTim.time().second()) + fmod(fObs->GPSWeeks,1.0);
467 _out << datTim.toString(" yy MM dd hh mm ").toAscii().data()
468 << setw(10) << setprecision(7) << sec
469 << " " << 0 << setw(3) << dumpList.size();
470
471 QListIterator<Observation*> it(dumpList); int iSat = 0;
472 while (it.hasNext()) {
473 iSat++;
474 Observation* ob = it.next();
475 _out << ob->satSys << setw(2) << ob->satNum;
476 if (iSat == 12 && it.hasNext()) {
477 _out << endl << " ";
478 iSat = 0;
479 }
480 }
481 _out << endl;
482
483 it.toFront();
484 while (it.hasNext()) {
485 Observation* ob = it.next();
486
487 char lli = ' ';
488 char snr = ' ';
489 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
490 _out << setw(14) << setprecision(3) << ob->C2 << lli << snr;
491 _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
492 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
493 _out << setw(14) << setprecision(3) << ob->L1 << lli
494 << setw(1) << ob->SNR1 << endl;
495 _out << setw(14) << setprecision(3) << ob->L2 << lli
496 << setw(1) << ob->SNR2;
497 _out << setw(14) << setprecision(3) << ob->S1 ;
498 _out << setw(16) << setprecision(3) << ob->S2 ;
499 _out << endl;
500
501 delete ob;
502 }
503
504 _out.flush();
505}
506
507// Close the Old RINEX File
508////////////////////////////////////////////////////////////////////////////
509void bncRinex::closeFile() {
510 _out.close();
511 if (!_rnxScriptName.isEmpty()) {
512 QProcess* cmd = new QProcess();
513 cmd->start(_rnxScriptName, QStringList() << _fName << "&");
514 cmd->waitForFinished(-1);
515 delete cmd;
516 /// system( QString(_rnxScriptName + " " + _fName + " &").toAscii().data() );
517 }
518}
Note: See TracBrowser for help on using the repository browser.