Skip to content

Commit 707cf9e

Browse files
Support InMemory(N) with split timeseries (#5607)
* fix split path * fix
1 parent 53d66ef commit 707cf9e

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

src/OutputReaders/field_time_series.jl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -909,23 +909,15 @@ function FieldTimeSeries(file::JLD2.JLDFile, name::String;
909909
Nt = time_indices_length(backend, times)
910910
@apply_regionally data = new_data(eltype(grid), grid, loc, indices, Nt)
911911

912-
# For OnDisk backend with split files, store a SplitFilePath so getindex
913-
# can find the correct part file for each time index.
914-
fts_path = if !isnothing(Nparts) && backend isa OnDisk
915-
SplitFilePath(part_paths, cumsum(iterations_per_part))
916-
else
917-
path
918-
end
912+
fts_path = isnothing(Nparts) ? path : SplitFilePath(part_paths, cumsum(iterations_per_part))
919913

920914
time_series = FieldTimeSeries{LX, LY, LZ}(data, grid, backend, boundary_conditions, indices,
921915
times, fts_path, name, time_indexing, reader_kw)
922916

923917
if isnothing(Nparts)
924918
set!(time_series, path, name)
925919
else
926-
for path in part_paths
927-
set!(time_series, path, name; warn_missing_data=false)
928-
end
920+
set!(time_series, fts_path, name)
929921
end
930922

931923
return time_series

src/OutputReaders/set_field_time_series.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ function set!(fts::InMemoryFTS, path::String, args::String...; kwargs...)
4040
end
4141
end
4242

43+
function set!(fts::InMemoryFTS, sfp::SplitFilePath, name::String=fts.name)
44+
Ntotal = last(sfp.cumulative_length)
45+
needed_paths = String[]
46+
for n in time_indices(fts)
47+
(n < 1 || n > Ntotal) && continue
48+
part_path, _ = file_and_local_index(sfp, n)
49+
part_path needed_paths && push!(needed_paths, part_path)
50+
end
51+
for part_path in needed_paths
52+
set!(fts, part_path, name; warn_missing_data=false)
53+
end
54+
return nothing
55+
end
56+
4357
# Convenience method with default path
4458
function set!(fts::InMemoryFTS; kwargs...)
4559
return set!(fts, fts.path; kwargs...)
@@ -141,6 +155,7 @@ function initialize_file!(file, name, fts)
141155
end
142156

143157
set!(fts::OnDiskFTS, path::String, name::String; kwargs...) = nothing
158+
set!(fts::OnDiskFTS, ::SplitFilePath, name::String=fts.name) = nothing
144159

145160
function set!(fts::InMemoryFTS, f::Function)
146161
cpu_times = on_architecture(CPU(), fts.times)

test/test_output_readers.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ include("dependencies_for_runtests.jl")
22

33
using Oceananigans.Units: Time
44
using Oceananigans.Fields: indices, interpolate!
5-
using Oceananigans.OutputReaders: Cyclical, Clamp, Linear
5+
using Oceananigans.OutputReaders: Cyclical, Clamp, Linear, SplitFilePath
66

77
using Random
88
using NCDatasets
@@ -325,6 +325,9 @@ function test_field_time_series_split_files(arch)
325325
model = NonhydrostaticModel(grid, tracers=:c)
326326
simulation = Simulation(model, Δt=1, stop_time=10)
327327

328+
set_tracer_to_iteration!(sim) = fill!(parent(sim.model.tracers.c), sim.model.clock.iteration)
329+
add_callback!(simulation, set_tracer_to_iteration!, IterationInterval(1))
330+
328331
simulation.output_writers[:fields] = JLD2Writer(model, model.tracers;
329332
filename = "split_test",
330333
dir = dir,
@@ -341,6 +344,7 @@ function test_field_time_series_split_files(arch)
341344
@test length(fts_mem.times) == 11
342345
@test fts_mem[1] isa Field
343346
@test fts_mem[11] isa Field
347+
@test fts_mem.path isa SplitFilePath
344348

345349
# Test OnDisk backend with split files
346350
fts_disk = FieldTimeSeries(abs_path, "c"; backend=OnDisk(), architecture=arch)
@@ -351,6 +355,12 @@ function test_field_time_series_split_files(arch)
351355
@test fts_disk[n] isa Field
352356
end
353357

358+
fts_partly = FieldTimeSeries(abs_path, "c"; backend=InMemory(2), architecture=arch)
359+
@test fts_partly.path isa SplitFilePath
360+
for n in 1:length(fts_partly.times)
361+
@test Array(interior(fts_partly[n])) == Array(interior(fts_mem[n]))
362+
end
363+
354364
rm(dir, recursive=true, force=true)
355365
return nothing
356366
end

0 commit comments

Comments
 (0)