File: crd2NormalPoints.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 (273 lines) | stat: -rw-r--r-- 9,146 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
/***********************************************/
/**
* @file crd2NormalPoints.cpp
*
* @brief read SLR data from CRD format.
*
* @author Torsten Mayer-Guerr
* @author Barbara Suesser-Rechberger
* @date 2022-05-07
*
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
Converts \href{https://ilrs.gsfc.nasa.gov/data_and_products/formats/crd.html}{CRD file}
and writes an \file{instrument file (METEOROLOGICAL)}{instrument} including meteorological data like
temperature, air pressure and humidity as well as an \file{instrument file (SATELLITELASERRANGING)}{instrument}
including normal point data like range, accuracy, redundancy, wavelength and window size.
)";

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

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

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

/** @brief Read SLR data from CRD format.
* @ingroup programsConversionGroup */
class Crd2NormalPoints
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(Crd2NormalPoints, SINGLEPROCESS, "read SLR data from CRD format for a given satellite", Conversion, Slr, Instrument)

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

void Crd2NormalPoints::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName fileNameNormalPoints;
    FileName fileNameMeteorological;
    FileName fileNameIn;

    readConfig(config, "outputfileNormalPoints",    fileNameNormalPoints,   Config::MUSTSET,  "output/normalsPoints_{station}.dat", "variable {station} available");
    readConfig(config, "outputfileMeteorological",  fileNameMeteorological, Config::MUSTSET,  "output/meteorological_{station}.dat", "variable {station} available");
    readConfig(config, "inputfileSlrData",          fileNameIn,            Config::MUSTSET,  "", "SLR CRD files");
    if(isCreateSchema(config)) return;

    // ==============================

    std::string stationMonumentNumber;
    Double transmitWavelength;
    std::string satelliteName;
    Time sessionDate;

    logStatus<<"read file <"<<fileNameIn<<">"<<Log::endl;
    InFile file(fileNameIn);
    std::string line;

    std::vector<SatelliteLaserRangingEpoch> normalPointEpoch;
    std::map<Time, MeteorologicalEpoch> metEpoch;
    std::multimap<std::string, std::map<Time, MeteorologicalEpoch>> metEpochStations;
    std::multimap<std::string, std::vector<SatelliteLaserRangingEpoch>> normalPointEpochStations;
    std::vector<std::string> stationMonumentNumbers;

    // Get a reference to MeteorologicalEpoch object at specified time. MeteorologicalEpoch object
    // will be default created (all NAN) if it does not already exist.
    auto metEpochGetOrInsert = [&metEpoch](const Time &time)-> MeteorologicalEpoch&
    {
      if(metEpoch.count(time) == 0)
      {
        metEpoch[time] = {};
        metEpoch[time].time = time;
        metEpoch[time].temperature = NAN;
        metEpoch[time].pressure = NAN;
        metEpoch[time].humidity = NAN;
        metEpoch[time].windSpeed = NAN;
        metEpoch[time].solarRadiation = NAN;
        metEpoch[time].precipitation = NAN;
      }
      return metEpoch[time];
    };

    while(std::getline(file, line))
    {
      if(line.empty())
        continue;
      std::string type = String::upperCase(line.substr(0,2));
      std::stringstream ss(line.substr(2));

      if(type == "H2") // station header
      {
        std::string stationName;
        ss>>stationName>>stationMonumentNumber;

        if(!stationMonumentNumbers.empty())
        {
          if(std::find(stationMonumentNumbers.begin(), stationMonumentNumbers.end(), stationMonumentNumber) == stationMonumentNumbers.end())
            stationMonumentNumbers.push_back(stationMonumentNumber);

        }
        else
        {
          stationMonumentNumbers.push_back(stationMonumentNumber);
        }
      }
      else if(type == "H3") // target header
      {
        ss>>satelliteName;
      }
      else if(type == "H4") // session header
      {
        Int dataType,year, month, day;

        ss>>dataType>>year>>month>>day;
        // Session time in UTC
        sessionDate = date2time(year, month, day);
      }
      else if(type == "H5") // Prediction Header
      {
      }
      else if(type == "H8") // end of session
      {
        normalPointEpochStations.insert({stationMonumentNumber, normalPointEpoch});
        metEpochStations.insert({stationMonumentNumber, metEpoch});

        // Clear the vector and map for the next session
        normalPointEpoch.clear();
        metEpoch.clear();
        continue;

      }
      else if(type == "C0")
      {
        Int detailType;
        ss>>detailType>>transmitWavelength;
      }
      else if(type == "11") // normal points record
      {
        Double      seconds, timeOfFlight;
        std::string systemId;
        Int         epoch, count;
        Double      window, binRms;
        SatelliteLaserRangingEpoch nPointEpoch;

        ss>>seconds>>timeOfFlight>>systemId>>epoch>>window>>count>>binRms;

        // Time in UTC
        Time time = sessionDate + seconds2time(seconds);

        switch(epoch)
        {
          case 0: time -= seconds2time(timeOfFlight);     break; // ground receive time (at SRP) (two-way)
          case 1: time -= seconds2time(0.5*timeOfFlight); break; // spacecraft bounce time (two-way)
          case 2: /* standard */                          break; // ground transmit time (at SRP) (two-way)
          default:
            logWarning<<epoch<<" Epoch Event (indicates the time event reference) not implemented"<<Log::endl;
        }

        // GROOPS uses GPS time
        nPointEpoch.time = timeUTC2GPS(time);
        nPointEpoch.range = 0.5*timeOfFlight*LIGHT_VELOCITY;

        if(count > 1)
          nPointEpoch.accuracy = 0.5*binRms*1e-12/std::sqrt(count-1)*LIGHT_VELOCITY;
        else
          nPointEpoch.accuracy = 0.5*binRms*1e-12*LIGHT_VELOCITY;

        nPointEpoch.redundancy = count;
        nPointEpoch.wavelength = transmitWavelength*1e-9;
        nPointEpoch.window = window;
        nPointEpoch.azmiuth = NAN;
        nPointEpoch.elevation = NAN;

        normalPointEpoch.push_back(nPointEpoch);
      }
      else if(type == "20") // meteorological record
      {
        Double seconds, surfacePressure, surfaceTemperature, relativeHumidity;

        ss>>seconds>>surfacePressure>>surfaceTemperature>>relativeHumidity;

        // GROOPS uses GPS time
        Time time = timeUTC2GPS(sessionDate + seconds2time(seconds));

        // Is neccessary due to record 20 and 21 may be in unspecified order.
        MeteorologicalEpoch& meteorologicalEpoch = metEpochGetOrInsert(time);
        // surface temperature in [K]
        meteorologicalEpoch.temperature = surfaceTemperature;
        // pressure in [Pa]: 1 millibar = 1 hPa = 100 Pa
        meteorologicalEpoch.pressure = surfacePressure*100;
        // relative humidity in percent
        meteorologicalEpoch.humidity = relativeHumidity;

      }
      else if(type == "21")
      {
        Double seconds, windSpeed;

        ss>>seconds>>windSpeed;

        // GROOPS uses GPS time
        Time time = timeUTC2GPS(sessionDate + seconds2time(seconds));

        // Is neccessary due to record 20 and 21 may be in unspecified order.
        MeteorologicalEpoch& meteorologicalEpoch = metEpochGetOrInsert(time);
        // Set wind speed at correct time
        meteorologicalEpoch.windSpeed = windSpeed;
      }
      else if(type == "30")
      {
        // Angles are not considered in the moment.
        logStatus<<"Record 30 available" <<Log::endl;
      }
      else
      {
      }
    } // while

    // write results
    // -------------
    for(auto stationMonumentNr : stationMonumentNumbers)
    {
      SatelliteLaserRangingArc arcSlr;
      MeteorologicalArc arcMeteorological;

      for(const auto &npStat : normalPointEpochStations)
      {
        if(npStat.first == stationMonumentNr)
          for(const auto &np : npStat.second)
            arcSlr.push_back(np);
      }

      for(const auto &metStat : metEpochStations)
      {
        if(metStat.first == stationMonumentNr)
          for(const auto &met: metStat.second)
            arcMeteorological.push_back(met.second);
      }

      VariableList varList;
      varList.setVariable("station", stationMonumentNr);

      if(arcSlr.size())
      {
        logStatus<<"write normal point data to file <"<<fileNameNormalPoints(varList)<<">"<<Log::endl;
        InstrumentFile::write(fileNameNormalPoints(varList), arcSlr);
        Arc::printStatistics(arcSlr);
      }

      if(arcMeteorological.size())
      {
        logStatus<<"write meteorological data to file <"<<fileNameMeteorological(varList)<<">"<<Log::endl;
        InstrumentFile::write(fileNameMeteorological(varList), arcMeteorological);
        Arc::printStatistics(arcMeteorological);
      }
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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