-
Notifications
You must be signed in to change notification settings - Fork 173
/
field_dataset.jl
53 lines (40 loc) 路 1.68 KB
/
field_dataset.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
struct FieldDataset{F, M, P}
fields :: F
metadata :: M
filepath :: P
end
"""
FieldDataset(filepath;
architecture=CPU(), grid=nothing, backend=InMemory(), metadata_paths=["metadata"])
Returns a `Dict` containing a `FieldTimeSeries` for each field in the JLD2 file located
at `filepath`. Note that model output **must** have been saved with halos.
Keyword arguments
=================
- `backend`: Either `InMemory()` (default) or `OnDisk()`. The `InMemory` backend will
load the data fully in memory as a 4D multi-dimensional array while the `OnDisk()`
backend will lazily load field time snapshots when the `FieldTimeSeries` is indexed
linearly.
- `metadata_paths`: A list of JLD2 paths to look for metadata. By default it looks in
`file["metadata"]`.
- `grid`: May be specified to override the grid used in the JLD2 file.
"""
function FieldDataset(filepath;
architecture=CPU(), grid=nothing, backend=InMemory(), metadata_paths=["metadata"])
file = jldopen(filepath)
field_names = keys(file["timeseries"])
filter!(k -> k != "t", field_names) # Time is not a field.
ds = Dict{String, FieldTimeSeries}(
name => FieldTimeSeries(filepath, name; architecture, backend, grid)
for name in field_names
)
metadata = Dict(
k => file["$mp/$k"]
for mp in metadata_paths if haskey(file, mp)
for k in keys(file["$mp"])
)
close(file)
return FieldDataset(ds, metadata, abspath(filepath))
end
Base.getindex(fds::FieldDataset, inds...) = Base.getindex(fds.fields, inds...)
Base.show(io::IO, fds::FieldDataset) =
print(io, "FieldDataset with $(length(fds.fields)) fields and $(length(fds.metadata)) metadata entries.")