Skip to content

Sorting things out#2003

Merged
visr merged 6 commits into
mainfrom
time-to-sort
Jan 13, 2025
Merged

Sorting things out#2003
visr merged 6 commits into
mainfrom
time-to-sort

Conversation

@visr
Copy link
Copy Markdown
Member

@visr visr commented Jan 9, 2025

Fixes #1976.
Fixes most of #601.

@visr
Copy link
Copy Markdown
Member Author

visr commented Jan 9, 2025

I removed the sorting information from the docs. We can add it back with Quarto using code so it won't be wrong again:

using Ribasim
using StructArrays: StructVector
using Legolas: AbstractRecord

"""
Get a string describing the sorting of a table type.

julia> sort_info(Ribasim.BasinTimeV1)
"Sorted by: node_id, time."
"""
function sort_info(table_type::Type{<:AbstractRecord})::String
    # create an empty table to get the sort by function
    table = StructVector{table_type}(undef, 0)
    # extract the column names from the getproperty calls in the expression
    by = Ribasim.sort_by(table)
    codeinfo = Base.uncompressed_ast(only(methods(by)))
    is_getproperty(ex) = ex isa Expr && ex.args[1].name == :getproperty
    cols = [ex.args[3].value for ex in codeinfo.code if is_getproperty(ex)]
    return "Sorted by: $(join(cols, ", "))."
end

using Test
@test sort_info(Ribasim.BasinStaticV1) == "Sorted by: node_id."
@test sort_info(Ribasim.BasinTimeV1) == "Sorted by: time, node_id."

There are just a few places tables that don't have a Julia schema like Basin / area and Terminal that do have sorting in Python.

@visr visr marked this pull request as ready for review January 13, 2025 09:28
@visr visr merged commit f074e0c into main Jan 13, 2025
@visr visr deleted the time-to-sort branch January 13, 2025 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor TabulatedRatingCurve / time updating

1 participant