File: gnssEstimateClockShift.cpp

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (209 lines) | stat: -rw-r--r-- 7,283 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/***********************************************/
/**
* @file GnssEstimateClockShift.cpp
*
* @brief Estimate clock shift for a constellation of satellites.
*
* @author Sebastian Strasser
* @date 2018-02-06
*
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring =
R"(
This program estimates an epoch-wise clock shift in a constellation of GNSS satellites.
Each separate \config{data} represents a satellite... (e.g. 32 GPS satellites).
The shift to reference clocks can be estimated by providing \configFile{inputfileInstrumentRef}{instrument}.
Clock shifts are estimated for each epoch given by \configClass{timeSeries}{timeSeriesType}.
)";

/***********************************************/

#include "base/import.h"
#include "classes/timeSeries/timeSeries.h"
#include "files/fileMatrix.h"
#include "files/fileInstrument.h"
#include "parallel/parallel.h"
#include "programs/program.h"

/***** CLASS ***********************************/

/** @brief Estimate clock shift for a constellation of satellites.
* @ingroup programsGroup */
class GnssEstimateClockShift
{
public:
  class Data
  {
  public:
    FileName outNameInstrument, outNameInstrumentDiff, inNameInstrument, inNameInstrumentRef;
    std::vector<Time> times;
    std::vector<UInt> index;
    Vector clock, clockDiff;
    Bool   useable;

    void init(const std::vector<Time> &timesGlobal, Double margin);
  };

  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(GnssEstimateClockShift, SINGLEPROCESS, "Estimate clock shift for a constellation of satellites.", Gnss)

/***********************************************/

template<> Bool readConfig(Config &config, const std::string &name, GnssEstimateClockShift::Data &var, Config::Appearance mustSet, const std::string &defaultValue, const std::string &annotation)
{
  if(!readConfigSequence(config, name, mustSet, defaultValue, annotation))
    return FALSE;
  readConfig(config, "outputfileInstrument",     var.outNameInstrument,     Config::OPTIONAL, "", "corrected clocks");
  readConfig(config, "outputfileInstrumentDiff", var.outNameInstrumentDiff, Config::OPTIONAL, "", "clock difference after correction");
  readConfig(config, "inputfileInstrument",      var.inNameInstrument,      Config::MUSTSET,  "", "input clocks");
  readConfig(config, "inputfileInstrumentRef",   var.inNameInstrumentRef,   Config::OPTIONAL, "", "reference clocks (subtracted from input clocks)");
  endSequence(config);
  return TRUE;
}

/***********************************************/

void GnssEstimateClockShift::Data::init(const std::vector<Time> &timesGlobal, Double margin)
{
  try
  {
    useable = FALSE;

    MiscValueArc arc, arcRef;
    try
    {
      arc = InstrumentFile::read(inNameInstrument);
      if(!inNameInstrumentRef.empty())
        arcRef = InstrumentFile::read(inNameInstrumentRef);
    }
    catch(std::exception &e)
    {
      logWarning << e.what() << Log::endl;
      return;
    }

    if(!arc.size() || (!inNameInstrumentRef.empty() && arc.times() != arcRef.times()))
    {
      logWarning << "arcs empty or not synchronized: " << arc.size() << " != " << arcRef.size() << ", skipping" << Log::endl;
      return;
    }

    times = arc.times();
    clock = clockDiff = arc.matrix().column(1);
    if(arcRef.size())
      clockDiff -= arcRef.matrix().column(1);

    // find indices of given epochs
    Time timeMargin = seconds2time(margin);
    index.resize(timesGlobal.size(), MAX_UINT);
    UInt idEpochGlobal = 0;
    for(UInt idEpoch = 0; idEpoch < times.size(); idEpoch++)
    {
      while(idEpochGlobal+1 < timesGlobal.size() && timesGlobal.at(idEpochGlobal+1) < times.at(idEpoch)+timeMargin)
        idEpochGlobal++;
      if(timesGlobal.at(idEpochGlobal) >= times.at(idEpoch)-timeMargin && timesGlobal.at(idEpochGlobal) <= times.at(idEpoch)+timeMargin)
        index.at(idEpochGlobal) = idEpoch;
    }

    useable = TRUE;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

void GnssEstimateClockShift::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName          fileNameShiftTimeSeries;
    std::vector<Data> data;
    TimeSeriesPtr     timeSeries;
    Double            margin;

    readConfig(config, "outputfileShiftTimeSeries", fileNameShiftTimeSeries, Config::OPTIONAL, "",    "columns: mjd, clock shift");
    readConfig(config, "data",                      data,                    Config::MUSTSET,  "",    "e.g. satellite");
    readConfig(config, "timeSeries",                timeSeries,              Config::MUSTSET,  "",    "clock epochs");
    readConfig(config, "margin",                    margin,                  Config::DEFAULT,  "0.1", "[s] margin for time comparison");
    if(isCreateSchema(config)) return;

    const std::vector<Time> times = timeSeries->times();

    // initialize clock data
    for(auto &&d : data)
    {
      logStatus << "read instrument file <" << d.inNameInstrument << ">" << Log::endl;
      d.init(times, margin);
    }
    data.erase(std::remove_if(data.begin(), data.end(), [](const Data &d){ return !d.useable; }), data.end());
    if(!data.size())
      throw(Exception("no data found"));

    std::vector<Double> shiftTimeSeries(times.size(), NAN_EXPR);
    Single::forEach(times.size(), [&](UInt idEpoch)
    {
      // build observation vector
      std::vector<Double> l;
      for(const auto &d : data)
        if(d.index.at(idEpoch) != MAX_UINT)
          l.push_back(d.clockDiff(d.index.at(idEpoch)));

      if(l.size() == 0)
      {
        logWarning<<"No data found at epoch "+times.at(idEpoch).dateTimeStr()+". continue with next epoch"<<Log::endl;
        return;
      }

      // compute and remove shift
      shiftTimeSeries.at(idEpoch) = mean(Vector(l));
      for(auto &&d : data)
        if(d.index.at(idEpoch) != MAX_UINT)
        {
          d.clock(d.index.at(idEpoch))     -= shiftTimeSeries.at(idEpoch);
          d.clockDiff(d.index.at(idEpoch)) -= shiftTimeSeries.at(idEpoch);
        }
    });

    // save output files
    for(const auto &d : data)
    {
      if(!d.outNameInstrument.empty())
      {
        logStatus << "write corrected instrument file <" << d.outNameInstrument << ">" << Log::endl;
        Matrix A(d.times.size(), 2);
        copy(d.clock, A.column(1));
        InstrumentFile::write(d.outNameInstrument, Arc(d.times, A, Epoch::MISCVALUE));
      }
      if(!d.outNameInstrumentDiff.empty())
      {
        logStatus << "write clock difference instrument file <" << d.outNameInstrumentDiff << ">" << Log::endl;
        Matrix A(d.times.size(), 2);
        copy(d.clockDiff, A.column(1));
        InstrumentFile::write(d.outNameInstrumentDiff, Arc(d.times, A, Epoch::MISCVALUE));
      }
    }

    if(!fileNameShiftTimeSeries.empty())
    {
      logStatus<<"Write clock shift time series to file <"<<fileNameShiftTimeSeries<<">"<<Log::endl;
      Matrix A(shiftTimeSeries.size(), 2);
      copy(Vector(shiftTimeSeries), A.column(1));
      InstrumentFile::write(fileNameShiftTimeSeries, Arc(times, A));
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/