Skip to content

Commit

Permalink
Merge pull request #3 from mitchphillipson/hdf5
Browse files Browse the repository at this point in the history
Hdf5
  • Loading branch information
mitchphillipson authored May 9, 2023
2 parents 18f6cef + 5359a5c commit d84ac07
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "0.1.0"
[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
3 changes: 3 additions & 0 deletions src/GamsSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ function GamsSet(x::Tuple...;description = "")
return GamsSet([GamsElement(a,b) for (a,b) in x],description)
end

function GamsSet(x::Vector{Tuple{Symbol,String}};description = "")
return GamsSet([GamsElement(a,b) for (a,b) in x],description)
end

function GamsSet(e::Vector{Symbol};description = "")
return GamsSet([GamsElement(i,"") for ie],description)
Expand Down
2 changes: 2 additions & 0 deletions src/GamsStructure.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ using MacroTools
using JSON
using DelimitedFiles

using HDF5

export GamsElement, GamsSet, GamsParameter, @GamsSets,
@GamsParameters,@GamsDomainSets,GamsDomainSet,
GamsUniverse,add_set,add_parameter,alias,GamsScalar,
Expand Down
30 changes: 21 additions & 9 deletions src/io/load.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function load_universe(path::String;to_load = [],nGU::GamsUniverse = GamsUniverse())
function load_universe(path::String;to_load = [],nGU::GamsUniverse = GamsUniverse(),raw_text=true)

#nGU = GamsUniverse()

Expand All @@ -18,14 +18,26 @@ function load_universe(path::String;to_load = [],nGU::GamsUniverse = GamsUnivers
end
end


for (key,parm) in info["parm"]
key = Symbol(key)
if to_load == [] || key to_load
sets,desc,cols = parm
cols = [e for e in cols]
sets = Tuple([Symbol(e) for e in sets])
add_parameter(nGU,key,GamsParameter(path,key,sets,nGU,cols,description = desc))
if raw_text
for (key,parm) in info["parm"]
key = Symbol(key)
if to_load == [] || key to_load
sets,desc,cols = parm
cols = [e for e in cols]
sets = Tuple([Symbol(e) for e in sets])
add_parameter(nGU,key,GamsParameter(path,key,sets,nGU,cols,description = desc))
end
end
else
parameters = h5open("$path/parameters.h5","r")
for (key,parm) in info["parm"]
name = Symbol(key)
if to_load == [] || name to_load
sets,desc,cols = parm
sets = Tuple([Symbol(e) for e in sets])
add_parameter(nGU,name,GamsParameter(nGU,sets,desc))
nGU[name][sets...] = read(parameters[key])
end
end
end

Expand Down
37 changes: 21 additions & 16 deletions src/io/unload.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,33 @@ function unload(S::GamsSet,path,set_name)
end


function unload(GU::GamsUniverse,P::GamsParameter,path,parm_name)
function unload(GU::GamsUniverse,P::GamsParameter,path,parm_name;raw_text=true)

out = Vector{Vector{Any}}()
if raw_text
out = Vector{Vector{Any}}()

tmp = [string(s) for s in P.sets]
push!(tmp,"value")
push!(out,tmp)

axes = [[e for eGU[s]] for sP.sets]
for idx Iterators.product(axes...)
tmp = Vector{Any}()
append!(tmp,[idx...])
ind = [[i] for iidx]
push!(tmp,P[ind...])
tmp = [string(s) for s in P.sets]
push!(tmp,"value")
push!(out,tmp)
end

writedlm("$path/$parm_name.csv",out,",")
axes = [[e for eGU[s]] for sP.sets]
for idx Iterators.product(axes...)
tmp = Vector{Any}()
append!(tmp,[idx...])
ind = [[i] for iidx]
push!(tmp,P[ind...])
push!(out,tmp)
end

writedlm("$path/$parm_name.csv",out,",")

else
h5write("$path/parameters.h5",String(parm_name),P.value)
end
end


function unload(GU::GamsUniverse,path;to_unload = [])
function unload(GU::GamsUniverse,path;to_unload = [],raw_text = true)
info = Dict()
info[:set] = Dict()
info[:parm] = Dict()
Expand All @@ -46,7 +51,7 @@ function unload(GU::GamsUniverse,path;to_unload = [])

for (key,parm) in GU.parameters
if to_unload == [] || keyto_unload
unload(GU,parm,path,key)
unload(GU,parm,path,key;raw_text=raw_text)
cols = collect(1:length(parm.sets))
info[:parm][key] = [parm.sets,parm.description,cols]
end
Expand Down

0 comments on commit d84ac07

Please sign in to comment.