mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-04-25 13:38:35 -05:00
Replaced duplicated code with tle_helper
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
local transforms = asset.require('scene/solarsystem/planets/earth/transforms')
|
||||
local assetHelper = asset.require('util/asset_helper')
|
||||
|
||||
local satImageFolder = asset.syncedResource({
|
||||
Name = "Satellite Image Files",
|
||||
Type = "HttpSynchronization",
|
||||
Identifier = "tle_satellites_images",
|
||||
Version = 1
|
||||
})
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
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 * 2.0
|
||||
|
||||
return per
|
||||
end
|
||||
|
||||
local registerSatelliteGroupObjects = function(containingAsset, group, tleFolder, shouldAddDuplicates)
|
||||
local filename = group.Url:match("([^/]+)$")
|
||||
local filenameSansExt = filename:gsub(filename:match("(%.%w+)$"), "")
|
||||
|
||||
local path = tleFolder .. "/" .. filename
|
||||
|
||||
function satellites(title, file, color)
|
||||
return {
|
||||
Identifier = title,
|
||||
Parent = transforms.EarthInertial.Identifier,
|
||||
Renderable = {
|
||||
Type = "RenderableSatellites",
|
||||
Path = file,
|
||||
Segments = 120,
|
||||
Color = color,
|
||||
Fade = 0.5
|
||||
},
|
||||
--Tag = { "earth_satellite_" .. group, "earth_satellite_" .. group .. "_trail"},
|
||||
GUI = {
|
||||
Path = "/Solar System/Planets/Earth/Satellites"
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
local SatelliteBatch = satellites(filenameSansExt, path, group.TrailColor)
|
||||
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)
|
||||
Reference in New Issue
Block a user