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

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

* empty log message *

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