UNPKG

3.59 kBJavaScriptView Raw
1function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
2
3/**
4 * @copyright 2016 commenthol
5 * @license MIT
6 * @module vsop87
7 */
8/**
9 * Converts VSOP87 data files to javascript modules
10 */
11
12import fs from 'fs';
13import path from 'path';
14
15var REGVSOP = /^\sVSOP87.*VARIABLE (\d) \((LBR|XYZ)\).{1,20}\*T\*\*(\d)\s{1,20}(\d{1,20}) TERMS/;
16
17// planet names in VSOP87 files
18var planets = ['mercury', 'venus', 'earth', 'mars', 'jupiter', 'saturn', 'uranus', 'neptune'];
19
20// VSOP planet extension names
21var exts = ['mer', 'ven', 'ear', 'mar', 'jup', 'sat', 'ura', 'nep'];
22
23var toFloat = function toFloat(f) {
24 return parseFloat(f, 10);
25};
26
27export var VSOP = function () {
28 /**
29 * load VSOP87 planet data from VSOP87 files
30 * Data can be obtained from ftp://cdsarc.u-strasbg.fr/pub/cats/VI%2F81/
31 * @throws {Error}
32 * @param {String} planet - MERCURY VENUS EARTH MARS JUPITER SATURN URANUS NEPTUNE
33 * @param {String} dirname - folder containing VSOP87 files
34 * @param {Object} [opts]
35 * @param {String} [opts.type] - file type A, B, C, D - See vsop87.txt
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'; // HELIOCENTRIC DYNAMICAL ECLIPTIC AND EQUINOX J2000
46 } else {
47 throw new Error('Invalid planet ' + planet);
48 }
49 }
50
51 /** get file extension for planet */
52
53
54 VSOP.prototype._getExt = function _getExt() {
55 return exts[planets.indexOf(this.planet)];
56 };
57
58 /** load data from file */
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 /** sync loading */
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 * parse data
86 * @param {String} data - content of VSOP file
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 * get parsed data
118 * @return {Object}
119 * ```js
120 * { L: { '0': [[<A>, <B>, <C>], ...], '1': [], '2': [], '3': [], '4': [], '5': [] },
121 * B: { '0': [], '1': [], '2': [], '3': [], '4': [], '5': [] },
122 * R: { '0': [], '1': [], '2': [], '3': [], '4': [], '5': [] } }
123 * ```
124 */
125
126
127 VSOP.prototype.getData = function getData() {
128 return this.data;
129 };
130
131 return VSOP;
132}();
133
134export default {
135 VSOP: VSOP
136};
\No newline at end of file