File: graceSequenceOfEvents.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 (199 lines) | stat: -rw-r--r-- 7,974 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
/***********************************************/
/**
* @file graceSequenceOfEvents.cpp
*
* @brief Read GRACE SOE file.
*
* @author Beate Klinger
* @author Torsten Mayer-Guerr
* @date 2015-01-09
*
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This program converts the GRACE SOE (sequence of events) file/format into \file{instrument file (MISCVALUES)}{instrument}.
The GRACE SOE format is described in "GRACE Level 1B Data Product User Handbook JPL D-22027" and "TN-03\_SOE\_format.txt"
given at \url{http://podaac.jpl.nasa.gov/grace/documentation.html}.
The output is one arc of satellite data which can include data gaps.
)";

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

#include "programs/program.h"
#include "inputOutput/file.h"
#include "files/fileInstrument.h"

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

/** @brief Read GRACE SOE file.
* @ingroup programsConversionGroup */
class GraceSequenceOfEvents
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(GraceSequenceOfEvents, SINGLEPROCESS, "read GRACE SOE file", Conversion, Grace)

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

void GraceSequenceOfEvents::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName    fileNameGraceA, fileNameGraceB;
    FileName    fileName;
    std::string eventType, mode;
    Double      sampling = 0;

    std::string choice;
    readConfig(config, "outputfileGraceA", fileNameGraceA, Config::OPTIONAL, "", "");
    readConfig(config, "outputfileGraceB", fileNameGraceB, Config::OPTIONAL, "", "");
    readConfig(config, "inputfile",        fileName,       Config::MUSTSET,  "{groopsDataDir}/grace/TN-01_SOE.txt", "SoE file");
    if(readConfigChoice(config, "events", choice, Config::MUSTSET, "", ""))
    {
      if(readConfigChoiceElement(config, "ACCT", choice, "DSHL HeaterDisconnect"))
      {
        eventType = "ACCT";
        std::string choice;
        if(readConfigChoice(config, "mode", choice, Config::OPTIONAL, "", ""))
        {
          if(readConfigChoiceElement(config, "Heater",   choice, "DSHL HeaterDisconnect")) {mode = "DSHL-HeaterDisconnect-Event";}
          if(readConfigChoiceElement(config, "SetPoint", choice, "temperature set point")) {mode = "SetPoint";}
          endChoice(config);
        }
      }
      if(readConfigChoiceElement(config, "AOCS", choice, "coarse pointing mode or attitude hold mode"))
      {
        eventType = "AOCS";
        std::string choice;
        if(readConfigChoice(config, "mode", choice, Config::OPTIONAL, "", ""))
        {
          if(readConfigChoiceElement(config, "CPM", choice, "coarse pointing mode")) {mode = "CPM";}
          if(readConfigChoiceElement(config, "AHM", choice, "attitude hold mode"))   {mode = "AHM";}
          if(readConfigChoiceElement(config, "SM",  choice, "science mode"))         {mode = "SM";}
          endChoice(config);
        }
      }
      if(readConfigChoiceElement(config, "ACCR",  choice, "ACCR")) {eventType = "ACCR";}
      if(readConfigChoiceElement(config, "CMCAL", choice, "CoM calibration maneuver"))
      {
        eventType = "CMCAL";
        readConfig(config, "sampling", sampling, Config::OPTIONAL, "", "[seconds] create events between start and end of maneuver");
      }
      if(readConfigChoiceElement(config, "KBRCAL", choice, "KBR calibration maneuver"))
      {
        eventType = "KBRCAL";
        readConfig(config, "sampling", sampling, Config::OPTIONAL, "", "[seconds] create events between start and end of maneuver");
      }
      if(readConfigChoiceElement(config, "VCM",   choice, "CoM coordinates in SRF (m)"))              {eventType = "VCM";}
      if(readConfigChoiceElement(config, "VKB",   choice, "KBR phase center coordinates in SRF (m)")) {eventType = "VKB";}
      if(readConfigChoiceElement(config, "ICUVP", choice, "ICUVP"))                                   {eventType = "ICUVP";}
      if(readConfigChoiceElement(config, "IPU",   choice, "IPU"))                                     {eventType = "IPU";}
      if(readConfigChoiceElement(config, "IPUR",  choice, "IPUR"))                                    {eventType = "IPUR";}
      if(readConfigChoiceElement(config, "KAMI",  choice, "KAMI: time tag offset to Ka-phase meas.")) {eventType = "KAMI";}
      if(readConfigChoiceElement(config, "KMI",   choice, "K_MI: time tag offset to K-phase meas."))  {eventType = "K_MI";}
      if(readConfigChoiceElement(config, "KTOFF", choice, "KTOFF: time tag offset to KBR meas."))     {eventType = "KTOFF";}
      if(readConfigChoiceElement(config, "MANV",  choice, "MANV"))                                    {eventType = "MANV";}
      if(readConfigChoiceElement(config, "MTE1",  choice, "MTE1"))                                    {eventType = "MTE1";}
      if(readConfigChoiceElement(config, "MTE2",  choice, "MTE2"))                                    {eventType = "MTE2";}
      if(readConfigChoiceElement(config, "OCC",   choice, "OCC"))                                     {eventType = "OCC";}
      if(readConfigChoiceElement(config, "QSA",   choice, "SCA to SRF frame rotation"))               {eventType = "QSA";}
      if(readConfigChoiceElement(config, "QKS",   choice, "SCA to KBR frame rotation"))               {eventType = "QKS";}
      endChoice(config);
    }
    if(isCreateSchema(config)) return;

    logStatus<<"read input file <"<<fileName<<">"<<Log::endl;
    MiscValuesArc arcGraceA;
    MiscValuesArc arcGraceB;

    InFile file(fileName);

    std::string line;
    while(std::getline(file, line))
    {
      if(line.empty())
        continue;
      if(line.at(0) == 'x')
        continue; // the line has been deleted from active use.

      std::stringstream ss(line);
      ss.exceptions(std::ios::badbit | std::ios::failbit);

      Double          seconds;
      std::string     spaceCraft;
      std::string     event;
      UInt            count;
      std::string     comment;
      ss>>seconds>>spaceCraft>>event>>count;
      MiscValuesEpoch epoch(count);
      epoch.time = mjd2time(51544.5) + seconds2time(seconds);
      for(UInt i=0; i<count; i++)
        ss>>epoch.values(i);
      std::getline(ss, comment);

      if(event != eventType)
        continue;

      if((!mode.empty()) && (comment.find(mode) == std::string::npos))
        continue;

      if((event == "CMCAL") || (event == "KBRCAL"))
        if((sampling > 0) && (epoch.values(0) == 0)) // maneuver stop
        {
          auto sample = [](const Time &timeEnd, const Time &sampling, MiscValuesArc &arc)
          {
            if(arc.size() == 0)
              return;
            for(;;)
            {
              MiscValuesEpoch epoch = arc.back();
              epoch.time += sampling;
              if(epoch.time > timeEnd)
                return;
              arc.push_back(epoch);
            }
          };

          if(spaceCraft == "GRACEA")
            sample(epoch.time, seconds2time(sampling), arcGraceA);
          else if(spaceCraft == "GRACEB")
            sample(epoch.time, seconds2time(sampling), arcGraceB);
          continue;
        }

      if(spaceCraft == "GRACEA")
        arcGraceA.push_back(epoch);
      else if(spaceCraft == "GRACEB")
        arcGraceB.push_back(epoch);
    } // for(line)

    logInfo<<"GRACEA: events = "<<arcGraceA.size()<<Log::endl;
    logInfo<<"GRACEB: events = "<<arcGraceB.size()<<Log::endl;


    // write data
    // ----------
    if(!fileNameGraceA.empty())
    {
      logInfo<<"write data to <"<<fileNameGraceA<<">"<<Log::endl;
      InstrumentFile::write(fileNameGraceA, arcGraceA);
    }

    if(!fileNameGraceB.empty())
    {
      logInfo<<"write data to <"<<fileNameGraceB<<">"<<Log::endl;
      InstrumentFile::write(fileNameGraceB, arcGraceB);
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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