using JSON #CSV, NamedArrays, DataFrames (now with HH.jl script)

datadomain = JSON.parsefile(joinpath(@__DIR__,"domain.json"))
set_dir = joinpath(@__DIR__,"data_$datayear\\sets\\")

all_sets = Dict()
for s in readdir(set_dir)
    STS = CSV.read(set_dir * s, DataFrame)
    all_sets[split(s,".")[1]] = STS[!,1]
end

all_sets

all_parameters = Dict()
for (parm,D) in datadomain

    if parm != "yh0" && length(D) > 1
        #println(parm)
        df = CSV.read(joinpath(@__DIR__,"data_$datayear/parameters/$(parm).csv"), DataFrame)
        dd = [String.(all_sets[d]) for d∈D]
        all_parameters[parm] = NamedArray(zeros(length.(dd)...), dd, D)
        for row in eachrow(df)
            all_parameters[parm][[String(row[d]) for d∈D]...] = row[:value]
        end
    elseif length(D) == 1
        df = CSV.read(joinpath(@__DIR__,"data_$datayear/parameters/$(parm).csv"), DataFrame)
        dd = [String.(all_sets[d]) for d∈D]
        all_parameters[parm] = NamedArray(zeros(length.(dd)...), (dd...), (D...))
        for row in eachrow(df)
            all_parameters[parm][[String(row[d]) for d∈D]...] = row[:value]
        end
    elseif length(D) == 0
        df = CSV.read(joinpath(@__DIR__,"data_$datayear/parameters/$(parm).csv"), DataFrame)
        all_parameters[parm] = df[1,1]
    elseif parm == "yh0"
        dd = [String.(all_sets[d]) for d∈D]
        all_parameters[parm] = NamedArray(zeros(length.(dd)...), dd, D)
    else
        println(parm)
    end
end
all_parameters

HHdata = Dict()
HHdata["sets"] = all_sets
HHdata["parameters"] = all_parameters