File: fileSinex.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 (278 lines) | stat: -rw-r--r-- 9,843 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
/***********************************************/
/**
* @file fileSinex.cpp
*
* @brief SINEX file representation.
*
* @author Sebastian Strasser
* @author Torsten Mayer-Guerr
* @date 2017-05-15
*
*/
/***********************************************/

#include "base/import.h"
#include "base/string.h"
#include "inputOutput/logging.h"
#include "inputOutput/file.h"
#include "inputOutput/system.h"
#include "config/config.h"
#include "fileSinex.h"

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

SinexBlockPtr Sinex::addBlock(const std::string &label)
{
  try
  {
    SinexBlockPtr block = std::make_shared<SinexBlock>();
    block->label = label;
    blocks.push_back(block);
    return block;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

SinexBlockPtr Sinex::findBlock(const std::string &label)
{
  try
  {
    auto iter = std::find_if(blocks.begin(), blocks.end(), [&](const auto &b) {return b->label == label;});
    if(iter == blocks.end())
      throw(Exception("SINEX block not found: "+label));
    return *iter;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

std::string Sinex::format(Double value, UInt length, UInt precision)
{
  try
  {
    std::string s = value%("%"+length%"%i"s+"."+precision%"%i"s+"f");
    if(s.size() > length && s.substr(0,3) == "-0.")
      return "-." + s.substr(3, s.size());
    return s;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

std::string Sinex::time2str(Time time, Bool fourDigitYear)
{
  try
  {
    if(time == Time() || time >= date2time(2500, 1, 1))
      return (fourDigitYear ? "0000" : "00") + ":000:00000"s;

    // round to full second including rollover
    UInt   year, month, day, hour, minute;
    Double second;
    time.date(year, month, day, hour, minute, second);
    time = date2time(year, month, day, hour, minute, std::round(second)+0.1);
    time.date(year, month, day, hour, minute, second);

    std::stringstream ss;
    if(fourDigitYear)
      ss<<year%"%04i"s<<":";
    else
      ss<<(year%100)%"%02i"s<<":";
    ss<<time.dayOfYear()%"%03i"s<<":";
    ss<<std::round(time.mjdMod()*86400)%"%05i"s;
    return ss.str();
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

Time Sinex::str2time(const std::string &line, std::size_t pos, Bool zeroIsMaxTime, Bool fourDigitYear)
{
  try
  {
    UInt posOffset = fourDigitYear ? 2 : 0;
    UInt year = static_cast<UInt>(String::toInt(line.substr(pos+0, 2+posOffset)));
    UInt day  = static_cast<UInt>(String::toInt(line.substr(pos+3+posOffset, 3)));
    UInt sec  = static_cast<UInt>(String::toInt(line.substr(pos+7+posOffset, 5)));
    if((year == 0) && (day == 0) && (sec == 0))
      return zeroIsMaxTime ? date2time(2500, 1, 1) : Time();
    if(!fourDigitYear)
      year += (year <= 50) ? 2000 : 1900;
    return date2time(year,1,1) + mjd2time(day-1.) + seconds2time(static_cast<Double>(sec));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

void writeFileSinex(const FileName &fileName, const Sinex &sinex)
{
  try
  {
    OutFile file(fileName);
    file<<sinex.header<<std::endl;
    file<<"*"<<std::string(79, '-')<<std::endl;
    for(const auto &block : sinex.blocks)
    {
      file<<"+"<<block->label<<std::endl;
      file<<block->ss.str();
      file<<"-"<<block->label<<std::endl;
      file<<"*"<<std::string(79, '-')<<std::endl;
    }
    file<<"%ENDSNX";
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

void readFileSinex(const FileName &fileName, Sinex &sinex)
{
  try
  {
    InFile file(fileName);

    std::string line;
    if(file.peek() == '%')
      std::getline(file, sinex.header);
    else
      logWarning<<"mandatory header in first line (starting with %) is missing"<<Log::endl;

    SinexBlockPtr block;
    while(std::getline(file, line))
    {
      line = String::trimRight(line); // trim from end
      if(line.empty() || (line.at(0) == '*')) // skip comments
        continue;
      else if(line.at(0) == '%')              // %ENDSNX
        break;
      else if(line.at(0) == '+')              // start data block
      {
        if(block && (block->label == "FILE/COMMENT"))
          continue;
        if(block)
          throw(Exception("New SINEX block starts unexpectedly: '"+line+"' in block '"+block->label+"'"));
        block = sinex.addBlock(String::trim(line.substr(1)));
      }
      else if(line.at(0) == '-') // end data block
      {
        // Do not close the FILE/COMMENT block in case of comment lines starting incorrectly with "-"
        if(block && (block->label == "FILE/COMMENT") && (block->label != String::trim(line.substr(1))))
          continue;
        if(!block || (block->label != String::trim(line.substr(1))))
          throw(Exception("SINEX block ends unexpectedly: '"+line+"'"));
        block = nullptr;
      }
      else if(!block || (line.at(0) != ' ')) // unknown line
      {
        if(!block || (block->label != "FILE/COMMENT"))
          logWarning<<"Unknown line identifier: '"<<line<<"'"<<Log::endl;
      }
      else
        block->lines.push_back(String::trimRight(line));
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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

template<> Bool readConfig(Config &config, const std::string &name, Sinex &sinex, Config::Appearance mustSet, const std::string &defaultValue, const std::string &annotation)
{
  try
  {
    Time                     timeStart, timeEnd;
    std::string              agencyCode, observationCode, constraintCode, solutionContent;
    std::string              description, output, contact, software, hardware, input;
    std::vector<std::string> comments;
    FileName                 fileNameComment;

    if(!readConfigSequence(config, name, mustSet, defaultValue, annotation))
      return FALSE;
    readConfig(config, "agencyCode",       agencyCode,       Config::OPTIONAL, "TUG",    "identify the agency providing the data");
    readConfig(config, "timeStart",        timeStart,        Config::OPTIONAL, "",       "start time of the data");
    readConfig(config, "timeEnd",          timeEnd,          Config::OPTIONAL, "",       "end time of the data ");
    readConfig(config, "observationCode",  observationCode,  Config::OPTIONAL, "C",      "technique used to generate the SINEX solution");
    readConfig(config, "constraintCode",   constraintCode,   Config::OPTIONAL, "2",      "0: tight constraint, 1: siginficant constraint, 2: unconstrained");
    readConfig(config, "solutionContent",  solutionContent,  Config::OPTIONAL, "",       "solution types contained in the SINEX solution (S O E T C A)");
    readConfig(config, "description",      description,      Config::OPTIONAL, "",       "organizitions gathering/alerting the file contents");
    readConfig(config, "contact",          contact,          Config::OPTIONAL, "",       "Address of the relevant contact. e-mail");
    readConfig(config, "output",           output,           Config::OPTIONAL, "",       "Description of the file contents");
    readConfig(config, "input",            input,            Config::OPTIONAL, "",       "Brief description of the input used to generate this solution");
    readConfig(config, "software",         software,         Config::OPTIONAL, "GROOPS", "Software used to generate the file");
    readConfig(config, "hardware",         hardware,         Config::OPTIONAL, "",       "Computer hardware on which above software was run");
    readConfig(config, "inputfileComment", fileNameComment,  Config::OPTIONAL, "",       "comments in the comment block from a file (truncated at 80 characters)");
    readConfig(config, "comment",          comments,         Config::OPTIONAL, "",       "comments in the comment block");
    endSequence(config);
    if(isCreateSchema(config))
      return TRUE;

    // header line
    std::stringstream ss;
    ss<<"%=SNX 2.02 "<<Sinex::resize(agencyCode.substr(0,3),3)<<" "<<Sinex::time2str(System::now())<<" "<<Sinex::resize(agencyCode.substr(0,3),3);
    ss<<" "<<Sinex::time2str(timeStart)<<" "<<Sinex::time2str(timeEnd)<<" "<<Sinex::resize(observationCode.substr(0,1),1)<<" 00000";
    ss<<" "<<Sinex::resize(constraintCode.substr(0,1),1)<<" "<<solutionContent;
    sinex.header = ss.str();

    {
      SinexBlockPtr block = sinex.addBlock("FILE/REFERENCE");
      *block<<"*INFO_TYPE_________ INFO________________________________________________________"<<std::endl;
      if(!description.empty()) *block<<" DESCRIPTION        "<<description<<std::endl;
      if(!output.empty())      *block<<" OUTPUT             "<<output<<std::endl;
      if(!contact.empty())     *block<<" CONTACT            "<<contact<<std::endl;
      if(!software.empty())    *block<<" SOFTWARE           "<<software<<std::endl;
      if(!hardware.empty())    *block<<" HARDWARE           "<<hardware<<std::endl;
      if(!input.empty())       *block<<" INPUT              "<<input<<std::endl;
    }

    // comment block
    if(!fileNameComment.empty() || comments.size())
    {
      SinexBlockPtr block = sinex.addBlock("FILE/COMMENT");
      if(!fileNameComment.empty())
      {
        InFile commentFile(fileNameComment);
        std::string line;
        while(std::getline(commentFile, line))
          *block<<" "<<line<<std::endl;
      }
      for(const auto &line : comments)
        *block<<" "<<line<<std::endl;
    }

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

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