File: archiveJson.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 (426 lines) | stat: -rw-r--r-- 11,569 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/***********************************************/
/**
* @file archiveJson.cpp
*
* @brief Read/write archive files in JSON format.
*
* @author Torsten Mayer-Guerr
* @date 2025-01-22
*
*/
/***********************************************/

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

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

static std::string quoted(const std::string &str)
{
  if(str.empty())
    return str;
  std::stringstream ss;
  ss<<std::quoted(str);
  return ss.str();
}

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

static void writeJsonData(std::ostream &stream, XmlNodePtr xmlNode, UInt depth)
{
  // has children -> struct
  if(xmlNode->hasChildren())
  {
    stream<<"{"<<std::endl;
    while(xmlNode->hasChildren())
    {
      XmlNodePtr child = xmlNode->getNextChild();
      stream<<std::string(depth, '\t')<<std::quoted(child->getName())<<": ";
      // is array?
      if(xmlNode->findChild(child->getName()))
      {
        stream<<"[";
        while(child)
        {
          writeJsonData(stream, child, depth+1);
          child = xmlNode->getChild(child->getName());
          if(child)
            stream<<", ";
        }
        stream<<"]";
      }
      else
        writeJsonData(stream, child, depth+1);
      if(xmlNode->hasChildren())
        stream<<","<<std::endl;
    }
    stream<<std::endl<<std::string(depth-1, '\t')<<"}";
  }
  else if(!xmlNode->getText().empty()) // value
  {
    stream<<xmlNode->getText();
  }
  else // empty
    stream<<"null";
}

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

OutArchiveJson::OutArchiveJson(std::ostream &_stream, const std::string &type, UInt version) : stream(_stream)
{
  XmlNodePtr xmlNode = XmlNode::create("groops");
  writeXml(xmlNode, "fileType",    quoted(type));
  writeXml(xmlNode, "fileVersion", version);
  stack.push(xmlNode);
}

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

OutArchiveJson::~OutArchiveJson()
{
  XmlNodePtr xmlNode = stack.top();
  writeJsonData(stream, xmlNode, 1);
}

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

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

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

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

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

static void readObject(std::istream &stream, XmlNodePtr parent);

static void readData(std::istream &stream, XmlNodePtr xmlNode)
{
  char c;
  stream>>c;
  if(c == '{')
  {
    do
    {
      readObject(stream, xmlNode);
      stream>>c;
    }
    while(c == ',');
    if(c != '}')
      throw(Exception("json parser error in '"+xmlNode->getName()+"': '}' expected"));
    return;
  }

  stream.putback(c);
  std::string value;
  if(c == '"')
  {
    stream>>std::quoted(value);
    xmlNode->setValue(value);
  }
  else
  {
    // read until delimiter (,]} or white space)
    while(std::string(",]} \n\t").find_first_of((c = stream.get())) == std::string::npos)
      value.push_back(c);
    stream.putback(c);
    if(value == "true")
      xmlNode->setValue(1);
    else if(value == "false")
      xmlNode->setValue(0);
    else if(value != "null")
      xmlNode->setValue(value);
  }
}

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

static void readObject(std::istream &stream, XmlNodePtr parent)
{
  std::string name;
  stream>>std::quoted(name);
  XmlNodePtr xmlNode = createXmlNode(parent, name);

  char c;
  stream>>c; // ":"
  if(c != ':')
    throw(Exception("json parser error in '"+name+"': ':' expected"));

  // content
  stream>>c;
  if(c == '[') // array
  {
    for(;;)
    {
      readData(stream, xmlNode);
      stream>>c;
      if(c != ',')
        break;
      xmlNode = createXmlNode(parent, name);
    }
    if(c != ']')
      throw(Exception("json parser error in '"+name+"': ']' expected"));
    return;
  }

  stream.putback(c);
  readData(stream, xmlNode);
}

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

InArchiveJson::InArchiveJson(std::istream &stream) : _version(0)
{
  XmlNodePtr xmlNode = XmlNode::create("groops");
  stack.push(xmlNode);
  readData(stream, xmlNode);
  readXml(xmlNode, "fileType",    typeStr);
  readXml(xmlNode, "fileVersion", _version);
}

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

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

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

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

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

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

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

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

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

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

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

void OutArchiveJson::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(16);
    stream_<<mjd;

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

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

