Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermebodin committed Sep 13, 2024
1 parent ca2e862 commit 3e20bfd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
12 changes: 6 additions & 6 deletions src/reader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,18 @@ end
"""
file_to_array(
filename::String,
implementation::DataType;
implementation::Type{I};
labels_to_read::Vector{String} = String[],
)
) where {I <: Implementation}
Reads a file and returns the data and metadata as a tuple.
"""
function file_to_array(
filename::String,
implementation::DataType;
implementation::Type{I};
labels_to_read::Vector{String} = String[],
)
reader = Reader{implementation}(
) where {I <: Implementation}
reader = Reader{I}(
filename;
labels_to_read,
carrousel = false, # carrousel does not make sense in this implemetations
Expand All @@ -124,7 +124,7 @@ function file_to_array(
data = zeros(
Float32,
length(reader.labels_to_read),
dimensions_sizes...,
dimension_sizes...,
)

for dims in Iterators.product([1:size for size in dimension_sizes]...)
Expand Down
17 changes: 9 additions & 8 deletions src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@ end
array_to_file(
filename::String,
data::Array{T, N},
implementation::DataType;
implementation::Type{I};
dimensions::Vector{String},
labels::Vector{String},
time_dimension::String,
dimension_size::Vector{Int},
initial_date::Union{String, DateTime} = "",
unit::String = "",
round_digits::Union{Int, Nothing} = nothing,
) where {T, N}
) where {I <:Implementation, T, N}
Write a time series file in Quiver format.
Required arguments:
- `file_path::String`: Path to file.
- `data::Array{T, N}`: Data to be written.
- `implementation::Type{I}`: Implementation to be used. It can be `Quiver.csv` or `Quiver.binary`.
- `dimensions::Vector{String}`: Dimensions of the data.
- `labels::Vector{String}`: Labels of the data.
- `time_dimension::String`: Name of the time dimension.
Expand All @@ -66,15 +67,15 @@ Optional arguments:
function array_to_file(
filename::String,
data::Array{T, N},
implementation::DataType;
implementation::Type{I};
dimensions::Vector{String},
labels::Vector{String},
time_dimension::String,
dimension_size::Vector{Int},
initial_date::Union{String, DateTime} = "",
unit::String = "",
round_digits::Union{Int, Nothing} = nothing,
) where {T, N}
digits::Union{Int, Nothing} = nothing,
) where {I <: Implementation, T, N}
kwargs_dict = Dict{Symbol, Any}()
if initial_date !== ""
if isa(initial_date, String)
Expand All @@ -101,7 +102,7 @@ function array_to_file(

for dims in Iterators.product([1:size for size in reverse(dimension_size)]...)
dim_kwargs = OrderedDict(reverse_dimensions .=> dims)
Quiver.write!(writer, round_digits(data[:, dims...]); dim_kwargs...)
Quiver.write!(writer, round_digits(data[:, dims...], digits); dim_kwargs...)
end

Quiver.close!(writer)
Expand All @@ -113,6 +114,6 @@ function round_digits(vec::Vector{T}, ::Nothing) where {T}
return vec
end

function round_digits(vec::Vector{T}, round_digits::Int) where {T}
return round.(vec, digits = round_digits)
function round_digits(vec::Vector{T}, digits::Int) where {T}
return round.(vec, digits)
end
14 changes: 8 additions & 6 deletions test/test_read_write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,9 @@ function read_file_to_array(impl)
filename = joinpath(@__DIR__, "test_read_file_to_array")

initial_date = DateTime(2006, 1, 1)
num_stages = 10
num_stages = 4
dates = collect(initial_date:Dates.Month(1):initial_date + Dates.Month(num_stages - 1))
num_scenarios = 12
num_scenarios = 3
num_blocks_per_stage = Int32.(Dates.daysinmonth.(dates) .* 24)
num_time_series = 3

Expand All @@ -977,10 +977,11 @@ function read_file_to_array(impl)
Quiver.array_to_file(
filename,
data,
impl,
dimensions,
labels,
time_dimension,
impl;
dimensions,
labels,
time_dimension,
dimension_size,
initial_date
)

Expand Down Expand Up @@ -1008,6 +1009,7 @@ function test_read_write_implementations()
read_outside_bounds_4(impl)
read_filtering_labels(impl)
read_write_out_of_order_kwargs(impl)
read_file_to_array(impl)
if impl == Quiver.csv
read_write_goto_csv_1()
read_write_goto_csv_2()
Expand Down

0 comments on commit 3e20bfd

Please sign in to comment.