File: psmslOceanBottomPressure2TimeSeries.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 (195 lines) | stat: -rw-r--r-- 5,905 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
/***********************************************/
/**
* @file psmslOceanBottomPressure2TimeSeries.cpp
*
* @brief Read ocean bottom pressure (OBP) time series in the PSMSL file format.
*
* @author Andreas Kvas
* @date 2019-12-10
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This programs reads ocean bottom pressure time series from the Permanent Service for Mean Sea Level (PSMSL).

In addition to the OBP measurements, the recorder position can be written to a \file{grid file}{griddedData}.
)";

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

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

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

/** @brief Read ocean bottom pressure (OBP) time series in the PSMSL file format
* @ingroup programsConversionGroup */
class PsmslOceanBottomPressure2TimeSeries
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(PsmslOceanBottomPressure2TimeSeries, SINGLEPROCESS, "read ocean bottom pressure (OBP) time series in the PSMSL file format", Conversion)
GROOPS_RENAMED_PROGRAM(ReadOceanBottomPressurePSMSL, PsmslOceanBottomPressure2TimeSeries, date2time(2020, 9, 10))

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

void PsmslOceanBottomPressure2TimeSeries::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName      outName, posName, timeName;
    FileName      inName;
    Double        a, f;
    TimeSeriesPtr timeSeries;
    Bool          isDaily, ignoreBadData;

    readConfig(config, "outputfileTimeSeries", outName,       Config::MUSTSET,  "",  "");
    readConfig(config, "outputfilePosition",   posName,       Config::OPTIONAL, "",  "recorder position as gridded data");
    readConfig(config, "inputfile",            inName,        Config::MUSTSET,  "",  "");
    readConfig(config, "isDaily",              isDaily,       Config::MUSTSET,  "0", "");
    readConfig(config, "ignoreBadData",        ignoreBadData, Config::MUSTSET,  "1", "");
    readConfig(config, "R",                    a,             Config::DEFAULT,  STRING_DEFAULT_GRS80_a, "");
    readConfig(config, "inverseFlattening",    f,             Config::DEFAULT,  STRING_DEFAULT_GRS80_f, "");
    readConfig(config, "timeSeries",           timeSeries,    Config::MUSTSET,  "",  "");
    if(isCreateSchema(config)) return;

    std::vector<Time> times = timeSeries->times();

    logStatus<<"read inputfile <"<<inName<<">"<<Log::endl;
    InFile file(inName);

    // Header einlesen
    Double L=0, B=0;
    std::string line;
    while(std::getline(file, line))
    {
      std::stringstream ss(line);
      std::string tag;
      std::string tmp;
      ss>>tag;
      if(tag == "Latitude")
        ss>>tmp>>tmp>>B;
      if(tag == "Longitude")
        ss>>tmp>>tmp>>L;
      if(tag == "Recno")
        break;
    }

    std::vector<Time>   times2;
    std::vector<Double> values2;
    while(std::getline(file, line))
    {
      std::stringstream ss(line);
      std::string tag;
      std::string tmp;
      UInt year, day, flag;
      Double hour;
      ss>>tmp;
      ss>>flag;
      if(ignoreBadData && flag==1) continue;
      ss>>year>>day;

      Time time = date2time(year, 1, 0);
      time += seconds2time(static_cast<Double>(day)*86400.0);
      if(!isDaily)
      {
        ss>>hour;
        time += seconds2time(hour*60.0*60.0);
      }
      Double total, tide, drift, resid;
      if(!isDaily) ss>>total>>tide;
      ss>>drift>>resid;
      if(!ss.good())
        continue;

      times2.push_back(time);
      values2.push_back(resid);
    }

    Double mean = 0.0;
    for(UInt i=0; i<values2.size(); i++)
      mean += values2.at(i);
    mean /= values2.size();
    for(UInt i=0; i<values2.size(); i++)
      values2.at(i) -= mean;

    UInt idx = 0;
    std::vector<UInt>   count(times.size()-1, 0);
    std::vector<Double> value(times.size()-1, 0.0);
    for(UInt i=0; i<times2.size(); i++)
    {
      if(times2.at(i) < times.at(idx))
        continue;
      while((idx<times.size()-1) && (times2.at(i)>=times.at(idx+1))) idx++;
      if((idx>=times.size()-1)||(times2.at(i)>=times.at(times.size()-1)))
        break;

      count.at(idx)++;
      value.at(idx) += values2.at(i);
    }

    logStatus<<"remove mean"<<Log::endl;
    mean = 0.0;
    UInt meanCount = 0;
    for(UInt i=0; i<value.size(); i++)
      if(count.at(i) != 0)
      {
        value.at(i) /= count.at(i);
        mean += value.at(i);
        meanCount++;
      }
    mean /= meanCount;
    meanCount = 0;
    for(UInt i=0; i<value.size(); i++)
      if(count.at(i)!=0)
      {
        value.at(i) -= mean;
        meanCount++;
      }
      else
        count.at(i)=0;

    logInfo<<"  mean  = "<<mean<<Log::endl;
    logInfo<<"  count = "<<meanCount<<Log::endl;
    logInfo<<"  L     = "<<L<<Log::endl;
    logInfo<<"  B     = "<<B<<Log::endl;

    if(meanCount == 0)
      return;

    if(!outName.empty())
    {
      logStatus<<"write time series to file <"<<outName<<">"<<Log::endl;
      Matrix A(meanCount,2);
      UInt idx = 0;
      for(UInt i=0; i<value.size(); i++)
        if(count.at(i) != 0)
        {
          A(idx,0) = times.at(i).mjd();//(0.5*times.at(i)+0.5*times.at(i+1)).mjd();
          A(idx,1) = value.at(i);
          idx++;
        }
      InstrumentFile::write(outName, Arc(A));
    }

    if(!posName.empty())
    {
      logStatus<<"write position to file <"<<posName<<">"<<Log::endl;
      Ellipsoid ellipsoid(a,f);
      writeFileGriddedData(posName, GriddedData(ellipsoid, {ellipsoid(Angle(L*DEG2RAD), Angle(B*DEG2RAD), 0.0)}, {1.}, {}));
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

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