| 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 |
|
|---|
| 31 | using namespace std;
|
|---|
| 32 |
|
|---|
| 33 | // Constructor
|
|---|
| 34 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 35 | bncRinex::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 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 64 | bncRinex::~bncRinex() {
|
|---|
| 65 | _out.close();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 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 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 98 | void 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 | path += _statID.left(4) +
|
|---|
| 164 | QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0')) +
|
|---|
| 165 | hlpStr +
|
|---|
| 166 | datTim.toString(".yyO");
|
|---|
| 167 |
|
|---|
| 168 | _fName = path.toAscii();
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | // Write RINEX Header
|
|---|
| 172 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 173 | void bncRinex::writeHeader(const QDateTime& datTim) {
|
|---|
| 174 |
|
|---|
| 175 | // Open the Output File
|
|---|
| 176 | // --------------------
|
|---|
| 177 | resolveFileName(datTim);
|
|---|
| 178 | _out.open(_fName.data());
|
|---|
| 179 | _out.setf(ios::showpoint | ios::fixed);
|
|---|
| 180 |
|
|---|
| 181 | // Copy Skeleton Header
|
|---|
| 182 | // --------------------
|
|---|
| 183 | if (_headerLines.size() > 0) {
|
|---|
| 184 | QStringListIterator it(_headerLines);
|
|---|
| 185 | while (it.hasNext()) {
|
|---|
| 186 | QString line = it.next();
|
|---|
| 187 | if (line.indexOf("PGM / RUN BY / DATE") != -1) {
|
|---|
| 188 | QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
|
|---|
| 189 | _out << _pgmName.toAscii().data() << _userName.toAscii().data()
|
|---|
| 190 | << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
|
|---|
| 191 | }
|
|---|
| 192 | else if (line.indexOf("# / TYPES OF OBSERV") != -1) {
|
|---|
| 193 | _out << " 4 P1 P2 L1 L2"
|
|---|
| 194 | " # / TYPES OF OBSERV" << endl;
|
|---|
| 195 | }
|
|---|
| 196 | else if (line.indexOf("TIME OF FIRST OBS") != -1) {
|
|---|
| 197 | _out << datTim.toString(" yyyy MM dd"
|
|---|
| 198 | " hh mm ss.zzz0000").toAscii().data();
|
|---|
| 199 | _out << " TIME OF FIRST OBS" << endl;
|
|---|
| 200 | QString hlp = QString("GENERATED FROM STREAM %1").arg(_mountPoint)
|
|---|
| 201 | .leftJustified(60, ' ', true);
|
|---|
| 202 | _out << hlp.toAscii().data() << "COMMENT" << endl;
|
|---|
| 203 | }
|
|---|
| 204 | else {
|
|---|
| 205 | _out << line.toAscii().data() << endl;
|
|---|
| 206 | }
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | // Write Dummy Header
|
|---|
| 211 | // ------------------
|
|---|
| 212 | else {
|
|---|
| 213 | double approxPos[3]; approxPos[0] = approxPos[1] = approxPos[2] = 0.0;
|
|---|
| 214 | double antennaNEU[3]; antennaNEU[0] = antennaNEU[1] = antennaNEU[2] = 0.0;
|
|---|
| 215 |
|
|---|
| 216 | _out << " 2.10 OBSERVATION DATA G (GPS) RINEX VERSION / TYPE" << endl;
|
|---|
| 217 | QString hlp = QDate::currentDate().toString("dd-MMM-yyyy").leftJustified(20, ' ', true);
|
|---|
| 218 | _out << _pgmName.toAscii().data() << _userName.toAscii().data()
|
|---|
| 219 | << hlp.toAscii().data() << "PGM / RUN BY / DATE" << endl;
|
|---|
| 220 | _out.setf(ios::left);
|
|---|
| 221 | _out << setw(60) << _statID.data() << "MARKER NAME" << endl;
|
|---|
| 222 | _out << setw(60) << "BKG" << "OBSERVER / AGENCY" << endl;
|
|---|
| 223 | _out << setw(20) << "unknown"
|
|---|
| 224 | << setw(20) << "unknown"
|
|---|
| 225 | << setw(20) << "unknown" << "REC # / TYPE / VERS" << endl;
|
|---|
| 226 | _out << setw(20) << "unknown"
|
|---|
| 227 | << setw(20) << "unknown"
|
|---|
| 228 | << setw(20) << " " << "ANT # / TYPE" << endl;
|
|---|
| 229 | _out.unsetf(ios::left);
|
|---|
| 230 | _out << setw(14) << setprecision(4) << approxPos[0]
|
|---|
| 231 | << setw(14) << setprecision(4) << approxPos[1]
|
|---|
| 232 | << setw(14) << setprecision(4) << approxPos[2]
|
|---|
| 233 | << " " << "APPROX POSITION XYZ" << endl;
|
|---|
| 234 | _out << setw(14) << setprecision(4) << antennaNEU[0]
|
|---|
| 235 | << setw(14) << setprecision(4) << antennaNEU[1]
|
|---|
| 236 | << setw(14) << setprecision(4) << antennaNEU[2]
|
|---|
| 237 | << " " << "ANTENNA: DELTA H/E/N" << endl;
|
|---|
| 238 | _out << " 1 1 WAVELENGTH FACT L1/2" << endl;
|
|---|
| 239 | _out << " 4 P1 P2 L1 L2 # / TYPES OF OBSERV" << endl;
|
|---|
| 240 | _out << datTim.toString(" yyyy MM dd"
|
|---|
| 241 | " hh mm ss.zzz0000").toAscii().data();
|
|---|
| 242 | _out << " " << "TIME OF FIRST OBS" << endl;
|
|---|
| 243 | hlp = QString("GENERATED FROM STREAM %1").arg(_mountPoint)
|
|---|
| 244 | .leftJustified(60, ' ', true);
|
|---|
| 245 | _out << hlp.toAscii().data() << "COMMENT" << endl;
|
|---|
| 246 | _out << " END OF HEADER" << endl;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | _headerWritten = true;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | // Stores Observation into Internal Array
|
|---|
| 253 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 254 | void bncRinex::deepCopy(const Observation* obs) {
|
|---|
| 255 | Observation* newObs = new Observation();
|
|---|
| 256 | memcpy(newObs, obs, sizeof(*obs));
|
|---|
| 257 | _obs.push_back(newObs);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | // Write One Epoch into the RINEX File
|
|---|
| 261 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 262 | void bncRinex::dumpEpoch(long maxTime) {
|
|---|
| 263 |
|
|---|
| 264 | // Select observations older than maxTime
|
|---|
| 265 | // --------------------------------------
|
|---|
| 266 | QList<Observation*> dumpList;
|
|---|
| 267 | QMutableListIterator<Observation*> mIt(_obs);
|
|---|
| 268 | while (mIt.hasNext()) {
|
|---|
| 269 | Observation* ob = mIt.next();
|
|---|
| 270 | if (ob->GPSWeek * 7*24*3600 + ob->GPSWeeks < maxTime) {
|
|---|
| 271 | dumpList.push_back(ob);
|
|---|
| 272 | mIt.remove();
|
|---|
| 273 | }
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | // Easy Return
|
|---|
| 277 | // -----------
|
|---|
| 278 | if (dumpList.isEmpty()) {
|
|---|
| 279 | return;
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | // Time of Epoch
|
|---|
| 283 | // -------------
|
|---|
| 284 | Observation* firstObs = *dumpList.begin();
|
|---|
| 285 |
|
|---|
| 286 | QDateTime datTim = dateAndTimeFromGPSweek( firstObs->GPSWeek,
|
|---|
| 287 | firstObs->GPSWeeks +
|
|---|
| 288 | fmod(firstObs->sec, 1.0) );
|
|---|
| 289 |
|
|---|
| 290 | // Close the file
|
|---|
| 291 | // --------------
|
|---|
| 292 | if (_nextCloseEpoch.isValid() && datTim >= _nextCloseEpoch) {
|
|---|
| 293 | closeFile();
|
|---|
| 294 | _headerWritten = false;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | // Write RINEX Header
|
|---|
| 298 | // ------------------
|
|---|
| 299 | if (!_headerWritten) {
|
|---|
| 300 | writeHeader(datTim);
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | _out << datTim.toString(" yy MM dd hh mm ss.zzz0000").toAscii().data()
|
|---|
| 304 | << " " << 0 << setw(3) << dumpList.size();
|
|---|
| 305 |
|
|---|
| 306 | QListIterator<Observation*> it(dumpList); int iSat = 0;
|
|---|
| 307 | while (it.hasNext()) {
|
|---|
| 308 | iSat++;
|
|---|
| 309 | Observation* ob = it.next();
|
|---|
| 310 | _out << " " << setw(2) << int(ob->SVPRN);
|
|---|
| 311 | if (iSat == 12 && it.hasNext()) {
|
|---|
| 312 | _out << endl << " ";
|
|---|
| 313 | iSat = 0;
|
|---|
| 314 | }
|
|---|
| 315 | }
|
|---|
| 316 | _out << endl;
|
|---|
| 317 |
|
|---|
| 318 | it.toFront();
|
|---|
| 319 | while (it.hasNext()) {
|
|---|
| 320 | Observation* ob = it.next();
|
|---|
| 321 |
|
|---|
| 322 | char lli = ' ';
|
|---|
| 323 | char snr = ' ';
|
|---|
| 324 | _out << setw(14) << setprecision(3) << ob->C1 << lli << snr;
|
|---|
| 325 | _out << setw(14) << setprecision(3) << ob->P2 << lli << snr;
|
|---|
| 326 | _out << setw(14) << setprecision(3) << ob->L1 / t_CST::lambda1 << lli << snr;
|
|---|
| 327 | _out << setw(14) << setprecision(3) << ob->L2 / t_CST::lambda2 << lli << snr;
|
|---|
| 328 | _out << endl;
|
|---|
| 329 |
|
|---|
| 330 | delete ob;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | _out.flush();
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | // Close the Old RINEX File
|
|---|
| 337 | ////////////////////////////////////////////////////////////////////////////
|
|---|
| 338 | void bncRinex::closeFile() {
|
|---|
| 339 | _out.close();
|
|---|
| 340 | if (!_rnxScriptName.isEmpty()) {
|
|---|
| 341 | _rnxScript.start(_rnxScriptName, QStringList() << _fName);
|
|---|
| 342 | }
|
|---|
| 343 | }
|
|---|