File: instrumentEstimateHelmertTransformation.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 (310 lines) | stat: -rw-r--r-- 13,285 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/***********************************************/
/**
* @file instrumentEstimateHelmertTransformation.cpp
*
* @brief Estimate Helmert transformation parameters between two networks (frame realizations).
*
* @author Sebastian Strasser
* @date 2018-01-31
*
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This program estimates a 3D Helmert transformation between two networks
(frame realizations, e.g. GNSS satellite or station network).
Each separate \config{data} represents a satellite/station/\ldots (e.g. 32 GPS satellites).
The instrument data (x,y,z position) considered can be set with \config{startData}.
The Helmert parameters are set up according to \configClass{parametrizationTemporal}{parametrizationTemporalType}
for each \configClass{timeIntervals}{timeSeriesType} and are estimated using a
\reference{robust least squares adjustment}{fundamentals.robustLeastSquares}.
)";

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

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

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

/** @brief Estimate Helmert transformation parameters between two networks (frame realizations).
* @ingroup programsGroup */
class InstrumentEstimateHelmertTransformation
{
public:
  class Data
  {
  public:
    FileName outNameInstrumentTransformed, outNameInstrumentDiff, inNameInstrument, inNameInstrumentRef;
    std::vector<Time> times;
    std::vector<UInt> intervalBoundaries;
    Matrix pos, posRef;
    UInt   startData;
    Bool   useable;

    void init(const std::vector<Time> &intervals);
    void removeEpochsOutsideIntervals(const FileName &fileName, const std::vector<Time> &intervals, Arc &arc) const;
  };

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

GROOPS_REGISTER_PROGRAM(InstrumentEstimateHelmertTransformation, SINGLEPROCESS, "Compute RMS time series from instrument file(s).", Instrument, Gnss)

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

template<> Bool readConfig(Config &config, const std::string &name, InstrumentEstimateHelmertTransformation::Data &var, Config::Appearance mustSet, const std::string &defaultValue, const std::string &annotation)
{
  if(!readConfigSequence(config, name, mustSet, defaultValue, annotation))
    return FALSE;

  renameDeprecatedConfig(config, "startData", "startDataFields", date2time(2020, 7, 14));

  readConfig(config, "outputfileInstrument",         var.outNameInstrumentTransformed, Config::OPTIONAL, "", "transformed positions as instrument type Vector3d");
  readConfig(config, "outputfileInstrumentDiff",     var.outNameInstrumentDiff,        Config::OPTIONAL, "", "position difference as instrument type Vector3d");
  readConfig(config, "inputfileInstrument",          var.inNameInstrument,             Config::MUSTSET,  "", "");
  readConfig(config, "inputfileInstrumentReference", var.inNameInstrumentRef,          Config::MUSTSET,  "", "");
  readConfig(config, "startDataFields",              var.startData,                    Config::DEFAULT,  "0", "start index of position (x,y,z) columns");
  endSequence(config);
  return TRUE;
}

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

void InstrumentEstimateHelmertTransformation::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName                   fileNameHelmertTimeSeries;
    std::vector<Data>          data;
    TimeSeriesPtr              intervalsPtr;
    Bool                       estimateShift, estimateScale, estimateRotation;
    ParametrizationTemporalPtr parametrization;
    Double                     huber, huberPower;
    UInt                       iterCount;

    renameDeprecatedConfig(config, "temporalRepresentation", "parametrizationTemporal", date2time(2020, 6, 3));
    renameDeprecatedConfig(config, "intervals",              "timeIntervals",           date2time(2020, 7, 14));

    readConfig(config, "outputfileHelmertTimeSeries", fileNameHelmertTimeSeries, Config::OPTIONAL, "",  "columns: mjd, Tx,Ty,Tz,s,Rx,Ry,Rz according to temporal parametrization");
    readConfig(config, "data",                        data,                      Config::MUSTSET,  "",  "e.g. satellite, station");
    readConfig(config, "timeIntervals",               intervalsPtr,              Config::MUSTSET,  "",  "parameters are estimated per interval");
    readConfig(config, "parametrizationTemporal",     parametrization,           Config::MUSTSET,  "",  "temporal parametrization");
    readConfig(config, "estimateShift",               estimateShift,             Config::DEFAULT,  "1", "coordinate center");
    readConfig(config, "estimateScale",               estimateScale,             Config::DEFAULT,  "1", "scale factor of position");
    readConfig(config, "estimateRotation",            estimateRotation,          Config::DEFAULT,  "1", "rotation");
    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;

