Repository navigation
Expand file tree
/
Copy pathasc2eph.cpp
More file actions
370 lines (345 loc) · 9.19 KB
/
Copy pathasc2eph.cpp
File metadata and controls
370 lines (345 loc) · 9.19 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "asc2eph_struct.cpp"
#define LOG_INFO
int main(int argc, char **argv)
{
// parse options
io_info o;
// o.parse(argc, argv);
if (!o.valid)
{
o.err << "Usage :: " << argv[0] << " [-i [input]] [-o [output]] [-l [log]] [-e [error]]" << std::endl;
return 1;
}
// temporary variables
std::string _;
// constant
const int oldmax = 400;
const int nmax = 1000;
// main function
int group;
int nrecl = 4;
double
db2z = -99999999,
t1 = -99999999,
t2 = 99999999;
if (nrecl != 4 && nrecl != 1)
{
o.err << "Please set nrecl first!\nDefault : 4";
return 2;
}
// readin KSIZE
int ksize;
o.in >> _ >> ksize;
#ifdef LOG_INFO
o.log << "ksize = " << ksize << std::endl;
#endif
int irecsz = nrecl * ksize;
// readin NCOEFF
int ncoeff;
o.in >> _ >> ncoeff;
_.clear();
// next group (1010)
while ( _ != "GROUP" ) o.in >> _;
o.in >> group;
if (group != 1010)
{
o.err << "ERROR header position 1010";
return 1010;
}
// TTL
// Different from origin fortran
std::string ttl;
ttl += "KSIZE=";
int cnt_ksize = 0;
for ( int kk = ksize ; kk ; kk /= 10 ) ++cnt_ksize;
for ( int cc = 6-cnt_ksize ; cc ; --cc ) ttl += ' ';
ttl += std::to_string(ksize);
ttl += ' ';
_.clear();
while (true)
{
o.in >> _;
if ( _ == "GROUP" ) break;
ttl += _ + ' ';
}
while ( static_cast<int>(ttl.size()) != 84*3) ttl += ' ';
#ifdef LOG_INFO
o.log << ttl << std::endl;
#endif
// next group (1030)
while ( _ != "GROUP" ) o.in >> _;
o.in >> group;
if (group != 1030)
{
o.err << "ERROR header position 1030";
return 1030;
}
// SS
double ss[3];
for ( int _i = 0 ; _i < 3 ; ++_i ) o.in >> ss[_i];
_.clear();
// next group (1040)
while ( _ != "GROUP" ) o.in >> _ ;
o.in >> group;
if (group != 1040)
{
o.err << "ERROR header position 1040";
return 1040;
}
// CNAM
int N;
o.in >> N;
std::vector<std::string> cNam;
for ( int _i = 0 ; _i < N ; ++_i )
{
o.in >> _;
cNam.push_back(_);
}
int NCon = N;
_.clear();
// next group (1041)
while ( _ != "GROUP" ) o.in >> _ ;
o.in >> group;
if (group != 1041)
{
o.err << "ERROR header position 1041";
return 1041;
}
// CVAL
o.in >> N;
std::vector<double> cVal(N);
for ( int _i = 0 ; _i < N ; ++_i ) o.in >> cVal[_i];
double AU, EMRAT, NUMDE;
for ( int _i = 0 ; _i < N ; ++_i )
{
if (cNam[_i] == "AU") AU = cVal[_i];
if (cNam[_i] == "EMRAT") EMRAT = cVal[_i];
if (cNam[_i] == "DENUM" ) NUMDE = cVal[_i];
}
o.log << AU << ' ' << EMRAT << ' ' << NUMDE << std::endl;
#ifdef LOG_INFO
for ( int _i = 0 ; _i < N ; ++_i ) o.log << cNam[_i] << "\t=\t" << cVal[_i] << std::endl;
#endif
_.clear();
// next group (1050)
while ( _ != "GROUP" ) o.in >> _ ;
o.in >> group;
if (group != 1050)
{
o.err << "ERROR header position 1050";
return 1050;
}
// IPT LPT RPT TPT
std::vector<int> vec1;
while (true)
{
o.in >> _;
if ( _ == "GROUP" ) break;
vec1.push_back(std::stoi(_));
}
std::vector<int>::iterator p1 = vec1.begin();
int sz = vec1.size();
int ipt[3][12], lpt[3], rpt[3], tpt[3];
for ( int _i = 0 ; _i < 3 ; ++_i )
{
for ( int _j = 0 ; _j < 12 ; ++_j ) ipt[_i][_j] = *p1++;
lpt[_i] = *p1++;
if (sz == 3*15)
{
rpt[_i] = *p1++;
tpt[_i] = *p1++;
}
else if (sz == 3*13)
{
rpt[_i] = tpt[_i] = 0;
}
else
{
o.err << "UNKNOWN GROUP 1050 size " << sz << std::endl;
return 10501;
}
}
// next group (1070) and last group
while ( _ != "GROUP" ) o.in >> _ ;
o.in >> group;
if (group != 1070)
{
o.err << "ERROR header position 1070";
return 1070;
}
// header_pre
o.new_block(irecsz);
o.new_block(irecsz);
// ephemeris
// pass ending 0.0000000000000000D+00
int nrw;
while (o.in)
{
bool flag = true;
o.in >> _;
for (char x : _)
{
if (not ('0' <= x && x <= '9')) break;
flag = false;
}
if (!flag) break;
}
nrw = std::stoi(_);
o.in >> ncoeff;
std::vector<double> db;
for ( int _i = 0 ; _i < ncoeff ; _i += 3 )
{
int kp2 = std::min(_i+3, ncoeff);
for ( int _j = _i ; _j < kp2 ; ++_j )
{
while (static_cast<int>(db.size()) <= _j) db.push_back(0);
o.in >> db[_j];
if (!o.in)
{
o.err << "ERROR Reading 1st coeffs" << std::endl;
return 10701;
}
}
}
bool first = true;
int nrout = 0;
while (o.in && db[1] < t2)
{
if (2 * ncoeff != ksize)
{
o.err << "2*ncoeff != ksize" << std::endl;
return 10702;
}
if (db[1] >= t1 && db[0] >= db2z)
{
if (first)
{
db2z = db[0];
first = false;
}
if (db[0] != db2z)
{
o.err << nrw << "nd do not overlap or abut" << std::endl;
return 10703;
}
db2z = db[1];
++nrout;
for ( int _i = 0 ; _i < ncoeff ; ++_i ) o << db[_i];
o.add_out(irecsz);
if (!o.out)
{
o.err << nrout << "th record ERROR" << std::endl;
return 10704;
}
if (nrout == 1)
{
ss[0] = db[0];
ss[2] = db[1] - db[0];
}
ss[1] = db[1];
#ifdef LOG_INFO
if (nrout % 100 == 1)
{
if (db[0] >= t1)
o.log << nrout << " EPHEMERIS RECORDS WRITTEN. LAST JED = " << db[1] << std::endl;
else
o.log << " Searching for first requested record... " << std::endl;
}
#endif
}
// pass ending 0.0000000000000000D+00
while (o.in)
{
bool flag = true;
o.in >> _;
for (char x : _)
{
if (not ('0' <= x && x <= '9'))
{
flag = false;
break;
}
}
if (flag) break;
}
nrw = std::stoi(_);
o.in >> ncoeff;
if (o.in) for ( int _i = 0 ; _i < ncoeff ; _i += 3 )
{
int kp2 = std::min(_i+3, ncoeff);
for ( int _j = _i ; _j < kp2 ; ++_j )
{
while (static_cast<int>(db.size()) <= _j) db.push_back(0);
o.in >> db[_j];
}
if (!o.in)
{
o.err << "ERROR Reading nth coeffs" << std::endl;
return 10701;
}
}
}
#ifdef LOG_INFO
o.log << nrout << " EPHEMERIS RECORDS WRITTEN. LAST JED = " << db[1];
#endif
// header
const std::string ZERO = {'\0', '\0', '\0', '\0', '\0', '\0'};
for ( int _i = cNam.size() ; _i < oldmax ; ++_i )
{
cNam.push_back(ZERO);
cVal.push_back(0);
}
o.seekp(0);
if (NCon <= oldmax)
{
o << ttl;
for ( int _i = 0 ; _i < oldmax ; ++_i ) o << cNam[_i];
for ( int _i = 0 ; _i < 3 ; ++_i ) o << ss[_i];
o << NCon << AU << EMRAT;
for ( int _j = 0 ; _j < 12 ; ++_j ) for ( int _i = 0 ; _i < 3 ; ++_i ) o << ipt[_i][_j];
o << static_cast<int>(NUMDE);
for ( int _i = 0 ; _i < 3 ; ++_i ) o << lpt[_i];
if ( sz == 3*15 )
{
for ( int _i = 0 ; _i < 3 ; ++_i ) o << rpt[_i];
for ( int _i = 0 ; _i < 3 ; ++_i ) o << tpt[_i];
}
}
else
{
o << ttl;
for ( int _i = 0 ; _i < oldmax ; ++_i ) o << cNam[_i];
for ( int _i = 0 ; _i < 3 ; ++_i ) o << ss[_i];
o << NCon << AU << EMRAT;
for ( int _j = 0 ; _j < 12 ; ++_j ) for ( int _i = 0 ; _i < 3 ; ++_i ) o << ipt[_i][_j];
o << static_cast<int>(NUMDE);
for ( int _i = 0 ; _i < 3 ; ++_i ) o << lpt[_i];
for ( int _i = oldmax ; _i < NCon ; ++_i ) o << cNam[_i];
if ( sz == 3*15 )
{
for ( int _i = 0 ; _i < 3 ; ++_i ) o << rpt[_i];
for ( int _i = 0 ; _i < 3 ; ++_i ) o << tpt[_i];
}
}
o.nxt_block(irecsz);
if (!o.out)
{
o.err << "1st record not written because of error" << std::endl;
return 10709;
}
if (NCon <= oldmax) for ( int _i = 0 ; _i < oldmax ; ++_i ) o << cVal[_i];
else for ( int _i = 0 ; _i < NCon ; ++_i ) o << cVal[_i];
o.add_out(irecsz);
if (!o.out)
{
o.err << "2nd record not written because of error" << std::endl;
return 10709;
}
o.close();
return 0;
}