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 |
|
---|
33 | using namespace std;
|
---|
34 |
|
---|
35 | // Constructor
|
---|
36 | ////////////////////////////////////////////////////////////////////////////
|
---|
37 | bncRinex::bncRinex(const char* StatID) {
|
---|
38 | _statID = StatID;
|
---|
39 | _headerWritten = false;
|
---|
40 | readSkeleton();
|
---|
41 |
|
---|
42 | QSettings settings;
|
---|
43 | _rnxScriptName = settings.value("rnxScript").toString();
|
---|
44 | expandEnvVar(_rnxScriptName);
|
---|
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) {
|
---|
52 | QUrl url(hlp);
|
---|
53 | _mountPoint = url.host() + url.path();
|
---|
54 | break;
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | _pgmName = ((bncApp*)qApp)->bncVersion().leftJustified(20, ' ', true);
|
---|
59 | _userName = QString("${USER}");
|
---|
60 | expandEnvVar(_userName);
|
---|
61 | _userName = _userName.leftJustified(20, ' ', true);
|
---|
62 | }
|
---|
63 |
|
---|
64 | // Destructor
|
---|
65 | ////////////////////////////////////////////////////////////////////////////
|
---|
66 | bncRinex::~bncRinex() {
|
---|
67 | _out.close();
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Read Skeleton Header File
|
---|
71 | ////////////////////////////////////////////////////////////////////////////
|
---|
72 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
100 | void bncRinex::resolveFileName(const QDateTime& datTim) {
|
---|
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 |
|
---|
110 | QString intStr = settings.value("rnxIntr").toString();
|
---|
111 | QString hlpStr;
|
---|
112 |
|
---|
113 | QTime nextTime;
|
---|
114 | QDate nextDate;
|
---|
115 |
|
---|
116 | int indHlp = intStr.indexOf("min");
|
---|
117 |
|
---|
118 | if ( indHlp != -1) {
|
---|
119 | int step = intStr.left(indHlp-1).toInt();
|
---|
120 | char ch = 'A' + datTim.time().hour();
|
---|
121 | hlpStr = ch;
|
---|
122 | if (datTim.time().minute() >= 60-step) {
|
---|
123 | hlpStr += QString("%1").arg(60-step, 2, 10, QChar('0'));
|
---|
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 {
|
---|
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 | }
|
---|
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 | ////////////////////////////////////////////////////////////////////////////
|
---|
190 | void 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 << " 5 C1 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 << " 5 C1 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
271 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
279 | void 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* fObs = *dumpList.begin();
|
---|
302 | QDateTime datTim = dateAndTimeFromGPSweek(fObs->GPSWeek, fObs->GPSWeeks);
|
---|
303 |
|
---|
304 | // Close the file
|
---|
305 | // --------------
|
---|
306 | if (_nextCloseEpoch.isValid() && datTim >= _nextCloseEpoch) {
|
---|
307 | closeFile();
|
---|
308 | _headerWritten = false;
|
---|
309 | }
|
---|
310 |
|
---|
311 | // Write RINEX Header
|
---|
312 | // ------------------
|
---|
313 | if (!_headerWritten) {
|
---|
314 | writeHeader(datTim);
|
---|
315 | }
|
---|
316 |
|
---|
317 | _out << datTim.toString(" yy MM dd hh mm ss.zzz0000").toAscii().data()
|
---|
318 | << " " << 0 << setw(3) << dumpList.size();
|
---|
319 |
|
---|
320 | QListIterator<Observation*> it(dumpList); int iSat = 0;
|
---|
321 | while (it.hasNext()) {
|
---|
322 | iSat++;
|
---|
323 | Observation* ob = it.next();
|
---|
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 {
|
---|
332 | _out << "R" << setw(2) << prn % 100;
|
---|
333 | }
|
---|
334 | if (iSat == 12 && it.hasNext()) {
|
---|
335 | _out << endl << " ";
|
---|
336 | iSat = 0;
|
---|
337 | }
|
---|
338 | }
|
---|
339 | _out << endl;
|
---|
340 |
|
---|
341 | it.toFront();
|
---|
342 | while (it.hasNext()) {
|
---|
343 | Observation* ob = it.next();
|
---|
344 |
|
---|
345 | char lli = ' ';
|
---|
346 | char snr = ' ';
|
---|
347 | _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
|
---|
348 | _out << setw(14) << setprecision(3) << ob->P1 << lli << snr;
|
---|
349 | _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
|
---|
350 | _out << setw(14) << setprecision(3) << ob->L1 << lli << snr;
|
---|
351 | _out << setw(14) << setprecision(3) << ob->L2 << lli << snr;
|
---|
352 | _out << endl;
|
---|
353 |
|
---|
354 | delete ob;
|
---|
355 | }
|
---|
356 |
|
---|
357 | _out.flush();
|
---|
358 | }
|
---|
359 |
|
---|
360 | // Close the Old RINEX File
|
---|
361 | ////////////////////////////////////////////////////////////////////////////
|
---|
362 | void bncRinex::closeFile() {
|
---|
363 | _out.close();
|
---|
364 | if (!_rnxScriptName.isEmpty()) {
|
---|
365 | _rnxScript.start(_rnxScriptName, QStringList() << _fName);
|
---|
366 | }
|
---|
367 | }
|
---|