forked from dsuarezv/satellite-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tle.js
82 lines (57 loc) · 1.94 KB
/
tle.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
import * as satellite from 'satellite.js/lib/index';
export const EarthRadius = 6371;
const rad2Deg = 180 / 3.141592654;
export const parseTleFile = (fileContent, stationOptions) => {
const result = [];
const lines = fileContent.split("\n");
let current = null;
for (let i = 0; i < lines.length; ++i) {
const line = lines[i].trim();
if (line.length === 0) continue;
if (line[0] === '1') {
current.tle1 = line;
}
else if (line[0] === '2') {
current.tle2 = line;
}
else {
current = {
name: line,
...stationOptions
};
result.push(current);
}
}
return result;
}
// __ Satellite locations _________________________________________________
const latLon2Xyz = (radius, lat, lon) => {
var phi = (90-lat)*(Math.PI/180)
var theta = (lon+180)*(Math.PI/180)
const x = -((radius) * Math.sin(phi) * Math.cos(theta))
const z = ((radius) * Math.sin(phi) * Math.sin(theta))
const y = ((radius) * Math.cos(phi))
return { x, y, z };
}
const toThree = (v) => {
return { x: v.x, y: v.z, z: -v.y };
}
const getSolution = (station, date) => {
if (!station.satrec) {
const { tle1, tle2 } = station;
if (!tle1 || !tle2) return null;
station.satrec = satellite.twoline2satrec(tle1, tle2);;
}
return satellite.propagate(station.satrec, date);
}
// type: 1 ECEF coordinates 2: ECI coordinates
export const getPositionFromTle = (station, date, type = 2) => {
if (!station || !date) return null;
const positionVelocity = getSolution(station, date);
const positionEci = positionVelocity.position;
if (type === 2) return toThree(positionEci);
const gmst = satellite.gstime(date);
if (!positionEci) return null; // Ignore
const positionEcf = satellite.eciToEcf(positionEci, gmst);
return toThree(positionEcf);
}