void InArchiveJson::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 OutArchiveJson::save(const Vector &x)
{
  Matrix A = x;
  save(A);
}

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

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

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

void OutArchiveJson::save(const const_MatrixSlice &A)
{
  XmlNodePtr xmlNode = stack.top();

  std::string type;
  if(A.getType()==Matrix::GENERAL)
    type = "general";
  else
  {
    type = (A.isUpper() ? "upper" : "lower");
    if(A.getType()==Matrix::TRIANGULAR)
      type += "TriangularMatrix";
    else if(A.getType()==Matrix::SYMMETRIC)
      type += "SymmetricMatrix";
  }

  std::stringstream ss;
  ss.setf(std::ios::scientific,std::ios::floatfield);
  ss.precision(18);
  UInt minCol = 0, maxCol = A.columns();
  ss<<std::endl<<"[";
  for(UInt i=0; i<A.rows(); i++)
  {
    if((A.getType() != Matrix::GENERAL) &&  A.isUpper()) minCol = i;
    if((A.getType() != Matrix::GENERAL) && !A.isUpper()) maxCol = i+1;
    ss<<"[";
    for(UInt k=minCol; k<maxCol; k++)
      ss<<A(i,k)<<((k<maxCol-1) ? ", " : "]");
    ss<<((i < A.rows()-1) ? ",\n " : "]");
  }

  writeXml(xmlNode, "type",    quoted(type));
  writeXml(xmlNode, "rows",    A.rows());
  writeXml(xmlNode, "columns", A.columns());
  if(A.size())
    writeXml(xmlNode, "data",  ss.str());
  else
    writeXml(xmlNode, "data",  "[]");
}

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

void InArchiveJson::load(Matrix &A)
{
  XmlNodePtr xmlNode = stack.top();

  std::string type;
  UInt rows, columns = 1;
  readXml(xmlNode, "type",    type, TRUE);
  readXml(xmlNode, "rows",    rows, TRUE);
  readXml(xmlNode, "columns", columns);

  if(type == "general")
    A = Matrix(rows, columns, Matrix::NOFILL);
  else if(type == "upperTriangularMatrix")
    A = Matrix(rows, Matrix::TRIANGULAR, Matrix::UPPER);
  else if(type == "lowerTriangularMatrix")
    A = Matrix(rows, Matrix::TRIANGULAR, Matrix::LOWER);
  else if(type == "upperSymmetricMatrix")
    A = Matrix(rows, Matrix::SYMMETRIC, Matrix::UPPER);
  else if(type == "lowerSymmetricMatrix")
    A = Matrix(rows, Matrix::SYMMETRIC, Matrix::LOWER);
  else
    throw(Exception("unknown matrix type: "+type));

  UInt minCol = 0, maxCol = A.columns();
  for(UInt i=0; i<A.rows(); i++)
  {
    if((A.getType() != Matrix::GENERAL) &&  A.isUpper()) minCol = i;
    if((A.getType() != Matrix::GENERAL) && !A.isUpper()) maxCol = i+1;
    for(UInt k=minCol; k<maxCol; k++)
      readXml(xmlNode, "data", A(i,k), TRUE);
  }
}

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

void OutArchiveJson::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++)
    {
      writeXml(xmlNode, "cnm", harm.cnm()(n,m));
      writeXml(xmlNode, "snm", harm.snm()(n,m));
      if(harm.sigma2cnm().size())
      {
        writeXml(xmlNode, "sigmacnm", std::sqrt(harm.sigma2cnm()(n,m)));
        writeXml(xmlNode, "sigmasnm", std::sqrt(harm.sigma2snm()(n,m)));
      }
    }
}

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

void InArchiveJson::load(SphericalHarmonics &harm)
{
  XmlNodePtr xmlNode = stack.top();
  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);

  for(UInt n=0; n<=maxDegree; n++)
    for(UInt m=0; m<=n; m++)
    {
      readXml(xmlNode, "cnm", cnm(n,m), TRUE);
      readXml(xmlNode, "snm", snm(n,m), TRUE);
      readXml(xmlNode, "sigmacnm", sigma2cnm(n,m));
      readXml(xmlNode, "sigmasnm", sigma2snm(n,m));
      sigma2cnm(n,m) *= sigma2cnm(n,m);
      sigma2snm(n,m) *= sigma2snm(n,m);
    }

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

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