Changeset 11228 in ntrip for trunk/BNC/src/orbComp/sp3Comp.cpp


Ignore:
Timestamp:
Nov 26, 2026, 8:21:15 PM (70 minutes ago)
Author:
stuerze
Message:

SSP3 comparison with clock RINEX files added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/BNC/src/orbComp/sp3Comp.cpp

    r11158 r11228  
    3535 * Created:    24-Nov-2014
    3636 *
    37  * Changes:
     37 * Changes:    26-Nov-2026: optional clock RINEX files (sp3CompClkFile): clocks
     38 *                           and SIS at the epochs of the clock files, orbits
     39 *                           interpolated between the SP3 epochs
    3840 *
    3941 * -----------------------------------------------------------------------*/
    … …  
    4143#include <QRegularExpression>
    4244#include <iostream>
     45#include <fstream>
    4346#include <iomanip>
     47#include <algorithm>
    4448#include "sp3Comp.h"
    4549#include "bnccore.h"
    … …  
    5862  for (int ii = 0; ii < _sp3FileNames.size(); ii++) {
    5963    expandEnvVar(_sp3FileNames[ii]);
     64  }
     65  // Clock RINEX files, in the order of the SP3 files; one name refers to
     66  // SP3 file 1, an empty entry or "-" to no clock file
     67  QString clkFiles = settings.value("sp3CompClkFile").toString().trimmed();
     68  if (!clkFiles.isEmpty()) {
     69    _clkFileNames = clkFiles.split(",", Qt::KeepEmptyParts);
     70    for (int ii = 0; ii < _clkFileNames.size(); ii++) {
     71      _clkFileNames[ii] = _clkFileNames[ii].trimmed();
     72      if (_clkFileNames[ii] == "-") {
     73        _clkFileNames[ii].clear();
     74      }
     75      expandEnvVar(_clkFileNames[ii]);
     76    }
    6077  }
    6178  _logFileName = settings.value("sp3CompOutLogFile").toString(); expandEnvVar(_logFileName);
    … …  
    94111    *_log << "! SP3 File " << ii+1 << ": " << _sp3FileNames[ii] << "\n";
    95112  }
     113  for (int ii = 0; ii < _clkFileNames.size(); ii++) {
     114    if (!_clkFileNames[ii].isEmpty()) {
     115      *_log << "! CLK File " << ii+1 << ": " << _clkFileNames[ii] << "\n";
     116    }
     117  }
    96118  if (_sp3FileNames.size() != 2) {
    97119    *_log << "ERROR: sp3Comp requires two input SP3 files" << "\n";
     120    goto end;
     121  }
     122  if (_clkFileNames.size() > 2) {
     123    *_log << "ERROR: sp3Comp accepts at most two clock RINEX files" << "\n";
    98124    goto end;
    99125  }
    … …  
    280306void t_sp3Comp::compare(ostringstream& out) const {
    281307
    282   // Synchronize reading of two sp3 files
    283   // ------------------------------------
    284   bncSP3 in1(_sp3FileNames[0]); in1.nextEpoch();
    285   bncSP3 in2(_sp3FileNames[1]); in2.nextEpoch();
     308  // Read the SP3 files (and clock RINEX files)
     309  // -------------------------------------------
     310  t_sp3Data sp3[2];
     311  map<bncTime, map<t_prn, double> > clk[2];
     312  bool withClkFiles = false;
     313  for (int ii = 0; ii < 2; ii++) {
     314    readSp3(_sp3FileNames[ii], sp3[ii]);
     315    if (ii < _clkFileNames.size() && !_clkFileNames[ii].isEmpty()) {
     316      readClk(_clkFileNames[ii], clk[ii]);
     317      withClkFiles = true;
     318      if (clk[ii].empty()) {
     319        throw "t_sp3Comp: no clocks in clock RINEX file " + string(_clkFileNames[ii].toLatin1().data());
     320      }
     321    }
     322    else {
     323      clk[ii] = sp3[ii]._clk;
     324    }
     325  }
    286326
    287327  vector<t_epoch*> epochs;
    288   while (in1.currEpoch() && in2.currEpoch()) {
    289     bncTime t1 = in1.currEpoch()->_tt;
    290     bncTime t2 = in2.currEpoch()->_tt;
    291     if      (t1 < t2) {
    292       in1.nextEpoch();
    293     }
    294     else if (t1 > t2) {
    295       in2.nextEpoch();
    296     }
    297     else if (t1 == t2) {
    298       t_epoch* epo = new t_epoch; epo->_tt = t1;
    299       bool epochOK = false;
    300       for (int i1 = 0; i1 < in1.currEpoch()->_sp3Sat.size(); i1++) {
    301         bncSP3::t_sp3Sat* sat1 = in1.currEpoch()->_sp3Sat[i1];
    302         for (int i2 = 0; i2 < in2.currEpoch()->_sp3Sat.size(); i2++) {
    303           bncSP3::t_sp3Sat* sat2 = in2.currEpoch()->_sp3Sat[i2];
    304           if (sat1->_prn == sat2->_prn &&
    305               sat1->_clkValid && sat2->_clkValid) {
    306             epochOK = true;
    307             epo->_dr[sat1->_prn]  = sat1->_xyz - sat2->_xyz;
    308             epo->_xyz[sat1->_prn] = sat1->_xyz;
    309             epo->_dc[sat1->_prn]  = sat1->_clk - sat2->_clk;
    310           }
    311         }
    312       }
    313       if (epochOK) {
     328  if (!withClkFiles) {
     329    // Common epochs of the two SP3 files, satellites with valid clocks in both
     330    // -------------------------------------------------------------------------
     331    map<bncTime, map<t_prn, ColumnVector> >::const_iterator it1;
     332    for (it1 = sp3[0]._xyz.begin(); it1 != sp3[0]._xyz.end(); it1++) {
     333      const bncTime& tt = it1->first;
     334      map<bncTime, map<t_prn, ColumnVector> >::const_iterator it2 = sp3[1]._xyz.find(tt);
     335      if (it2 == sp3[1]._xyz.end()) {
     336        continue;
     337      }
     338      const map<t_prn, double>& c1 = clk[0][tt];
     339      const map<t_prn, double>& c2 = clk[1][tt];
     340      t_epoch* epo = new t_epoch; epo->_tt = tt;
     341      for (map<t_prn, ColumnVector>::const_iterator is = it1->second.begin(); is != it1->second.end(); is++) {
     342        const t_prn& prn = is->first;
     343        map<t_prn, ColumnVector>::const_iterator jj = it2->second.find(prn);
     344        if (jj != it2->second.end() && c1.find(prn) != c1.end() && c2.find(prn) != c2.end()) {
     345          epo->_dr[prn]  = is->second - jj->second;
     346          epo->_xyz[prn] = is->second;
     347          epo->_dc[prn]  = c1.find(prn)->second - c2.find(prn)->second;
     348        }
     349      }
     350      if (epo->_dc.size() > 0) {
    314351        epochs.push_back(epo);
    315352      }
    … …  
    317354        delete epo;
    318355      }
    319       in1.nextEpoch();
    320       in2.nextEpoch();
     356    }
     357  }
     358  else {
     359    // Common epochs of the two clock sources; the orbits are interpolated
     360    // between the SP3 epochs where necessary and then used for the clock
     361    // comparison and the SIS only (orbit statistics: common SP3 epochs)
     362    // ---------------------------------------------------------------------
     363    map<bncTime, map<t_prn, double> >::const_iterator it1;
     364    for (it1 = clk[0].begin(); it1 != clk[0].end(); it1++) {
     365      const bncTime& tt = it1->first;
     366      map<bncTime, map<t_prn, double> >::const_iterator it2 = clk[1].find(tt);
     367      if (it2 == clk[1].end()) {
     368        continue;
     369      }
     370      t_epoch* epo = new t_epoch; epo->_tt = tt;
     371      for (map<t_prn, double>::const_iterator is = it1->second.begin(); is != it1->second.end(); is++) {
     372        const t_prn& prn = is->first;
     373        map<t_prn, double>::const_iterator jj = it2->second.find(prn);
     374        if (jj == it2->second.end()) {
     375          continue;
     376        }
     377        ColumnVector xyz1, xyz2;
     378        bool interp1, interp2;
     379        if (!orbit(sp3[0], prn, tt, xyz1, interp1) || !orbit(sp3[1], prn, tt, xyz2, interp2)) {
     380          continue;
     381        }
     382        epo->_dr[prn]  = xyz1 - xyz2;
     383        epo->_xyz[prn] = xyz1;
     384        epo->_dc[prn]  = is->second - jj->second;
     385        if (interp1 || interp2) {
     386          epo->_orbInterp.insert(prn);
     387        }
     388      }
     389      if (epo->_dc.size() > 0) {
     390        epochs.push_back(epo);
     391      }
     392      else {
     393        delete epo;
     394      }
    321395    }
    322396  }
    … …  
    413487              << setw(7) << setprecision(4) << rao[2] << "    ";
    414488        }
    415         stat[prn.toString()]._rao += SP(rao, rao); // Schur product
    416         stat[all.str()     ]._rao += SP(rao, rao);
    417         stat[prn.toString()]._nr  += 1;
    418         stat[all.str()     ]._nr  += 1;
     489        if (epo->_orbInterp.find(prn) == epo->_orbInterp.end()) {
     490          stat[prn.toString()]._rao += SP(rao, rao); // Schur product
     491          stat[all.str()     ]._rao += SP(rao, rao);
     492          stat[prn.toString()]._nr  += 1;
     493          stat[all.str()     ]._nr  += 1;
     494        }
    419495        if (dc.find(prn) != dc.end() && dcRed.find(prn) != dc.end()) {
    420496          double clkRes    = dc.find(prn)->second;
    … …  
    482558    char sys = prn[0];
    483559    stringstream all; all << sys << 99;
    484     if (stat._nr > 0) {
    485       stat._rao[0] = sqrt(stat._rao[0] / stat._nr);
    486       stat._rao[1] = sqrt(stat._rao[1] / stat._nr);
    487       stat._rao[2] = sqrt(stat._rao[2] / stat._nr);
    488       stat._rao3DRMS = stat._rao.NormFrobenius();
     560    if (stat._nr > 0 || stat._nc > 0) {
     561      if (stat._nr > 0) {
     562        stat._rao[0] = sqrt(stat._rao[0] / stat._nr);
     563        stat._rao[1] = sqrt(stat._rao[1] / stat._nr);
     564        stat._rao[2] = sqrt(stat._rao[2] / stat._nr);
     565        stat._rao3DRMS = stat._rao.NormFrobenius();
     566      }
    489567      // orbit values in millimeter
    490568      if (prn != all.str()) {
    … …  
    529607}
    530608
     609// Read an SP3 file (positions, valid clocks, orbit series per satellite)
     610////////////////////////////////////////////////////////////////////////////
     611void t_sp3Comp::readSp3(const QString& fileName, t_sp3Data& data) const {
     612  bncSP3 in(fileName);
     613  bncTime lastTime;
     614  while (const bncSP3::t_sp3Epoch* epo = in.nextEpoch()) {
     615    const bncTime& tt = epo->_tt;
     616    if (lastTime.valid()) {
     617      double dt = tt - lastTime;
     618      if (dt > 0.0 && (data._sampl == 0.0 || dt < data._sampl)) {
     619        data._sampl = dt;
     620      }
     621    }
     622    lastTime = tt;
     623    map<t_prn, ColumnVector>& xyz = data._xyz[tt];
     624    for (int ii = 0; ii < epo->_sp3Sat.size(); ii++) {
     625      const bncSP3::t_sp3Sat* sat = epo->_sp3Sat[ii];
     626      xyz[sat->_prn] = sat->_xyz;
     627      if (sat->_clkValid) {
     628        data._clk[tt][sat->_prn] = sat->_clk;
     629      }
     630    }
     631  }
     632  map<bncTime, map<t_prn, ColumnVector> >::const_iterator it;
     633  for (it = data._xyz.begin(); it != data._xyz.end(); it++) {
     634    map<t_prn, ColumnVector>::const_iterator is;
     635    for (is = it->second.begin(); is != it->second.end(); is++) {
     636      t_orbSeries& series = data._series[is->first];
     637      series._tt.push_back(it->first);
     638      series._xyz.push_back(is->second);
     639    }
     640  }
     641}
     642
     643// Read the satellite clocks (AS records) of a clock RINEX file [m]
     644////////////////////////////////////////////////////////////////////////////
     645void t_sp3Comp::readClk(const QString& fileName,
     646                        map<bncTime, map<t_prn, double> >& clk) const {
     647  ifstream in(fileName.toLocal8Bit().data());
     648  if (!in.good()) {
     649    throw "t_sp3Comp: cannot open clock RINEX file " + string(fileName.toLatin1().data());
     650  }
     651  string line;
     652  bool header = true;
     653  while (getline(in, line)) {
     654    if (header) {
     655      if (line.find("END OF HEADER") != string::npos) {
     656        header = false;
     657      }
     658      continue;
     659    }
     660    if (line.compare(0, 3, "AS ") != 0) {   // other records, continuation lines
     661      continue;
     662    }
     663    replace(line.begin(), line.end(), 'D', 'E');   // Fortran exponent
     664    replace(line.begin(), line.end(), 'd', 'E');
     665    istringstream inLine(line.substr(3));
     666    string prnStr;
     667    int    YY, MM, DD, hh, mm, numValues;
     668    double ss, value;
     669    inLine >> prnStr >> YY >> MM >> DD >> hh >> mm >> ss >> numValues >> value;
     670    if (inLine.fail() || numValues < 1 || prnStr.size() < 3) {
     671      continue;
     672    }
     673    t_prn prn;
     674    try {
     675      prn.set(prnStr);
     676    }
     677    catch (...) {        // satellite system not supported
     678      continue;
     679    }
     680    if (prn.number() == 0) {
     681      continue;
     682    }
     683    bncTime tt; tt.set(YY, MM, DD, hh, mm, ss);
     684    clk[tt][prn] = value * t_CST::c;
     685  }
     686}
     687
     688// Satellite position at epoch tt: the SP3 position if tt is an SP3 epoch,
     689// otherwise Lagrange interpolation with 10 consecutive SP3 epochs around tt
     690// (no extrapolation, no data gaps longer than 1.5 sampling intervals)
     691////////////////////////////////////////////////////////////////////////////
     692bool t_sp3Comp::orbit(const t_sp3Data& data, const t_prn& prn, const bncTime& tt,
     693                      ColumnVector& xyz, bool& interpolated) const {
     694  const unsigned NUMNODES = 10;
     695  interpolated = false;
     696  map<t_prn, t_orbSeries>::const_iterator itS = data._series.find(prn);
     697  if (itS == data._series.end()) {
     698    return false;
     699  }
     700  const t_orbSeries& series = itS->second;
     701  const vector<bncTime>& times = series._tt;
     702  vector<bncTime>::const_iterator it = lower_bound(times.begin(), times.end(), tt);
     703  if (it != times.end() && *it == tt) {
     704    xyz = series._xyz[it - times.begin()];
     705    return true;
     706  }
     707  if (it == times.begin() || it == times.end() || data._sampl <= 0.0) {
     708    return false;
     709  }
     710  const double maxGap = 1.5 * data._sampl;
     711  int lo = (it - times.begin()) - 1;
     712  int hi = lo + 1;
     713  if (times[hi] - times[lo] > maxGap) {
     714    return false;
     715  }
     716  // extend the window alternately to both sides
     717  while (unsigned(hi - lo + 1) < NUMNODES) {
     718    bool left  = (lo > 0 && times[lo] - times[lo-1] <= maxGap);
     719    bool right = (hi < int(times.size()) - 1 && times[hi+1] - times[hi] <= maxGap);
     720    if (!left && !right) {
     721      return false;
     722    }
     723    if (left && (!right || (tt - times[lo]) <= (times[hi] - tt))) {
     724      --lo;
     725    }
     726    else {
     727      ++hi;
     728    }
     729  }
     730  xyz.ReSize(3); xyz = 0.0;
     731  for (int ii = lo; ii <= hi; ii++) {
     732    double weight = 1.0;
     733    for (int jj = lo; jj <= hi; jj++) {
     734      if (jj != ii) {
     735        weight *= (tt - times[jj]) / (times[ii] - times[jj]);
     736      }
     737    }
     738    xyz += weight * series._xyz[ii];
     739  }
     740  interpolated = true;
     741  return true;
     742}
     743
    531744//
    532745////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset for help on using the changeset viewer.