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

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