File: normalsSphericalHarmonics2Sinex.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 (206 lines) | stat: -rw-r--r-- 8,066 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
/***********************************************/
/**
* @file normalsSphericalHarmonics2Sinex.cpp
*
* @brief Write potential coefficients and normal equations to SINEX format.
*
* @author Saniya Behzadpour
* @author Sebastian Strasser
* @date 2015-03-17
*
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
Write potential coefficients and \file{normal equations}{normalEquation} to
\href{http://www.iers.org/IERS/EN/Organization/AnalysisCoordinator/SinexFormat/sinex.html}{SINEX format}.

See also \program{Sinex2Normals} and \program{GnssNormals2Sinex}.
)";

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

#include "programs/program.h"
#include "inputOutput/fileSinex.h"
#include "files/fileMatrix.h"
#include "files/fileNormalEquation.h"

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

/** @brief Write potential coefficients and normal equations to SINEX format.
* @ingroup programsConversionGroup */
class NormalsSphericalHarmonics2Sinex
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(NormalsSphericalHarmonics2Sinex, SINGLEPROCESS, "Write potential coefficients and normal equations to SINEX format.", Conversion, NormalEquation)

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

void NormalsSphericalHarmonics2Sinex::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName    fileNameSinex;
    FileName    fileNameNormals;
    FileName    fileNameSolution, fileNameSigmax, fileNameApriori, fileNameAprMat;
    Time        time0;
    Sinex       sinex;

    readConfig(config, "outputfileSinex",        fileNameSinex,    Config::MUSTSET,  "", "solutions in SINEX format");
    readConfig(config, "inputfileNormals",       fileNameNormals,  Config::MUSTSET,  "", "normal equation matrix");
    readConfig(config, "inputfileSolution",      fileNameSolution, Config::OPTIONAL, "", "parameter vector");
    readConfig(config, "inputfileSigmax",        fileNameSigmax,   Config::OPTIONAL, "", "standard deviations of the parameters (sqrt of the diagonal of the inverse normal equation)");
    readConfig(config, "inputfileApriori",       fileNameApriori,  Config::MUSTSET,  "", "apriori parameter vector");
    readConfig(config, "inputfileAprioriMatrix", fileNameAprMat,   Config::OPTIONAL, "", "normal equation matrix of applied constraints");
    readConfig(config, "time",                   time0,            Config::MUSTSET,  "", "reference time for parameters");
    readConfig(config, "sinexHeader",            sinex,            Config::MUSTSET,  "", "");
    if(isCreateSchema(config)) return;

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

    // read data from files
    // --------------------
    Matrix x;
    if(!fileNameSolution.empty())
    {
      logStatus<<"reading solution from <"<<fileNameSolution<<">"<<Log::endl;
      readFileMatrix(fileNameSolution, x);
    }

    Matrix sigmax;
    if(!fileNameSigmax.empty())
    {
      logStatus<<"reading standard deviations from <"<<fileNameSigmax<<">"<<Log::endl;
      readFileMatrix(fileNameSigmax, sigmax);
    }

    Matrix x0;
    if(!fileNameApriori.empty())
    {
      logStatus<<"reading apriori solution from <"<<fileNameApriori<<">"<<Log::endl;
      readFileMatrix(fileNameApriori, x0);
    }

    Matrix N, n;
    NormalEquationInfo info;
    logStatus<<"reading normal equation matrix from <"<<fileNameNormals<<">"<<Log::endl;
    readFileNormalEquation(fileNameNormals, info, N, n);
    fillSymmetric(N);
    UInt countParameter = N.rows();

    Matrix dN;
    std::vector<Bool> parameterIsConstrained(countParameter, FALSE);
    if(!fileNameAprMat.empty())
    {
      logStatus<<"reading normal equation matrix of applied constraints <"<<fileNameAprMat<<">"<<Log::endl;
      Vector n;
      NormalEquationInfo info;
      readFileNormalEquation(fileNameAprMat, info, dN, n);
      fillSymmetric(dN);
      if(dN.rows() != parameterIsConstrained.size())
        throw(Exception("Parameter count in constraint matrix and normal equation matrix differs ("+dN.rows()%"%i"s+" vs. "+N.rows()%"%i"s+" )."));
      for(UInt i=0; i<dN.rows(); i++)
        parameterIsConstrained.at(i) = (dN(i, i) != 0.0);
    }

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

    auto writeVector = [&](SinexBlockPtr block, const Vector x, const Vector sigma=Vector())
    {
      for(UInt i=0; i<x.size(); i++)
      {
        if(info.parameterName.at(i).type.substr(0,18) != "sphericalHarmonics")
          throw(Exception("non spherical harmonics parameter: " + info.parameterName.at(i).str()));

        const UInt idxDegree = info.parameterName.at(i).type.find_first_of('_')+1;
        const UInt idxOrder  = info.parameterName.at(i).type.find_last_of('_')+1;

        const std::string degree = std::atoi(info.parameterName.at(i).type.substr(idxDegree,idxOrder-idxDegree-1).c_str())%"%4i"s; // degree
        const std::string order  = std::atoi(info.parameterName.at(i).type.substr(idxOrder).c_str())%"%4i"s;                       // order

        *block<<(i+1)%" %5i "s<<((info.parameterName.at(i).type[idxDegree-2]=='c') ? "CN" : "SN")<<"     ";
        *block<<degree<<" -- "<<order<<" "<<Sinex::time2str(time0)<<" ---- "<<(parameterIsConstrained.at(i) ? "1" : "2")<<x(i)%" %21.14e"s;
        if(sigma.size())
          *block<<sigma(i)%" %11.4e"s;
        *block<<std::endl;
      }
    };

    auto writeMatrix = [&](SinexBlockPtr block, const Matrix &N)
    {
      for(UInt i=0; i<N.rows(); i++)
        for(UInt k=i; k<N.rows(); k++)
          if(N(i,k))
          {
            *block<<(i+1)%" %5i"s<<(k+1)%" %5i"s<<N(i, k)%" %21.14e"s;
            for(UInt l=1; (l<3) && (k+1<N.rows()) && N(i,k+1); l++, k++)
              *block<<N(i, k+1)%" %21.14e"s;
            *block<<std::endl;
          }
    };

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

    // add data to SINEX
    // -----------------
    {
      SinexBlockPtr block = sinex.addBlock("SOLUTION/STATISTICS");
      *block<<"*____STATISTICAL_PARAMETER_____ _______VALUE(S)_______"<<std::endl;
      *block<<" NUMBER OF OBSERVATIONS         "<<info.observationCount%"%22i"s<<std::endl;
      *block<<" NUMBER OF UNKNOWNS             "<<countParameter%"%22i"s<<std::endl;
      *block<<" NUMBER OF DEGREES OF FREEDOM   "<<(info.observationCount-countParameter)%"%22i"s<<std::endl;
      *block<<" WEIGHTED SQUARE SUM OF O-C     "<<info.lPl(0)%"%22.15e"s<<std::endl;
    }

    if(x.size())
    {
      SinexBlockPtr block = sinex.addBlock("SOLUTION/ESTIMATE");
      *block<<"*INDEX _TYPE_ CODE PT SOLN _REF_EPOCH__ UNIT S ___ESTIMATED_VALUE___ __STD_DEV__"<<std::endl;
      writeVector(block, x, sigmax);
    }

    if(x0.size())
    {
      SinexBlockPtr block = sinex.addBlock("SOLUTION/APRIORI");
      *block<<"*INDEX _TYPE_ CODE PT SOLN _REF_EPOCH__ UNIT S ____APRIORI_VALUE____ __STD_DEV__"<<std::endl;
      writeVector(block, x0, Vector(x0.size()));
    }

    if(n.size())
    {
      SinexBlockPtr block = sinex.addBlock("SOLUTION/NORMAL_EQUATION_VECTOR");
      *block<<"*INDEX _TYPE_ CODE PT SOLN _REF_EPOCH__ UNIT S ___RIGHT_HAND_SIDE___"<<std::endl;
      writeVector(block, n);
    }

    if(N.size())
    {
      SinexBlockPtr block = sinex.addBlock("SOLUTION/NORMAL_EQUATION_MATRIX U");
      *block<<"*PARA1 PARA2 _______PARA2+0_______ _______PARA2+1_______ _______PARA2+2_______"<<std::endl;
      writeMatrix(block, N);
    }

    if(dN.size())
    {
      SinexBlockPtr block = sinex.addBlock("SOLUTION/MATRIX_APRIORI U INFO");
      *block<<"*PARA1 PARA2 _______PARA2+0_______ _______PARA2+1_______ _______PARA2+2_______"<<std::endl;
      writeMatrix(block, dN);
    }

    logStatus<<"write SINEX file <"<<fileNameSinex<<">"<<Log::endl;
    if(sinex.header.size() > 65)
      sinex.header.replace(60, 5, countParameter%"%05i"s);
    writeFileSinex(fileNameSinex, sinex);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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