1 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import fs from 'fs';
|
13 | import path from 'path';
|
14 |
|
15 | var REGVSOP = /^\sVSOP87.*VARIABLE (\d) \((LBR|XYZ)\).{1,20}\*T\*\*(\d)\s{1,20}(\d{1,20}) TERMS/;
|
16 |
|
17 |
|
18 | var planets = ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune'];
|
19 |
|
20 |
|
21 | var exts = ['mer', 'ven', 'ear', 'mar', 'jup', 'sat', 'ura', 'nep'];
|
22 |
|
23 | var toFloat = function toFloat(f) {
|
24 | return parseFloat(f, 10);
|
25 | };
|
26 |
|
27 | export var VSOP = function () {
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function VSOP(planet, dirname, opts) {
|
38 | _classCallCheck(this, VSOP);
|
39 |
|
40 | planet = planet.toLowerCase();
|
41 | if (~planets.indexOf(planet)) {
|
42 | opts = opts || {};
|
43 | this.planet = planet;
|
44 | this.dirname = dirname;
|
45 | this.type = opts.type || 'B';
|
46 | } else {
|
47 | throw new Error('Invalid planet ' + planet);
|
48 | }
|
49 | }
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | VSOP.prototype._getExt = function _getExt() {
|
55 | return exts[planets.indexOf(this.planet)];
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | VSOP.prototype.load = function load(cb) {
|
62 | var _this = this;
|
63 |
|
64 | var ext = this._getExt();
|
65 | var filename = path.resolve(this.dirname, 'VSOP87' + this.type + '.' + ext);
|
66 | fs.readFile(filename, 'utf8', function (err, data) {
|
67 | if (!err) {
|
68 | _this.parse(data);
|
69 | }
|
70 | cb(err);
|
71 | });
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | VSOP.prototype.loadSync = function loadSync() {
|
78 | var ext = this._getExt();
|
79 | var filename = path.resolve(this.dirname, 'VSOP87' + this.type + '.' + ext);
|
80 | var data = fs.readFileSync(filename, 'utf8');
|
81 | this.parse(data);
|
82 | };
|
83 |
|
84 | |
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | VSOP.prototype.parse = function parse(data) {
|
91 | var _this2 = this;
|
92 |
|
93 | this.data = {};
|
94 | var lines = data.split(/\n/);
|
95 | var varName = void 0;
|
96 | var ref = void 0;
|
97 |
|
98 | lines.forEach(function (line) {
|
99 | if (REGVSOP.test(line)) {
|
100 | var _line$match = line.match(REGVSOP),
|
101 | varCnt = _line$match[1],
|
102 | type = _line$match[2],
|
103 | pos = _line$match[3];
|
104 |
|
105 | varName = type.split('')[varCnt - 1];
|
106 | if (!_this2.data[varName]) _this2.data[varName] = {};
|
107 | ref = _this2.data[varName][pos] = [];
|
108 | } else {
|
109 | if (line.length > 79) {
|
110 | ref.push([toFloat(line.substr(79, 97).trim()), toFloat(line.substr(98, 111).trim()), toFloat(line.substr(111, 131).trim())]);
|
111 | }
|
112 | }
|
113 | });
|
114 | };
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | VSOP.prototype.getData = function getData() {
|
128 | return this.data;
|
129 | };
|
130 |
|
131 | return VSOP;
|
132 | }();
|
133 |
|
134 | export default {
|
135 | VSOP: VSOP
|
136 | }; |
\ | No newline at end of file |