Created
August 11, 2014 14:33
-
-
Save xmichaelx/a4935c7311b4e66c9187 to your computer and use it in GitHub Desktop.
Small snippet for converting strings in Two-Line Element into JSON format (with additional computed values such ass period, semi-major axis, JD and MJD).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var TLE = | |
| (function () { | |
| function isCorrupted(line) { | |
| var checksum = parseInt(line[line.length-1]); | |
| var computedChecksum = line.slice(0,line.length-1) | |
| .replace(/[\-\-]/g,"1") | |
| .replace(/[a-zA-Z\.+\s]/g,"") | |
| .split("") | |
| .map(function(digit) { return parseInt(digit) }) | |
| .reduce(function(prev, current) {return prev + current;}, 0) % 10; | |
| return checksum !== computedChecksum; | |
| } | |
| function computeJD(str) { | |
| var y = parseInt(str.slice(0,2)); | |
| var day = parseFloat(str.slice(2)); | |
| y += y < 57 ? 2000 : 1900; | |
| var days = Date.parse( y + "-01-01") / (24 * 60 * 60 * 1000); // days since 01-01-1970 | |
| return (day - 1) + days + 2440587.5; // + JD of 00:00:00 01-01-1970 | |
| } | |
| function calculatePeriod(str) { | |
| return 86400 / parseFloat(str); // secs in a day devided by revolutions per day | |
| } | |
| function identity(x) { | |
| return x; | |
| } | |
| function floatWithDecimalPointAssumed(x) { | |
| x = x.trim(); | |
| if (x.indexOf("-") > 0) { | |
| x = x.replace("-","e-"); | |
| } | |
| return parseFloat("." + x); | |
| } | |
| var converters = { | |
| firstLineCorrupted : { | |
| line : 1, col: 1, chars : 69 , f : isCorrupted | |
| }, | |
| secondLineCorrupted : { | |
| line : 2, col: 1, chars : 69, f : isCorrupted | |
| }, | |
| sateliteNumber : { | |
| line : 1, col: 3, chars : 5, f : parseInt | |
| }, | |
| classification : { | |
| line : 1, col: 8, chars : 1, f : identity | |
| }, | |
| internationalDesignator : { | |
| line : 1, col: 10, chars : 8, f : identity | |
| }, | |
| epoch : { | |
| line : 1, col: 21, chars : 14, f : parseFloat | |
| }, | |
| epochYear : { | |
| line : 1, col: 19, chars : 2, f : parseInt | |
| }, | |
| jd : { | |
| line : 1, col: 19, chars : 16, f : computeJD | |
| }, | |
| mjd : { | |
| line : 1, col: 19, chars : 16, f : function(x) { return computeJD(x) - 2400000.5; } // counting from 0h Nov 17, 1858 | |
| }, | |
| firstDerivateOfMeanMotion : { | |
| line : 1, col: 34, chars : 10, f : parseFloat | |
| }, | |
| secondDerivateOfMeanMotion : { | |
| line : 1, col: 45, chars : 8, f : floatWithDecimalPointAssumed | |
| }, | |
| BSTARdragTerm : { | |
| line : 1, col: 54, chars : 8, f : floatWithDecimalPointAssumed | |
| }, | |
| ephemerisType : { | |
| line : 1, col: 63, chars : 1, f : parseInt | |
| }, | |
| elementNumber : { | |
| line : 1, col: 65, chars : 4, f : parseInt | |
| }, | |
| eccentricity : { | |
| line : 2, col: 27, chars : 7, f : floatWithDecimalPointAssumed | |
| }, | |
| inclination : { | |
| line : 2, col: 9, chars : 8, f : parseFloat | |
| }, | |
| ascendingNodeRightAscension : { | |
| line : 2, col: 18, chars : 8, f : parseFloat | |
| }, | |
| argumetOfPeriapsis : { | |
| line : 2, col: 35, chars : 8, f : parseFloat | |
| }, | |
| meanAnomaly : { | |
| line : 2, col: 44, chars : 8 , f : parseFloat | |
| }, | |
| revolutionsPerDay : { | |
| line : 2, col: 53, chars : 11 , f : parseFloat | |
| }, | |
| period : { | |
| line : 2, col: 53, chars : 11, f : calculatePeriod // secs in a day devided by revolutions per day | |
| }, | |
| semiMajorAxis : { | |
| line : 2, col: 53, chars : 11 , // (GM * (T / (2 * PI)) ^ 2) ^ 1/3 | |
| f : function(x) { return Math.pow(398600.4418 * Math.pow( calculatePeriod(x) / (2 * Math.PI),2), 1/3); } | |
| } | |
| }; | |
| function parse(tleString) { | |
| var tle = { titleLine : "" }; | |
| var lines = tleString.split("\n"); | |
| if (lines.length == 3) { | |
| tle.titleLine = lines[0].trim(); | |
| lines.shift(); | |
| } | |
| for (var property in converters) { | |
| if (converters.hasOwnProperty(property)) { | |
| var c = converters[property]; | |
| var begin = c.col - 1, | |
| end = c.col + c.chars - 1; | |
| tle[property] = c.f(lines[c.line- 1].slice(begin, end)); | |
| } | |
| } | |
| return tle; | |
| } | |
| return {parse : parse}; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment