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

Fix OpenBinary reader when using header #233

Merged
merged 1 commit into from
Dec 3, 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
6 changes: 5 additions & 1 deletion src/OpenBinary/reader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ function PSRI.open(
end
end

queried_agents = length(header)
agent_names = header

if !allow_empty && isempty(indices)
if isempty(header)
error(
Expand All @@ -314,12 +317,13 @@ function PSRI.open(
end
end
else
queried_agents = total_agents
indices = collect(1:total_agents)
end

data_buffer = zeros(Float32, total_agents)

data = zeros(Float64, total_agents)
data = zeros(Float64, queried_agents)

if initial_stage != Dates.Date(1900, 1, 1)
_year = initial_year
Expand Down
144 changes: 144 additions & 0 deletions test/OpenBinary/use_header.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
function use_header()
file_path = joinpath(".", "data")

STAGES = 2
SCENARIOS = 2
AGENTS = ["X", "Y", "Z"]
UNIT = "MW"

iow = PSRI.open(
PSRI.OpenBinary.Writer,
file_path;
is_hourly = true,
scenarios = SCENARIOS,
stages = STAGES,
agents = AGENTS,
unit = UNIT,
# optional:
initial_stage = 2,
initial_year = 2006,
stage_type = PSRI.STAGE_MONTH,
)

for t in 1:STAGES, s in 1:SCENARIOS
for b in 1:PSRI.blocks_in_stage(iow, t)
X = 10_000.0 * t + 1000.0 * s + b
Y = b + 0.0
Z = 10.0 * t + s
PSRI.write_registry(
iow,
[X, Y, Z],
t,
s,
b,
)
end
end

PSRI.close(iow)

# All agents

ior = PSRI.open(
PSRI.OpenBinary.Reader,
file_path;
use_header = true,
header = ["X", "Y", "Z"],
)

@test PSRI.agent_names(ior) == ["X", "Y", "Z"]

for t in 1:1, s in 1:1
for b in 1:PSRI.blocks_in_stage(ior, t)
X = 10_000.0 * t + 1000.0 * s + b
Y = b + 0.0
Z = 10.0 * t + s
ref = [X, Y, Z]
for agent in 1:3
@test ior[agent] == ref[agent]
end
PSRI.next_registry(ior)
end
end

PSRI.close(ior)

# # Only X

ior = PSRI.open(
PSRI.OpenBinary.Reader,
file_path;
use_header = true,
header = ["X"],
)

@test PSRI.agent_names(ior) == ["X"]

for t in 1:1, s in 1:1
for b in 1:PSRI.blocks_in_stage(ior, t)
X = 10_000.0 * t + 1000.0 * s + b
ref = [X]
@test ior[1] == ref[1]
PSRI.next_registry(ior)
end
end

PSRI.close(ior)
ior = nothing

# Only Y

ior = PSRI.open(
PSRI.OpenBinary.Reader,
file_path;
use_header = true,
header = ["Y"],
)

@test PSRI.agent_names(ior) == ["Y"]

for t in 1:1, s in 1:1
for b in 1:PSRI.blocks_in_stage(ior, t)
Y = b + 0.0
ref = [Y]
@test ior[1] == ref[1]
PSRI.next_registry(ior)
end
end

PSRI.close(ior)
ior = nothing

# All agents reverse

ior = PSRI.open(
PSRI.OpenBinary.Reader,
file_path;
use_header = true,
header = ["Z", "Y", "X"],
)

@test PSRI.agent_names(ior) == ["Z", "Y", "X"]

for t in 1:1, s in 1:1
for b in 1:PSRI.blocks_in_stage(ior, t)
X = 10_000.0 * t + 1000.0 * s + b
Y = b + 0.0
Z = 10.0 * t + s
ref = [Z, Y, X]
for agent in 1:3
@test ior[agent] == ref[agent]
end
PSRI.next_registry(ior)
end
end

PSRI.close(ior)
ior = nothing

rm(file_path * ".bin")
rm(file_path * ".hdr")
return
end

use_header()
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ end
@testset "Write file partially" begin
@time include("OpenBinary/incomplete_file.jl")
end
@testset "Use header" begin
@time include("OpenBinary/use_header.jl")
end
end
@testset "ReaderMapper" begin
@time include("reader_mapper.jl")
Expand Down
Loading