File: fileEphemerides.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 (283 lines) | stat: -rw-r--r-- 9,126 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
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
274
275
276
277
278
279
280
281
282
283
/***********************************************/
/**
* @file fileEphemerides.cpp
*
* @brief Ephemerides of sun, moon, and planets.
*
* @author Torsten Mayer-Guerr
* @date 2020-08-11
*
*/
/***********************************************/

#define DOCSTRING_FILEFORMAT_Ephemerides

#include "base/import.h"
#include "inputOutput/fileArchive.h"
#include "files/fileFormatRegister.h"
#include "files/fileEphemerides.h"

GROOPS_REGISTER_FILEFORMAT(Ephemerides, FILE_EPHEMERIDES_TYPE)

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

void InFileEphemerides::open(const FileName &name)
{
  try
  {
    close();
    if(name.empty())
      return;

    UInt intervalsCount, bodyCount;

    file.open(name, FILE_EPHEMERIDES_TYPE, FILE_EPHEMERIDES_VERSION);
    file>>nameValue("bodyCount",      bodyCount);
    file>>nameValue("intervalsCount", intervalsCount);
    file>>nameValue("earthMoonRatio", earthMoonRatio);

    degree.resize(bodyCount);
    subintervals.resize(bodyCount);
    components.resize(bodyCount);

    for(UInt idBody=0; idBody<bodyCount; idBody++)
    {
      file>>beginGroup("body");
      file>>nameValue("subintervals", subintervals.at(idBody));
      file>>nameValue("components",   components.at(idBody));
      file>>nameValue("degree",       degree.at(idBody));
      file>>endGroup("body");
    }

    times.resize(intervalsCount+1);
    for(UInt i=0; i<times.size(); i++)
      file>>nameValue("time", times.at(i));

    coeff.resize(bodyCount);
    for(UInt idBody=0; idBody<bodyCount; idBody++)
      coeff.at(idBody).resize(subintervals.at(idBody), Matrix(components.at(idBody), degree.at(idBody)+1));

    // If we have a binary file, we can efficiently seek to the needed position in the file.
    if(file.canSeek())
      seekStart = file.position();
    seekSize = 0;
    idInterval = 0;
    readInterval(0);
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW_EXTRA("filename=<"+name.str()+">", e)
  }
}

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

void InFileEphemerides::close()
{
  file.close();
}

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

