Files
OpenSpace/data/assets/util/tle_helper.asset
T
Gene Payne d9da0bef74 Remove debris assets that are no longer supported by celestrak (#2146)
* Remove debris assets that are no longer supported by celestrak
* Revert "Remove debris assets that are no longer supported by celestrak" (commit c8e5f3f)
* Updated all satellite assets to use the new celestrak URL format
2022-06-21 13:01:51 -06:00

125 lines
3.4 KiB
Lua

local transforms = asset.require("scene/solarsystem/planets/earth/transforms")
function downloadTLEFile(sceneAsset, url, name, filename)
local identifier = name
identifier = identifier:gsub(" ", "")
identifier = identifier:gsub("&", "")
identifier = identifier:gsub("-", "")
local urlSyncTable = {
Name = "Satellite TLE Data (" .. name .. ")",
Type = "UrlSynchronization",
Identifier = "satellite_tle_data_" .. identifier,
Url = url,
Override = true
}
if (filename ~= "") then
urlSyncTable.Filename = filename
end
return sceneAsset.syncedResource(urlSyncTable)
end
-- Check format of a set of 3 TLE file lines and return nonzero if there is a format error
function isValidTLEFileFormat(lineArr)
local function isEmpty(s) return s == nil or s == "" end
if isEmpty(lineArr[1]) or isEmpty(lineArr[2]) or isEmpty(lineArr[3]) then
return false
end
if string.sub(lineArr[2], 1, 2) ~= "1 " then
return false
end
if string.sub(lineArr[3], 1, 2) ~= "2 " then
return false
end
return true
end
local makeSingleLineElement = function(tle, filename)
local path = tle .. "/" .. filename
local file = io.open(path, "r")
assert(file, "File not found: " .. path)
local line = {
file:read("*l"), --title line
file:read("*l"),
file:read("*l")
}
assert(isValidTLEFileFormat(line), "TLE file syntax error on line " .. 1 .. ": " .. path)
-- Trim string
line[1] = line[1]:gsub("^%s*(.-)%s*$", "%1")
line[1] = line[1]:gsub("%s+", "_")
line[1] = line[1]:gsub("[%-()]", "")
--local title = line[1]
return line
end
function numLinesInFile(filename)
local ctr = 0
for _ in io.lines(filename) do ctr = ctr + 1 end
return ctr
end
local getPeriodFromElement = function(element)
-- Get period from correct location of the string
local per = tonumber(string.sub(element[3], 53, 63))
-- Trail for 2x a single revolution
per = 1.0 / per
return per
end
function satellites(title, file, color, group)
return {
Identifier = title,
Parent = transforms.EarthInertial.Identifier,
Renderable = {
Type = "RenderableSatellites",
Path = file,
SegmentQuality = 3,
Color = color,
Fade = 1.5,
RenderBinMode = "PostDeferredTransparent",
StartRenderIdx = group.StartRenderIdx,
RenderSize = group.RenderSize
},
Tag = { "earth_satellites" },
GUI = {
Name = group.Title,
Path = "/Solar System/Planets/Earth/Satellites",
Description = group.Description
}
}
end
local registerSatelliteGroupObjects = function(containingAsset, group, tleFolder, shouldAddDuplicates)
local filename = group.Filename
local filenameSansExt = filename:gsub(filename:match("(%.%w+)$"), "")
local path = tleFolder .. "/" .. filename
local SatelliteBatch = satellites(filenameSansExt, path, group.TrailColor, group)
containingAsset.onInitialize(function()
openspace.addSceneGraphNode(SatelliteBatch)
end)
containingAsset.onDeinitialize(function()
openspace.removeSceneGraphNode(SatelliteBatch)
end)
containingAsset.export(SatelliteBatch)
end
asset.export("downloadTLEFile", downloadTLEFile)
asset.export("isValidTLEFileFormat", isValidTLEFileFormat)
asset.export("numLinesInFile", numLinesInFile)
asset.export("makeSingleLineElement", makeSingleLineElement)
asset.export("getPeriodFromElement", getPeriodFromElement)
asset.export("registerSatelliteGroupObjects", registerSatelliteGroupObjects)