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 <QDate>
|
---|
21 | #include <QFile>
|
---|
22 | #include <QTextStream>
|
---|
23 | #include <iomanip>
|
---|
24 |
|
---|
25 | #include "bncrinex.h"
|
---|
26 | #include "bncutils.h"
|
---|
27 | #include "bncconst.h"
|
---|
28 |
|
---|
29 | using namespace std;
|
---|
30 |
|
---|
31 | // Constructor
|
---|
32 | ////////////////////////////////////////////////////////////////////////////
|
---|
33 | bncRinex::bncRinex(const char* StatID) {
|
---|
34 | _statID = StatID;
|
---|
35 | _headerWritten = false;
|
---|
36 | readSkeleton();
|
---|
37 |
|
---|
38 | QSettings settings;
|
---|
39 | _rnxScriptName = settings.value("rnxScript").toString();
|
---|
40 | expandEnvVar(_rnxScriptName);
|
---|
41 | }
|
---|
42 |
|
---|
43 | // Destructor
|
---|
44 | ////////////////////////////////////////////////////////////////////////////
|
---|
45 | bncRinex::~bncRinex() {
|
---|
46 | _out.close();
|
---|
47 | }
|
---|
48 |
|
---|
49 | // Read Skeleton Header File
|
---|
50 | ////////////////////////////////////////////////////////////////////////////
|
---|
51 | void bncRinex::readSkeleton() {
|
---|
52 |
|
---|
53 | // Resolve Skeleton File Name
|
---|
54 | // --------------------------
|
---|
55 | QSettings settings;
|
---|
56 | QString sklName = settings.value("rnxPath").toString();
|
---|
57 | expandEnvVar(sklName);
|
---|
58 | if ( sklName[sklName.length()-1] != QDir::separator() ) {
|
---|
59 | sklName += QDir::separator();
|
---|
60 | }
|
---|
61 | sklName += _statID.left(4) + "." + settings.value("rnxSkel").toString();
|
---|
62 |
|
---|
63 | // Read the File
|
---|
64 | // -------------
|
---|
65 | QFile skl(sklName);
|
---|
66 | if ( skl.exists() && skl.open(QIODevice::ReadOnly) ) {
|
---|
67 | QTextStream in(&skl);
|
---|
68 | while ( !in.atEnd() ) {
|
---|
69 | _headerLines.append( in.readLine() );
|
---|
70 | if (_headerLines.last().indexOf("END OF HEADER") != -1) {
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | // File Name according to RINEX Standards
|
---|
78 | ////////////////////////////////////////////////////////////////////////////
|
---|
79 | void bncRinex::resolveFileName(const QDateTime& datTim) {
|
---|
80 |
|
---|
81 | QSettings settings;
|
---|
82 | QString path = settings.value("rnxPath").toString();
|
---|
83 | expandEnvVar(path);
|
---|
84 |
|
---|
85 | if ( path[path.length()-1] != QDir::separator() ) {
|
---|
86 | path += QDir::separator();
|
---|
87 | }
|
---|
88 |
|
---|
89 | QString intStr = settings.value("rnxIntr").toString();
|
---|
90 | QString hlpStr;
|
---|
91 |
|
---|
92 | QTime nextTime;
|
---|
93 | QDate nextDate;
|
---|
94 |
|
---|
95 | // //// beg test
|
---|
96 | // if (1) {
|
---|
97 | // hlpStr = datTim.toString("_hh_mm_ss");
|
---|
98 | // nextDate = datTim.date();
|
---|
99 | // nextTime = datTim.time().addSecs(10);
|
---|
100 | // } else
|
---|
101 | // //// end test
|
---|
102 | if (intStr == "15 min") {
|
---|
103 | char ch = 'A' + datTim.time().hour();
|
---|
104 | hlpStr = ch;
|
---|
105 | if (datTim.time().minute() < 15) {
|
---|
106 | hlpStr += "00";
|
---|
107 | nextTime.setHMS(datTim.time().hour(), 15, 0);
|
---|
108 | nextDate = datTim.date();
|
---|
109 | }
|
---|
110 | else if (datTim.time().minute() < 30) {
|
---|
111 | hlpStr += "15";
|
---|
112 | nextTime.setHMS(datTim.time().hour(), 30, 0);
|
---|
113 | nextDate = datTim.date();
|
---|
114 | }
|
---|
115 | else if (datTim.time().minute() < 45) {
|
---|
116 | hlpStr += "30";
|
---|
117 | nextTime.setHMS(datTim.time().hour(), 45, 0);
|
---|
118 | nextDate = datTim.date();
|
---|
119 | }
|
---|
120 | else {
|
---|
121 | hlpStr += "45";
|
---|
122 | if (datTim.time().hour() < 23) {
|
---|
123 | nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
|
---|
124 | nextDate = datTim.date();
|
---|
125 | }
|
---|
126 | else {
|
---|
127 | nextTime.setHMS(0, 0, 0);
|
---|
128 | nextDate = datTim.date().addDays(1);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | else if (intStr == "1 hour") {
|
---|
133 | char ch = 'A' + datTim.time().hour();
|
---|
134 | hlpStr = ch;
|
---|
135 | if (datTim.time().hour() < 23) {
|
---|
136 | nextTime.setHMS(datTim.time().hour() + 1 , 0, 0);
|
---|
137 | nextDate = datTim.date();
|
---|
138 | }
|
---|
139 | else {
|
---|
140 | nextTime.setHMS(0, 0, 0);
|
---|
141 | nextDate = datTim.date().addDays(1);
|
---|
142 | }
|
---|
143 | }
|
---|
144 | else {
|
---|
145 | hlpStr = "0";
|
---|
146 | nextTime.setHMS(0, 0, 0);
|
---|
147 | nextDate = datTim.date().addDays(1);
|
---|
148 | }
|
---|
149 | _nextCloseEpoch = QDateTime(nextDate, nextTime);
|
---|
150 |
|
---|
151 | path += _statID.left(4) +
|
---|
152 | QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
|
---|
153 | hlpStr +
|
---|
154 | datTim.toString(".yyO");
|
---|
155 |
|
---|
156 | _fName = path.toAscii();
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Write RINEX Header
|
---|
160 | ////////////////////////////////////////////////////////////////////////////
|
---|
161 | void bncRinex::writeHeader(const QDateTime& datTim) {
|
---|
162 |
|
---|
163 | // Open the Output File
|
---|
164 | // --------------------
|
---|
165 | resolveFileName(datTim);
|
---|
166 | _out.open(_fName.data());
|
---|
167 | _out.setf(ios::showpoint | ios::fixed);
|
---|
168 |
|
---|
169 | // Copy Skeleton Header
|
---|
170 | // --------------------
|
---|
171 | if (_headerLines.size() > 0) {
|
---|
172 | QStringListIterator it(_headerLines);
|
---|
173 | while (it.hasNext()) {
|
---|
174 | QString line = it.next();
|
---|
175 | if (line.indexOf("# / TYPES OF OBSERV") != -1) {
|
---|
176 | _out << " 4 P1 P2 L1 L2"
|
---|
177 | " # / TYPES OF OBSERV" << endl;
|
---|
178 | }
|
---|
179 | else if (line.indexOf("TIME OF FIRST OBS") != -1) {
|
---|
180 | _out << datTim.toString(" yyyy MM dd"
|
---|
181 | " hh mm ss.zzz0000").toAscii().data();
|
---|
182 | _out << " TIME OF FIRST OBS" << endl;
|
---|
183 | }
|
---|
184 | else {
|
---|
185 | _out << line.toAscii().data() << endl;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | // Write Dummy Header
|
---|
191 | // ------------------
|
---|
192 | else {
|
---|
193 | double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
|
---|
194 | double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
|
---|
195 |
|
---|
196 | _out << " 2.10 OBSERVATION DATA G (GPS) RINEX VERSION / TYPE" << endl;
|
---|
197 | _out << "BNC LM 27-Aug-2006 PGM / RUN BY / DATE" << endl;
|
---|
198 | _out.setf(ios::left);
|
---|
199 | _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
|
---|
200 | _out << setw(60) << "BKG" << "OBSERVER / AGENCY" << endl;
|
---|
201 | _out << setw(20) << "unknown"
|
---|
202 | << setw(20) << "unknown"
|
---|
203 | << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
|
---|
204 | _out << setw(20) << "unknown"
|
---|
205 | << setw(20) << "unknown"
|
---|
206 | << setw(20) << " " << "ANT # / TYPE" << endl;
|
---|
207 | _out.unsetf(ios::left);
|
---|
208 | _out << setw(14) << setprecision(4) << approxPos[0]
|
---|
209 | << setw(14) << setprecision(4) << approxPos[1]
|
---|
210 | << setw(14) << setprecision(4) << approxPos[2]
|
---|
211 | << " " << "APPROX POSITION XYZ" << endl;
|
---|
212 | _out << setw(14) << setprecision(4) << antennaNEU[0]
|
---|
213 | << setw(14) << setprecision(4) << antennaNEU[1]
|
---|
214 | << setw(14) << setprecision(4) << antennaNEU[2]
|
---|
215 | << " " << "ANTENNA: DELTA H/E/N" << endl;
|
---|
216 | _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
|
---|
217 | _out << " 4 P1 P2 L1 L2 # / TYPES OF OBSERV" << endl;
|
---|
218 | _out << datTim.toString(" yyyy MM dd"
|
---|
219 | " hh mm ss.zzz0000").toAscii().data();
|
---|
220 | _out << " " << "TIME OF FIRST OBS" << endl;
|
---|
221 | _out << " END OF HEADER" << endl;
|
---|
222 | }
|
---|
223 |
|
---|
224 | _headerWritten = true;
|
---|
225 | }
|
---|
226 |
|
---|
227 | // Stores Observation into Internal Array
|
---|
228 | ////////////////////////////////////////////////////////////////////////////
|
---|
229 | void bncRinex::deepCopy(const Observation* obs) {
|
---|
230 | Observation* newObs = new Observation();
|
---|
231 | memcpy(newObs, obs, sizeof(*obs));
|
---|
232 | _obs.push_back(newObs);
|
---|
233 | }
|
---|
234 |
|
---|
235 | // Write One Epoch into the RINEX File
|
---|
236 | ////////////////////////////////////////////////////////////////////////////
|
---|
237 | void bncRinex::dumpEpoch() {
|
---|
238 |
|
---|
239 | // Easy Return
|
---|
240 | // -----------
|
---|
241 | if (_obs.isEmpty()) {
|
---|
242 | return;
|
---|
243 | }
|
---|
244 |
|
---|
245 | // Time of Epoch
|
---|
246 | // -------------
|
---|
247 | Observation* firstObs = *_obs.begin();
|
---|
248 |
|
---|
249 | QDateTime datTim = dateAndTimeFromGPSweek( firstObs->GPSWeek,
|
---|
250 | firstObs->GPSWeeks +
|
---|
251 | fmod(firstObs->sec, 1.0) );
|
---|
252 |
|
---|
253 | // Close the file
|
---|
254 | // --------------
|
---|
255 | if (_nextCloseEpoch.isValid() && datTim >= _nextCloseEpoch) {
|
---|
256 | closeFile();
|
---|
257 | _headerWritten = false;
|
---|
258 | }
|
---|
259 |
|
---|
260 | // Write RINEX Header
|
---|
261 | // ------------------
|
---|
262 | if (!_headerWritten) {
|
---|
263 | writeHeader(datTim);
|
---|
264 | }
|
---|
265 |
|
---|
266 | _out << datTim.toString(" yy MM dd hh mm ss.zzz0000").toAscii().data()
|
---|
267 | << " " << 0 << setw(3) << _obs.size();
|
---|
268 |
|
---|
269 | QListIterator<Observation*> it(_obs); int iSat = 0;
|
---|
270 | while (it.hasNext()) {
|
---|
271 | iSat++;
|
---|
272 | Observation* ob = it.next();
|
---|
273 | _out << " " << setw(2) << int(ob->SVPRN);
|
---|
274 | if (iSat == 12 && it.hasNext()) {
|
---|
275 | _out << endl << " ";
|
---|
276 | iSat = 0;
|
---|
277 | }
|
---|
278 | }
|
---|
279 | _out << endl;
|
---|
280 |
|
---|
281 | it.toFront();
|
---|
282 | while (it.hasNext()) {
|
---|
283 | Observation* ob = it.next();
|
---|
284 |
|
---|
285 | char lli = ' ';
|
---|
286 | char snr = ' ';
|
---|
287 | _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
|
---|
288 | _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
|
---|
289 | _out << setw(14) << setprecision(3) << ob->L1 / t_CST::lambda1 << lli << snr;
|
---|
290 | _out << setw(14) << setprecision(3) << ob->L2 / t_CST::lambda2 << lli << snr;
|
---|
291 | _out << endl;
|
---|
292 |
|
---|
293 | delete ob;
|
---|
294 | }
|
---|
295 |
|
---|
296 | _out.flush();
|
---|
297 | _obs.clear();
|
---|
298 | }
|
---|
299 |
|
---|
300 | // Close the Old RINEX File
|
---|
301 | ////////////////////////////////////////////////////////////////////////////
|
---|
302 | void bncRinex::closeFile() {
|
---|
303 | _out.close();
|
---|
304 | if (!_rnxScriptName.isEmpty()) {
|
---|
305 | _rnxScript.start(_rnxScriptName, QStringList() << _fName);
|
---|
306 | }
|
---|
307 | }
|
---|