Files
OpenSpace/data/assets/util/tle_helper.asset
2021-04-28 19:19:51 +02:00

115 lines
3.2 KiB
Plaintext

local transforms = asset.require('scene/solarsystem/planets/earth/transforms')
local assetHelper = asset.require('util/asset_helper')
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 = {
Path = "/Solar System/Planets/Earth/Satellites"
}
}
end
local registerSatelliteGroupObjects = function(containingAsset, group, tleFolder, shouldAddDuplicates)
local filename = group.Url:match("([^/]+)$")
local filenameSansExt = filename:gsub(filename:match("(%.%w+)$"), "")
local path = tleFolder .. "/" .. filename
local SatelliteBatch = satellites(filenameSansExt, path, group.TrailColor, group)
assetHelper.registerSceneGraphNodesAndExport(containingAsset, { 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)