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

Last change on this file since 249 was 238, checked in by mervart, 19 years ago

* empty log message *

File size: 11.3 KB
RevLine 
[73]1
2/* -------------------------------------------------------------------------
[93]3 * BKG NTRIP Client
[73]4 * -------------------------------------------------------------------------
5 *
6 * Class: bncRinex
7 *
8 * Purpose: writes RINEX files
9 *
10 * Author: L. Mervart
11 *
12 * Created: 27-Aug-2006
13 *
14 * Changes:
15 *
16 * -----------------------------------------------------------------------*/
17
[82]18#include <QSettings>
19#include <QDir>
[156]20#include <QUrl>
[82]21#include <QDate>
22#include <QFile>
23#include <QTextStream>
[75]24#include <iomanip>
[221]25#include <math.h>
[75]26
[73]27#include "bncrinex.h"
[157]28#include "bncapp.h"
[82]29#include "bncutils.h"
[126]30#include "bncconst.h"
[223]31#include "RTCM3/rtcm3torinex.h"
[75]32
33using namespace std;
34
[73]35// Constructor
36////////////////////////////////////////////////////////////////////////////
37bncRinex::bncRinex(const char* StatID) {
[77]38 _statID = StatID;
39 _headerWritten = false;
[82]40 readSkeleton();
[132]41
42 QSettings settings;
43 _rnxScriptName = settings.value("rnxScript").toString();
44 expandEnvVar(_rnxScriptName);
[153]45
46 // Find the corresponding mountPoint
47 // ---------------------------------
48 QListIterator<QString> it(settings.value("mountPoints").toStringList());
49 while (it.hasNext()) {
50 QString hlp = it.next();
51 if (hlp.indexOf(_statID) != -1) {
[156]52 QUrl url(hlp);
53 _mountPoint = url.host() + url.path();
[153]54 break;
55 }
56 }
[157]57
58 _pgmName = ((bncApp*)qApp)->bncVersion().leftJustified(20, ' ', true);
[158]59 _userName = QString("${USER}");
[157]60 expandEnvVar(_userName);
[158]61 _userName = _userName.leftJustified(20, ' ', true);
[73]62}
63
64// Destructor
65////////////////////////////////////////////////////////////////////////////
66bncRinex::~bncRinex() {
[77]67 _out.close();
[73]68}
69
[82]70// Read Skeleton Header File
71////////////////////////////////////////////////////////////////////////////
72void bncRinex::readSkeleton() {
73
74 // Resolve Skeleton File Name
75 // --------------------------
76 QSettings settings;
77 QString sklName = settings.value("rnxPath").toString();
78 expandEnvVar(sklName);
79 if ( sklName[sklName.length()-1] != QDir::separator() ) {
80 sklName += QDir::separator();
81 }
82 sklName += _statID.left(4) + "." + settings.value("rnxSkel").toString();
83
84 // Read the File
85 // -------------
86 QFile skl(sklName);
87 if ( skl.exists() && skl.open(QIODevice::ReadOnly) ) {
88 QTextStream in(&skl);
89 while ( !in.atEnd() ) {
90 _headerLines.append( in.readLine() );
91 if (_headerLines.last().indexOf("END OF HEADER") != -1) {
92 break;
93 }
94 }
95 }
96}
97
98// File Name according to RINEX Standards
99////////////////////////////////////////////////////////////////////////////
[125]100void bncRinex::resolveFileName(const QDateTime& datTim) {
[82]101
102 QSettings settings;
103 QString path = settings.value("rnxPath").toString();
104 expandEnvVar(path);
105
106 if ( path[path.length()-1] != QDir::separator() ) {
107 path += QDir::separator();
108 }
109
[127]110 QString intStr = settings.value("rnxIntr").toString();
111 QString hlpStr;
[129]112
113 QTime nextTime;
114 QDate nextDate;
115
[204]116 int indHlp = intStr.indexOf("min");
117
118 if ( indHlp != -1) {
119 int step = intStr.left(indHlp-1).toInt();
[127]120 char ch = 'A' + datTim.time().hour();
121 hlpStr = ch;
[204]122 if (datTim.time().minute() >= 60-step) {
123 hlpStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
[199]124 if (datTim.time().hour() < 23) {
125 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
126 nextDate = datTim.date();
127 }
128 else {
129 nextTime.setHMS(0, 0, 0);
130 nextDate = datTim.date().addDays(1);
131 }
132 }
133 else {
[204]134 for (int limit = step; limit <= 60-step; limit += step) {
135 if (datTim.time().minute() < limit) {
136 hlpStr += QString("%1").arg(limit-step, 2, 10, QChar('0'));
137 nextTime.setHMS(datTim.time().hour(), limit, 0);
138 nextDate = datTim.date();
139 break;
140 }
[199]141 }
142 }
143 }
[127]144 else if (intStr == "1 hour") {
145 char ch = 'A' + datTim.time().hour();
146 hlpStr = ch;
[129]147 if (datTim.time().hour() < 23) {
148 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
149 nextDate = datTim.date();
150 }
151 else {
152 nextTime.setHMS(0, 0, 0);
153 nextDate = datTim.date().addDays(1);
154 }
[127]155 }
156 else {
157 hlpStr = "0";
[129]158 nextTime.setHMS(0, 0, 0);
159 nextDate = datTim.date().addDays(1);
[127]160 }
[129]161 _nextCloseEpoch = QDateTime(nextDate, nextTime);
[82]162
[183]163 QString ID4 = _statID.left(4);
164
165 // Check name conflict
166 // -------------------
167 QString distStr;
168 int num = 0;
169 QListIterator<QString> it(settings.value("mountPoints").toStringList());
170 while (it.hasNext()) {
171 QString mp = it.next();
172 if (mp.indexOf(ID4) != -1) {
173 ++num;
174 }
175 }
176 if (num > 1) {
177 distStr = "_" + _statID.mid(4);
178 }
179
180 path += ID4 +
[127]181 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
[183]182 hlpStr + distStr +
[127]183 datTim.toString(".yyO");
[82]184
185 _fName = path.toAscii();
186}
187
[77]188// Write RINEX Header
189////////////////////////////////////////////////////////////////////////////
[125]190void bncRinex::writeHeader(const QDateTime& datTim) {
[77]191
192 // Open the Output File
193 // --------------------
[125]194 resolveFileName(datTim);
[82]195 _out.open(_fName.data());
[85]196 _out.setf(ios::showpoint | ios::fixed);
[77]197
[82]198 // Copy Skeleton Header
199 // --------------------
200 if (_headerLines.size() > 0) {
201 QStringListIterator it(_headerLines);
202 while (it.hasNext()) {
203 QString line = it.next();
[157]204 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
[159]205 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[157]206 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
207 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
208 }
209 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
[225]210 _out << " 5 C1 P1 P2 L1 L2"
211 " # / TYPES OF OBSERV" << endl;
[82]212 }
213 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
[125]214 _out << datTim.toString(" yyyy MM dd"
215 " hh mm ss.zzz0000").toAscii().data();
216 _out << " TIME OF FIRST OBS" << endl;
[188]217 QString hlp = QString("STREAM %1").arg(_mountPoint)
[156]218 .leftJustified(60, ' ', true);
219 _out << hlp.toAscii().data() << "COMMENT" << endl;
[82]220 }
221 else {
222 _out << line.toAscii().data() << endl;
223 }
224 }
225 }
[78]226
[82]227 // Write Dummy Header
228 // ------------------
229 else {
230 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
231 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
232
233 _out << " 2.10 OBSERVATION DATA G (GPS) RINEX VERSION / TYPE" << endl;
[159]234 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
[157]235 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
236 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
[86]237 _out.setf(ios::left);
[82]238 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
239 _out << setw(60) << "BKG" << "OBSERVER / AGENCY" << endl;
240 _out << setw(20) << "unknown"
241 << setw(20) << "unknown"
242 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
243 _out << setw(20) << "unknown"
244 << setw(20) << "unknown"
245 << setw(20) << " " << "ANT # / TYPE" << endl;
[86]246 _out.unsetf(ios::left);
[82]247 _out << setw(14) << setprecision(4) << approxPos[0]
248 << setw(14) << setprecision(4) << approxPos[1]
249 << setw(14) << setprecision(4) << approxPos[2]
250 << " " << "APPROX POSITION XYZ" << endl;
251 _out << setw(14) << setprecision(4) << antennaNEU[0]
252 << setw(14) << setprecision(4) << antennaNEU[1]
253 << setw(14) << setprecision(4) << antennaNEU[2]
254 << " " << "ANTENNA: DELTA H/E/N" << endl;
255 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
[225]256 _out << " 5 C1 P1 P2 L1 L2 # / TYPES OF OBSERV" << endl;
[125]257 _out << datTim.toString(" yyyy MM dd"
258 " hh mm ss.zzz0000").toAscii().data();
259 _out << " " << "TIME OF FIRST OBS" << endl;
[188]260 hlp = QString("STREAM %1").arg(_mountPoint)
[157]261 .leftJustified(60, ' ', true);
[156]262 _out << hlp.toAscii().data() << "COMMENT" << endl;
[82]263 _out << " END OF HEADER" << endl;
264 }
[78]265
[77]266 _headerWritten = true;
267}
268
[73]269// Stores Observation into Internal Array
270////////////////////////////////////////////////////////////////////////////
271void bncRinex::deepCopy(const Observation* obs) {
[74]272 Observation* newObs = new Observation();
273 memcpy(newObs, obs, sizeof(*obs));
274 _obs.push_back(newObs);
[73]275}
276
277// Write One Epoch into the RINEX File
278////////////////////////////////////////////////////////////////////////////
[160]279void bncRinex::dumpEpoch(long maxTime) {
[73]280
[160]281 // Select observations older than maxTime
282 // --------------------------------------
283 QList<Observation*> dumpList;
284 QMutableListIterator<Observation*> mIt(_obs);
285 while (mIt.hasNext()) {
286 Observation* ob = mIt.next();
[238]287 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime - 0.05) {
[160]288 dumpList.push_back(ob);
289 mIt.remove();
290 }
291 }
292
[75]293 // Easy Return
294 // -----------
[160]295 if (dumpList.isEmpty()) {
[75]296 return;
297 }
298
299 // Time of Epoch
300 // -------------
[222]301 Observation* fObs = *dumpList.begin();
302 QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
[75]303
[130]304 // Close the file
305 // --------------
306 if (_nextCloseEpoch.isValid() && datTim >= _nextCloseEpoch) {
307 closeFile();
308 _headerWritten = false;
309 }
310
[78]311 // Write RINEX Header
312 // ------------------
313 if (!_headerWritten) {
[125]314 writeHeader(datTim);
[78]315 }
316
[131]317 _out << datTim.toString(" yy MM dd hh mm ss.zzz0000").toAscii().data()
[160]318 << " " << 0 << setw(3) << dumpList.size();
[75]319
[160]320 QListIterator<Observation*> it(dumpList); int iSat = 0;
[74]321 while (it.hasNext()) {
[75]322 iSat++;
323 Observation* ob = it.next();
[223]324 int prn = ob->SVPRN;
325 if (prn <= PRN_GPS_END) {
326 _out << "G" << setw(2) << prn;
327 }
328 else if (prn >= PRN_GLONASS_START && prn <= PRN_GLONASS_END) {
329 _out << "R" << setw(2) << prn - PRN_GLONASS_START + 1;
330 }
331 else {
[224]332 _out << "R" << setw(2) << prn % 100;
[223]333 }
[75]334 if (iSat == 12 && it.hasNext()) {
[77]335 _out << endl << " ";
[75]336 iSat = 0;
337 }
[74]338 }
[77]339 _out << endl;
[75]340
341 it.toFront();
342 while (it.hasNext()) {
343 Observation* ob = it.next();
[77]344
345 char lli = ' ';
346 char snr = ' ';
347 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
[225]348 _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
[77]349 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
[222]350 _out << setw(14) << setprecision(3) << ob->L1 << lli << snr;
351 _out << setw(14) << setprecision(3) << ob->L2 << lli << snr;
[77]352 _out << endl;
353
[75]354 delete ob;
355 }
356
[77]357 _out.flush();
[73]358}
359
[130]360// Close the Old RINEX File
361////////////////////////////////////////////////////////////////////////////
362void bncRinex::closeFile() {
363 _out.close();
[132]364 if (!_rnxScriptName.isEmpty()) {
365 _rnxScript.start(_rnxScriptName, QStringList() << _fName);
366 }
[130]367}
Note: See TracBrowser for help on using the repository browser.