File: archiveXml.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 (357 lines) | stat: -rw-r--r-- 9,924 bytes parent folder | download
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/***********************************************/
/**
* @file archiveXml.cpp
*
* @brief Read/write archive files in XML format.
*
* @author Torsten Mayer-Guerr
* @date 2005-01-01
*
*/
/***********************************************/

#include "base/importStd.h"
#include "base/doodson.h"
#include "base/sphericalHarmonics.h"
#include "base/gnssType.h"
#include "parser/xml.h"
#include "archive.h"
#include "archiveXml.h"

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

OutArchiveXml::OutArchiveXml(std::ostream &_stream, const std::string &type, UInt version) : stream(_stream)
{
  XmlNodePtr xmlNode = XmlNode::create("groops");
  if(!type.empty())
    writeAttribute(xmlNode, "type", type);
  if(version)
    writeAttribute(xmlNode, "version", version);
  stack.push(xmlNode);
}

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

OutArchiveXml::~OutArchiveXml()
{
  XmlNodePtr xmlNode = stack.top();
  XmlNode::write(stream, xmlNode);
}

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

void OutArchiveXml::startTag(const std::string &name)
{
  stack.push(createXmlNode(stack.top(), name));
}

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

void OutArchiveXml::endTag(const std::string &/*name*/)
{
  stack.top() = XmlNodePtr();
  stack.pop();
}

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

InArchiveXml::InArchiveXml(std::istream &stream) : _version(0)
{
  XmlNodePtr xmlNode = XmlNode::read(stream);

  XmlAttrPtr attr = xmlNode->getAttribute("type");
  if(attr)
    typeStr = attr->getText();

  attr = xmlNode->getAttribute("version");
  if(attr)
    _version = versionStr2version(attr->getText());

  stack.push(xmlNode);
}

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

InArchiveXml::~InArchiveXml()
{
  while(!stack.empty())
    stack.pop();
}

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

void InArchiveXml::startTag(const std::string &name)
{
  stack.push(getChild(stack.top(), name, TRUE));
}

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

void InArchiveXml::endTag(const std::string &/*name*/)
{
  stack.pop();
}

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

void OutArchiveXml::save(const Int         &x) {stack.top()->setValue(x);}
void OutArchiveXml::save(const UInt        &x) {stack.top()->setValue(x);}
void OutArchiveXml::save(const Double      &x) {stack.top()->setValue(x);}
void OutArchiveXml::save(const Bool        &x) {stack.top()->setValue(x);}
void OutArchiveXml::save(const std::string &x) {stack.top()->setValue(x);}
void OutArchiveXml::save(const Angle       &x) {stack.top()->setValue(x);}
void OutArchiveXml::save(const Doodson     &x) {stack.top()->setValue(x.code());}
void OutArchiveXml::save(const GnssType    &x) {stack.top()->setValue(x.str());}

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

void InArchiveXml::load(Int         &x) {stack.top()->getValue(x);}
void InArchiveXml::load(UInt        &x) {stack.top()->getValue(x);}
void InArchiveXml::load(Double      &x) {stack.top()->getValue(x);}
void InArchiveXml::load(Bool        &x) {stack.top()->getValue(x);}
void InArchiveXml::load(std::string &x) {stack.top()->getValue(x);}
void InArchiveXml::load(Angle       &x) {stack.top()->getValue(x);}
void InArchiveXml::load(Doodson     &x) {std::string str; stack.top()->getValue(str); x = Doodson(str);}
void InArchiveXml::load(GnssType    &x) {std::string str; stack.top()->getValue(str); x = GnssType(str);}

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

