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 check_next_registry #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "GrafCSV"
uuid = "070ba913-1d04-4147-8b30-d43c8af0fb3e"
version = "0.2.0"
version = "0.2.1"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
63 changes: 62 additions & 1 deletion src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ mutable struct Writer <: PSRI.AbstractWriter
initial_stage::Int
initial_year::Int
row_separator::String
last_stage_registry::Int
last_scenario_registry::Int
last_block_registry::Int
end

PSRI.is_hourly(graf::Writer) = graf.is_hourly
Expand Down Expand Up @@ -48,6 +51,9 @@ function PSRI.open(
sequential_model::Bool = true,
# addtional
allow_unsafe_name_length::Bool = false,
last_stage_registry::Int = 1,
last_scenario_registry::Int = 1,
last_block_registry::Int = 0,
)

# TODO: consider name length
Expand Down Expand Up @@ -144,10 +150,58 @@ function PSRI.open(
initial_stage,
initial_year,
row_separator,
last_stage_registry,
last_scenario_registry,
last_block_registry,
)
end

# TODO check next entry is in the correct order
function check_next_registry(
writer::Writer,
stage::Integer,
scenario::Integer,
block::Integer,
)
blocks_in_stage = PSRI.blocks_in_stage(writer, writer.last_stage_registry)

if writer.last_block_registry == blocks_in_stage
expected_block = 1
reset_block = true
else
expected_block = writer.last_block_registry + 1
reset_block = false
end

if reset_block && writer.last_scenario_registry == writer.scenarios
expected_scenario = 1
reset_scenario = true
elseif reset_block
expected_scenario = writer.last_scenario_registry + 1
reset_scenario = false
else
expected_scenario = writer.last_scenario_registry
reset_scenario = false
end

if reset_scenario && reset_block
expected_stage = writer.last_stage_registry + 1
reset_stage = false
else
expected_stage = writer.last_stage_registry
reset_stage = false
end

if (expected_stage == stage) && (expected_scenario == scenario) && (expected_block == block)
return
else
error("In GrafCSV, registries must be written by iterating indexes in (stages, scenarios, blocks) order.\n
Last registry: (stage: $(writer.last_stage_registry), scenario: $(writer.last_scenario_registry), block: $(writer.last_block_registry)) \n
Current registry: (stage: $(stage), scenario: $(scenario), block: $(block)) \n
Expected registry: (stage: $(expected_stage), scenario: $(expected_scenario), block: $(expected_block)) \n
To add registries in any order, use OpenBinary format.")
end

end

function PSRI.write_registry(
writer::Writer,
Expand All @@ -173,6 +227,9 @@ function PSRI.write_registry(
if length(data) != writer.agents
error("data vector has length $(length(data)) and expected was $(writer.agents)")
end

check_next_registry(writer, stage, scenario, block)

str = ""
str *= string(stage) * ','
str *= string(scenario) * ','
Expand All @@ -183,6 +240,10 @@ function PSRI.write_registry(
str = chop(str; tail = 1) # remove last comma
str *= writer.row_separator
Base.write(writer.io, str)

writer.last_stage_registry = stage
writer.last_scenario_registry = scenario
writer.last_block_registry = block
return nothing
end

Expand Down
57 changes: 57 additions & 0 deletions test/check_registry_error.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
function check_registry_error_test()
FILE_PATH = joinpath(".", "example_2")

STAGES = 12
BLOCKS = 3
SCENARIOS = 4
STAGE_TYPE = PSRI.STAGE_MONTH
INITIAL_STAGE = 1
INITIAL_YEAR = 2006
UNIT = "MW"

iow = PSRI.open(
GrafCSV.Writer,
FILE_PATH,
blocks = BLOCKS,
scenarios = SCENARIOS,
stages = STAGES,
agents = ["X", "Y", "Z"],
unit = UNIT,
# optional:
stage_type = STAGE_TYPE,
initial_stage = INITIAL_STAGE,
initial_year = INITIAL_YEAR
)

# ---------------------------------------------
# Parte 3 - Gravacao dos registros do resultado
# ---------------------------------------------

stage, scenario, block = 1, 1, 1
X = stage + scenario + 0.
Y = scenario - stage + 0.
Z = stage + scenario + block * 100.
PSRI.write_registry(
iow,
[X, Y, Z],
stage,
scenario,
block
)

stage, scenario, block = 1, 2, 1
X = stage + scenario + 0.
Y = scenario - stage + 0.
Z = stage + scenario + block * 100.
@test_throws ErrorException PSRI.write_registry(
iow,
[X, Y, Z],
stage,
scenario,
block
)

# Finaliza gravacao
PSRI.close(iow)
end
check_registry_error_test()
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ const PSRI = GrafCSV.PSRClassesInterface
@testset "Utils" begin
include("time_series_utils.jl")
end
@testset "Check registry error" begin
include("check_registry_error.jl")
end
end