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

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

* empty log message *

File size: 12.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>
43
[82]44#include <QSettings>
45#include <QDir>
[156]46#include <QUrl>
[82]47#include <QDate>
48#include <QFile>
49#include <QTextStream>
[75]50#include <iomanip>
[221]51#include <math.h>
[75]52
[73]53#include "bncrinex.h"
[157]54#include "bncapp.h"
[82]55#include "bncutils.h"
[126]56#include "bncconst.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 }
102}
103
104// File Name according to RINEX Standards
105////////////////////////////////////////////////////////////////////////////
[125]106void bncRinex::resolveFileName(const QDateTime& datTim) {
[82]107
108 QSettings settings;
109 QString path = settings.value("rnxPath").toString();
110 expandEnvVar(path);
111
112 if ( path[path.length()-1] != QDir::separator() ) {
113 path += QDir::separator();
114 }
115
[127]116 QString intStr = settings.value("rnxIntr").toString();
117 QString hlpStr;
[129]118
119 QTime nextTime;
120 QDate nextDate;
121
[204]122 int indHlp = intStr.indexOf("min");
123
124 if ( indHlp != -1) {
125 int step = intStr.left(indHlp-1).toInt();
[127]126 char ch = 'A' + datTim.time().hour();
127 hlpStr = ch;
[204]128 if (datTim.time().minute() >= 60-step) {
129 hlpStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
[199]130 if (datTim.time().hour() < 23) {
131 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
132 nextDate = datTim.date();
133 }
134 else {
135 nextTime.setHMS(0, 0, 0);
136 nextDate = datTim.date().addDays(1);
137 }
138 }
139 else {
[204]140 for (int limit = step; limit <= 60-step; limit += step) {
141 if (datTim.time().minute() < limit) {
142 hlpStr += QString("%1").arg(limit-step, 2, 10, QChar('0'));
143 nextTime.setHMS(datTim.time().hour(), limit, 0);
144 nextDate = datTim.date();
145 break;
146 }
[199]147 }
148 }
149 }
[127]150 else if (intStr == "1 hour") {
151 char ch = 'A' + datTim.time().hour();
152 hlpStr = ch;
[129]153 if (datTim.time().hour() < 23) {
154 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
155 nextDate = datTim.date();
156 }
157 else {
158 nextTime.setHMS(0, 0, 0);
159 nextDate = datTim.date().addDays(1);
160 }
[127]161 }
162 else {
163 hlpStr = "0";
[129]164 nextTime.setHMS(0, 0, 0);
165 nextDate = datTim.date().addDays(1);
[127]166 }
[267]167 _nextCloseEpoch = QDateTime(nextDate, nextTime);
[82]168
[183]169 QString ID4 = _statID.left(4);
170
171 // Check name conflict
172 // -------------------
173 QString distStr;
174 int num = 0;
175 QListIterator<QString> it(settings.value("mountPoints").toStringList());
176 while (it.hasNext()) {
177 QString mp = it.next();
178 if (mp.indexOf(ID4) != -1) {
179 ++num;
180 }
181 }
182 if (num > 1) {
183 distStr = "_" + _statID.mid(4);
184 }
185
[276]186 QString sklExt = settings.value("rnxSkel").toString();
187 if (!sklExt.isEmpty()) {
188 _sklName = path + ID4 + distStr + "." + sklExt;
189 }
190
[183]191 path += ID4 +
[127]192 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
[276]193 hlpStr + distStr + datTim.toString(".yyO");
[82]194
195 _fName = path.toAscii();
196}
197
[77]198// Write RINEX Header
199////////////////////////////////////////////////////////////////////////////
[267]200void bncRinex::writeHeader(const QDateTime& datTim,
201 const QDateTime& datTimNom) {
[77]202
203 // Open the Output File
204 // --------------------
[267]205 resolveFileName(datTimNom);
[260]206
207 // Append to existing file and return
208 // ----------------------------------
209 if ( QFile::exists(_fName) ) {
210 QSettings settings;
211 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
212 _out.open(_fName.data(), ios::app);
213 _out.setf(ios::showpoint | ios::fixed);
214 _headerWritten = true;
215 return;
216 }
217 }
218
[82]219 _out.open(_fName.data());
[85]220 _out.setf(ios::showpoint | ios::fixed);
[77]221
[82]222 // Copy Skeleton Header
223 // --------------------
[276]224 readSkeleton();
[82]225 if (_headerLines.size() > 0) {
226 QStringListIterator it(_headerLines);
227 while (it.hasNext()) {
228 QString line = it.next();
[157]229 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
[159]230 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[157]231 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
232 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
233 }
234 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
[225]235 _out << " 5 C1 P1 P2 L1 L2"
236 " # / TYPES OF OBSERV" << endl;
[82]237 }
238 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
[125]239 _out << datTim.toString(" yyyy MM dd"
240 " hh mm ss.zzz0000").toAscii().data();
241 _out << " TIME OF FIRST OBS" << endl;
[256]242 QString hlp = QString("STREAM %1").arg(_mountPoint.host() + _mountPoint.path())
[156]243 .leftJustified(60, ' ', true);
244 _out << hlp.toAscii().data() << "COMMENT" << endl;
[82]245 }
246 else {
247 _out << line.toAscii().data() << endl;
248 }
249 }
250 }
[78]251
[82]252 // Write Dummy Header
253 // ------------------
254 else {
255 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
256 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
257
[255]258 _out << " 2.10 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
[159]259 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[157]260 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
261 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
[86]262 _out.setf(ios::left);
[82]263 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
264 _out << setw(60) << "BKG" << "OBSERVER / AGENCY" << endl;
265 _out << setw(20) << "unknown"
266 << setw(20) << "unknown"
267 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
268 _out << setw(20) << "unknown"
269 << setw(20) << "unknown"
270 << setw(20) << " " << "ANT # / TYPE" << endl;
[86]271 _out.unsetf(ios::left);
[82]272 _out << setw(14) << setprecision(4) << approxPos[0]
273 << setw(14) << setprecision(4) << approxPos[1]
274 << setw(14) << setprecision(4) << approxPos[2]
275 << " " << "APPROX POSITION XYZ" << endl;
276 _out << setw(14) << setprecision(4) << antennaNEU[0]
277 << setw(14) << setprecision(4) << antennaNEU[1]
278 << setw(14) << setprecision(4) << antennaNEU[2]
279 << " " << "ANTENNA: DELTA H/E/N" << endl;
280 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
[225]281 _out << " 5 C1 P1 P2 L1 L2 # / TYPES OF OBSERV" << endl;
[125]282 _out << datTim.toString(" yyyy MM dd"
283 " hh mm ss.zzz0000").toAscii().data();
284 _out << " " << "TIME OF FIRST OBS" << endl;
[256]285 hlp = QString("STREAM %1").arg(_mountPoint.host() + _mountPoint.path())
[157]286 .leftJustified(60, ' ', true);
[156]287 _out << hlp.toAscii().data() << "COMMENT" << endl;
[82]288 _out << " END OF HEADER" << endl;
289 }
[78]290
[77]291 _headerWritten = true;
292}
293
[73]294// Stores Observation into Internal Array
295////////////////////////////////////////////////////////////////////////////
296void bncRinex::deepCopy(const Observation* obs) {
[74]297 Observation* newObs = new Observation();
298 memcpy(newObs, obs, sizeof(*obs));
299 _obs.push_back(newObs);
[73]300}
301
302// Write One Epoch into the RINEX File
303////////////////////////////////////////////////////////////////////////////
[160]304void bncRinex::dumpEpoch(long maxTime) {
[73]305
[160]306 // Select observations older than maxTime
307 // --------------------------------------
308 QList<Observation*> dumpList;
309 QMutableListIterator<Observation*> mIt(_obs);
310 while (mIt.hasNext()) {
311 Observation* ob = mIt.next();
[238]312 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime - 0.05) {
[160]313 dumpList.push_back(ob);
314 mIt.remove();
315 }
316 }
317
[75]318 // Easy Return
319 // -----------
[160]320 if (dumpList.isEmpty()) {
[75]321 return;
322 }
323
324 // Time of Epoch
325 // -------------
[222]326 Observation* fObs = *dumpList.begin();
[267]327 QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
328 QDateTime datTimNom = dateAndTimeFromGPSweek(fObs->GPSWeek,
329 floor(fObs->GPSWeeks+0.5));
[75]330
[130]331 // Close the file
332 // --------------
[267]333 if (_nextCloseEpoch.isValid() && datTimNom >= _nextCloseEpoch) {
[130]334 closeFile();
335 _headerWritten = false;
336 }
337
[78]338 // Write RINEX Header
339 // ------------------
340 if (!_headerWritten) {
[267]341 writeHeader(datTim, datTimNom);
[78]342 }
343
[131]344 _out << datTim.toString(" yy MM dd hh mm ss.zzz0000").toAscii().data()
[160]345 << " " << 0 << setw(3) << dumpList.size();
[75]346
[160]347 QListIterator<Observation*> it(dumpList); int iSat = 0;
[74]348 while (it.hasNext()) {
[75]349 iSat++;
350 Observation* ob = it.next();
[223]351 int prn = ob->SVPRN;
352 if (prn <= PRN_GPS_END) {
353 _out << "G" << setw(2) << prn;
354 }
355 else if (prn >= PRN_GLONASS_START && prn <= PRN_GLONASS_END) {
356 _out << "R" << setw(2) << prn - PRN_GLONASS_START + 1;
357 }
358 else {
[224]359 _out << "R" << setw(2) << prn % 100;
[223]360 }
[75]361 if (iSat == 12 && it.hasNext()) {
[77]362 _out << endl << " ";
[75]363 iSat = 0;
364 }
[74]365 }
[77]366 _out << endl;
[75]367
368 it.toFront();
369 while (it.hasNext()) {
370 Observation* ob = it.next();
[77]371
372 char lli = ' ';
373 char snr = ' ';
374 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
[225]375 _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
[77]376 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
[222]377 _out << setw(14) << setprecision(3) << ob->L1 << lli << snr;
378 _out << setw(14) << setprecision(3) << ob->L2 << lli << snr;
[77]379 _out << endl;
380
[75]381 delete ob;
382 }
383
[77]384 _out.flush();
[73]385}
386
[130]387// Close the Old RINEX File
388////////////////////////////////////////////////////////////////////////////
389void bncRinex::closeFile() {
390 _out.close();
[132]391 if (!_rnxScriptName.isEmpty()) {
[292]392 system( QString(_rnxScriptName + " " + _fName + " &").toAscii().data() );
[132]393 }
[130]394}
Note: See TracBrowser for help on using the repository browser.