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

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

* empty log message *

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