void InFileEphemerides::readInterval(UInt idInterval_)
{
  try
  {
    // must restart?
    if(idInterval > idInterval_)
    {
      if(file.canSeek() && seekSize)
        idInterval = 0;
      else
        open(file.fileName());
    }

    while(idInterval <= idInterval_)
    {
      // Seek to appropriate interval.
      if(file.canSeek() && seekSize)
      {
        file.seek(seekStart + static_cast<std::streamoff>(idInterval_ * seekSize));
        idInterval = idInterval_;
      }

      file>>beginGroup("interval");
      for(UInt idBody=0; idBody<subintervals.size(); idBody++)
      {
        file>>beginGroup("body");
        for(UInt idSub=0; idSub<subintervals.at(idBody); idSub++)
        {
          file>>beginGroup("subinterval");
          for(UInt n=0; n<=degree.at(idBody); n++)
            for(UInt i=0; i<components.at(idBody); i++)
              file>>nameValue("coefficient", coeff.at(idBody).at(idSub)(i, n));
          file>>endGroup("subinterval");
        }
        file>>endGroup("body");
      }
      file>>endGroup("interval");

      if(file.canSeek() && (idInterval == 0))
        seekSize = file.position() - seekStart;
      idInterval++;
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW_EXTRA("filename=<"+file.fileName().str()+">", e)
  }
}

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

Matrix InFileEphemerides::interpolate(const Time &time, Planet planet)
{
  try
  {
    if(file.fileName().empty())
      throw(Exception("no file open"));

    // read new data block?
    if((time < times.at(idInterval-1)) || (time >= times.at(idInterval)))
    {
      if((time < times.front()) || (time >= times.back()))
        throw(Exception(time.dateTimeStr()+" outside interval ["+times.front().dateTimeStr()+", "+times.back().dateTimeStr()+")"));
      readInterval(static_cast<UInt>(std::floor((time-times.front()).mjd()/(times.back()-times.front()).mjd()*(times.size()-1))));
    }

    UInt idBody = 0;
    switch(planet)
    {
      case MERCURY:             idBody =  0; break;
      case VENUS:               idBody =  1; break;
      case EARTH:               idBody =  9; break; // moon relative to earth
      case MARS:                idBody =  3; break;
      case JUPITER:             idBody =  4; break;
      case SATURN:              idBody =  5; break;
      case URANUS:              idBody =  6; break;
      case NEPTUNE:             idBody =  7; break;
      case PLUTO:               idBody =  8; break;
      case MOON:                idBody =  9; break;  // moon relative to earth
      case SUN:                 idBody = 10; break;
      case SOLARBARYCENTER:     return Matrix(3, 2);
      case EARTHMOONBARYCENTER: idBody =  2; break;
      case NUTATION:            idBody = 11; break;
      case LIBRATION:           idBody = 12; break;
      case MOONFROMEARTH:       idBody =  9; break;
    }

    if(idBody >= subintervals.size() || subintervals.at(idBody) == 0)
      throw(Exception(static_cast<UInt>(planet)%"No data for requested planet id %i"s));

    Double dt = (times.at(idInterval)-times.at(idInterval-1)).seconds()/subintervals.at(idBody);
    Double t  = (time-times.at(idInterval-1)).seconds()/dt;
    const UInt idSub = static_cast<UInt>(std::floor(t));
    t = 2.*(t-idSub)-1.; // [-1, 1) in subinterval idSub

    // Chebyshev polynomials and derivative in second column
    Matrix p(degree.at(idBody)+1, 2);
    p(0, 0) = 1.;
    p(1, 0) = t;
    p(1, 1) = 2./dt;
    for(UInt n=2; n<p.rows(); n++)
    {
      p(n, 0) = 2*t*p(n-1, 0) - p(n-2, 0);
      p(n, 1) = 2*t*p(n-1, 1) - p(n-2, 1) + 4./dt*p(n-1, 0);
    }

    Matrix posVel = coeff.at(idBody).at(idSub) * p;
    if(planet == MOON)
    {
      posVel *= earthMoonRatio/(1.+earthMoonRatio);     // relative to earth-moon bary center
      posVel += interpolate(time, EARTHMOONBARYCENTER); // add earth-moon bary center
    }
    if(planet == EARTH)
    {
      posVel *= -1./(1.+earthMoonRatio);                // relative to earth-moon bary center
      posVel += interpolate(time, EARTHMOONBARYCENTER); // add earth-moon bary center
    }
    return posVel;
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW_EXTRA("filename=<"+file.fileName().str()+">", e)
  }
}

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

void InFileEphemerides::ephemeris(const Time &time, Planet planet, Planet center, Vector3d &position, Vector3d &velocity)
{
  try
  {
    Matrix posVel;
    if(planet == MOON && center == EARTH)
      posVel = interpolate(time, MOONFROMEARTH);
    else
      posVel = interpolate(time, planet) - interpolate(time, center);
    position = Vector3d(posVel.column(0));
    velocity = Vector3d(posVel.column(1));
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW_EXTRA("filename=<"+file.fileName().str()+">", e)
  }
}


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

void writeFileEphemerides(const FileName &fileName, Double earthMoonRatio, const std::vector<Time> &times,
                          const std::vector<UInt> &subintervals, const std::vector<UInt> &components, const std::vector<UInt> &degree,
                          const std::vector<std::vector<std::vector<Matrix>>> &coefficients)
{
  try
  {
    OutFileArchive file(fileName, FILE_EPHEMERIDES_TYPE, FILE_EPHEMERIDES_VERSION);
    file<<nameValue("bodyCount",      components.size());
    file<<nameValue("intervalsCount", times.size()-1);
    file<<nameValue("earthMoonRatio", earthMoonRatio);
    file.comment("subintervals  components   degree");
    file.comment("=================================");
    for(UInt idBody=0; idBody<components.size(); idBody++)
    {
      file<<beginGroup("body");
      file<<nameValue("subintervals", subintervals.at(idBody));
      file<<nameValue("components",   components.at(idBody));
      file<<nameValue("degree",       degree.at(idBody));
      file<<endGroup("body");
    }
    file.comment("times");
    file.comment("=====");
    for(UInt idEpoch=0; idEpoch<times.size(); idEpoch++)
      file<<nameValue("time", times.at(idEpoch));

    file.comment("Chebyshev polynomial coefficients");
    file.comment("=================================");
    for(UInt idInterval=0; idInterval<coefficients.size(); idInterval++)
    {
      file<<beginGroup("interval");
      for(UInt idBody=0; idBody<subintervals.size(); idBody++)
      {
        file<<beginGroup("body");
        for(UInt idSub=0; idSub<subintervals.at(idBody); idSub++)
        {
          file<<beginGroup("subinterval");
          for(UInt n=0; n<=degree.at(idBody); n++)
            for(UInt i=0; i<components.at(idBody); i++)
              file<<nameValue("coefficient", coefficients.at(idInterval).at(idBody).at(idSub)(i, n));
          file<<endGroup("subinterval");
        }
        file<<endGroup("body");
      }
      file<<endGroup("interval");
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW_EXTRA("filename=<"+fileName.str()+">", e)
  }
}

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