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

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

* empty log message *

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