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

Last change on this file since 329 was 324, checked in by mervart, 19 years ago

* empty log message *

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