-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathfield_dataset.jl
80 lines (62 loc) · 2.51 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
struct FieldDataset{F, M, P, KW}
fields :: F
metadata :: M
filepath :: P
reader_kw :: KW
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.
- `reader_kw`: A named tuple or dictionary of keyword arguments to pass to the reader
(currently only JLD2) to be used when opening files.
"""
function FieldDataset(filepath;
architecture = CPU(),
grid = nothing,
backend = InMemory(),
metadata_paths = ["metadata"],
reader_kw = NamedTuple())
file = jldopen(filepath; reader_kw...)
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, reader_kw)
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), reader_kw)
end
Base.getindex(fds::FieldDataset, inds...) = Base.getindex(fds.fields, inds...)
Base.getindex(fds::FieldDataset, i::Symbol) = Base.getindex(fds, string(i))
function Base.getproperty(fds::FieldDataset, name::Symbol)
if name in propertynames(fds)
return getfield(fds, name)
else
return getindex(fds, name)
end
end
function Base.show(io::IO, fds::FieldDataset)
s = "FieldDataset with $(length(fds.fields)) fields and $(length(fds.metadata)) metadata entries:\n"
n_fields = length(fds.fields)
for (i, (name, fts)) in enumerate(pairs(fds.fields))
prefix = i == n_fields ? "└── " : "├── "
s *= prefix * "$name: " * summary(fts) * '\n'
end
return print(io, s)
end