    const std::vector<Time> intervals = intervalsPtr->times();
    if(intervals.size() < 2)
      throw(Exception("invalid intervals"));

    for(auto &&d : data)
    {
      logStatus << "read instrument file <" << d.inNameInstrument << ">" << Log::endl;
      d.init(intervals);
    }
    data.erase(std::remove_if(data.begin(), data.end(), [](const Data &d){ return !d.useable; }), data.end());

    const UInt paramCount    = 3*estimateShift+estimateScale+3*estimateRotation;
    const UInt intervalCount = intervals.size() - 1;
    std::vector<Vector> helmertTimeSeries;
    Single::forEach(intervalCount, [&](UInt idInterval)
    {
      parametrization->setInterval(intervals.at(idInterval), intervals.at(idInterval+1));
      const UInt trendCount = parametrization->parameterCount();

      // check if interval has data
      UInt epochCount = 0;
      for(const auto &d : data)
        epochCount += d.intervalBoundaries.at(idInterval+1) - d.intervalBoundaries.at(idInterval);
      if(epochCount == 0)
      {
        logWarning<<"No data found in interval "+intervals.at(idInterval).dateTimeStr()+" -> "+intervals.at(idInterval+1).dateTimeStr()+". continue with next interval"<<Log::endl;
        return;
      }

      // check if interval is solvable
      if(3*static_cast<UInt>(std::count_if(data.begin(), data.end(), [&](const Data &d){ return ((d.intervalBoundaries.at(idInterval+1)-d.intervalBoundaries.at(idInterval)) >= trendCount); })) < paramCount)
        throw(Exception("interval "+intervals.at(idInterval).dateTimeStr()+" -> "+intervals.at(idInterval+1).dateTimeStr()+" not solvable"));

      // set up observation vector and design matrix
      UInt idRow = 0;
      Vector l(3*epochCount);
      Matrix A(3*epochCount, paramCount*trendCount);
      for(const auto &d : data)
      {
        for(UInt idEpoch=d.intervalBoundaries.at(idInterval); idEpoch<d.intervalBoundaries.at(idInterval+1); idEpoch++)
        {
          copy((d.pos.row(idEpoch) - d.posRef.row(idEpoch)).trans(), l.row(idRow, 3));

          std::vector<UInt>   index;
          std::vector<Double> factor;
          parametrization->factors(d.times.at(idEpoch), index, factor);

          for(UInt k = 0; k < index.size(); k++)
          {
            UInt idCol = index.at(k)*paramCount;
            if(estimateShift)
            {
              A(idRow+0, idCol++) = factor.at(k);
              A(idRow+1, idCol++) = factor.at(k);
              A(idRow+2, idCol++) = factor.at(k);
            }
            if(estimateScale)
            {
              Vector3d position(d.posRef.row(idEpoch));
              A(idRow+0, idCol)   = position.x()/DEFAULT_R*factor.at(k);
              A(idRow+1, idCol)   = position.y()/DEFAULT_R*factor.at(k);
              A(idRow+2, idCol++) = position.z()/DEFAULT_R*factor.at(k);
            }
            if(estimateRotation)
            {
              Vector3d position(d.posRef.row(idEpoch));
              A(idRow+1, idCol)   =  position.z()/DEFAULT_R*factor.at(k); //rx
              A(idRow+2, idCol++) = -position.y()/DEFAULT_R*factor.at(k); //rx
              A(idRow+0, idCol)   = -position.z()/DEFAULT_R*factor.at(k); //ry
              A(idRow+2, idCol++) =  position.x()/DEFAULT_R*factor.at(k); //ry
              A(idRow+0, idCol)   =  position.y()/DEFAULT_R*factor.at(k); //rz
              A(idRow+1, idCol++) = -position.x()/DEFAULT_R*factor.at(k); //rz
            }
          }
          idRow += 3;
        }
      }

      // solve system and compute residuals
      Vector x;
      if(A.size())
      {
        Vector sigma;
        x = Vce::robustLeastSquares(A, l, 3, huber, huberPower, iterCount, sigma);
        l -= A*x;
      }

      // write residuals into input data
      idRow = 0;
      for(const auto &d : data)
        for(UInt idEpoch=d.intervalBoundaries.at(idInterval); idEpoch<d.intervalBoundaries.at(idInterval+1); idEpoch++)
          copy(l.row(3*idRow++, 3).trans(), d.pos.row(idEpoch));

      // add Helmert parameters to time series
      Vector h(1+paramCount*trendCount);
      h(0) = 0.5*(intervals.at(idInterval)+intervals.at(idInterval+1)).mjd();
      if(x.size())
      {
        copy(x, h.row(1, x.size()));
        for(UInt k = 0; k < trendCount; k++)
          if(estimateScale || estimateRotation)
            h.row(1+k*paramCount+3*estimateShift, paramCount-3*estimateShift) /= DEFAULT_R;
      }
      helmertTimeSeries.push_back(h);
    });

