File: instrument2RmsPlotGrid.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 (288 lines) | stat: -rw-r--r-- 12,596 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/***********************************************/
/**
* @file instrument2RmsPlotGrid.cpp
*
* @brief Compute RMS plot grid from instrument file(s) containing 3D data (e.g. orbits, station positions).
*
* @author Torsten Mayer-Guerr
* @author Sebastian Strasser
* @date 2016-11-20
*
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This program computes an RMS plot grid from one or more \configFile{inputfileInstrument}{instrument}
containing 3D data (e.g. orbits or station positions), which can then be plotted as gridded data in \program{PlotGraph}.
The RMS is computed from the difference between \configFile{inputfileInstrument}{instrument} and
\configFile{inputfileInstrumentReference}{instrument}.
All instrument files must be synchronized (see \program{InstrumentSynchronize}).

Each separate \configFile{inputfileInstrument}{instrument} represents an entry (e.g. a satellite or station)
in the resulting grid. Therefore, providing, for example, 32 orbit files of GPS satellites
results in a grid with columns: mjd, id (0-31), rms.

The first three data columns of the instrument data are considered for computation of the RMS values.
The \config{factor} can be set to, for example, sqrt(3) to get 3D instead of 1D RMS values.

If \configClass{timeIntervals}{timeSeriesType} are provided, each \configFile{inputfileInstrument}{instrument}
and \configFile{inputfileInstrumentReference}{instrument} serves as a template with variable \verb|loopTime|.
This allows concatenation of instrument files, for example to create a month-long RMS plot grid from daily GPS
orbit files (see below).

Helmert parameters between the two frames can be estimated each epoch optionally if
\config{estimateShift}, \config{estimateScale}, or \config{estimateRotation} are set.
It uses a \reference{robust least squares adjustment}{fundamentals.robustLeastSquares}.

\fig{!hb}{0.8}{instrument2RmsPlotGrid}{fig:instrument2RmsPlotGrid}{Comparison of estimated GPS orbits with IGS final solution.}
)";

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

#include "programs/program.h"
#include "parser/dataVariables.h"
#include "files/fileMatrix.h"
#include "files/fileInstrument.h"
#include "classes/timeSeries/timeSeries.h"
#include "misc/varianceComponentEstimation.h"

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

/** @brief Compute RMS plot grid from instrument file(s) containing 3D data (e.g. orbits, station positions).
* @ingroup programsGroup */
class Instrument2RmsPlotGrid
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(Instrument2RmsPlotGrid, SINGLEPROCESS, "Compute RMS plot grid from instrument file(s) containing 3D data (e.g. orbits, station positions).", Instrument, Plot, Gnss)

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

