-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhorizons.js
122 lines (103 loc) · 4.58 KB
/
horizons.js
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
const fs = require('fs');
const et = require('expect-telnet');
const host = 'horizons.jpl.nasa.gov';
const port = 6775;
const hostAndPort = `${host}:${port}`;
const bodiesData = { bodies: [] };
class JPL {
async getOrbitalElements(hId, hIdCenter) {
const colonPatt = /\:\s*$/;
return new Promise((resolve, reject) => {
et(hostAndPort, [
{ expect: /Horizons> $/, send: `${hId}\n` },
{ expect: colonPatt, send: 'E\n' }, // Select ... [E]phemeris, [F]tp, [M]ail, [R]edisplay, ?, <cr>:
{ expect: colonPatt, send: 'e\n' }, // Observe, Elements, Vectors [o,e,v,?] :
{ expect: colonPatt, send: `${hIdCenter}\n` }, // Coordinate system center [ ###, ? ] :
{ expect: colonPatt, send: 'eclip\n' }, // Reference plane [eclip, frame, body ] :
{ expect: colonPatt, send: '2018-Nov-14 00:00\n' }, // Starting TDB [>= 9999BC-Mar-20 00:00] :
{ expect: colonPatt, send: '2018-Nov-14 00:01\n' }, // Ending TDB [<= 9999-Dec-31 12:00] :
{ expect: colonPatt, send: '1d\n' }, // Output interval [ex: 10m, 1h, 1d, ? ] :
// { expect: colonPatt, send: '\n' }, // Accept default output [ cr=(y), n, ?] :
{ expect: colonPatt, send: 'n\n' }, // Accept default output [ cr=(y), n, ?] :
{ expect: colonPatt, send: 'J2000\n' }, // Output reference frame [J2000, B1950] :
{ expect: colonPatt, send: '1\n' }, // Output units [1=KM-S, 2=AU-D, 3=KM-D] :
{ expect: colonPatt, send: 'YES\n' }, // Spreadsheet CSV format [ YES, NO ] :
{ expect: colonPatt, send: 'YES\n' }, // Label osculating elements [ YES, NO ] :
{ expect: colonPatt, send: 'ABS\n' }, // Type of periapsis time [ ABS, REL ] :
{ expect: colonPatt, out: (output) => {
// console.log(`Fetched orbital elements for ${hId}`);
// console.log(output)
const params = this.parseElements(output);
resolve(params);
}, send: 'x\r' },
], (err) => {
if (err) {
console.error(err);
reject(err);
} else {
reject('Something went terribly awry...');
}
});
});
}
// $$SOE
// 2458436.500000000, A.D. 2018-Nov-14 00:00:00.0000 TDB, EC= 6.319796616101976E-02 ...
// $$EOE
//
// JDTDB Julian Day Number, Barycentric Dynamical Time
// EC Eccentricity, e
// QR Periapsis distance, q (km)
// IN Inclination w.r.t XY-plane, i (degrees)
// OM Longitude of Ascending Node, OMEGA, (degrees)
// W Argument of Perifocus, w (degrees)
// Tp Time of periapsis (Julian Day Number)
// N Mean motion, n (degrees/day)
// MA Mean anomaly, M (degrees)
// TA True anomaly, nu (degrees)
// A Semi-major axis, a (km)
// AD Apoapsis distance (km)
// PR Sidereal orbit period (day)
parseElements(data) {
const paramsPatt = /\$\$SOE\n([^\$]+)\n\$\$EOE/m;
const [,params] = paramsPatt.exec(data);
// get rid of JDTDB (Julian Day Number, Barycentric Dynamical Time)
const paramsList = params.split(/,/).slice(2).map(val => Number(val.trim()));
const [EC, QR, IN, OM, W, Tp, N, MA, TA, A, AD, PR] = paramsList;
return { EC, QR, IN, OM, W, Tp, N, MA, TA, A, AD, PR };
}
};
const jpl = new JPL();
const bodyList = [
{ name: 'Mercury', hId: 199, hIdCenter: 'Sun' },
{ name: 'Venus', hId: 299, hIdCenter: 'Sun' },
{ name: 'Earth', hId: 399, hIdCenter: 'Sun' },
{ name: 'Luna', hId: 301, hIdCenter: 399 },
{ name: 'Mars', hId: 499, hIdCenter: 'Sun' },
{ name: 'Phobos', hId: 401, hIdCenter: 499 },
{ name: 'Deimos', hId: 402, hIdCenter: 499 },
{ name: 'Jupiter', hId: 599, hIdCenter: 'Sun' },
{ name: 'Io', hId: 501, hIdCenter: 599 },
{ name: 'Europa', hId: 502, hIdCenter: 599 },
{ name: 'Ganymede', hId: 503, hIdCenter: 599 },
{ name: 'Callisto', hId: 504, hIdCenter: 599 },
{ name: 'Amalthea', hId: 505, hIdCenter: 599 },
{ name: 'Thebe', hId: 514, hIdCenter: 599 },
{ name: 'Adrastea', hId: 515, hIdCenter: 599 },
{ name: 'Metis', hId: 516, hIdCenter: 599 },
{ name: 'Saturn', hId: 699, hIdCenter: 'Sun' },
{ name: 'Uranus', hId: 799, hIdCenter: 'Sun' },
{ name: 'Neptune', hId: 899, hIdCenter: 'Sun' },
{ name: 'Pluto', hId: 999, hIdCenter: 'Sun' },
];
const promises = bodyList.map(async (spec, ndx) => {
spec.elements = await jpl.getOrbitalElements(spec.hId, spec.hIdCenter);
bodiesData.bodies.push(spec);
});
Promise.all(promises)
.then(() => {
const json = JSON.stringify(bodiesData);
fs.writeFile('elements.json', json, (err) => {
if (err) throw err;
console.log('Created elements.json');
});
});