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

Add environment variable to override time series directory #327

Merged
merged 2 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
41 changes: 32 additions & 9 deletions src/hdf5_time_series_storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Constructs Hdf5TimeSeriesStorage.
- `create_file::Bool`: create new file
- `filename=nothing`: if nothing, create a temp file, else use this name.
- `directory=nothing`: if set and filename is nothing, create a temp file in this
directory. Use tempdir() if not set. This should be set if the time series data is larger
directory. If it is not set, use the environment variable SIENNA_TIME_SERIES_DIRECTORY.
If that is not set, use tempdir(). This should be set if the time series data is larger
than the tmp filesystem can hold.
- `read_only = false`: If true, don't allow changes to the file. Allows simultaneous read
access.
Expand All @@ -48,9 +49,7 @@ function Hdf5TimeSeriesStorage(
)
if create_file
if isnothing(filename)
if isnothing(directory)
directory = tempdir()
end
directory = _get_time_series_parent_dir(directory)
filename, io = mktemp(directory)
close(io)
end
Expand Down Expand Up @@ -82,7 +81,7 @@ function from_file(
if read_only
file_path = abspath(filename)
else
parent = isnothing(directory) ? tempdir() : directory
parent = _get_time_series_parent_dir(directory)
file_path, io = mktemp(parent)
close(io)
copy_h5_file(filename, file_path)
Expand All @@ -101,6 +100,31 @@ function from_file(
return storage
end

function _get_time_series_parent_dir(directory = nothing)
# Ensure that a user-passed directory has highest precedence.
if !isnothing(directory)
if !isdir(directory)
error("User passed time series directory, $directory, does not exist.")
end
return directory
end

directory = get(ENV, "SIENNA_TIME_SERIES_DIRECTORY", nothing)
if !isnothing(directory)
if !isdir(directory)
error(
"The directory specified by the environment variable " *
"SIENNA_TIME_SERIES_DIRECTORY, $directory, does not exist.",
)
end
@debug "Use time series directory specified by the environment variable" _group =
LOG_GROUP_TIME_SERIES directory
return directory
end

return tempdir()
end

function Base.isempty(storage::Hdf5TimeSeriesStorage)
return HDF5.h5open(storage.file_path, "r+") do file
root = _get_root(storage, file)
Expand All @@ -115,12 +139,11 @@ undergoing a deepcopy.
# Arguments

- `storage::Hdf5TimeSeriesStorage`: storage instance
- `directory::String`: If nothing, use tempdir
- `directory::String`: If nothing, use the directory specified by the environment variable
SIENNA_TIME_SERIES_DIRECTORY or the system tempdir.
"""
function copy_to_new_file!(storage::Hdf5TimeSeriesStorage, directory = nothing)
if directory === nothing
directory = tempdir()
end
directory = _get_time_series_parent_dir(directory)

# If we ever choose to keep the HDF5 file open then this will break.
# Any open buffers will need to be flushed.
Expand Down
3 changes: 2 additions & 1 deletion src/system_data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ Construct SystemData to store components and time series data.
- `time_series_in_memory = false`: Controls whether time series data is stored in memory or
in a file.
- `time_series_directory = nothing`: Controls what directory time series data is stored in.
Default is tempdir().
Default is the environment variable SIENNA_TIME_SERIES_DIRECTORY or tempdir() if that
isn't set.
- `compression = CompressionSettings()`: Controls compression of time series data.
"""
function SystemData(;
Expand Down
15 changes: 15 additions & 0 deletions test/test_time_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2253,3 +2253,18 @@ end
@test counts.static_time_series_count == 1
@test counts.components_with_time_series == 2
end

@testset "Test custom time series directories" begin
@test IS._get_time_series_parent_dir(nothing) == tempdir()
@test IS._get_time_series_parent_dir(pwd()) == pwd()
@test_throws ErrorException IS._get_time_series_parent_dir("/some/invalid/directory/")

ENV["SIENNA_TIME_SERIES_DIRECTORY"] = pwd()
try
@test IS._get_time_series_parent_dir() == pwd()
ENV["SIENNA_TIME_SERIES_DIRECTORY"] = "/some/invalid/directory/"
@test_throws ErrorException IS._get_time_series_parent_dir()
finally
pop!(ENV, "SIENNA_TIME_SERIES_DIRECTORY")
end
end
Loading