File: swarm2Starcamera.cpp

package info (click to toggle)
groops 0%2Bgit20240830%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 11,052 kB
  • sloc: cpp: 134,939; fortran: 1,569; makefile: 20
file content (136 lines) | stat: -rw-r--r-- 4,216 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
/***********************************************/
/**
* @file swarm2Starcamera.cpp
*
* @brief read SWARM attitude data.
*
* @author Norbert Zehentner
* @date 2014-05-23
*
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This program reads SWARM star camera data given in the cdf format
and before converted to an ascii file using the program \verb|cdfexport|
provided by the Goddard Space Flight Center (\url{http://cdf.gsfc.nasa.gov/}).
)";

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

#include "programs/program.h"
#include "base/string.h"
#include "files/fileInstrument.h"
#include "classes/earthRotation/earthRotation.h"

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

/** @brief read SWARM star camera data.
* @ingroup programsConversionGroup */
class Swarm2Starcamera
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(Swarm2Starcamera, SINGLEPROCESS, "read SWARM star camera data", Conversion, Instrument)

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

void Swarm2Starcamera::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName              fileNameSca;
    std::vector<FileName> fileNamesIn;
    EarthRotationPtr      earthRotation;

    readConfig(config, "outputfileStarCamera", fileNameSca,   Config::MUSTSET,  "", "");
    readConfig(config, "earthRotation",        earthRotation, Config::MUSTSET,  "", "");
    readConfig(config, "inputfile",            fileNamesIn,   Config::MUSTSET,  "", "");
    if(isCreateSchema(config)) return;

    logStatus<<"read input files"<<Log::endl;
    StarCameraArc arcSca;
    for(const auto &fileName : fileNamesIn)
    {
      try
      {
        logStatus<<"read file <"<<fileName<<">"<<Log::endl;
        InFile file(fileName);
        file.exceptions(std::ios::badbit|std::ios::failbit);

        // find begin of data
        for(;;)
        {
          std::string line;
          std::getline(file, line);
          if(line.find("Timestamp") != std::string::npos)
            break;
        }

        // read data
        for(;;)
        {
          std::string line;
          try
          {
            std::getline(file, line);
            if(line.empty())
              continue;
            }
            catch(std::exception &/*e*/)
            {
              break;
            }

          const std::vector<std::string> monthStr = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec"};
          UInt   day     = String::toInt(line.substr(0, 2));
          UInt   month   = std::distance(monthStr.begin(), std::find(monthStr.begin(), monthStr.end(), line.substr(3,3))) + 1;
          UInt   year    = String::toInt(line.substr(7, 4));
          UInt   hour    = String::toInt(line.substr(12, 2));
          UInt   minute  = String::toInt(line.substr(15, 2));
          Double seconds = String::toDouble(line.substr(18, 6));

          Vector q(4);
          q(1) = String::toDouble(line.substr(25, 12));
          std::getline(file, line);
          q(2) = String::toDouble(line.substr(25, 12));
          std::getline(file, line);
          q(3) = String::toDouble(line.substr(25, 12));
          std::getline(file, line);
          q(0) = String::toDouble(line.substr(25, 12));

          if(std::fabs(norm(q)-1) > 1e-5)
          {
            logWarning<<"strange norm = "<<norm(q)<<Log::endl;
            continue;
          }

          StarCameraEpoch epoch;
          epoch.time   = timeUTC2GPS(date2time(year, month, day, hour, minute, seconds));
          epoch.rotary = inverse(Rotary3d(q) * earthRotation->rotaryMatrix(epoch.time));
          arcSca.push_back(epoch);
        }
      }
      catch(std::exception &e)
      {
        logError<<e.what()<<": continue..."<<Log::endl;
      }
    } // for(idFile)

    // write data
    // ----------
    logStatus<<"write star camera data to file <"<<fileNameSca<<">"<<Log::endl;
    InstrumentFile::write(fileNameSca, arcSca);
    Arc::printStatistics(arcSca);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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