File: graceAod2TimeSplines.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 (234 lines) | stat: -rw-r--r-- 8,112 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
/***********************************************/
/**
* @file graceAod2TimeSplines.cpp
*
* @brief Convert AOD1B dealiasing data into linear splines.
*
* @author Torsten Mayer-Guerr
* @date 2005-01-19
*
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This program converts the atmospheric and ocean de-aliasing product (AOD1B)
from the GRACE SDS format into \file{time spline files}{timeSplinesGravityField}.
Multiple \config{inputfile}s must be given in the correct time order.
A linear method is assumed for the interpolation between the given points in time.

The GRACE SDS format is described in "AOD1B Product Description Document"
given at \url{http://podaac.jpl.nasa.gov/grace/documentation.html}.
)";

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

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

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

/** @brief Convert AOD1B dealiasing data into linear splines.
* @ingroup programsConversionGroup */
class GraceAod2TimeSplines
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(GraceAod2TimeSplines, SINGLEPROCESS, "convert AOD1B dealiasing data into linear splines", Conversion, TimeSplines)
GROOPS_RENAMED_PROGRAM(GraceCsrAod2TimeSplines, GraceAod2TimeSplines, date2time(2020, 6, 14))

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

void GraceAod2TimeSplines::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName outputName, atmosName, oceanName, obaName, miscName;
    std::vector<FileName> fileNameIn;

    readConfig(config, "outputfileDealiasing",     outputName, Config::OPTIONAL, "", "");
    readConfig(config, "outputfileAtmosphere",     atmosName,  Config::OPTIONAL, "", "");
    readConfig(config, "outputfileOcean",          oceanName,  Config::OPTIONAL, "", "");
    readConfig(config, "outputfileBottomPressure", obaName,    Config::OPTIONAL, "", "");
    readConfig(config, "outputfileMisc",           miscName,   Config::OPTIONAL, "", "");
    readConfig(config, "inputfile",                fileNameIn, Config::MUSTSET,  "", "");
    if(isCreateSchema(config)) return;

    Double GM = DEFAULT_GM;
    Double R  = DEFAULT_R;
    std::vector<Time>   timeList, timeAtmosList, timeOceanList, timeObaList, timeMiscList;
    std::vector<Matrix> cnmList,      snmList;
    std::vector<Matrix> cnmAtmosList, snmAtmosList;
    std::vector<Matrix> cnmOceanList, snmOceanList;
    std::vector<Matrix> cnmObaList,   snmObaList;
    std::vector<Matrix> cnmMiscList,  snmMiscList;

    for(UInt idFile=0; idFile<fileNameIn.size(); idFile++)
    {
      try
      {
        logStatus<<"read file <"<<fileNameIn.at(idFile)<<">"<<Log::endl;
        InFile file(fileNameIn.at(idFile));

        // Header
        std::string line;
        UInt dataCount = 0;
        UInt version   = 9999;
        UInt degree    = 100;
        while(!file.eof())
        {
          std::getline(file, line);
          if(line.find("CONSTANT GM")==0)
            GM = String::toDouble(line.substr(31, 22));
          if(line.find("CONSTANT A")==0)
            R = String::toDouble(line.substr(31, 22));
          if(line.find("MAXIMUM DEGREE")==0)
            degree = String::toInt(line.substr(32, 3));
          if(line.find("NUMBER OF DATA SETS")==0)
            dataCount = String::toInt(line.substr(31, 22));
          if(line.find("SOFTWARE VERSION")==0)
            version = String::toInt(line.substr(50, 2));
          if(line.find("END OF HEADER")==0)
            break;
        }

        if(version==9999)
          logWarning<<"No SOFTWARE VERSION header record found"<<Log::endl;
        if(dataCount==0)
          logWarning<<"No NUMBER OF DATA SETS header record found"<<Log::endl;

        for(UInt k=0; k<dataCount; k++)
        {
          // Data Header
          std::getline(file, line);

          Int year, month, day, hour;
          if(version==0)
          {
            year  = String::toInt(line.substr(39, 4));
            month = String::toInt(line.substr(44, 2));
            day   = String::toInt(line.substr(47, 2));
            hour  = String::toInt(line.substr(50, 2));
          }
          else
          {
            year  = String::toInt(line.substr(37, 4));
            month = String::toInt(line.substr(42, 2));
            day   = String::toInt(line.substr(45, 2));
            hour  = String::toInt(line.substr(48, 2));
          }
          Time time = date2time(year, month, day, hour, 0, 0);

          std::string type;
          if(version==0)
            type = "glo";
          else
            type = line.substr(65,3);

          Matrix cnm(degree+1, Matrix::TRIANGULAR, Matrix::LOWER);
          Matrix snm(degree+1, Matrix::TRIANGULAR, Matrix::LOWER);

          if(type != "tst")
          {
            for(UInt n=0; n<=degree; n++)
              for(UInt m=0; m<=n; m++)
              {
                UInt n2,m2;
                file>>n2>>m2;
                file>>cnm(n2,m2)>>snm(n2,m2);
                std::getline(file, line); // get rest of line
              }
          }

          if(type == "glo")
          {
            timeList.push_back(time);
            cnmList.push_back(cnm);
            snmList.push_back(snm);
          }
          else if(type == "atm")
          {
            timeAtmosList.push_back(time);
            cnmAtmosList.push_back(cnm);
            snmAtmosList.push_back(snm);
          }
          else if(type == "ocn")
          {
            timeOceanList.push_back(time);
            cnmOceanList.push_back(cnm);
            snmOceanList.push_back(snm);
          }
          else if(type == "oba")
          {
            timeObaList.push_back(time);
            cnmObaList.push_back(cnm);
            snmObaList.push_back(snm);
          }
          else
          {
            logInfo<<"Type: "<<type<<", "<<time.dateTimeStr()<<Log::endl;
            timeMiscList.push_back(time);
            cnmMiscList.push_back(cnm);
            snmMiscList.push_back(snm);
          }
        }
      }
      catch(std::exception &e)
      {
        logError<<e.what()<<": continue..."<<Log::endl;
      }
    }

    if(!isRegular(timeList) || !isRegular(timeAtmosList) || !isRegular(timeOceanList) || !isRegular(timeObaList) || !isRegular(timeMiscList))
    {
      const Time sampling = medianSampling(timeList);
      for(UInt i=0; i<timeList.size()-1; i++)
        if((timeList.at(i+1)-timeList.at(i)) > sampling)
          logWarning<<"gap between "<<timeList.at(i).dateTimeStr()<<" and "<<timeList.at(i+1).dateTimeStr()<<Log::endl;
      throw(Exception("Spline time series is not regular."));
    }

    // write data
    // ----------
    if(!outputName.empty())
    {
      logStatus<<"write dealiasing data to <"<<outputName.str()<<">"<<Log::endl;
      writeFileTimeSplinesGravityfield(outputName, GM, R, 1/*splineDegree*/, timeList, cnmList, snmList);
    }

    if(!atmosName.empty())
    {
      logStatus<<"write atmosphere data to <"<<atmosName.str()<<">"<<Log::endl;
      writeFileTimeSplinesGravityfield(atmosName, GM, R, 1/*splineDegree*/, timeAtmosList, cnmAtmosList, snmAtmosList);
    }

    if(!oceanName.empty())
    {
      logStatus<<"write ocean data to <"<<oceanName.str()<<">"<<Log::endl;
      writeFileTimeSplinesGravityfield(oceanName, GM, R, 1/*splineDegree*/, timeOceanList, cnmOceanList, snmOceanList);
    }

    if(!obaName.empty())
    {
      logStatus<<"write bottom pressure data to <"<<obaName.str()<<">"<<Log::endl;
      writeFileTimeSplinesGravityfield(obaName, GM, R, 1/*splineDegree*/, timeObaList, cnmObaList, snmObaList);
    }

    if(!miscName.empty())
    {
      logStatus<<"write misc data to <"<<miscName.str()<<">"<<Log::endl;
      writeFileTimeSplinesGravityfield(miscName, GM, R, 1/*splineDegree*/, timeMiscList, cnmMiscList, snmMiscList);
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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