void Instrument2RmsPlotGrid::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName              fileNameOutRmsPlotGrid;
    FileName              fileNameHelmertTimeSeries;
    std::vector<FileName> fileNameInInstrument, fileNameInInstrumentRef;
    TimeSeriesPtr         timesIntervalPtr;
    Double                factor;
    Bool                  estimateShift, estimateScale, estimateRotation;
    Double                huber, huberPower;
    UInt                  iterCount;

    renameDeprecatedConfig(config, "intervals", "timeIntervals", date2time(2020, 7, 14));

    readConfig(config, "outputfileRmsPlotGrid",        fileNameOutRmsPlotGrid,    Config::OPTIONAL, "",     "columns: mjd, id, rms");
    readConfig(config, "outputfileHelmertTimeSeries",  fileNameHelmertTimeSeries, Config::OPTIONAL, "",     "columns: mjd, tx, ty, tz, scale, rx, ry, rz");
    readConfig(config, "inputfileInstrument",          fileNameInInstrument,      Config::MUSTSET,  "",     "one file per satellite/station");
    readConfig(config, "inputfileInstrumentReference", fileNameInInstrumentRef,   Config::MUSTSET,  "",     "one file per satellite/station, same order as above");
    readConfig(config, "timeIntervals",                timesIntervalPtr,          Config::DEFAULT,  "",     "for {loopTime} variable in inputfile");
    readConfig(config, "factor",                       factor,                    Config::DEFAULT,  "1",    "e.g. sqrt(3) for 3D RMS");
    readConfig(config, "estimateShift",                estimateShift,             Config::DEFAULT,  "1",    "coordinate center every epoch");
    readConfig(config, "estimateScale",                estimateScale,             Config::DEFAULT,  "1",    "scale factor of position every epoch");
    readConfig(config, "estimateRotation",             estimateRotation,          Config::DEFAULT,  "1",    "rotation every epoch");
    readConfig(config, "huber",                        huber,                     Config::DEFAULT,  "2.5", "for robust least squares");
    readConfig(config, "huberPower",                   huberPower,                Config::DEFAULT,  "1.5", "for robust least squares");
    readConfig(config, "huberMaxIteration",            iterCount,                 Config::DEFAULT,  "30",  "(maximum) number of iterations for robust estimation");
    if(isCreateSchema(config)) return;

    // ======================================================

    // init time intervals
    // -------------------
    std::vector<Time> timesInterval;
    if(timesIntervalPtr)
      timesInterval = timesIntervalPtr->times();

    VariableList fileNameVariableList;
    if(timesInterval.size())
      addTimeVariables(fileNameVariableList);

    const UInt countInterval   = timesInterval.size() ? timesInterval.size()-1 : 1;
    const UInt countInstrument = fileNameInInstrument.size();

    // ======================================================

    // read instrument data and compute RMS plot grid
    // -----------------------------------------
    logStatus<<"Reading instrument data and computing RMS plot grid"<<Log::endl;
    if(fileNameInInstrument.size() != fileNameInInstrumentRef.size())
      throw Exception("Number of instrument and instrument reference files does not match: " + fileNameInInstrument.size() % "%i"s + " != " + fileNameInInstrumentRef.size() % "%i"s);

    Matrix RmsPlotGrid(countInterval*countInstrument,3);
    std::vector<Vector> helmertTimeSeries;
    UInt countOutput = 0;
    Single::forEach(countInterval, [&](UInt idInterval)
    {
      Time time;
      if(timesInterval.size())
      {
        evaluateTimeVariables(idInterval, timesInterval.at(idInterval), timesInterval.at(idInterval+1), fileNameVariableList);
        time = 0.5*(timesInterval.at(idInterval+1)+timesInterval.at(idInterval));
      }

      // read instrument data of current interval
      // ----------------------------------------
      UInt epochCount = MAX_UINT;
      std::vector<Arc> arcsInstrument(countInstrument), arcsInstrumentRef(countInstrument);
      for(UInt idInstrument=0; idInstrument<countInstrument; idInstrument++)
      {
        Arc arc;
        try
        {
          arc = InstrumentFile::read(fileNameInInstrument.at(idInstrument)(fileNameVariableList));
        }
        catch(std::exception &) {} // --> arc.size() == 0, see subsequent if()

        if(!arc.size())
        {
          logWarning << "Instrument file <" << fileNameInInstrument.at(idInstrument)(fileNameVariableList) << "> not found or empty, skipping." << Log::endl;
          continue;
        }

        Arc arcRef;
        try
        {
          arcRef = InstrumentFile::read(fileNameInInstrumentRef.at(idInstrument)(fileNameVariableList));
        }
        catch(std::exception &) {} // --> arcRef.size() == 0, see subsequent if()

        if(!arcRef.size())
        {
          logWarning << "Instrument reference file <" << fileNameInInstrumentRef.at(idInstrument)(fileNameVariableList) << "> not found or empty, skipping." << Log::endl;
          continue;
        }

        // check if data of reference file matches instrument file
        if(arc.size() != arcRef.size())
          throw Exception("Instrument and instrument reference epoch counts do not match: " + arc.size() % "%i"s + " != " + arcRef.size() % "%i"s + " for files <" +
                          fileNameInInstrument.at(idInstrument)(fileNameVariableList).str() + "> and <" + fileNameInInstrumentRef.at(idInstrument)(fileNameVariableList).str() + ">");

        arcsInstrument.at(idInstrument)    = arc;
        arcsInstrumentRef.at(idInstrument) = arcRef;
        epochCount = std::min(epochCount, arc.size());
      }

      if(epochCount == MAX_UINT)
      {
        logWarning<<"No instrument data found. continue with next epoch"<<Log::endl;
        return;
      }

      // Estimate Helmert transformation
      // -------------------------------
      Matrix l(3*arcsInstrument.size(), epochCount);
      for(UInt idEpoch=0; idEpoch<epochCount; idEpoch++)
      {
        Time timeEpoch;
        Matrix A(3*arcsInstrument.size(), 3*estimateShift+estimateScale+3*estimateRotation);
        for(UInt idInstrument=0; idInstrument<countInstrument; idInstrument++)
        {
          if(arcsInstrument.at(idInstrument).size() == 0)
            continue;
          timeEpoch = arcsInstrument.at(idInstrument).at(idEpoch).time;

          const Vector3d pos(arcsInstrument.at(idInstrument).at(idEpoch).data().row(0,3));
          const Vector3d posRef(arcsInstrumentRef.at(idInstrument).at(idEpoch).data().row(0,3));
          const Vector3d diff = pos - posRef;
          l(3*idInstrument+0, idEpoch) = diff.x();
          l(3*idInstrument+1, idEpoch) = diff.y();
          l(3*idInstrument+2, idEpoch) = diff.z();

          UInt idx = 0;
          if(estimateShift)
          {
            A(3*idInstrument+0, idx++) = 1;
            A(3*idInstrument+1, idx++) = 1;
            A(3*idInstrument+2, idx++) = 1;
          }
          if(estimateScale)
          {
            A(3*idInstrument+0, idx)   = posRef.x()/DEFAULT_R;
            A(3*idInstrument+1, idx)   = posRef.y()/DEFAULT_R;
            A(3*idInstrument+2, idx++) = posRef.z()/DEFAULT_R;
          }
          if(estimateRotation)
          {
            A(3*idInstrument+1, idx)   =  posRef.z()/DEFAULT_R; //rx
            A(3*idInstrument+2, idx++) = -posRef.y()/DEFAULT_R; //rx
            A(3*idInstrument+0, idx)   = -posRef.z()/DEFAULT_R; //ry
            A(3*idInstrument+2, idx++) =  posRef.x()/DEFAULT_R; //ry
            A(3*idInstrument+0, idx)   =  posRef.y()/DEFAULT_R; //rz
            A(3*idInstrument+1, idx++) = -posRef.x()/DEFAULT_R; //rz
          }
        }

        Vector x;
        if(A.size())
        {
          Vector sigma;
          x = Vce::robustLeastSquares(A, l.column(idEpoch), 3, huber, huberPower, iterCount, sigma);
          l.column(idEpoch) -= A*x;
        }

        Vector h(8);
        h(0) = timeEpoch.mjd();
        UInt idx = 0;
        if(estimateShift)
        {
          h(1) = x(idx++);
          h(2) = x(idx++);
          h(3) = x(idx++);
        }
        if(estimateScale)
        {
          h(4) = x(idx++)/DEFAULT_R;
        }
        if(estimateRotation)
        {
          h(5) = x(idx++)/DEFAULT_R;
          h(6) = x(idx++)/DEFAULT_R;
          h(7) = x(idx++)/DEFAULT_R;
        }
        helmertTimeSeries.push_back(h);
      }

      // compute RMS values of current interval
      // -------------------------------------
      for(UInt idInstrument=0; idInstrument<countInstrument; idInstrument++)
        if(norm(l.row(3*idInstrument,3))>1e-10)
        {
          RmsPlotGrid(countOutput, 0) = time.mjd();
          RmsPlotGrid(countOutput, 1) = idInstrument;
          RmsPlotGrid(countOutput, 2) = factor * norm(l.row(3*idInstrument,3))/std::sqrt(3*l.columns());  // 1D RMS
          countOutput++;
        }
    });

    // ======================================================

    // save file
    // ---------
    if(!fileNameOutRmsPlotGrid.empty())
    {
      logStatus<<"Write RMS plot grid to file <"<<fileNameOutRmsPlotGrid<<">"<<Log::endl;
      writeFileMatrix(fileNameOutRmsPlotGrid, RmsPlotGrid.row(0, countOutput));
    }

    if(!fileNameHelmertTimeSeries.empty())
    {
      logStatus<<"Write Helmert time series to file <"<<fileNameHelmertTimeSeries<<">"<<Log::endl;
      Matrix H(helmertTimeSeries.size(), 8);
      for(UInt i=0; i<H.rows(); i++)
        copy(helmertTimeSeries.at(i).trans(), H.row(i));
      writeFileMatrix(fileNameHelmertTimeSeries, H);
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e);
  }
}

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