    // save output files
    for(const auto &d : data)
      if(!d.outNameInstrumentTransformed.empty())
      {
        logStatus << "write transformed instrument file <" << d.outNameInstrumentTransformed << ">" << Log::endl;
        Matrix A(d.times.size(), 4);
        copy(d.pos+d.posRef, A.column(1,3));
        InstrumentFile::write(d.outNameInstrumentTransformed, Arc(d.times, A, Epoch::VECTOR3D));
      }

    for(const auto &d : data)
      if(!d.outNameInstrumentDiff.empty())
      {
        logStatus << "write difference instrument file <" << d.outNameInstrumentDiff << ">" << Log::endl;
        Matrix A(d.times.size(), 4);
        copy(d.pos, A.column(1,3));
        InstrumentFile::write(d.outNameInstrumentDiff, Arc(d.times, A, Epoch::VECTOR3D));
      }

    if(!fileNameHelmertTimeSeries.empty() && (estimateShift || estimateScale || estimateRotation))
    {
      logStatus<<"write Helmert time series to file <"<<fileNameHelmertTimeSeries<<">"<<Log::endl;
      Matrix H(helmertTimeSeries.size(), 1+paramCount*parametrization->parameterCount());
      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)
  }
}

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

void InstrumentEstimateHelmertTransformation::Data::init(const std::vector<Time> &intervals)
{
  try
  {
    useable = FALSE;

    Arc arc, arcRef;
    try
    {
      arc    = InstrumentFile::read(inNameInstrument);
      arcRef = InstrumentFile::read(inNameInstrumentRef);
    }
    catch(std::exception &e)
    {
      logWarning << e.what() << Log::endl;
      return;
    }
    removeEpochsOutsideIntervals(inNameInstrument,    intervals, arc);
    removeEpochsOutsideIntervals(inNameInstrumentRef, intervals, arcRef);

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

    times  = arc.times();
    pos    = arc.matrix().column(1+startData,3);
    posRef = arcRef.matrix().column(1+startData,3);

    // find epochs IDs of interval boundaries
    UInt idEpoch = 0;
    intervalBoundaries.push_back(idEpoch);
    for(UInt idInterval = 0; idInterval < intervals.size()-1; idInterval++)
    {
      while(idEpoch < times.size() && times.at(idEpoch) < intervals.at(idInterval+1))
        idEpoch++;
      intervalBoundaries.push_back(idEpoch);
    }

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

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

void InstrumentEstimateHelmertTransformation::Data::removeEpochsOutsideIntervals(const FileName &fileName, const std::vector<Time> &intervals, Arc &arc) const
{
  UInt removedEpochs = arc.size();
  for(UInt idEpoch = arc.size(); idEpoch --> 0; )
    if(arc.at(idEpoch).time < intervals.front() || arc.at(idEpoch).time >= intervals.back())
      arc.remove(idEpoch);
  removedEpochs -= arc.size();
  if(removedEpochs > 0)
    logWarning << "removed " << removedEpochs << " epochs outside interval boundaries from <" << fileName << ">" << Log::endl;
}

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