Skip to content

Commit

Permalink
✨ Add possibility to load local files
Browse files Browse the repository at this point in the history
The function `init`, when used with a specific space index set, can
receive now the keyword `filepaths`. If this keyword is a
`Vector{String}`, the algorithm will use those filepaths to load the
space index instead downloading them from the places in the function
`urls`.
  • Loading branch information
ronisbr committed May 15, 2024
1 parent 7520df0 commit 8f2bf1a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 10 deletions.
21 changes: 17 additions & 4 deletions docs/src/man/initialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,29 @@ If the user wants to initialize only one space index set, they can pass it to th
function:

```julia
function SpaceIndices.init(::Type{T}; force_download::Bool = true) where T<:SpaceIndexSet -> Nothing
function SpaceIndices.init(::Type{T}; kwargs...) where T<:SpaceIndexSet -> Nothing
```

where `T` must be the space index set. In this case, the user have access to the keyword
`force_download`. If it is `true`, the remote files will be download regardless their
timestamp.
where `T` must be the space index set. In this case, the user have access to the following
keywords:

- `force_download::Bool`: If `true`, the remote files will be downloaded regardless of their
timestamps.
(**Default** = `false`)
- `filepaths::Union{Nothing, Vector{String}}`: If it is `nothing`, the function will
download the space index files from the locations specified in the [`urls`](@ref)
API function. However, the user can pass a vector with the file locations, which will be
used instead of downloading the data. In this case, the user must provide all the files
in the space index set `T`.
(**Default** = `nothing`)

```julia-repl
julia> SpaceIndices.init()
[ Info: Downloading the file 'DTCFILE.TXT' from 'http://sol.spacenvironment.net/jb2008/indices/DTCFILE.TXT'...
[ Info: Downloading the file 'SOLFSMY.TXT' from 'http://sol.spacenvironment.net/jb2008/indices/SOLFSMY.TXT'...
[ Info: Downloading the file 'SW-All.csv' from 'https://celestrak.org/SpaceData/SW-All.csv'...
```

