Skip to content

Commit

Permalink
Merge pull request #279 from NREL-SIIP/sd/copy_time_series
Browse files Browse the repository at this point in the history
Updating copy time series method
  • Loading branch information
jd-lara authored Feb 15, 2022
2 parents ce93824 + 1b7a1e8 commit 5460cfe
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
39 changes: 37 additions & 2 deletions src/component.jl
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ references.
function copy_time_series!(
dst::InfrastructureSystemsComponent,
src::InfrastructureSystemsComponent;
name_mapping::Union{Nothing, Dict{String, String}}=nothing,
name_mapping::Union{Nothing, Dict{Tuple{String, String}, String}}=nothing,
scaling_factor_multiplier_mapping::Union{Nothing, Dict{String, String}}=nothing,
)
storage = _get_time_series_storage(dst)
Expand All @@ -459,7 +459,7 @@ function copy_time_series!(
name = get_name(ts_metadata)
new_name = name
if !isnothing(name_mapping)
new_name = get(name_mapping, name, nothing)
new_name = get(name_mapping, (get_name(src), name), nothing)
if isnothing(new_name)
@debug "Skip copying ts_metadata" _group = LOG_GROUP_TIME_SERIES name
continue
Expand Down Expand Up @@ -623,6 +623,41 @@ function get_time_series_multiple(
end
end

function get_time_series_with_metadata_multiple(
component::InfrastructureSystemsComponent,
filter_func=nothing;
type=nothing,
start_time=nothing,
name=nothing,
)
container = get_time_series_container(component)
storage = _get_time_series_storage(component)

Channel() do channel
for key in keys(container.data)
ts_metadata = container.data[key]
ts_type = time_series_metadata_to_data(ts_metadata)
if !isnothing(type) && !(ts_type <: type)
continue
end
if !isnothing(name) && key.name != name
continue
end
ts = deserialize_time_series(
ts_type,
storage,
ts_metadata,
UnitRange(1, length(ts_metadata)),
UnitRange(1, get_count(ts_metadata)),
)
if !isnothing(filter_func) && !filter_func(ts)
continue
end
put!(channel, (ts, ts_metadata))
end
end
end

"""
Transform all instances of SingleTimeSeries to DeterministicSingleTimeSeries. Do nothing
if the component does not contain any instances.
Expand Down
54 changes: 52 additions & 2 deletions test/test_time_series.jl
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,56 @@ end
@test length(collect(IS.get_time_series_multiple(sys, filter_func; name="val2"))) == 0
end

@testset "Test get_time_series_with_metadata_multiple" begin
sys = IS.SystemData()
name = "Component1"
component_val = 5
component = IS.TestComponent(name, component_val)
IS.add_component!(sys, component)
initial_time1 = Dates.DateTime("2020-01-01T00:00:00")
initial_time2 = Dates.DateTime("2020-01-02T00:00:00")

dates1 = collect(initial_time1:Dates.Hour(1):Dates.DateTime("2020-01-01T23:00:00"))
dates2 = collect(initial_time2:Dates.Hour(1):Dates.DateTime("2020-01-02T23:00:00"))
data1 = collect(1:24)
data2 = collect(25:48)
ta1 = TimeSeries.TimeArray(dates1, data1, [IS.get_name(component)])
ta2 = TimeSeries.TimeArray(dates2, data2, [IS.get_name(component)])
time_series1 =
IS.SingleTimeSeries(name="val", data=ta1, scaling_factor_multiplier=IS.get_val)
time_series2 =
IS.SingleTimeSeries(name="val2", data=ta2, scaling_factor_multiplier=IS.get_val)
IS.add_time_series!(sys, component, time_series1)
IS.add_time_series!(sys, component, time_series2)

@test length(collect(IS.get_time_series_with_metadata_multiple(component))) == 2

@test length(
collect(
IS.get_time_series_with_metadata_multiple(component; type=IS.SingleTimeSeries),
),
) == 2
@test length(
collect(
IS.get_time_series_with_metadata_multiple(component; type=IS.Probabilistic),
),
) == 0

@test length(
collect(IS.get_time_series_with_metadata_multiple(component; name="val")),
) == 1
@test length(
collect(IS.get_time_series_with_metadata_multiple(component; name="bad_name")),
) == 0

filter_func = x -> TimeSeries.values(IS.get_data(x))[12] == 12
@test length(
collect(
IS.get_time_series_with_metadata_multiple(component, filter_func; name="val2"),
),
) == 0
end

@testset "Test add_time_series from TimeArray" begin
sys = IS.SystemData()
name = "Component1"
Expand Down Expand Up @@ -1068,7 +1118,7 @@ end
component2 = IS.TestComponent("component2", 6)
IS.add_component!(sys, component2)
name2 = "val2"
name_mapping = Dict(name1 => name2)
name_mapping = Dict((IS.get_name(component), name1) => name2)
IS.copy_time_series!(component2, component; name_mapping=name_mapping)
time_series = IS.get_time_series(IS.SingleTimeSeries, component2, name2)
@test time_series isa IS.SingleTimeSeries
Expand Down Expand Up @@ -1102,7 +1152,7 @@ end
component2 = IS.TestComponent("component2", 6)
IS.add_component!(sys, component2)
name2b = "val2b"
name_mapping = Dict(name2a => name2b)
name_mapping = Dict((IS.get_name(component), name2a) => name2b)
IS.copy_time_series!(component2, component; name_mapping=name_mapping)
time_series = IS.get_time_series(IS.SingleTimeSeries, component2, name2b)
@test time_series isa IS.SingleTimeSeries
Expand Down

0 comments on commit 5460cfe

Please sign in to comment.