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