Skip to content

Commit

Permalink
Merge pull request #16 from psrenergy/gb/extension-on-binary
Browse files Browse the repository at this point in the history
Extension to convert graf files
  • Loading branch information
guilhermebodin authored Aug 22, 2024
2 parents b2278a9 + e30b404 commit 8f5884d
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 5 deletions.
15 changes: 10 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Quiver"
uuid = "cdbb3f72-2527-4dbd-9d0e-93533a5519ac"
authors = ["raphasampaio", "guilhermebodin"]
version = "0.1.0"
version = "0.1.1"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand All @@ -11,19 +11,24 @@ OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[weakdeps]
PSRClassesInterface = "1eab49e5-27d8-4905-b9f6-327b6ea666c4"

[extensions]
PSRClassesInterfaceExt = "PSRClassesInterface"

[compat]
CSV = "0.10"
DataFrames = "1"
Dates = "1"
julia = "1.8"
OrderedCollections = "1"
Tables = "1"
TOML = "1"
Tables = "1"
julia = "1.8"

[extras]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["BenchmarkTools", "Random", "Test"]
test = ["Random", "Test", "PSRClassesInterface"]
69 changes: 69 additions & 0 deletions ext/PSRClassesInterfaceExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module PSRClassesInterfaceExt

using Dates
using Quiver
using PSRClassesInterface

const PSRI = PSRClassesInterface

function build_initial_date(initial_stage::Int, initial_year::Int, stage_type::PSRI.StageType)
if stage_type == PSRI.STAGE_MONTH
return DateTime(initial_year, initial_stage, 1)
elseif stage_type == PSRI.STAGE_DAY
return DateTime(initial_year, 1, 1) + Dates.Day(initial_stage - 1)
elseif stage_type == PSRI.STAGE_WEEK
return DateTime(initial_year, 1, 1) + Dates.Week(initial_stage - 1)
else
error("Convertion of graf file with stage type $stage_type is not supported.")
end
end

function Quiver.convert(
filepath::String,
from::Type{PSRI.OpenBinary.Reader},
to::Type{impl};
destination_directory::String = dirname(filepath),
) where impl <: Quiver.Implementation
filename = basename(filepath)
destination_path = joinpath(destination_directory, filename)

# Open graf file and read metadata
graf_reader = PSRI.open(
PSRI.OpenBinary.Reader,
filepath;
use_header = false,
)

stages = PSRI.max_stages(graf_reader)
scenarios = PSRI.max_scenarios(graf_reader)
blocks = PSRI.max_blocks(graf_reader)
stage_type = PSRI.stage_type(graf_reader)
initial_stage = PSRI.initial_stage(graf_reader)
initial_year = PSRI.initial_year(graf_reader)
agent_names = PSRI.agent_names(graf_reader)
unit = PSRI.data_unit(graf_reader)

initial_date = build_initial_date(initial_stage, initial_year, stage_type)

writer = Quiver.Writer{impl}(
destination_path;
dimensions = ["stage", "scenario", "block"],
labels = agent_names,
time_dimension = "stage",
dimension_size = [stages, scenarios, blocks],
initial_date = initial_date,
unit = unit,
)

for t in 1:stages, s in 1:scenarios, b in 1:PSRI.blocks_in_stage(graf_reader, t)
PSRI.goto(graf_reader, t, s, b)
Quiver.write!(writer, graf_reader[:]; stage = t, scenario = s, block = b)
end

Quiver.close!(writer)
PSRI.close(graf_reader)

return nothing
end

end
100 changes: 100 additions & 0 deletions test/test_graf_extension.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
module TestGrafExtension

using Dates
using PSRClassesInterface
using Quiver
using Test

PSRI = PSRClassesInterface

function test_graf_convertion_fixed_blocks()
BLOCKS = 3
SCENARIOS = 5
STAGES = 12
INITIAL_STAGE = 4

FILE_PATH = joinpath(@__DIR__, "test_convert_fixed_blocks")

for impl in Quiver.implementations()
for stage_type in [PSRI.STAGE_MONTH, PSRI.STAGE_WEEK, PSRI.STAGE_DAY]
iow = PSRClassesInterface.open(
PSRClassesInterface.OpenBinary.Writer,
FILE_PATH;
blocks = BLOCKS,
scenarios = SCENARIOS,
stages = STAGES,
agents = ["X", "Y", "Z"],
unit = "MW",
# optional:
initial_stage = INITIAL_STAGE,
initial_year = 2020,
stage_type = stage_type,
)

for t in 1:STAGES, s in 1:SCENARIOS, b in 1:BLOCKS
X = t + s + 0.0
Y = s - t + 0.0
Z = t + s + b * 100.0
PSRClassesInterface.write_registry(iow, [X, Y, Z], t, s, b)
end

PSRClassesInterface.close(iow)

Quiver.convert(
FILE_PATH,
PSRClassesInterface.OpenBinary.Reader,
impl
)

# Test if data was correctly converted
reader = Quiver.Reader{impl}(FILE_PATH)
num_stages = reader.metadata.dimension_size[1]
num_scenarios = reader.metadata.dimension_size[2]
num_blocks = reader.metadata.dimension_size[3]
for t in 1:num_stages
for s in 1:num_scenarios
for b in 1:num_blocks
X = t + s + 0.0
Y = s - t + 0.0
Z = t + s + b * 100.0
if impl == Quiver.csv
Quiver.next_dimension!(reader)
else
Quiver.goto!(reader; stage = t, scenario = s, block = b)
end
@test reader.data == [X, Y, Z]
end
end
end

Quiver.close!(reader)

@test reader.metadata.labels == ["X", "Y", "Z"]
@test reader.metadata.unit == "MW"
@test reader.metadata.time_dimension == :stage
@test reader.metadata.dimensions == [:stage, :scenario, :block]
end
end

rm(FILE_PATH * ".bin")
rm(FILE_PATH * ".hdr")
rm(FILE_PATH * ".quiv")
rm(FILE_PATH * ".csv")
rm(FILE_PATH * ".toml")
end

function runtests()
Base.GC.gc()
Base.GC.gc()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
end

TestGrafExtension.runtests()

end

2 comments on commit 8f5884d

@guilhermebodin
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/113682

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" 8f5884d9dcb51447d8eace6a41323a0cf580850b
git push origin v0.1.1

Please sign in to comment.