-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read some interpolation tables into the Model struct
- Loading branch information
Showing
15 changed files
with
213 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# This module exports a water level: | ||
# | ||
# * the water level of the original hydrodynamic model before lumping. | ||
# * a differently aggregated water level, used for e.g. coupling to MODFLOW. | ||
# | ||
# The second is arguably easier to interpret. | ||
|
||
""" | ||
basin_level: a view on Ribasim's basin level. | ||
level: the interpolated water level | ||
tables: the interpolator callables | ||
All members of this struct have length n_elem. | ||
""" | ||
struct LevelExporter | ||
basin_index::Vector{Int} | ||
interpolations::Vector{ScalarInterpolation} | ||
level::Vector{Float64} | ||
end | ||
|
||
function LevelExporter(tables, node_to_basin::Dict{Int, Int})::LevelExporter | ||
basin_ids = Int[] | ||
interpolations = ScalarInterpolation[] | ||
|
||
for group in IterTools.groupby(row -> row.element_id, tables) | ||
node_id = first(getproperty.(group, :node_id)) | ||
basin_level = getproperty.(group, :basin_level) | ||
element_level = getproperty.(group, :level) | ||
# Ensure it doesn't extrapolate before the first value. | ||
new_interp = LinearInterpolation([element_level[1], element_level...], [prevfloat(basin_level[1]), basin_level...]) | ||
push!(basin_ids, node_to_basin[node_id]) | ||
push!(interpolations, new_interp) | ||
end | ||
|
||
return LevelExporter(basin_ids, interpolations, fill(NaN, length(basin_ids))) | ||
end | ||
|
||
function create_level_exporters(db::DB, config::Config, basin::Basin)::Dict{String, LevelExporter} | ||
node_to_basin = Dict(node_id => index for (index, node_id) in enumerate(basin.node_id)) | ||
tables = load_structvector(db, config, LevelExporterStaticV1) | ||
level_exporters = Dict{String, LevelExporter}() | ||
if length(tables) > 0 | ||
for group in IterTools.groupby(row -> row.name, tables) | ||
name = first(getproperty.(group, :name)) | ||
level_exporters[name] = LevelExporter(group, node_to_basin) | ||
end | ||
end | ||
return level_exporters | ||
end | ||
|
||
""" | ||
Compute a new water level for each external element. | ||
""" | ||
function update!(exporter::LevelExporter, basin_level) | ||
for (i, (index, interp)) in enumerate(zip(exporter.basin_index, exporter.interpolations)) | ||
exporter.level[i] = interp(basin_level[index]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"$id": "https://deltares.github.io/Ribasim/schema/LevelExporterStatic.schema.json", | ||
"title": "LevelExporterStatic", | ||
"description": "A LevelExporterStatic object based on Ribasim.LevelExporterStaticV1", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"format": "default", | ||
"type": "string" | ||
}, | ||
"element_id": { | ||
"format": "default", | ||
"type": "integer" | ||
}, | ||
"node_id": { | ||
"format": "default", | ||
"type": "integer" | ||
}, | ||
"basin_level": { | ||
"format": "double", | ||
"type": "number" | ||
}, | ||
"level": { | ||
"format": "double", | ||
"type": "number" | ||
}, | ||
"remarks": { | ||
"description": "a hack for pandera", | ||
"type": "string", | ||
"format": "default", | ||
"default": "" | ||
} | ||
}, | ||
"required": [ | ||
"name", | ||
"element_id", | ||
"node_id", | ||
"basin_level", | ||
"level" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"$id": "https://deltares.github.io/Ribasim/schema/level_exporter.schema.json", | ||
"title": "level_exporter", | ||
"description": "A level_exporter object based on Ribasim.config.level_exporter", | ||
"type": "object", | ||
"properties": { | ||
"static": { | ||
"format": "default", | ||
"anyOf": [ | ||
{ | ||
"type": "null" | ||
}, | ||
{ | ||
"type": "string" | ||
} | ||
], | ||
"default": null | ||
} | ||
}, | ||
"required": [ | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from pandera.typing import DataFrame | ||
|
||
from ribasim.input_base import TableModel | ||
from ribasim.schemas import LevelExporterStaticSchema # type: ignore | ||
|
||
|
||
class LevelExporter(TableModel): | ||
"""The level exporter export Ribasim water levels.""" | ||
|
||
static: DataFrame[LevelExporterStaticSchema] | ||
|
||
def sort(self): | ||
self.static.sort_values( | ||
["name", "element_id", "node_id", "basin_level"], | ||
ignore_index=True, | ||
inplace=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters