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

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

* empty log message *

File size: 11.0 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
26#include "bncrinex.h"
27#include "bncapp.h"
28#include "bncutils.h"
29#include "bncconst.h"
30
31using namespace std;
32
33// Constructor
34////////////////////////////////////////////////////////////////////////////
35bncRinex::bncRinex(const char* StatID) {
36 _statID = StatID;
37 _headerWritten = false;
38 readSkeleton();
39
40 QSettings settings;
41 _rnxScriptName = settings.value("rnxScript").toString();
42 expandEnvVar(_rnxScriptName);
43
44 // Find the corresponding mountPoint
45 // ---------------------------------
46 QListIterator<QString> it(settings.value("mountPoints").toStringList());
47 while (it.hasNext()) {
48 QString hlp = it.next();
49 if (hlp.indexOf(_statID) != -1) {
50 QUrl url(hlp);
51 _mountPoint = url.host() + url.path();
52 break;
53 }
54 }
55
56 _pgmName = ((bncApp*)qApp)->bncVersion().leftJustified(20, ' ', true);
57 _userName = QString("${USER}");
58 expandEnvVar(_userName);
59 _userName = _userName.leftJustified(20, ' ', true);
60}
61
62// Destructor
63////////////////////////////////////////////////////////////////////////////
64bncRinex::~bncRinex() {
65 _out.close();
66}
67
68// Read Skeleton Header File
69////////////////////////////////////////////////////////////////////////////
70void bncRinex::readSkeleton() {
71
72 // Resolve Skeleton File Name
73 // --------------------------
74 QSettings settings;
75 QString sklName = settings.value("rnxPath").toString();
76 expandEnvVar(sklName);
77 if ( sklName[sklName.length()-1] != QDir::separator() ) {
78 sklName += QDir::separator();
79 }
80 sklName += _statID.left(4) + "." + settings.value("rnxSkel").toString();
81
82 // Read the File
83 // -------------
84 QFile skl(sklName);
85 if ( skl.exists() && skl.open(QIODevice::ReadOnly) ) {
86 QTextStream in(&skl);
87 while ( !in.atEnd() ) {
88 _headerLines.append( in.readLine() );
89 if (_headerLines.last().indexOf("END OF HEADER") != -1) {
90 break;
91 }
92 }
93 }
94}
95
96// File Name according to RINEX Standards
97////////////////////////////////////////////////////////////////////////////
98void bncRinex::resolveFileName(const QDateTime& datTim) {
99
100 QSettings settings;
101 QString path = settings.value("rnxPath").toString();
102 expandEnvVar(path);
103
104 if ( path[path.length()-1] != QDir::separator() ) {
105 path += QDir::separator();
106 }
107
108 QString intStr = settings.value("rnxIntr").toString();
109 QString hlpStr;
110
111 QTime nextTime;
112 QDate nextDate;
113
114 if (intStr == "15 min") {
115 char ch = 'A' + datTim.time().hour();
116 hlpStr = ch;
117 if (datTim.time().minute() < 15) {
118 hlpStr += "00";
119 nextTime.setHMS(datTim.time().hour(), 15, 0);
120 nextDate = datTim.date();
121 }
122 else if (datTim.time().minute() < 30) {
123 hlpStr += "15";
124 nextTime.setHMS(datTim.time().hour(), 30, 0);
125 nextDate = datTim.date();
126 }
127 else if (datTim.time().minute() < 45) {
128 hlpStr += "30";
129 nextTime.setHMS(datTim.time().hour(), 45, 0);
130 nextDate = datTim.date();
131 }
132 else {
133 hlpStr += "45";
134 if (datTim.time().hour() < 23) {
135 nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
136 nextDate = datTim.date();
137 }
138 else {
139 nextTime.setHMS(0, 0, 0);
140 nextDate = datTim.date().addDays(1);
141 }
142 }
143 }
144 else if (intStr == "1 hour") {
145 char ch = 'A' + datTim.time().hour();
146 hlpStr = ch;
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 }
155 }
156 else {
157 hlpStr = "0";
158 nextTime.setHMS(0, 0, 0);
159 nextDate = datTim.date().addDays(1);
160 }
161 _nextCloseEpoch = QDateTime(nextDate, nextTime);
162
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 +
181 QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
182 hlpStr + distStr +
183 datTim.toString(".yyO");
184
185 _fName = path.toAscii();
186}
187
188// Write RINEX Header
189////////////////////////////////////////////////////////////////////////////
190void bncRinex::writeHeader(const QDateTime& datTim) {
191
192 // Open the Output File
193 // --------------------
194 resolveFileName(datTim);
195 _out.open(_fName.data());
196 _out.setf(ios::showpoint | ios::fixed);
197
198 // Copy Skeleton Header
199 // --------------------
200 if (_headerLines.size() > 0) {
201 QStringListIterator it(_headerLines);
202 while (it.hasNext()) {
203 QString line = it.next();
204 if (line.indexOf("PGM / RUN BY / DATE") != -1) {
205 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
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) {
210 _out << " 4 P1 P2 L1 L2"
211 " # / TYPES OF OBSERV" << endl;
212 }
213 else if (line.indexOf("TIME OF FIRST OBS") != -1) {
214 _out << datTim.toString(" yyyy MM dd"
215 " hh mm ss.zzz0000").toAscii().data();
216 _out << " TIME OF FIRST OBS" << endl;
217 QString hlp = QString("STREAM %1").arg(_mountPoint)
218 .leftJustified(60, ' ', true);
219 _out << hlp.toAscii().data() << "COMMENT" << endl;
220 }
221 else {
222 _out << line.toAscii().data() << endl;
223 }
224 }
225 }
226
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;
234 QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
235 _out << _pgmName.toAscii().data() << _userName.toAscii().data()
236 << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
237 _out.setf(ios::left);
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;
246 _out.unsetf(ios::left);
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;
256 _out << " 4 P1 P2 L1 L2 # / TYPES OF OBSERV" << endl;
257 _out << datTim.toString(" yyyy MM dd"
258 " hh mm ss.zzz0000").toAscii().data();
259 _out << " " << "TIME OF FIRST OBS" << endl;
260 hlp = QString("STREAM %1").arg(_mountPoint)
261 .leftJustified(60, ' ', true);
262 _out << hlp.toAscii().data() << "COMMENT" << endl;
263 _out << " END OF HEADER" << endl;
264 }
265
266 _headerWritten = true;
267}
268
269// Stores Observation into Internal Array
270////////////////////////////////////////////////////////////////////////////
271void bncRinex::deepCopy(const Observation* obs) {
272 Observation* newObs = new Observation();
273 memcpy(newObs, obs, sizeof(*obs));
274 _obs.push_back(newObs);
275}
276
277// Write One Epoch into the RINEX File
278////////////////////////////////////////////////////////////////////////////
279void bncRinex::dumpEpoch(long maxTime) {
280
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();
287 if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime) {
288 dumpList.push_back(ob);
289 mIt.remove();
290 }
291 }
292
293 // Easy Return
294 // -----------
295 if (dumpList.isEmpty()) {
296 return;
297 }
298
299 // Time of Epoch
300 // -------------
301 Observation* firstObs = *dumpList.begin();
302
303 QDateTime datTim = dateAndTimeFromGPSweek( firstObs->GPSWeek,
304 firstObs->GPSWeeks +
305 fmod(firstObs->sec, 1.0) );
306
307 // Close the file
308 // --------------
309 if (_nextCloseEpoch.isValid() && datTim >= _nextCloseEpoch) {
310 closeFile();
311 _headerWritten = false;
312 }
313
314 // Write RINEX Header
315 // ------------------
316 if (!_headerWritten) {
317 writeHeader(datTim);
318 }
319
320 _out << datTim.toString(" yy MM dd hh mm ss.zzz0000").toAscii().data()
321 << " " << 0 << setw(3) << dumpList.size();
322
323 QListIterator<Observation*> it(dumpList); int iSat = 0;
324 while (it.hasNext()) {
325 iSat++;
326 Observation* ob = it.next();
327 _out << " " << setw(2) << int(ob->SVPRN);
328 if (iSat == 12 && it.hasNext()) {
329 _out << endl << " ";
330 iSat = 0;
331 }
332 }
333 _out << endl;
334
335 it.toFront();
336 while (it.hasNext()) {
337 Observation* ob = it.next();
338
339 char lli = ' ';
340 char snr = ' ';
341 _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
342 _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
343 _out << setw(14) << setprecision(3) << ob->L1 / t_CST::lambda1 << lli << snr;
344 _out << setw(14) << setprecision(3) << ob->L2 / t_CST::lambda2 << lli << snr;
345 _out << endl;
346
347 delete ob;
348 }
349
350 _out.flush();
351}
352
353// Close the Old RINEX File
354////////////////////////////////////////////////////////////////////////////
355void bncRinex::closeFile() {
356 _out.close();
357 if (!_rnxScriptName.isEmpty()) {
358 _rnxScript.start(_rnxScriptName, QStringList() << _fName);
359 }
360}
Note: See TracBrowser for help on using the repository browser.