Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swapped out ItemGraphs -> Graphs for compatibility updates #73

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "0.7.0"
[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
EarthOrientation = "732a3c5d-d6c0-58bc-adb1-1b51709a25e2"
ItemGraphs = "d5eda45b-7e79-5788-9687-2c6ab7b96158"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
LeapSeconds = "2f5f767c-a11e-5269-a972-637d4b97c32d"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
MuladdMacro = "46d2c3a1-f734-5fdb-9937-b9b9aeba4221"
Expand All @@ -14,7 +14,7 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
[compat]
ERFA = "0.5, 0.6, 1.0"
EarthOrientation = "0.7"
ItemGraphs = "0.4"
Graphs = "1.12.0"
LeapSeconds = "1.1"
MacroTools = "0.5"
Measurements = "2"
Expand Down
63 changes: 47 additions & 16 deletions src/TimeScales.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module TimeScales

using ItemGraphs: ItemGraph, add_edge!, add_vertex!, items

using Graphs
import Dates

export
Expand Down Expand Up @@ -77,24 +76,50 @@ for (acronym, name) in zip(ACRONYMS, NAMES)
end
end

const SCALES = ItemGraph{TimeScale}()
# Create a DiGraph to store the time scales
const SCALES = SimpleDiGraph{Int}()
const SCALE_VERTICES = Dict{TimeScale, Int}()
const VERTEX_SCALES = Dict{Int, TimeScale}()

function register_scale!(s)
add_vertex!(SCALES, s)
function register_scale!(s::TimeScale)
if !haskey(SCALE_VERTICES, s)
v = add_vertex!(SCALES)
SCALE_VERTICES[s] = v
VERTEX_SCALES[v] = s
end
end

function link_scales!(s1, s2; oneway=false)
add_edge!(SCALES, s1, s2)
oneway || add_edge!(SCALES, s2, s1)
function link_scales!(s1::TimeScale, s2::TimeScale; oneway=false)
# Ensure both scales are registered
register_scale!(s1)
register_scale!(s2)

# Add the edge
add_edge!(SCALES, SCALE_VERTICES[s1], SCALE_VERTICES[s2])
oneway || add_edge!(SCALES, SCALE_VERTICES[s2], SCALE_VERTICES[s1])
end

link_scales!(TAI, TT)
link_scales!(TAI, UT1)
link_scales!(TT, TCG)
link_scales!(TT, TDB)
link_scales!(TCB, TDB)

find_path(from, to) = items(SCALES, from, to)
function find_path(from::TimeScale, to::TimeScale)
# Get vertex indices
v_from = SCALE_VERTICES[from]
v_to = SCALE_VERTICES[to]

# Find shortest path
path = dijkstra_shortest_paths(SCALES, v_from)

# Extract path vertices
path_vertices = Int[]
current = v_to
while current != v_from
pushfirst!(path_vertices, current)
current = path.parents[current]
current == 0 && return TimeScale[] # No path exists
end
pushfirst!(path_vertices, v_from)

# Convert vertices back to TimeScales
return [VERTEX_SCALES[v] for v in path_vertices]
end

struct NotATimeScale <: TimeScale end

Expand All @@ -110,5 +135,11 @@ tryparse(::Any) = nothing
return val, i
end

end
# Initialize the graph with the time scale relationships
link_scales!(TAI, TT)
link_scales!(TAI, UT1)
link_scales!(TT, TCG)
link_scales!(TT, TDB)
link_scales!(TCB, TDB)

end
leerosenthalj marked this conversation as resolved.
Show resolved Hide resolved
Loading