void OutArchiveXml::save(const Time &x)
{
  try
  {
    LongDouble mjd = x.mjdMod();
    mjd += x.mjdInt();

    std::stringstream stream_;
    stream_.setf(std::ios::fixed,std::ios::floatfield);
    stream_.precision(18);
    stream_<<mjd;

    stack.top()->setValue(stream_.str());
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

void InArchiveXml::load(Time &x)
{
  LongDouble mjd;
  stack.top()->getValue(mjd);
  Int mjdInt = static_cast<Int>(std::floor(mjd));
  x = Time(mjdInt, static_cast<Double>(mjd-mjdInt));
}

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

void OutArchiveXml::save(const Vector &x)
{
  Matrix A = x;
  save(A);
}

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

void InArchiveXml::load(Vector &x)
{
  Matrix A;
  load(A);
  x = A;
}

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

void OutArchiveXml::save(const const_MatrixSlice &x)
{
  XmlNodePtr xmlNode = stack.top();
  writeXml(xmlNode, "type", x.getType());

  if(x.getType()==Matrix::GENERAL)
  {
    writeXml(xmlNode, "rows",    x.rows());
    writeXml(xmlNode, "columns", x.columns());
    for(UInt s=0; s<x.columns(); s++)
      for(UInt z=0; z<x.rows(); z++)
      {
        if(x(z,s)==0.0) continue;
        XmlNodePtr xmlNode2 = writeXml(xmlNode, "cell", x(z,s));
        writeAttribute(xmlNode2, "row", z);
        writeAttribute(xmlNode2, "col", s);
      }
  }
  else
  {
    writeXml(xmlNode, "upper", x.isUpper());
    writeXml(xmlNode, "dimension", x.rows());
    if(x.isUpper())
    {
      for(UInt s=0; s<x.columns(); s++)
        for(UInt z=0; z<=s; z++)
        {
          if(x(z,s)==0.0) continue;
          XmlNodePtr xmlNode2 = writeXml(xmlNode, "cell",x(z,s));
          writeAttribute(xmlNode2, "row", z);
          writeAttribute(xmlNode2, "col", s);
        }
    }
    else
    {
      for(UInt s=0; s<x.columns(); s++)
        for(UInt z=s; z<x.rows(); z++)
        {
          if(x(z,s)==0.0) continue;
          XmlNodePtr xmlNode2 = writeXml(xmlNode, "cell", x(z,s));
          writeAttribute(xmlNode2, "row", z);
          writeAttribute(xmlNode2, "col", s);
        }
    }
  }
}

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

void InArchiveXml::load(Matrix &x)
{
  XmlNodePtr xmlNode = stack.top();

  Int  type = Matrix::GENERAL;
  readXml(xmlNode, "type", type);

  if(type==Matrix::GENERAL)
  {
    UInt rows, columns = 1;
    readXml(xmlNode, "rows",    rows, TRUE);
    readXml(xmlNode, "columns", columns);
    x = Matrix(rows, columns);
  }
  else
  {
    UInt dimension;
    Bool upper;
    readXml(xmlNode, "upper", upper);
    readXml(xmlNode, "dimension", dimension);
    x = Matrix(dimension, static_cast<Matrix::Type>(type), ((upper) ? Matrix::UPPER : Matrix::LOWER));
  }

  UInt  count = childCount(xmlNode, "cell");
  for(UInt i=0; i<count; i++)
  {
    Double a = 0;
    UInt n = 0, m = 0;
    XmlNodePtr xmlNode2 = readXml(xmlNode, "cell", a, TRUE);
    readAttribute(xmlNode2, "row", n, TRUE);
    readAttribute(xmlNode2, "col", m, FALSE);
    x(n,m) = a;
  }
}

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

void OutArchiveXml::save(const SphericalHarmonics &harm)
{
  XmlNodePtr xmlNode = stack.top();
  writeXml(xmlNode, "GM", harm.GM());
  writeXml(xmlNode, "R", harm.R());
  writeXml(xmlNode, "maxDegree", harm.maxDegree());

  for(UInt n=0; n<=harm.maxDegree(); n++)
    for(UInt m=0; m<=n; m++)
    {
      if(harm.cnm()(n,m)!=0.0)
      {
        XmlNodePtr xmlNode2 = writeXml(xmlNode, "cnm", harm.cnm()(n,m));
        writeAttribute(xmlNode2, "degree", n);
        writeAttribute(xmlNode2, "order",  m);
      }
      if(harm.snm()(n,m)!=0.0)
      {
        XmlNodePtr xmlNode2 = writeXml(xmlNode, "snm", harm.snm()(n,m));
        writeAttribute(xmlNode2, "degree", n);
        writeAttribute(xmlNode2, "order",  m);
      }
      if(harm.sigma2cnm().size() && harm.sigma2cnm()(n,m)!=0.0)
      {
        XmlNodePtr xmlNode2 = writeXml(xmlNode, "sigmacnm", sqrt(harm.sigma2cnm()(n,m)));
        writeAttribute(xmlNode2, "degree", n);
        writeAttribute(xmlNode2, "order",  m);
      }
      if(harm.sigma2snm().size() && harm.sigma2snm()(n,m)!=0.0)
      {
        XmlNodePtr xmlNode2 = writeXml(xmlNode, "sigmasnm", sqrt(harm.sigma2snm()(n,m)));
        writeAttribute(xmlNode2, "degree", n);
        writeAttribute(xmlNode2, "order",  m);
      }
    }
}

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

void InArchiveXml::load(SphericalHarmonics &harm)
{
  XmlNodePtr xmlNode = stack.top();
  UInt   n,m;
  UInt   maxDegree;
  Double GM = DEFAULT_GM;
  Double R  = DEFAULT_R;

  readXml(xmlNode, "GM", GM);
  readXml(xmlNode, "R",  R);
  readXml(xmlNode, "maxDegree", maxDegree, TRUE);

  Matrix cnm      (maxDegree+1, Matrix::TRIANGULAR, Matrix::LOWER);
  Matrix snm      (maxDegree+1, Matrix::TRIANGULAR, Matrix::LOWER);
  Matrix sigma2cnm(maxDegree+1, Matrix::TRIANGULAR, Matrix::LOWER);
  Matrix sigma2snm(maxDegree+1, Matrix::TRIANGULAR, Matrix::LOWER);
  Bool hasSigma = FALSE;

  std::string name;
  while(xmlNode->hasChildren())
  {
    XmlNodePtr xmlNode2 = getNextChild(xmlNode, name);
    if(name=="cnm")
    {
      readAttribute(xmlNode2, "degree", n, TRUE);
      readAttribute(xmlNode2, "order",  m, TRUE);
      xmlNode2->getValue(cnm(n,m));
    }
    else if(name=="snm")
    {
      readAttribute(xmlNode2, "degree", n, TRUE);
      readAttribute(xmlNode2, "order",  m, TRUE);
      xmlNode2->getValue(snm(n,m));
    }
    else if(name=="sigmacnm")
    {
      readAttribute(xmlNode2, "degree", n, TRUE);
      readAttribute(xmlNode2, "order",  m, TRUE);
      xmlNode2->getValue(sigma2cnm(n,m));
      sigma2cnm(n,m) *= sigma2cnm(n,m);
      hasSigma = TRUE;
    }
    else if(name=="sigmasnm")
    {
      readAttribute(xmlNode2, "degree", n, TRUE);
      readAttribute(xmlNode2, "order",  m, TRUE);
      xmlNode2->getValue(sigma2snm(n,m));
      sigma2snm(n,m) *= sigma2snm(n,m);
      hasSigma = TRUE;
    }
  }

  if(hasSigma)
    harm = SphericalHarmonics(GM, R, cnm, snm, sigma2cnm, sigma2snm);
  else
    harm = SphericalHarmonics(GM, R, cnm, snm);
}

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