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

Last change on this file since 360 was 356, checked in by weber, 18 years ago

* empty log message *

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