File: ensembleAveragingScaleModel.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 (217 lines) | stat: -rw-r--r-- 7,869 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
/***********************************************/
/**
* @file ensembleAveragingScaleModel.cpp
*
* @brief First order Ensemble Averaging
*
* @author Saniya Behzadpour
* @date 2018-05-01
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This programs estimate satellite-to-satellite-tracking (SST) deterministic signals due to eclipse transits from residuals.
The ensemble averaging method is used to characterize the average properties of signal shapes across all transit events.
Each shape is assigned to one arc of 3 hours (default). This can be modefied by enabling \config{averagingInterval}.
)";

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

#include "base/import.h"
#include "files/fileMatrix.h"
#include "files/fileInstrument.h"
#include "programs/program.h"

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

/** @brief First order Ensemble Averaging
* @ingroup programsGroup */
class EnsembleAveragingScaleModel
{
public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(EnsembleAveragingScaleModel, SINGLEPROCESS, "Ensamble Averaging of eclipse transit signals", Grace)

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

void EnsembleAveragingScaleModel::run(Config &config, Parallel::CommunicatorPtr /*comm*/)
{
  try
  {
    FileName outName;
    FileName inName1, inName2, inNameSst;
    UInt     timeMargin, waveLength, neighborNumber;
    Bool     perArc = TRUE;

    readConfig(config, "outputfileScaleModel" ,        outName,    Config::MUSTSET, "",   "");
    readConfig(config, "inputfileGrace1EclipseFactor", inName1,    Config::MUSTSET, "",   "GRACE-A eclipse factors computed with integrated orbit");
    readConfig(config, "inputfileGrace2EclipseFactor", inName2,    Config::MUSTSET, "",   "GRACE-B eclipse factors computed with integrated orbit");
    readConfig(config, "inputfileGraceResiduals",      inNameSst,  Config::MUSTSET, "",   "SST Residuals");
    readConfig(config, "timeMargin",                   timeMargin, Config::MUSTSET, "25", "epochs before eclipse mode");
    readConfig(config, "waveLength",                   waveLength, Config::MUSTSET, "60", "length of the sample wave");
    if(readConfigSequence(config, "averagingInterval", Config::OPTIONAL, "", ""))
    {
      perArc = FALSE;
      readConfig(config, "nearestNeighborNumber",  neighborNumber,   Config::DEFAULT,  "24", "");
      endSequence(config);
    }
    if(isCreateSchema(config)) return;

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

    std::vector<MiscValueArc> arcListEF1, arcListEF2;
    std::vector<SatelliteTrackingArc> arcListRes;
    SatelliteTrackingArc sstArc;

    logStatus<<"read satellite data"<<Log::endl;
    InstrumentFile fileSst(inNameSst);
    InstrumentFile fileEF1(inName1);
    InstrumentFile fileEF2(inName2);
    InstrumentFile::checkArcCount({fileSst, fileEF1, fileEF2});

    UInt arcCount = fileSst.arcCount();
    arcListRes.resize(arcCount);
    arcListEF1.resize(arcCount);
    arcListEF2.resize(arcCount);
    UInt countSst = 0;

    for(UInt arcNo=0; arcNo<arcCount; arcNo++)
    {
      arcListRes.at(arcNo)  = fileSst.readArc(arcNo);
      arcListEF1.at(arcNo)  = fileEF1.readArc(arcNo);
      arcListEF2.at(arcNo)  = fileEF2.readArc(arcNo);
    }


    std::vector<std::vector<Double>> sumWaveP (arcCount); //sum of the waves in each arc with positive criteria
    std::vector<std::vector<Double>> sumWaveN (arcCount); //sum of the waves in each arc with negative criteria
    std::vector<UInt> nP (arcCount); //number of the waves in each arc with positive criteria
    std::vector<UInt> nN (arcCount); //number of the waves in each arc with positive criteria

    logStatus<<"find candidates and compute the sum"<<Log::endl;
    for(UInt arcNo=0; arcNo<arcCount; arcNo++)
    {
      sumWaveP.at(arcNo).resize(waveLength);
      sumWaveN.at(arcNo).resize(waveLength);
      nP.at(arcNo) = 1;
      nN.at(arcNo) = 1;
      Double criteria = 0;

      countSst = arcListRes.at(arcNo).size();
      for(UInt i=0; i<countSst; i++)
      {
        if(i < countSst - timeMargin)
        {
          criteria = arcListEF2.at(arcNo).at(i+timeMargin).value - arcListEF1.at(arcNo).at(i+timeMargin).value;
          if(criteria >0)
          {
            UInt maxj = waveLength;
            if (countSst - i < maxj)
                maxj = countSst - i;
            for(UInt j=i; j< i+maxj; j++)
              sumWaveP.at(arcNo).at(j-i) += arcListRes.at(arcNo).at(j).rangeRate;
            i = i + maxj;
            nP.at(arcNo) = nP.at(arcNo) + 1 ;
          }
          else if(criteria < 0)
          {
            UInt maxj = waveLength;
            if (countSst - i < maxj)
                maxj = countSst - i;
            for(UInt j=i; j< i+maxj; j++)
              sumWaveN.at(arcNo).at(j-i) += arcListRes.at(arcNo).at(j).rangeRate;
            i = i + maxj;
            nN.at(arcNo) = nN.at(arcNo) + 1 ;
          }
        }
      }
    }

    logStatus<<"compute the first order averaging"<<Log::endl;
    UInt m = perArc ? arcCount: (UInt) arcCount/neighborNumber; //number of data segments
    Matrix sumTotalP (waveLength,m);
    Matrix sumTotalN (waveLength,m);
    for(UInt arcNo=0; arcNo<arcCount; arcNo++)
      for(UInt i=0; i< m; i++)
        if ((arcNo >= i*arcCount/m)&&(arcNo < (i+1)*arcCount/m))
        {
          for(UInt j=0; j< waveLength; j++)
            sumTotalP(j,i) += sumWaveP.at(arcNo).at(j) / nP.at(arcNo);
          for(UInt j=0; j< waveLength; j++)
            sumTotalN(j,i) += sumWaveN.at(arcNo).at(j) / nN.at(arcNo);
        }

    // Compute the desired signal
    for(UInt arcNo=0; arcNo<arcCount; arcNo++)
    {
      UInt k =0;
      for(UInt i=0; i< m; i++)
        if((arcNo >= i*arcCount/m)&&(arcNo < (i+1)*arcCount/m))
          k= i;
      Double criteria = 0;
      countSst = arcListRes.at(arcNo).size();
      for(UInt i=0; i<countSst; i++)
      {
        if(i < countSst - timeMargin)
        {
          criteria = arcListEF2.at(arcNo).at(i+timeMargin).value - arcListEF1.at(arcNo).at(i+timeMargin).value;
          if(criteria > 0)
          {
            UInt maxj = waveLength;
            if (countSst - i < maxj)
                maxj = countSst - i;
            for(UInt j=i; j< i+maxj; j++)
            {
              SatelliteTrackingEpoch epoch;
              epoch.time  = arcListRes.at(arcNo).at(j).time;
              epoch.range = epoch.rangeRate = epoch.rangeAcceleration = sumTotalP(j-i,k)/(arcCount/m);
              sstArc.push_back(epoch);
            }
            i = i + maxj;
          }
          else if(criteria < 0)
          {
            UInt maxj = waveLength;
            if (countSst - i < maxj)
                maxj = countSst - i;
            for(UInt j=i; j< i+maxj; j++)
            {
              SatelliteTrackingEpoch epoch;
              epoch.time  = arcListRes.at(arcNo).at(j).time;
              epoch.range = epoch.rangeRate = epoch.rangeAcceleration = sumTotalN(j-i,k)/(arcCount/m);
              sstArc.push_back(epoch);
            }
            i = i + maxj;
          }
          else
          {
            SatelliteTrackingEpoch epoch;
            epoch.time  = arcListRes.at(arcNo).at(i).time;
            epoch.range = epoch.rangeRate = epoch.rangeAcceleration = 0;
            sstArc.push_back(epoch);
          }
        }
        else
        {
          SatelliteTrackingEpoch epoch;
          epoch.time  = arcListRes.at(arcNo).at(i).time;
          epoch.range = epoch.rangeRate = epoch.rangeAcceleration = 0;
          sstArc.push_back(epoch);
        }
      }
    }

    logStatus<<"write the output signal <"<<outName<<">"<<Log::endl;
    InstrumentFile::write(outName, sstArc);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}
/***************************************/