```julia-repl
julia> SpaceIndices.init(SpaceIndices.Celestrak; filepaths = ["./SW-All.csv"])
```
35 changes: 30 additions & 5 deletions src/initialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,43 @@ and populate the object to be accessed by the function [`space_index`](@ref).
# Keywords
- `force_download::Bool`: If `true`, the remote files will be downloaded regardless of their
timestamps. (**Default** = `false`)
timestamps.
(**Default** = `false`)
- `filepaths::Union{Nothing, Vector{String}}`: If it is `nothing`, the function will
download the space index files from the locations specified in the [`urls`](@ref)
API function. However, the user can pass a vector with the file locations, which will be
used instead of downloading the data. In this case, the user must provide all the files
in the space index set `T`.
(**Default** = `nothing`)
"""
function init(::Type{T}; force_download::Bool = false) where T<:SpaceIndexSet
function init(
::Type{T};
filepaths::Union{Nothing, Vector{String}} = nothing,
force_download::Bool = false
) where T<:SpaceIndexSet
id = findfirst(x -> first(x) === T, _SPACE_INDEX_SETS)
isnothing(id) && throw(ArgumentError("The space index set $T is not registered!"))

# Get the space index file handler.
handler = _SPACE_INDEX_SETS[id] |> last

# Fetch the files, if necessary, and parse it.
filepaths = _fetch_files(T; force_download=force_download)
obj = parse_files(T, filepaths)
if isnothing(filepaths)
fp = _fetch_files(T; force_download=force_download)
else
# We must check if the number of files in `filepaths` is the same as in the `url`
# function.
lu = length(urls(T))
lf = length(filepaths)

(lu != lf) && error(
"We expected $lu files for the space index set $T in the keyword `filepath`. However, $lf files were provided."
)

fp = filepaths
end

obj = parse_files(T, fp)
push!(handler, obj)

return nothing
Expand All @@ -42,7 +67,7 @@ end
Initialize all the registered space index sets.
This function will download the remote files associated to the space index sets if they do
not exist or if the expiry period has been elapsed. Aftward, it will parse the files and
not exist or if the expiry period has been elapsed. Afterward, it will parse the files and
populate the objects to be accessed by the function [`space_index`](@ref).
If the user does not want to initialize some sets, they can pass them in the keyword
Expand Down
28 changes: 28 additions & 0 deletions test/SW-All.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
DATE,BSRN,ND,KP1,KP2,KP3,KP4,KP5,KP6,KP7,KP8,KP_SUM,AP1,AP2,AP3,AP4,AP5,AP6,AP7,AP8,AP_AVG,CP,C9,ISN,F10.7_OBS,F10.7_ADJ,F10.7_DATA_TYPE,F10.7_OBS_CENTER81,F10.7_OBS_LAST81,F10.7_ADJ_CENTER81,F10.7_ADJ_LAST81
2024-06-01,2602,15,27,27,27,27,27,27,27,27,216,12,12,12,12,12,12,12,12,12,0.7,3,128,209.1,215.0,PRD,177.5,171.5,182.1,173.3
2024-06-02,2602,16,27,27,27,27,27,27,27,27,216,12,12,12,12,12,12,12,12,12,0.7,3,128,213.9,220.0,PRD,176.6,172.5,181.2,174.4
2024-06-03,2602,17,22,22,22,22,22,22,22,22,176,8,8,8,8,8,8,8,8,8,0.4,2,128,218.7,225.0,PRD,175.7,173.7,180.4,175.7
2024-06-04,2602,18,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,213.8,220.0,PRD,175.1,174.7,179.8,176.8
2024-06-05,2602,19,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,213.7,220.0,PRD,174.9,175.6,179.7,177.8
2024-06-06,2602,20,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,218.5,225.0,PRD,174.9,176.4,179.7,178.7
2024-06-07,2602,21,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,218.4,225.0,PRD,174.9,176.9,179.7,179.3
2024-06-08,2602,22,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,218.4,225.0,PRD,175.0,177.5,179.9,180.0
2024-06-09,2602,23,24,24,24,24,24,24,24,24,192,10,10,10,10,10,10,10,10,10,0.6,3,128,213.5,220.0,PRD,175.2,178.0,180.0,180.6
2024-06-10,2602,24,27,27,27,27,27,27,27,27,216,12,12,12,12,12,12,12,12,12,0.7,3,128,208.6,215.0,PRD,175.4,178.1,180.3,180.8
2024-06-11,2602,25,22,22,22,22,22,22,22,22,176,8,8,8,8,8,8,8,8,8,0.4,2,128,194.0,200.0,PRD,175.6,178.1,180.6,180.9
2024-06-12,2602,26,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,184.3,190.0,PRD,175.7,177.8,180.7,180.6
2024-06-13,2602,27,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,174.5,180.0,PRD,175.6,177.5,180.7,180.5
2024-06-14,2603,1,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,164.8,170.0,PRD,175.4,177.2,180.5,180.2
2024-06-15,2603,2,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,164.8,170.0,PRD,175.1,177.0,180.2,180.1
2024-06-16,2603,3,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,155.0,160.0,PRD,174.8,176.8,179.9,180.0
2024-06-17,2603,4,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,159.9,165.0,PRD,174.2,176.6,179.3,179.9
2024-06-18,2603,5,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,159.8,165.0,PRD,173.2,176.5,178.4,179.8
2024-06-19,2603,6,22,22,22,22,22,22,22,22,176,8,8,8,8,8,8,8,8,8,0.4,2,128,155.0,160.0,PRD,172.2,176.7,177.3,180.1
2024-06-20,2603,7,27,27,27,27,27,27,27,27,216,12,12,12,12,12,12,12,12,12,0.7,3,128,154.9,160.0,PRD,171.3,177.0,176.4,180.4
2024-06-21,2603,8,22,22,22,22,22,22,22,22,176,8,8,8,8,8,8,8,8,8,0.4,2,128,154.9,160.0,PRD,170.5,177.4,175.7,180.9
2024-06-22,2603,9,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,159.7,165.0,PRD,169.7,178.0,174.8,181.6
2024-06-23,2603,10,27,27,27,27,27,27,27,27,216,12,12,12,12,12,12,12,12,12,0.7,3,128,169.4,175.0,PRD,168.9,178.7,174.0,182.3
2024-06-24,2603,11,22,22,22,22,22,22,22,22,176,8,8,8,8,8,8,8,8,8,0.4,2,128,188.7,195.0,PRD,168.1,179.6,173.2,183.3
2024-06-25,2603,12,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,188.7,195.0,PRD,167.6,180.5,172.7,184.3
2024-06-26,2603,13,13,13,13,13,13,13,13,13,104,5,5,5,5,5,5,5,5,5,0.2,1,128,198.4,205.0,PRD,167.1,181.4,172.2,185.3
2024-06-27,2603,14,22,22,22,22,22,22,22,22,176,8,8,8,8,8,8,8,8,8,0.4,2,128,198.4,205.0,PRD,166.8,182.3,171.9,186.3
30 changes: 29 additions & 1 deletion test/initialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #


@testset "Functions init_space_index_sets and init_space_index_set" begin
# Initialize all space indices.
SpaceIndices.init()
Expand Down Expand Up @@ -39,6 +38,28 @@

end

@testset "Initialize Indices Using Local Files" begin
SpaceIndices.init(SpaceIndices.Celestrak; filepaths = ["./SW-All.csv"])
@test SpaceIndices._OPDATA_CELESTRAK.data isa SpaceIndices.Celestrak

# Make sure we loaded the local file by testing a data outside the available interval.
@test_throws Exception space_index(Val(:Kp), DateTime("2024-05-31T00:00:00"))

# Test some data from the local file.
kp = space_index(Val(:Kp), DateTime("2024-06-19T10:00:00"))
@test kp[1] == 2.333
@test kp[2] == 2.333
@test kp[3] == 2.333
@test kp[4] == 2.333
@test kp[5] == 2.333
@test kp[6] == 2.333
@test kp[7] == 2.333
@test kp[2] == 2.333

f10obs = space_index(Val(:F10obs), DateTime("2024-06-19T10:00:00"))
@test f10obs == 155.0
end

@testset "Errors Related To Unitialized Space Indices" begin
SpaceIndices.destroy()
dt = DateTime(1900, 1, 1)
Expand Down Expand Up @@ -67,3 +88,10 @@ end
@test_throws Exception space_index(Val(:Y10), dt)
@test_throws Exception space_index(Val(:Y81a), dt)
end

@testset "Errors Related To Space Index Set Initialization" begin
@test_throws Exception SpaceIndices.init(
SpaceIndices.Celestrak;
filepaths = ["./SW-All.csv", "./SW-All.csv"]
)
end

0 comments on commit 8f2bf1a

Please sign in to comment.