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

Last change on this file since 306 was 304, checked in by mervart, 19 years ago

* empty log message *

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