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

Last change on this file since 307 was 307, checked in by mervart, 18 years ago

* empty log message *

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