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

Fix subgrid_id in output #1466

Merged
merged 1 commit into from
May 16, 2024
Merged
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
1 change: 1 addition & 0 deletions core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ end

"Subgrid linearly interpolates basin levels."
struct Subgrid
subgrid_id::Vector{Int32}
basin_index::Vector{Int32}
interpolations::Vector{ScalarInterpolation}
level::Vector{Float64}
Expand Down
9 changes: 6 additions & 3 deletions core/src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,8 @@ function Subgrid(db::DB, config::Config, basin::Basin)::Subgrid
node_to_basin = Dict(node_id => index for (index, node_id) in enumerate(basin.node_id))
tables = load_structvector(db, config, BasinSubgridV1)

basin_ids = Int32[]
subgrid_ids = Int32[]
basin_index = Int32[]
interpolations = ScalarInterpolation[]
has_error = false
for group in IterTools.groupby(row -> row.subgrid_id, tables)
Expand All @@ -987,16 +988,18 @@ function Subgrid(db::DB, config::Config, basin::Basin)::Subgrid
pushfirst!(subgrid_level, first(subgrid_level))
pushfirst!(basin_level, nextfloat(-Inf))
new_interp = LinearInterpolation(subgrid_level, basin_level; extrapolate = true)
push!(basin_ids, node_to_basin[node_id])
push!(subgrid_ids, subgrid_id)
push!(basin_index, node_to_basin[node_id])
push!(interpolations, new_interp)
else
has_error = true
end
end

has_error && error("Invalid Basin / subgrid table.")
level = fill(NaN, length(subgrid_ids))

return Subgrid(basin_ids, interpolations, fill(NaN, length(basin_ids)))
return Subgrid(subgrid_ids, basin_index, interpolations, level)
end

function Allocation(db::DB, config::Config, graph::MetaGraph)::Allocation
Expand Down
5 changes: 2 additions & 3 deletions core/src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,11 @@ function subgrid_level_table(
(; t, saveval) = saved.subgrid_level
subgrid = integrator.p.subgrid

nelem = length(subgrid.basin_index)
nelem = length(subgrid.subgrid_id)
ntsteps = length(t)
unique_elem_id = collect(1:nelem)

time = repeat(datetime_since.(t, config.starttime); inner = nelem)
subgrid_id = repeat(unique_elem_id; outer = ntsteps)
subgrid_id = repeat(subgrid.subgrid_id; outer = ntsteps)
subgrid_level = FlatVector(saveval)
return (; time, subgrid_id, subgrid_level)
end
Expand Down
3 changes: 1 addition & 2 deletions core/test/run_models_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@
basin_level = basin.level[1]
@test length(p.subgrid.level) == 3
@test diff(p.subgrid.level) ≈ [-1.0, 2.0]
# TODO The original subgrid IDs are lost and mapped to 1, 2, 3
@test subgrid.subgrid_id[1:3] == [11, 22, 33] broken = true
@test subgrid.subgrid_id[1:3] == [11, 22, 33]
@test subgrid.subgrid_level[1:3] ≈
[basin_level, basin_level - 1.0, basin_level + 1.0]
@test subgrid.subgrid_level[(end - 2):end] == p.subgrid.level
Expand Down