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

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

* empty log message *

File size: 11.4 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 _nextCloseEpoch = QDateTime(nextDate, nextTime);
151
152 QString ID4 = _statID.left(4);
153
154 // Check name conflict
155 // -------------------
156 QString distStr;
157 int num = 0;
158 QListIterator<QString> it(settings.value("mountPoints").toStringList());
159 while (it.hasNext()) {
160 QString mp = it.next();
161 if (mp.indexOf(ID4) != -1) {
162 ++num;
163 }
164 }
165 if (num > 1) {
166 distStr = "_" + _statID.mid(4);
167 }
168
169 path += ID4 +
170 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
171 hlpStr + distStr +
172 datTim.toString(".yyO");
173
174 _fName = path.toAscii();
175}
176
177// Write RINEX Header
178////////////////////////////////////////////////////////////////////////////
179void bncRinex::writeHeader(const QDateTime& datTim) {
180
181 // Open the Output File
182 // --------------------
183 resolveFileName(datTim);
184
185 // Append to existing file and return
186 // ----------------------------------
187 if ( QFile::exists(_fName) ) {
188 QSettings settings;
189 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
190 _out.open(_fName.data(), ios::app);
191 _out.setf(ios::showpoint | ios::fixed);
192 _headerWritten = true;
193 return;
194 }
195 }
196
197 _out.open(_fName.data());
198 _out.setf(ios::showpoint | ios::fixed);
199
200 // Copy Skeleton Header
201 // --------------------
202 if (_headerLines.size() > 0) {
203 QStringListIterator it(_headerLines);
204 while (it.hasNext()) {
205 QString line = it.next();
206 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
207 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
208 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
209 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
210 }
211 else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
212 _out << " 5 C1 P1 P2 L1 L2"
213 " # / TYPES OF OBSERV" << endl;
214 }
215 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
216 _out << datTim.toString(" yyyy MM dd"
217 " hh mm ss.zzz0000").toAscii().data();
218 _out << " TIME OF FIRST OBS" << endl;
219 QString hlp = QString("STREAM %1").arg(_mountPoint.host() + _mountPoint.path())
220 .leftJustified(60, ' ', true);
221 _out << hlp.toAscii().data() << "COMMENT" << endl;
222 }
223 else {
224 _out << line.toAscii().data() << endl;
225 }
226 }
227 }
228
229 // Write Dummy Header
230 // ------------------
231 else {
232 double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
233 double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
234
235 _out << " 2.10 OBSERVATION DATA M (MIXED) RINEX VERSION / TYPE" << endl;
236 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
237 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
238 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
239 _out.setf(ios::left);
240 _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
241 _out << setw(60) << "BKG" << "OBSERVER / AGENCY" << endl;
242 _out << setw(20) << "unknown"
243 << setw(20) << "unknown"
244 << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
245 _out << setw(20) << "unknown"
246 << setw(20) << "unknown"
247 << setw(20) << " " << "ANT # / TYPE" << endl;
248 _out.unsetf(ios::left);
249 _out << setw(14) << setprecision(4) << approxPos[0]
250 << setw(14) << setprecision(4) << approxPos[1]
251 << setw(14) << setprecision(4) << approxPos[2]
252 << " " << "APPROX POSITION XYZ" << endl;
253 _out << setw(14) << setprecision(4) << antennaNEU[0]
254 << setw(14) << setprecision(4) << antennaNEU[1]
255 << setw(14) << setprecision(4) << antennaNEU[2]
256 << " " << "ANTENNA: DELTA H/E/N" << endl;
257 _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
258 _out << " 5 C1 P1 P2 L1 L2 # / TYPES OF OBSERV" << endl;
259 _out << datTim.toString(" yyyy MM dd"
260 " hh mm ss.zzz0000").toAscii().data();
261 _out << " " << "TIME OF FIRST OBS" << endl;
262 hlp = QString("STREAM %1").arg(_mountPoint.host() + _mountPoint.path())
263 .leftJustified(60, ' ', true);
264 _out << hlp.toAscii().data() << "COMMENT" << endl;
265 _out << " END OF HEADER" << endl;
266 }
267
268 _headerWritten = true;
269}
270
271// Stores Observation into Internal Array
272////////////////////////////////////////////////////////////////////////////
273void bncRinex::deepCopy(const Observation* obs) {
274 Observation* newObs = new Observation();
275 memcpy(newObs, obs, sizeof(*obs));
276 _obs.push_back(newObs);
277}
278
279// Write One Epoch into the RINEX File
280////////////////////////////////////////////////////////////////////////////
281void bncRinex::dumpEpoch(long maxTime) {
282
283 // Select observations older than maxTime
284 // --------------------------------------
285 QList<Observation*> dumpList;
286 QMutableListIterator<Observation*> mIt(_obs);
287 while (mIt.hasNext()) {
288 Observation* ob = mIt.next();
289 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime - 0.05) {
290 dumpList.push_back(ob);
291 mIt.remove();
292 }
293 }
294
295 // Easy Return
296 // -----------
297 if (dumpList.isEmpty()) {
298 return;
299 }
300
301 // Time of Epoch
302 // -------------
303 Observation* fObs = *dumpList.begin();
304 QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
305
306 // Close the file
307 // --------------
308 if (_nextCloseEpoch.isValid() && datTim >= _nextCloseEpoch) {
309 closeFile();
310 _headerWritten = false;
311 }
312
313 // Write RINEX Header
314 // ------------------
315 if (!_headerWritten) {
316 writeHeader(datTim);
317 }
318
319 _out << datTim.toString(" yy MM dd hh mm ss.zzz0000").toAscii().data()
320 << " " << 0 << setw(3) << dumpList.size();
321
322 QListIterator<Observation*> it(dumpList); int iSat = 0;
323 while (it.hasNext()) {
324 iSat++;
325 Observation* ob = it.next();
326 int prn = ob->SVPRN;
327 if (prn <= PRN_GPS_END) {
328 _out << "G" << setw(2) << prn;
329 }
330 else if (prn >= PRN_GLONASS_START && prn <= PRN_GLONASS_END) {
331 _out << "R" << setw(2) << prn - PRN_GLONASS_START + 1;
332 }
333 else {
334 _out << "R" << setw(2) << prn % 100;
335 }
336 if (iSat == 12 && it.hasNext()) {
337 _out << endl << " ";
338 iSat = 0;
339 }
340 }
341 _out << endl;
342
343 it.toFront();
344 while (it.hasNext()) {
345 Observation* ob = it.next();
346
347 char lli = ' ';
348 char snr = ' ';
349 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
350 _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
351 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
352 _out << setw(14) << setprecision(3) << ob->L1 << lli << snr;
353 _out << setw(14) << setprecision(3) << ob->L2 << lli << snr;
354 _out << endl;
355
356 delete ob;
357 }
358
359 _out.flush();
360}
361
362// Close the Old RINEX File
363////////////////////////////////////////////////////////////////////////////
364void bncRinex::closeFile() {
365 _out.close();
366 if (!_rnxScriptName.isEmpty()) {
367 _rnxScript.start(_rnxScriptName, QStringList() << _fName);
368 }
369}
Note: See TracBrowser for help on using the repository browser.