From 7ab89884bc572b3e7b7a98045a5a18fd182130bf Mon Sep 17 00:00:00 2001 From: thiagonovaesb <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Wed, 23 Nov 2022 16:28:00 -0300 Subject: [PATCH 1/2] add check_next_registry --- src/writer.jl | 63 +++++++++++++++++++++++++++++++++++- test/check_registry_error.jl | 57 ++++++++++++++++++++++++++++++++ test/runtests.jl | 3 ++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 test/check_registry_error.jl diff --git a/src/writer.jl b/src/writer.jl index 3ee8e2b..d0526c4 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -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 @@ -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 @@ -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, @@ -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) * ',' @@ -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 diff --git a/test/check_registry_error.jl b/test/check_registry_error.jl new file mode 100644 index 0000000..2e1bfa6 --- /dev/null +++ b/test/check_registry_error.jl @@ -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() \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 6c5f72c..bf7515d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 \ No newline at end of file From c43d87e886aceeb258d30307c451864325d17939 Mon Sep 17 00:00:00 2001 From: thiagonovaesb <77932135+Thiago-NovaesB@users.noreply.github.com> Date: Wed, 23 Nov 2022 16:53:04 -0300 Subject: [PATCH 2/2] [no ci] update project version 0.2.0 -> 0.2.1 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 6a9aed1..d30db96 100644 --- a/Project.toml +++ b/Project.toml @@ -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"